15 #ifndef VISIONTRANSFER_DATABLOCKPROTOCOL_H 16 #define VISIONTRANSFER_DATABLOCKPROTOCOL_H 24 #include "visiontransfer/alignedallocator.h" 53 static const int MAX_TCP_BYTES_TRANSFER = 0xFFFF;
54 static const int MAX_UDP_RECEPTION = 0x4000;
55 static const int MAX_OUTSTANDING_BYTES = 2*MAX_TCP_BYTES_TRANSFER;
72 return protType == PROTOCOL_UDP ?
sizeof(int) : 0;
115 void setTransferData(
unsigned char* data,
int validBytes = 0x7FFFFFFF);
202 return droppedReceptions;
238 struct MissingReceiveSegment {
242 unsigned char subsequentData[4];
245 static constexpr
int HEARTBEAT_INTERVAL_MS = 1000;
246 static constexpr
int RECONNECT_TIMEOUT_MS = 1000;
248 static constexpr
unsigned char CONNECTION_MESSAGE = 0x01;
249 static constexpr
unsigned char CONFIRM_MESSAGE = 0x02;
250 static constexpr
unsigned char HEADER_MESSAGE = 0x03;
251 static constexpr
unsigned char RESEND_MESSAGE = 0x04;
252 static constexpr
unsigned char EOF_MESSAGE = 0x05;
253 static constexpr
unsigned char HEARTBEAT_MESSAGE = 0x06;
256 ProtocolType protType;
262 unsigned char* rawData;
266 int overwrittenTransferData;
267 int overwrittenTransferIndex;
268 unsigned char* transferHeaderData;
269 int transferHeaderSize;
272 std::deque<MissingReceiveSegment> missingReceiveSegments;
273 std::deque<std::pair<int, int> > missingTransferSegments;
274 bool waitingForMissingSegments;
275 int totalReceiveSize;
277 unsigned char controlMessageBuffer[1024];
280 bool connectionConfirmed;
281 bool confirmationMessagePending;
282 bool eofMessagePending;
283 bool clientConnectionPending;
284 bool resendMessagePending;
285 std::chrono::steady_clock::time_point lastRemoteHostActivity;
286 std::chrono::steady_clock::time_point lastSentHeartbeat;
287 std::chrono::steady_clock::time_point lastReceivedHeartbeat;
290 std::vector<unsigned char, AlignedAllocator<unsigned char> > receiveBuffer;
293 std::vector<unsigned char> receivedHeader;
294 bool finishedReception;
295 int droppedReceptions;
296 unsigned char unprocessedMsgPart[MAX_OUTSTANDING_BYTES];
297 int unprocessedMsgLength;
300 const unsigned char* extractPayload(
const unsigned char* data,
int& length,
bool& error);
301 bool processControlMessage(
int length);
302 void restoreTransferBuffer();
303 bool generateResendRequest(
int& length);
304 void getNextTransferSegment(
int& offset,
int& length);
305 void parseResendMessage(
int length);
306 void parseEofMessage(
int length);
307 int getNextUdpReceiveOffset(
int lastSegmentOffset,
int lastSegmentSize);
308 void processReceivedUdpMessage(
int length,
bool& transferComplete);
309 void processReceivedTcpMessage(
int length,
bool& transferComplete);
310 void resizeReceiveBuffer();
311 int parseReceivedHeader(
int length,
int offset);
unsigned char * getNextReceiveBuffer(int maxLength)
Gets a buffer for receiving the next network message.
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...
void setTransferHeader(unsigned char *data, int headerSize, int transferSize)
Sets a user-defined header that shall be transmitted with the next transfer.
bool isConnected() const
Returns true if a remote connection is established.
void setTransferValidBytes(int validBytes)
Updates the number of valid bytes in a partial transfer.
int getDroppedReceptions() const
Returns the internal counter of dropped transfers during reception.
bool newClientConnected()
Returns true if the last network message has established a new connection from a client.
int getMaxReceptionSize() const
Returns the maximum payload size that can be received.
bool transferComplete()
Returns true if the current transfer has been completed.
void resetTransfer()
Resets all transfer related internal variables.
unsigned char * getReceivedData(int &length)
Returns the data that has been received for the current transfer.
int getProtocolOverhead() const
Returns the size of the overhead data that is required for transferring a single network message...
void setTransferData(unsigned char *data, int validBytes=0x7FFFFFFF)
Sets the payload data for the next transfer.
A protocol for transmitting large blocks of data over a network.
unsigned char * getReceivedHeader(int &length)
Returns the header data that has been received for the current transfer.
void processReceivedMessage(int length, bool &transferComplete)
Handles a received network message.
DataBlockProtocol(bool server, ProtocolType protType, int maxUdpPacketSize)
Creates a new instance.
void resetReception(bool dropped)
Resets the message reception.