libvisiontransfer  5.1.0
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_TCP_BYTES_TRANSFER = 0xFFFF; //64K - 1
43  static const int MAX_UDP_RECEPTION = 0x4000; //16K
44  static const int MAX_OUTSTANDING_BYTES = 2*MAX_TCP_BYTES_TRANSFER;
45 
53  DataBlockProtocol(ProtocolType protType, int maxUdpPacketSize);
54 
59  int getProtocolOverhead() const {
60  return protType == PROTOCOL_UDP ? sizeof(unsigned short) : 0;
61  }
62 
66  int getMaxReceptionSize() const;
67 
73  void startTransfer();
74 
78  void resetTransfer();
79 
95  void setTransferData(unsigned char* data, int size, int validBytes = 0x7FFFFFFF);
96 
106  void setTransferValidBytes(int validBytes);
107 
117  const unsigned char* getTransferMessage(int& length);
118 
122  bool transferComplete();
123 
134  void setReceiveDataSize(int size);
135 
141  unsigned char* getNextReceiveBuffer(int maxLength);
142 
146  void resetReception();
147 
153  bool processReceivedMessage(int length);
154 
164  unsigned char* getReceivedData(int& length);
165 
172  void finishReception();
173 
174 private:
175  // The pimpl idiom is not necessary here, as this class is usually not
176  // used directly
177  static const int MIN_UDP_BYTES_TRANSFER = 512;
178 
179  ProtocolType protType;
180  int maxUdpPacketSize;
181  int maxPayloadSize;
182  int minPayloadSize;
183 
184  // Transfer related variables
185  bool transferDone;
186  unsigned char* rawData;
187  int rawValidBytes;
188  int transferOffset;
189  int transferSize;
190  unsigned short transferSeqNum;
191  unsigned short overwrittenTransferData;
192  bool restoreTransferData;
193 
194  // Reception related variables
195  std::vector<unsigned char, AlignedAllocator<unsigned char> > receiveBuffer;
196  int receiveDataSize;
197  unsigned short receiveSeqNum;
198  unsigned char unprocessedMsgPart[MAX_OUTSTANDING_BYTES];
199  int unprocessedMsgLength;
200  int receiveTotalSize;
201  int receiveOffset;
202  bool receptionDone;
203 
204  const unsigned char* extractPayload(const unsigned char* data, int& length, bool& error);
205 };
206 
207 #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.
DataBlockProtocol(ProtocolType protType, int maxUdpPacketSize)
Creates a new instance.
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.
int getMaxReceptionSize() const
Returns the maximum paload size that can be recedived.
void resetReception()
Resets the message reception.
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