15 #ifndef VISIONTRANSFER_DATABLOCKPROTOCOL_H 16 #define VISIONTRANSFER_DATABLOCKPROTOCOL_H 24 #include "visiontransfer/alignedallocator.h" 50 static const int MAX_TCP_BYTES_TRANSFER = 0xFFFF;
51 static const int MAX_UDP_RECEPTION = 0x4000;
52 static const int MAX_OUTSTANDING_BYTES = 2*MAX_TCP_BYTES_TRANSFER;
69 return protType == PROTOCOL_UDP ?
sizeof(int) : 0;
112 void setTransferData(
unsigned char* data,
int validBytes = 0x7FFFFFFF);
199 return droppedReceptions;
235 struct MissingReceiveSegment {
239 unsigned char subsequentData[4];
242 static constexpr
int HEARTBEAT_INTERVAL_MS = 1000;
243 static constexpr
int RECONNECT_TIMEOUT_MS = 1000;
245 static constexpr
unsigned char CONNECTION_MESSAGE = 0x01;
246 static constexpr
unsigned char CONFIRM_MESSAGE = 0x02;
247 static constexpr
unsigned char HEADER_MESSAGE = 0x03;
248 static constexpr
unsigned char RESEND_MESSAGE = 0x04;
249 static constexpr
unsigned char EOF_MESSAGE = 0x05;
250 static constexpr
unsigned char HEARTBEAT_MESSAGE = 0x06;
253 ProtocolType protType;
259 unsigned char* rawData;
263 int overwrittenTransferData;
264 int overwrittenTransferIndex;
265 unsigned char* transferHeaderData;
266 int transferHeaderSize;
269 std::deque<MissingReceiveSegment> missingReceiveSegments;
270 std::deque<std::pair<int, int> > missingTransferSegments;
271 bool waitingForMissingSegments;
272 int totalReceiveSize;
274 unsigned char controlMessageBuffer[1024];
277 bool connectionConfirmed;
278 bool confirmationMessagePending;
279 bool eofMessagePending;
280 bool clientConnectionPending;
281 bool resendMessagePending;
282 std::chrono::steady_clock::time_point lastRemoteHostActivity;
283 std::chrono::steady_clock::time_point lastSentHeartbeat;
284 std::chrono::steady_clock::time_point lastReceivedHeartbeat;
287 std::vector<unsigned char, AlignedAllocator<unsigned char> > receiveBuffer;
290 std::vector<unsigned char> receivedHeader;
291 bool finishedReception;
292 int droppedReceptions;
293 unsigned char unprocessedMsgPart[MAX_OUTSTANDING_BYTES];
294 int unprocessedMsgLength;
297 const unsigned char* extractPayload(
const unsigned char* data,
int& length,
bool& error);
298 bool processControlMessage(
int length);
299 void restoreTransferBuffer();
300 bool generateResendRequest(
int& length);
301 void getNextTransferSegment(
int& offset,
int& length);
302 void parseResendMessage(
int length);
303 void parseEofMessage(
int length);
304 int getNextUdpReceiveOffset(
int lastSegmentOffset,
int lastSegmentSize);
305 void processReceivedUdpMessage(
int length,
bool& transferComplete);
306 void processReceivedTcpMessage(
int length,
bool& transferComplete);
307 void resizeReceiveBuffer();
308 int parseReceivedHeader(
int length,
int offset);
void setTransferData(unsigned char *data, int validBytes=0x7FFFFFFF)
Sets the payload data for the next transfer.
const unsigned char * getTransferMessage(int &length)
Gets the next network message for the current transfer.
const unsigned char * getNextControlMessage(int &length)
If a control message is pending to be transmitted, then the message data will be returned by this met...
A protocol for transmitting large blocks of data over a network.
DataBlockProtocol(bool server, ProtocolType protType, int maxUdpPacketSize)
Creates a new instance.
int getProtocolOverhead() const
Returns the size of the overhead data that is required for transferring a single network message...
unsigned char * getReceivedData(int &length)
Returns the data that has been received for the current transfer.
int getDroppedReceptions() const
Returns the internal counter of dropped transfers during reception.
void resetTransfer()
Resets all transfer related internal variables.
unsigned char * getNextReceiveBuffer(int maxLength)
Gets a buffer for receiving the next network message.
int getMaxReceptionSize() const
Returns the maximum payload size that can be received.
bool isConnected() const
Returns true if a remote connection is established.
bool newClientConnected()
Returns true if the last network message has established a new connection from a client.
void processReceivedMessage(int length, bool &transferComplete)
Handles a received network message.
void resetReception(bool dropped)
Resets the message reception.
bool transferComplete()
Returns true if the current transfer has been completed.
void setTransferHeader(unsigned char *data, int headerSize, int transferSize)
Sets a user-defined header that shall be transmitted with the next transfer.
void setTransferValidBytes(int validBytes)
Updates the number of valid bytes in a partial transfer.
unsigned char * getReceivedHeader(int &length)
Returns the header data that has been received for the current transfer.