OpendTect  6.3
netreqpacket.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: K. Tingdahl
8  Date: August 2014
9 ________________________________________________________________________
10 
11 -*/
12 
13 #include "networkmod.h"
14 #include "bufstringset.h"
15 #include "refcount.h"
16 
17 #define mRequestPacketHeaderSize 10
18 
19 
20 namespace Network
21 {
22 
23 /*\brief Standardized packet that can be sent over a Tcp connection
24 
25  The request header contains a unique (int) request ID and a (short) sub-ID.
26  The sub-ID is a flag that can be used to implement your protocol. Negative
27  sub-IDs are special and cannot be freely used.
28 
29  Servers will not send Begin packets as they don't initiate requests. Replies
30  are never Begin packets. Error always implies End.
31 
32  Request IDs are obtained by using setIsNewRequest().
33 
34  At the end of the class there is a public section for expert usage.
35 
36  */
37 
38 mExpClass(Network) RequestPacket : public RefCount::Referenced
39 {
40 public:
41  RequestPacket(od_int32 payloadsize=0);
43 
44  od_int32 requestID() const;
45  bool isNewRequest() const { return subID()==cBeginSubID(); }
46  bool isRequestEnd() const { return subID()<cMoreSubID(); }
47  bool isError() const { return subID()==cErrorSubID(); }
48  const void* payload() const;
49  void getStringPayload(BufferString&) const;
50  od_int32 payloadSize() const;
52  { return payloadSize() + mRequestPacketHeaderSize; }
53 
54  int setIsNewRequest();
55  void setRequestID(od_int32);
56  void setIsError() { setSubID( cErrorSubID() ); }
57  void setIsLast() { setSubID( cEndSubID() ); }
58 
59  void* allocPayload(od_int32 size);
60  void setPayload(void*,od_int32 size);
61  void setStringPayload(const char*);
62 
63  void addErrMsg(BufferString&) const;
64 
65  // essentially, there is no limit unless user sets it
66  static od_int32 systemSizeLimit(); //< if < 1 no limit
67  static void setSystemSizeLimit(od_int32); //< no limit set to 0
68 
69 protected:
70  ~RequestPacket();
71 
72  union Header
73  {
74  od_int32 int32s_[2];
75  od_int16 int16s_[5];
76  };
77 
78 
80  char* payload_;
81 
82  static od_int16 cBeginSubID() { return -1; }
83  static od_int16 cMoreSubID() { return -2; }
84  static od_int16 cEndSubID() { return -4; }
85  static od_int16 cErrorSubID() { return -8; }
86 
87  friend class PacketFiller;
88  friend class PacketInterpreter;
89 
90 public:
91 
92  bool isOK() const;
94  od_int16 subID() const;
95 
96  void setSubID(od_int16);
97 
99  static od_int32 getPayloadSize(const void*);
100 
101  void* payload(bool takeover=false);
104  void* getRawHeader() { return header_.int32s_; }
105  const void* getRawHeader() const { return header_.int32s_; }
106 };
107 
108 
109 /*\brief fills a packet's payload.
110 
111  Use the sizeFor() functions to calculate the total payload size.
112 
113 */
114 
116 {
117 public:
118  PacketFiller( RequestPacket& p, int startpos=0 )
119  : pkt_(p)
120  , curpos_(startpos) {}
121 
122 # define mNPFSizeFor(v) Network::PacketFiller::sizeFor(v)
123 # define mNPFSizeForArr(a,s) Network::PacketFiller::sizeFor(a,s)
124  template <class T>
125  static int sizeFor(const T&);
126  template <class T>
127  static int sizeFor(const T*,int nrelems,bool rawmode=false);
128  static int sizeFor(bool);
129  static int sizeFor(const char*);
130  static int sizeFor(const OD::String&);
131  static int sizeFor(const BufferStringSet&);
132 
133  template <class T> const PacketFiller&
134  put(const T&) const;
135  template <class T> const PacketFiller&
136  put(const T*,int nrelems,bool rawmode=false) const;
137  const PacketFiller& put(const char*) const;
138  const PacketFiller& put(bool) const;
139  const PacketFiller& put(const OD::String&) const;
140  const PacketFiller& put(const BufferStringSet&) const;
141 
142  const PacketFiller& putBytes(const void*,int nrbytes) const;
143 
144 protected:
145 
147  mutable int curpos_;
148 
149 };
150 
151 
152 /*\brief interprets what is in a packet.
153 
154  You need to know what should be in there. Arrays/sets are always
155  preceeded by their size.
156 
157 */
158 
160 {
161 public:
163  int startpos=0 )
164  : pkt_(p)
165  , curpos_(startpos) {}
166 
167  template <class T> void get(T&) const;
168  inline bool getBool() const;
169  inline int getInt() const;
170  inline float getFloat() const;
171  inline double getDouble() const;
172  inline BufferString getString() const;
173 
174  template <class T> void getArr(T*,int maxsz,bool rawmode=false) const;
175  template <class T> void getSet(TypeSet<T>&,int maxsz=-1,
176  bool rawmode=false) const;
177  inline void getSet(BufferStringSet&,int maxsz=-1) const;
178 
179  inline void move( int nrb ) const { curpos_ += nrb; }
180  inline void moveTo( int pos ) const { curpos_ = pos; }
181  template <class T> void peek(T&) const;
182  inline int peekInt() const;
183  void getBytes(void*,int nrbytes) const;
184 
185 protected:
186 
188  mutable int curpos_;
189 
190 };
191 
192 
193 template <class T>
194 inline int PacketFiller::sizeFor( const T& var )
195 {
196  return sizeof(T);
197 }
198 inline int PacketFiller::sizeFor( bool var )
199 {
200  return sizeFor( ((int)var) );
201 }
202 
203 template <class T>
204 inline int PacketFiller::sizeFor( const T* arr, int nrelems, bool rawmode )
205 {
206  return (rawmode ? 0 : sizeof(int)) + nrelems * sizeof(T);
207 }
208 
209 inline int PacketFiller::sizeFor( const char* str )
210 {
211  return sizeFor( FixedString(str) );
212 }
213 
214 inline int PacketFiller::sizeFor( const OD::String& str )
215 {
216  return sizeof(int) + str.size();
217 }
218 
219 inline int PacketFiller::sizeFor( const BufferStringSet& bss )
220 {
221  int ret = sizeof(int);
222  for ( int idx=0; idx<bss.size(); idx++ )
223  ret += sizeFor( bss.get(idx) );
224  return ret;
225 }
226 
227 
228 inline const PacketFiller& PacketFiller::put( bool var ) const
229 {
230  const int toput = var ? 1 : 0;
231  return put( toput );
232 }
233 
234 
235 template <class T>
236 inline const PacketFiller& PacketFiller::put( const T& var ) const
237 {
238  return putBytes( &var, sizeof(T) );
239 }
240 
241 template <class T>
242 inline const PacketFiller& PacketFiller::put( const T* arr, int nrelems,
243  bool rawmode ) const
244 {
245  if ( !rawmode )
246  put( nrelems );
247  return putBytes( arr, nrelems * sizeof(T) );
248 }
249 
250 inline const PacketFiller& PacketFiller::put( const char* str ) const
251 {
252  return put( FixedString(str) );
253 }
254 
255 inline const PacketFiller& PacketFiller::put( const OD::String& str ) const
256 {
257  const int sz = str.size();
258  put( sz );
259  return putBytes( str.str(), sz );
260 }
261 
262 inline const PacketFiller& PacketFiller::put( const BufferStringSet& bss ) const
263 {
264  const int sz = bss.size();
265  put( sz );
266  for ( int idx=0; idx<sz; idx++ )
267  put( bss.get(idx) );
268  return *this;
269 }
270 
271 inline const PacketFiller& PacketFiller::putBytes( const void* ptr,
272  int sz ) const
273 {
274  if ( sz > 0 )
275  {
276  OD::memCopy( pkt_.payload_+curpos_, ptr, sz );
277  curpos_ += sz;
278  }
279  return *this;
280 }
281 
282 
283 
284 
285 template <class T>
286 inline void PacketInterpreter::peek( T& var ) const
287 {
288  OD::memCopy( &var, pkt_.payload_+curpos_, sizeof(T) );
289 }
290 
291 template <class T>
292 inline void PacketInterpreter::get( T& var ) const
293 {
294  getBytes( &var, sizeof(T) );
295 }
296 
297 template <>
298 inline void PacketInterpreter::get( BufferString& var ) const
299 {
300  int sz; get( sz );
301  if ( sz < 1 )
302  {
303  if ( sz < 0 )
304  { pErrMsg("Invalid string size"); }
305  var.setEmpty();
306  return;
307  }
308  var.setBufSize( sz+1 );
309  char* cstr = var.getCStr();
310  getBytes( cstr, sz );
311  cstr[sz] = '\0';
312 }
313 
314 inline int PacketInterpreter::peekInt() const
315 { int ret = 0; peek(ret); return ret; }
316 inline bool PacketInterpreter::getBool() const
317 { int ret = 0; get(ret); return ret != 0; }
318 inline int PacketInterpreter::getInt() const
319 { int ret = 0; get(ret); return ret; }
320 inline float PacketInterpreter::getFloat() const
321 { float ret = 0.f; get(ret); return ret; }
322 inline double PacketInterpreter::getDouble() const
323 { double ret = 0.; get(ret); return ret; }
325 { BufferString ret; get(ret); return ret; }
326 
327 template <class T>
328 inline void PacketInterpreter::getArr( T* arr, int maxsz, bool rawmode ) const
329 {
330  int arrsz = maxsz;
331  if ( !rawmode )
332  get( arrsz );
333  int sz = arrsz;
334  if ( sz > maxsz )
335  sz = maxsz;
336  if ( arrsz < 1 )
337  return;
338 
339  getBytes( arr, sz*sizeof(T) );
340 }
341 
342 template <class T>
343 inline void PacketInterpreter::getSet( TypeSet<T>& ts, int maxsz,
344  bool rawmode ) const
345 {
346  int arrsz = maxsz;
347  if ( !rawmode )
348  get( arrsz );
349  int sz = arrsz;
350  if ( maxsz >= 0 && sz > maxsz )
351  sz = maxsz;
352  if ( arrsz < 1 )
353  { ts.setEmpty(); return; }
354 
355  ts.setSize( sz );
356  getBytes( ts.arr(), sz*sizeof(T) );
357 }
358 
360  int maxsz ) const
361 {
362  int setsz; get( setsz );
363  int sz = setsz;
364  if ( maxsz >= 0 && sz > maxsz )
365  sz = maxsz;
366  bss.setEmpty();
367  if ( setsz < 1 )
368  return;
369 
370  for ( int idx=0; idx<sz; idx++ )
371  {
372  BufferString* bs = new BufferString;
373  get( *bs );
374  bss += bs;
375  }
376 
377  for ( int idx=sz; idx<setsz; idx++ )
378  getString();
379 }
380 
381 inline void PacketInterpreter::getBytes( void* ptr, int nrbytes ) const
382 {
383  if ( nrbytes > 0 )
384  {
385  OD::memCopy( ptr, pkt_.payload_+curpos_, nrbytes );
386  curpos_ += nrbytes;
387  }
388 }
389 
390 
391 }; //Namespace Network
static od_int32 headerSize()
Definition: netreqpacket.h:98
#define mExpClass(module)
Definition: commondefs.h:157
Header header_
Definition: netreqpacket.h:79
BufferString getString() const
Definition: netreqpacket.h:324
static od_int16 cMoreSubID()
Definition: netreqpacket.h:83
RequestPacket & pkt_
Definition: netreqpacket.h:146
void move(int nrb) const
Definition: netreqpacket.h:179
static int sizeFor(const T &)
Definition: netreqpacket.h:194
void peek(T &) const
Definition: netreqpacket.h:286
PacketFiller(RequestPacket &p, int startpos=0)
Definition: netreqpacket.h:118
const char * str() const
Definition: odstring.h:46
Definition: jobcommunic.h:22
int curpos_
Definition: netreqpacket.h:147
OD::String that holds an existing text string.
Definition: fixedstring.h:27
void setIsLast()
Definition: netreqpacket.h:57
const PacketFiller & putBytes(const void *, int nrbytes) const
Definition: netreqpacket.h:271
BufferString & setEmpty()
BufferString & get(size_type idx)
Definition: bufstringset.h:49
int curpos_
Definition: netreqpacket.h:188
#define mRequestPacketHeaderSize
Definition: netreqpacket.h:17
Set of BufferString objects.
Definition: bufstringset.h:25
void getArr(T *, int maxsz, bool rawmode=false) const
Definition: netreqpacket.h:328
unsigned int size() const
Definition: netreqpacket.h:38
char * payload_
Definition: netreqpacket.h:80
virtual T * arr()
3rd party access
Definition: typeset.h:86
const void * getRawHeader() const
Definition: netreqpacket.h:105
bool setBufSize(unsigned int)
Set of (small) copyable elements.
Definition: commontypes.h:26
static od_int16 cBeginSubID()
Definition: netreqpacket.h:82
encapsulates the read-access-only part of strings in OD.
Definition: odstring.h:29
bool isError() const
Definition: netreqpacket.h:47
void setEmpty()
Definition: odset.h:44
bool isNewRequest() const
Definition: netreqpacket.h:45
void * getRawHeader()
Definition: netreqpacket.h:104
void getBytes(void *, int nrbytes) const
Definition: netreqpacket.h:381
int peekInt() const
Definition: netreqpacket.h:314
void setEmpty()
Definition: bufstringset.h:56
void setIsError()
Definition: netreqpacket.h:56
#define od_int16
Definition: plftypes.h:25
const PacketFiller & put(const T &) const
Definition: netreqpacket.h:236
int getInt() const
Definition: netreqpacket.h:318
float getFloat() const
Definition: netreqpacket.h:320
Definition: netreqpacket.h:115
#define od_int32
Definition: plftypes.h:29
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:38
const RequestPacket & pkt_
Definition: netreqpacket.h:187
void get(T &) const
Definition: netreqpacket.h:292
bool getBool() const
Definition: netreqpacket.h:316
Definition: netreqpacket.h:72
void moveTo(int pos) const
Definition: netreqpacket.h:180
bool isRequestEnd() const
Definition: netreqpacket.h:46
virtual bool setSize(size_type, T val=T())
void getSet(TypeSet< T > &, int maxsz=-1, bool rawmode=false) const
Definition: netreqpacket.h:343
size_type size() const
Definition: bufstringset.h:37
od_int32 int32s_[2]
Definition: netreqpacket.h:74
static od_int16 cEndSubID()
Definition: netreqpacket.h:84
#define pErrMsg(msg)
Usual access point for programmer error messages.
Definition: errmsg.h:34
double getDouble() const
Definition: netreqpacket.h:322
static od_int16 cErrorSubID()
Definition: netreqpacket.h:85
char * getCStr()
Definition: bufstring.h:73
od_int32 totalSize() const
Definition: netreqpacket.h:51
PacketInterpreter(const RequestPacket &p, int startpos=0)
Definition: netreqpacket.h:162
Definition: netreqpacket.h:159

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2017