DataVector.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. capacity = other.capacity;
  34. data = other.data;
  35. other.capacity = 0;
  36. other.data = nullptr;
  37. }
  38. return *this;
  39. }
  40. bool DataVector::read(size_t fromIndex, void* buffer, size_t length) const {
  41. if(fromIndex + length >= capacity) {
  42. return false;
  43. }
  44. memcpy(buffer, data + fromIndex, length);
  45. return true;
  46. }
  47. bool DataVector::write(size_t toIndex, const void* writeData, size_t length) {
  48. if(toIndex + length >= capacity) {
  49. return false;
  50. }
  51. memcpy(data + toIndex, writeData, length);
  52. return true;
  53. }
  54. bool DataVector::readSocket(int socket, size_t& readBytes) {
  55. readBytes = 0;
  56. uint32_t packetSize = 0;
  57. ssize_t readLength = recv(socket, &packetSize, 4, 0);
  58. if(readLength != 4) {
  59. return false;
  60. }
  61. packetSize = ntohl(packetSize);
  62. if(packetSize > capacity) {
  63. return false;
  64. }
  65. size_t bytesLeft = packetSize;
  66. while(true) {
  67. ssize_t readLength = recv(socket, data + readBytes, bytesLeft, MSG_DONTWAIT);
  68. if(readLength < 0) // an error occurred
  69. {
  70. if(errno == EAGAIN || errno == EWOULDBLOCK) {
  71. struct pollfd fds;
  72. fds.fd = socket;
  73. fds.events = POLLIN;
  74. fds.revents = 0;
  75. if(poll(&fds, 1, 3000) <= 0) {
  76. // client took to long to send the full packet
  77. readBytes = 0;
  78. return false;
  79. }
  80. continue;
  81. }
  82. // a real error occurred
  83. perror("cannot receive data");
  84. return true;
  85. } else if(readLength == 0) // socket closed / shutdown
  86. {
  87. return true;
  88. } else {
  89. readBytes += readLength;
  90. bytesLeft -= readLength;
  91. if(bytesLeft == 0) // packet fully read
  92. {
  93. return true;
  94. }
  95. }
  96. }
  97. }
  98. void DataVector::sendToSocket(int socket, size_t toIndex) const {
  99. size_t bufferOffset = 0;
  100. size_t sendLength = toIndex;
  101. while(sendLength > 0) {
  102. ssize_t writtenLength = send(socket, data + bufferOffset, sendLength, MSG_NOSIGNAL);
  103. if(writtenLength == -1) {
  104. perror("cannot send data");
  105. return;
  106. }
  107. sendLength -= writtenLength;
  108. bufferOffset += writtenLength;
  109. }
  110. }