DataVector.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <cstring>
  2. #include <iostream>
  3. #include <thread>
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <poll.h>
  8. #include "DataVector.h"
  9. DataVector::DataVector(size_t capacity) : capacity(capacity), data(new char[capacity]) {
  10. memset(data, 0, capacity);
  11. }
  12. DataVector::DataVector(const DataVector& orig) : capacity(orig.capacity), data(new char[capacity]) {
  13. memcpy(data, orig.data, capacity);
  14. }
  15. DataVector::DataVector(DataVector&& other) : capacity(other.capacity), data(other.data) {
  16. other.capacity = 0;
  17. other.data = nullptr;
  18. }
  19. DataVector::~DataVector() {
  20. delete[] data;
  21. }
  22. DataVector& DataVector::operator=(const DataVector& other) {
  23. if(this != &other) {
  24. delete[] data;
  25. capacity = other.capacity;
  26. data = new char[capacity];
  27. memcpy(data, other.data, capacity);
  28. }
  29. return *this;
  30. }
  31. DataVector& DataVector::operator=(DataVector&& other) {
  32. if(this != &other) {
  33. delete[] data;
  34. capacity = other.capacity;
  35. data = other.data;
  36. other.capacity = 0;
  37. other.data = nullptr;
  38. }
  39. return *this;
  40. }
  41. bool DataVector::read(size_t fromIndex, void* buffer, size_t length) const {
  42. if(fromIndex + length >= capacity) {
  43. return false;
  44. }
  45. memcpy(buffer, data + fromIndex, length);
  46. return true;
  47. }
  48. bool DataVector::write(size_t toIndex, const void* writeData, size_t length) {
  49. if(toIndex + length >= capacity) {
  50. return false;
  51. }
  52. memcpy(data + toIndex, writeData, length);
  53. return true;
  54. }
  55. bool DataVector::readSocket(int socket, size_t& readBytes) {
  56. readBytes = 0;
  57. uint32_t packetSize = 0;
  58. ssize_t readLength = recv(socket, &packetSize, 4, 0);
  59. if(readLength != 4) {
  60. return false;
  61. }
  62. packetSize = ntohl(packetSize);
  63. if(packetSize > capacity) {
  64. return false;
  65. }
  66. size_t bytesLeft = packetSize;
  67. while(true) {
  68. ssize_t readLength = recv(socket, data + readBytes, bytesLeft, MSG_DONTWAIT);
  69. if(readLength < 0) // an error occurred
  70. {
  71. if(errno == EAGAIN || errno == EWOULDBLOCK) {
  72. struct pollfd fds;
  73. fds.fd = socket;
  74. fds.events = POLLIN;
  75. fds.revents = 0;
  76. if(poll(&fds, 1, 3000) <= 0) {
  77. // client took to long to send the full packet
  78. readBytes = 0;
  79. return false;
  80. }
  81. continue;
  82. }
  83. // a real error occurred
  84. perror("cannot receive data");
  85. return true;
  86. } else if(readLength == 0) // socket closed / shutdown
  87. {
  88. return true;
  89. } else {
  90. readBytes += readLength;
  91. bytesLeft -= readLength;
  92. if(bytesLeft == 0) // packet fully read
  93. {
  94. return true;
  95. }
  96. }
  97. }
  98. }
  99. void DataVector::sendToSocket(int socket, size_t toIndex) const {
  100. size_t bufferOffset = 0;
  101. size_t sendLength = toIndex;
  102. while(sendLength > 0) {
  103. ssize_t writtenLength = send(socket, data + bufferOffset, sendLength, MSG_NOSIGNAL);
  104. if(writtenLength == -1) {
  105. perror("cannot send data");
  106. return;
  107. }
  108. sendLength -= writtenLength;
  109. bufferOffset += writtenLength;
  110. }
  111. }