DataVector.cpp 3.4 KB

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