OpendTect-6_4  6.4
netreqpacket.h
Go to the documentation of this file.
1 #ifndef netreqpacket_h
2 #define netreqpacket_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: K. Tingdahl
9  Date: August 2014
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "networkmod.h"
16 #include "bufstringset.h"
17 
18 #define mRequestPacketHeaderSize 10
19 
20 
21 namespace Network
22 {
23 
24 /*\brief Standardized packet that can be sent over a Tcp connection
25 
26  The request header contains a unique (int) request ID and a (short) sub-ID.
27  The sub-ID is a flag that can be used to implement your protocol. Negative
28  sub-IDs are special and cannot be freely used.
29 
30  Servers will not send Begin packets as they don't initiate requests. Replies
31  are never Begin packets. Error always implies End.
32 
33  Request IDs are obtained by using setIsNewRequest().
34 
35  At the end of the class there is a public section for expert usage.
36 
37  */
38 
40 {
41 public:
42  RequestPacket(od_int32 payloadsize=0);
43  ~RequestPacket();
44 
45  od_int32 requestID() const;
46  bool isNewRequest() const { return subID()==cBeginSubID(); }
47  bool isRequestEnd() const { return subID()<cMoreSubID(); }
48  bool isError() const { return subID()==cErrorSubID(); }
49  od_int32 payloadSize() const;
50  const void* payload() const;
51  void getStringPayload(BufferString&) const;
53  { return payloadSize() + mRequestPacketHeaderSize; }
54 
55  int setIsNewRequest();
56  void setRequestID(od_int32);
57  void setIsError() { setSubID( cErrorSubID() ); }
58  void setIsLast() { setSubID( cEndSubID() ); }
59 
60  void* allocPayload(od_int32 size);
61  void setPayload(void*,od_int32 size);
62  void setStringPayload(const char*);
63 
64  void addErrMsg(BufferString&) const;
65 
66  // essentially, there is no limit unless user sets it
67  static od_int32 systemSizeLimit(); //< if < 1 no limit
68  static void setSystemSizeLimit(od_int32); //< no limit set to 0
69 
70 protected:
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  inline bool atEndOfPkt() const;
186 
187 protected:
188 
190  mutable int curpos_;
191 
192 };
193 
194 
195 template <class T>
196 inline int PacketFiller::sizeFor( const T& var )
197 {
198  return sizeof(T);
199 }
200 inline int PacketFiller::sizeFor( bool var )
201 {
202  return sizeFor( ((int)var) );
203 }
204 
205 template <class T>
206 inline int PacketFiller::sizeFor( const T* arr, int nrelems, bool rawmode )
207 {
208  return (rawmode ? 0 : sizeof(int)) + nrelems * sizeof(T);
209 }
210 
211 inline int PacketFiller::sizeFor( const char* str )
212 {
213  return sizeFor( FixedString(str) );
214 }
215 
216 inline int PacketFiller::sizeFor( const OD::String& str )
217 {
218  return sizeof(int) + str.size();
219 }
220 
221 inline int PacketFiller::sizeFor( const BufferStringSet& bss )
222 {
223  int ret = sizeof(int);
224  for ( int idx=0; idx<bss.size(); idx++ )
225  ret += sizeFor( bss.get(idx) );
226  return ret;
227 }
228 
229 
230 inline const PacketFiller& PacketFiller::put( bool var ) const
231 {
232  const int toput = var ? 1 : 0;
233  return put( toput );
234 }
235 
236 
237 template <class T>
238 inline const PacketFiller& PacketFiller::put( const T& var ) const
239 {
240  return putBytes( &var, sizeof(T) );
241 }
242 
243 template <class T>
244 inline const PacketFiller& PacketFiller::put( const T* arr, int nrelems,
245  bool rawmode ) const
246 {
247  if ( !rawmode )
248  put( nrelems );
249  return putBytes( arr, nrelems * sizeof(T) );
250 }
251 
252 inline const PacketFiller& PacketFiller::put( const char* str ) const
253 {
254  return put( FixedString(str) );
255 }
256 
257 inline const PacketFiller& PacketFiller::put( const OD::String& str ) const
258 {
259  const int sz = str.size();
260  put( sz );
261  return putBytes( str.str(), sz );
262 }
263 
264 inline const PacketFiller& PacketFiller::put( const BufferStringSet& bss ) const
265 {
266  const int sz = bss.size();
267  put( sz );
268  for ( int idx=0; idx<sz; idx++ )
269  put( bss.get(idx) );
270  return *this;
271 }
272 
273 inline const PacketFiller& PacketFiller::putBytes( const void* ptr,
274  int sz ) const
275 {
276  if ( sz > 0 )
277  {
278  OD::memCopy( pkt_.payload_+curpos_, ptr, sz );
279  curpos_ += sz;
280  }
281  return *this;
282 }
283 
284 
285 
286 
287 template <class T>
288 inline void PacketInterpreter::peek( T& var ) const
289 {
290  OD::memCopy( &var, pkt_.payload_+curpos_, sizeof(T) );
291 }
292 
293 template <class T>
294 inline void PacketInterpreter::get( T& var ) const
295 {
296  getBytes( &var, sizeof(T) );
297 }
298 
299 template <>
300 inline void PacketInterpreter::get( BufferString& var ) const
301 {
302  int sz; get( sz );
303  if ( sz < 1 )
304  {
305  if ( sz < 0 )
306  { pErrMsg("Invalid string size"); }
307  var.setEmpty();
308  return;
309  }
310  var.setBufSize( sz+1 );
311  char* cstr = var.getCStr();
312  getBytes( cstr, sz );
313  cstr[sz] = '\0';
314 }
315 
316 inline int PacketInterpreter::peekInt() const
317 { int ret = 0; peek(ret); return ret; }
318 inline bool PacketInterpreter::getBool() const
319 { int ret = 0; get(ret); return ret != 0; }
320 inline int PacketInterpreter::getInt() const
321 { int ret = 0; get(ret); return ret; }
322 inline float PacketInterpreter::getFloat() const
323 { float ret = 0.f; get(ret); return ret; }
324 inline double PacketInterpreter::getDouble() const
325 { double ret = 0.; get(ret); return ret; }
327 { BufferString ret; get(ret); return ret; }
328 
329 template <class T>
330 inline void PacketInterpreter::getArr( T* arr, int maxsz, bool rawmode ) const
331 {
332  int arrsz = maxsz;
333  if ( !rawmode )
334  get( arrsz );
335  int sz = arrsz;
336  if ( sz > maxsz )
337  sz = maxsz;
338  if ( arrsz < 1 )
339  return;
340 
341  getBytes( arr, sz*sizeof(T) );
342 }
343 
344 template <class T>
345 inline void PacketInterpreter::getSet( TypeSet<T>& ts, int maxsz,
346  bool rawmode ) const
347 {
348  int arrsz = maxsz;
349  if ( !rawmode )
350  get( arrsz );
351  int sz = arrsz;
352  if ( maxsz >= 0 && sz > maxsz )
353  sz = maxsz;
354  if ( arrsz < 1 )
355  { ts.setEmpty(); return; }
356 
357  ts.setSize( sz );
358  getBytes( ts.arr(), sz*sizeof(T) );
359 }
360 
362  int maxsz ) const
363 {
364  int setsz; get( setsz );
365  int sz = setsz;
366  if ( maxsz >= 0 && sz > maxsz )
367  sz = maxsz;
368  bss.setEmpty();
369  if ( setsz < 1 )
370  return;
371 
372  for ( int idx=0; idx<sz; idx++ )
373  {
374  BufferString* bs = new BufferString;
375  get( *bs );
376  bss += bs;
377  }
378 
379  for ( int idx=sz; idx<setsz; idx++ )
380  getString();
381 }
382 
383 inline void PacketInterpreter::getBytes( void* ptr, int nrbytes ) const
384 {
385  if ( nrbytes > 0 )
386  {
387  OD::memCopy( ptr, pkt_.payload_+curpos_, nrbytes );
388  curpos_ += nrbytes;
389  }
390 }
391 
392 inline bool PacketInterpreter::atEndOfPkt() const
393 { return curpos_ >= pkt_.payloadSize(); }
394 
395 }; //Namespace Network
396 
397 
398 #endif
static od_int32 headerSize()
Definition: netreqpacket.h:98
#define mExpClass(module)
Definition: commondefs.h:160
Header header_
Definition: netreqpacket.h:79
BufferString getString() const
Definition: netreqpacket.h:326
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:196
void peek(T &) const
Definition: netreqpacket.h:288
PacketFiller(RequestPacket &p, int startpos=0)
Definition: netreqpacket.h:118
const char * str() const
Definition: odstring.h:48
Definition: jobcommunic.h:24
int curpos_
Definition: netreqpacket.h:147
OD::String that holds an existing text string.
Definition: fixedstring.h:29
void setIsLast()
Definition: netreqpacket.h:58
const PacketFiller & putBytes(const void *, int nrbytes) const
Definition: netreqpacket.h:273
BufferString & setEmpty()
int curpos_
Definition: netreqpacket.h:190
#define mRequestPacketHeaderSize
Definition: netreqpacket.h:18
Set of BufferString objects.
Definition: bufstringset.h:28
void getArr(T *, int maxsz, bool rawmode=false) const
Definition: netreqpacket.h:330
unsigned int size() const
Definition: netreqpacket.h:39
char * payload_
Definition: netreqpacket.h:80
bool atEndOfPkt() const
Definition: netreqpacket.h:392
virtual T * arr()
3rd party access
Definition: typeset.h:92
size_type size() const
Definition: objectset.h:50
const void * getRawHeader() const
Definition: netreqpacket.h:105
bool setBufSize(unsigned int)
Set of (small) copyable elements.
Definition: commontypes.h:30
static od_int16 cBeginSubID()
Definition: netreqpacket.h:82
encapsulates the read-access-only part of strings in OD.
Definition: odstring.h:31
bool isError() const
Definition: netreqpacket.h:48
void setEmpty()
Definition: odset.h:46
bool isNewRequest() const
Definition: netreqpacket.h:46
void * getRawHeader()
Definition: netreqpacket.h:104
void getBytes(void *, int nrbytes) const
Definition: netreqpacket.h:383
int peekInt() const
Definition: netreqpacket.h:316
void setIsError()
Definition: netreqpacket.h:57
#define od_int16
Definition: plftypes.h:27
const PacketFiller & put(const T &) const
Definition: netreqpacket.h:238
int getInt() const
Definition: netreqpacket.h:320
float getFloat() const
Definition: netreqpacket.h:322
Definition: netreqpacket.h:115
#define od_int32
Definition: plftypes.h:31
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:40
const RequestPacket & pkt_
Definition: netreqpacket.h:189
void get(T &) const
Definition: netreqpacket.h:294
bool getBool() const
Definition: netreqpacket.h:318
Definition: netreqpacket.h:72
void moveTo(int pos) const
Definition: netreqpacket.h:180
bool isRequestEnd() const
Definition: netreqpacket.h:47
virtual bool setSize(size_type, T val=T())
void getSet(TypeSet< T > &, int maxsz=-1, bool rawmode=false) const
Definition: netreqpacket.h:345
BufferString & get(int idx)
Definition: bufstringset.h:36
od_int32 int32s_[2]
Definition: netreqpacket.h:74
static od_int16 cEndSubID()
Definition: netreqpacket.h:84
#define pErrMsg(msg)
Definition: errmsg.h:60
double getDouble() const
Definition: netreqpacket.h:324
static od_int16 cErrorSubID()
Definition: netreqpacket.h:85
char * getCStr()
Definition: bufstring.h:75
od_int32 totalSize() const
Definition: netreqpacket.h:52
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. 2019