libvisiontransfer  4.1.5
datablockprotocol.h
1 /*******************************************************************************
2  * Copyright (c) 2017 Nerian Vision Technologies
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #ifndef VISIONTRANSFER_DATABLOCKPROTOCOL_H
16 #define VISIONTRANSFER_DATABLOCKPROTOCOL_H
17 
18 #include <map>
19 #include <vector>
20 #include <memory>
21 #include "visiontransfer/alignedallocator.h"
22 
35 public:
36  enum ProtocolType {
37  PROTOCOL_TCP,
38  PROTOCOL_UDP
39  };
40 
41  // Constants that are also used in other places.
42  static const int MAX_UDP_BYTES_TRANSFER = 1472;
43  //static const int MAX_UDP_BYTES_TRANSFER = 8972;
44  static const int MAX_TCP_BYTES_TRANSFER = 0xFFFF; //64K - 1
45  static const int MAX_OUTSTANDING_BYTES = 2*MAX_TCP_BYTES_TRANSFER;
46 
47  DataBlockProtocol(ProtocolType protType);
48 
53  int getProtocolOverhead() const {
54  return protType == PROTOCOL_UDP ? sizeof(unsigned short) : 0;
55  }
56 
60  int getMaxPayloadSize() const {return maxPayloadSize;}
61 
67  void startTransfer();
68 
72  void resetTransfer();
73 
89  void setTransferData(unsigned char* data, int size, int validBytes = 0x7FFFFFFF);
90 
100  void setTransferValidBytes(int validBytes);
101 
111  const unsigned char* getTransferMessage(int& length);
112 
116  bool transferComplete();
117 
128  void setReceiveDataSize(int size);
129 
135  unsigned char* getNextReceiveBuffer(int maxLength);
136 
140  void resetReception();
141 
147  bool processReceivedMessage(int length);
148 
158  unsigned char* getReceivedData(int& length);
159 
166  void finishReception();
167 
168 private:
169  // The pimpl idiom is not necessary here, as this class is usually not
170  // used directly
171  static const int MIN_UDP_BYTES_TRANSFER = 512;
172 
173  ProtocolType protType;
174  int maxPayloadSize;
175  int minPayloadSize;
176 
177  // Transfer related variables
178  bool transferDone;
179  unsigned char* rawData;
180  int rawValidBytes;
181  int transferOffset;
182  int transferSize;
183  unsigned short transferSeqNum;
184  unsigned short overwrittenTransferData;
185  bool restoreTransferData;
186 
187  // Reception related variables
188  std::vector<unsigned char, AlignedAllocator<unsigned char> > receiveBuffer;
189  int receiveDataSize;
190  unsigned short receiveSeqNum;
191  unsigned char unprocessedMsgPart[MAX_OUTSTANDING_BYTES];
192  int unprocessedMsgLength;
193  int receiveTotalSize;
194  int receiveOffset;
195  bool receptionDone;
196 
197  const unsigned char* extractPayload(const unsigned char* data, int& length, bool& error);
198 };
199 
200 #endif
void setReceiveDataSize(int size)
Sets the total size of the data that shall be received.
const unsigned char * getTransferMessage(int &length)
Gets the next network message for the current transfer.
A protocol for transmitting large blocks of data over a network.
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 data block.
void resetTransfer()
Stops the current transfer.
unsigned char * getNextReceiveBuffer(int maxLength)
Gets a buffer for receiving the next network message.
void resetReception()
Resets the message reception.
int getMaxPayloadSize() const
Returns the maximum paload size that can be transferred / received.
bool transferComplete()
Returns true if the current transfer has been completed.
bool processReceivedMessage(int length)
Handles a received network message.
void setTransferData(unsigned char *data, int size, int validBytes=0x7FFFFFFF)
Sets a new chunk of data that should be transferred.
void startTransfer()
Starts the transfer of a new data block.
void finishReception()
Finishes reception of the current data block.
void setTransferValidBytes(int validBytes)
Updates the number of valid bytes in a partial transmission.
Nerian Vision Technologies