VR-Link API Documentation for HLA 1516
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vlInetSocket.h
Go to the documentation of this file.
1 /*********************************************************************
2 ** Copyright (c) 1992-2010 MAK Technologies, Inc
3 ** All rights reserved.
4 *********************************************************************/
9 #pragma once
10 
11 #include <vlutil/vlInet.h>
12 #include <vlutil/vlInetAddr.h>
13 #include <vlutil/vlInetAddrUtils.h>
14 #include <vlutil/vlInetDevice.h>
15 #include <vlutil/vlInetEndpoint.h>
16 #include <vlutil/vlThreadSafe.h>
17 #include <vlutil/vlString.h>
18 #include <vlutil/vlTime.h>
19 #include <deque>
20 #include <map>
21 
22 #ifdef DtUSE_UTILITIES_NAMESPACE
23 namespace DtUSE_UTILITIES_NAMESPACE
24 {
25 #endif
26 
33 #define DtRead ((DtU32) 0x01)
34 #define DtWrite ((DtU32) 0x02)
36 #define DtReadWrite (DtRead | DtWrite)
38 #define DtBindToDestAddr ((DtU32) 0x08)
45 #define DtQuiet ((DtU32) 0x10)
50 
55 #define DtSockOptStrictMulticastBinding ((DtU32) 0x20)
56 #ifdef _WIN32
58 #define DtSockOptNonBlocking ((DtU32) 0x40) // MSG_DONTWAIT not supported
59 #else // !_WIN32
60 // Not sure if MSG_DONTWAIT is supported on Solaris, Irix, etc...
61 #define DtSockOptNonBlocking ((DtU32) MSG_DONTWAIT)
62 #endif
63 #define DtSockOptNoDelay ((DtU32) 0x80)
66 #define DtSockAsyncRead ((DtU32) 0x100)
74 #define DtSockAsyncWrite ((DtU32) 0x200)
75 #define DtAsyncReadWrite (DtSockAsyncRead | DtSockAsyncWrite)
76 #define DtSockWriteable ((DtU32)DtWrite | DtSockAsyncWrite)
77 #define DtSockReadable ((DtU32)DtRead | DtSockAsyncRead)
78 #define DtSockOptAddrReuse ((DtU32) 0x400)
81 #define DtSockOptPortReuse ((DtU32) 0x800)
82 #define DtSockOptAllReuse (DtSockOptAddrReuse | DtSockOptPortReuse)
83 #define DtSockDisablePreProcCallbacks ((DtU32) 0x1000)
86 #define DtSockDisablePostProcCallbacks ((DtU32) 0x2000)
87 #define DtSockDisableCallbacks (DtSockDisablePreProcCallbacks || DtSockDisablePostProcCallbacks)
88 // By default client sockets will be configured for blocking mode, read-write
89 // operation, address and port reuse enabled (UDP), NODELAY (TCP) operation.
90 #define DtDefaultSockOpts (DtU32)(DtReadWrite | DtSockOptAllReuse | DtSockOptNoDelay)
91 
94 class DtInetSocket;
95 
103 // STOPPED HERE -- use shared_ptr for buffers see Boost documentation on reference-counted buffers
104 typedef std::vector<char> DtInetBuffer;
105 typedef std::vector<DtInetBuffer*> DtInetBufferChain;
106 typedef std::pair<void*, DtInetBuffer*> DtInetBuffQueueEntry;
109 
113 {
125 
126 };
127 
130 {
137  // Any value greater than NO_PACKET represents
138  // the number of bytes received
140 
141 };
142 
145 {
149 
150 };
151 
154 {
159 };
160 
172 typedef int (*DtInetSocketProcessingCallback)( DtInetSocket* sock, int size,
173  DtInetBufferChain& buffChain,
174  DtInetEndpoint& ep, void* usr);
177 {
178 public:
179 
181  int order )
182  {
183  myCb = cb;
184  myUsr = usr;
185  myCallOrder = order;
186  }
187 
189  void* myUsr;
191 };
192 typedef std::multimap<int, DtInetSocketProcessingCbInfo*> DtInetSockCallbackList;
193 
195 
202 {
203 public:
204 
208  DtInetSocket();
209 
211  virtual ~DtInetSocket();
212 
219  virtual bool setDestination( const DtInetEndpoint& dest ) = 0;
220  virtual const DtInetEndpoint& destination() const;
221 
226  virtual const DtInetEndpoint& localEndpoint() const;
227 
231  virtual bool setLocalPort( DtU16 port );
232  virtual DtU16 localPort() const;
233 
245  virtual bool bindToDevice( const DtInetDevice* hostIf = NULL ) = 0;
246 
252  virtual const DtInetDevice* hostIf() const;
253 
258  virtual bool openSocket() = 0;
259 
264  virtual bool closeSocket() = 0;
265 
274  virtual bool connectToDestination() = 0;
275 
281 
284  virtual int send( caddr_t packet, size_t size, DtU32 flags = 0 ) = 0;
285  virtual int sendTo( caddr_t packet, size_t size,
286  DtInetEndpoint& dest, DtU32 flags = 0 ) = 0;
287 
291  virtual int sendChain( DtInetBufferChain& packets, DtU32 flags = 0 ) = 0;
292 
293  virtual int sendChainTo( DtInetBufferChain& packets,
294  DtInetEndpoint& dest, DtU32 flags = 0 ) = 0;
296 
306 
309  virtual DtInetSockRcvStatus recv( caddr_t buff, size_t buffSize,
310  DtU32 flags = 0 ) = 0;
311 
316  virtual DtInetSockRcvStatus recvToChain( DtInetBufferChain& packets,
317  DtU32 flags = 0 ) = 0;
318 
321  virtual DtInetSockRcvStatus recvFrom( caddr_t buff, size_t buffSize,
322  DtInetEndpoint& src, DtU32 flags = 0 ) = 0;
323 
324  virtual DtInetSockRcvStatus recvToChainFrom( DtInetBufferChain& packets,
325  DtInetEndpoint& src, DtU32 flags = 0 ) = 0;
327 
329  virtual DtInetSockState state();
330 
337  virtual bool isOk();
338 
340  virtual DtU32 protocol() const;
341 
343  virtual DtInetUtils::DtInetAddrFamily family() const;
344 
346  virtual unsigned descriptor() const = 0;
347 
349  virtual DtU32 flags() { return myFlags; }
350 
357  virtual bool setSocket( const DtInetEndpoint& dest, DtU16 localPort = 0,
358  DtInetDevice* hostIf = NULL, DtU32 flags = DtDefaultSockOpts ) = 0;
359 
367  virtual bool setNonBlockingOption( bool onOrOff ) = 0;
368  virtual bool setReuseAddressOption( bool onOrOff ) = 0;
369  virtual bool setSendBufferSize( DtU32 size ) = 0;
370  virtual bool setReceiveBufferSize( DtU32 size ) = 0;
371  virtual bool setDoNotRouteOption( bool onOrOff ) = 0;
372  virtual bool setSendLowWatermark( DtU32 size ) = 0;
373  virtual bool setReceiveLowWatermark( DtU32 size ) = 0;
374 
375  virtual bool isNonBlocking() const;
376  virtual bool isNoDelay() const;
377  virtual bool getReuseAddressOption() = 0;
378  virtual bool getDoNotRouteOption() = 0;
379  virtual DtU32 getSendBufferSize() = 0;
380  virtual DtU32 getReceiveBufferSize() = 0;
381  virtual DtU32 getBytesAvailable() = 0;
382  virtual DtU32 getSendLowWatermark() = 0;
383  virtual DtU32 getReceiveLowWatermark() = 0;
385 
388  virtual bool isIPv4() const;
389  virtual bool isIPv6() const;
390  virtual bool isTcp() const;
391  virtual bool isUdp() const;
393 
395  virtual int getLastError();
396  virtual DtString getLastErrorString();
397  virtual bool isErrorWouldBlockOrTryAgain();
398  virtual void clearError();
399 
401  virtual int getTerminationCode();
402  virtual DtString getTerminationString();
403 
410  virtual DtU64 totalBytesSent() const;
411  virtual DtU64 totalPacketsSent() const;
412  virtual DtU64 totalBytesReceived() const;
413  virtual DtU64 totalPacketsReceived() const;
414 
416  virtual void clearStats();
417 
424  virtual bool addProcessingCallback( DtInetSockProcessingType kind,
425  DtInetSocketProcessingCallback cb, void* usr, int callSequence = -1 );
426 
427  virtual bool removeProcessingCallback( DtInetSockProcessingType kind,
429 
430  // Absolute time of arrival of the datagram just read
431  virtual DtTime receiveTime() { return myReceiveTime; }
432 
433 #ifdef _WIN32
434 
438  inline HANDLE getReadHandle() const { return myInetReadHandle; }
439 #endif
440 
441 protected:
442  virtual bool findAndRemoveCallback( DtInetSockCallbackList& cbList,
444 
445 private:
447  DtInetSocket( const DtInetSocket& orig );
448 
450  DtInetSocket& operator=(const DtInetSocket& orig);
451 
452 protected:
453 #ifdef _WIN32
454  HANDLE myInetReadHandle;
455 #endif
456 
459 
461 
463 
465  bool myAsync;
469 
472 
475 
480 
481  // For internal statistics only
486 
487  // Absolute time of arrival of the datagram just read
489 };
490 // end of class DtInetSocket
491 
493 {
494  return mySentByteCount;
495 }
496 
498 {
499  return mySentPacketCount;
500 }
501 
503 {
504  return myReceivedByteCount;
505 }
506 
508 {
509  return myReceivedPacketCount;
510 }
511 
513 {
514  mySentByteCount = 0;
515  mySentPacketCount = 0;
516  myReceivedByteCount = 0;
517  myReceivedPacketCount = 0;
518 }
519 
522 {
523  bool found = false;
524  DtInetSockCallbackList::iterator iter = cbList.begin();
525  while ( !found && iter != cbList.end() )
526  {
527  if ( cb == iter->second->myCb )
528  {
529  DtDELETE(iter->second);
530  cbList.erase( iter );
531  found = true;
532  }
533  else
534  {
535  ++iter;
536  }
537  }
538 
539  return found;
540 }
541 
542 #ifdef DtUSE_UTILITIES_NAMESPACE
543 }
544 #endif
545 
546 
547 
virtual DtU64 totalBytesReceived() const
Definition: vlInetSocket.h:502
int myCallOrder
Definition: vlInetSocket.h:190
bool myNoDelayEnabled
Definition: vlInetSocket.h:467
Definition: vlInetSocket.h:139
DtU32 myFlags
Definition: vlInetSocket.h:464
Definition: vlInetSocket.h:133
Contains the DtString class declaration.
DtInetSockRcvStatus
Socket state enumeration.
Definition: vlInetSocket.h:129
std::multimap< int, DtInetSocketProcessingCbInfo * > DtInetSockCallbackList
Socket state enumeration.
Definition: vlInetSocket.h:192
std::vector< DtInetBuffer * > DtInetBufferChain
Socket state enumeration.
Definition: vlInetSocket.h:105
Definition: vlInetSocket.h:147
DtInetSockCallbackList myPreSendCallbacks
Definition: vlInetSocket.h:478
Definition: vlInetSocket.h:119
This file contains the declaration of DtClock and DtTime.
unsigned long long DtU64
-------—— Network Layout for a long long (64 bytes unsigned)
Definition: vlMachineTypes.h:185
The DtInetSocket class provides a protocol independant, address family independant base class for TCP...
Definition: vlInetSocket.h:201
int myErrorCode
Definition: vlInetSocket.h:470
Definition: vlInetSocket.h:120
DtThreadSafe< std::deque< DtTime > > DtInetThreadSafeTSQueue
Socket state enumeration.
Definition: vlInetSocket.h:108
Definition: vlInetSocket.h:117
DtInetEndpoint myLocalEndpoint
Definition: vlInetSocket.h:457
void * myUsr
Definition: vlInetSocket.h:189
Definition: vlInetSocket.h:176
Definition: vlInetSocket.h:115
int recv(int s, char *buf, int bufLen, int flags)
Definition: vlInetSocket.h:136
DtInetSockFlushStatus
Socket state enumeration.
Definition: vlInetSocket.h:144
virtual DtTime receiveTime()
Definition: vlInetSocket.h:431
This file contains the declaration of the DtInetEndpoint class.
bool myStartImmediately
Definition: vlInetSocket.h:468
DtInetSocketProcessingCallback myCb
Definition: vlInetSocket.h:188
DtInetAddrFamily
Definition: vlInetAddrUtils.h:55
The DtInetEndpoint class provides a protocol independant, address family independent mechanism to rep...
Definition: vlInetEndpoint.h:33
#define DtDELETE(x)
DT_FD_ZERO_USES_MEMSET.
Definition: vlUtil.h:95
DtInetSockCallbackList myPreRecvCallbacks
Definition: vlInetSocket.h:476
DtInetSockProcessingType
Socket state enumeration.
Definition: vlInetSocket.h:153
std::pair< void *, DtInetBuffer * > DtInetBuffQueueEntry
Socket state enumeration.
Definition: vlInetSocket.h:106
DtString myTerminationString
Definition: vlInetSocket.h:474
DtInetSockState
Socket state enumeration.
Definition: vlInetSocket.h:112
Definition: vlInetSocket.h:123
This file declares useful defaults constants and static methods for IP address handling.
DtInetSockCallbackList myPostSendCallbacks
Definition: vlInetSocket.h:479
DtInetDevice * myHostIf
Definition: vlInetSocket.h:460
Definition: vlInetSocket.h:134
DtString myErrorString
Definition: vlInetSocket.h:471
int myTerminationCode
Definition: vlInetSocket.h:473
Definition: vlInetSocket.h:156
Definition: vlInetSocket.h:158
virtual bool findAndRemoveCallback(DtInetSockCallbackList &cbList, DtInetSocketProcessingCallback cb)
Definition: vlInetSocket.h:520
Definition: vlInetSocket.h:118
Definition: vlInetSocket.h:124
virtual void clearStats()
Reset all statistics.
Definition: vlInetSocket.h:512
DtInetSocketProcessingCbInfo(DtInetSocketProcessingCallback cb, void *usr, int order)
Definition: vlInetSocket.h:180
std::vector< char > DtInetBuffer
Socket state enumeration.
Definition: vlInetSocket.h:104
DtInetSockState myState
Definition: vlInetSocket.h:462
This file contains the declaration of the DtInetAddr class.
Definition: vlInetDevice.h:101
Definition: vlInetSocket.h:122
Definition: vlInetSocket.h:135
virtual DtU32 flags()
Get the flags for this socket.
Definition: vlInetSocket.h:349
DtTime myReceiveTime
Definition: vlInetSocket.h:488
Definition: vlInetSocket.h:155
Definition: vlInetSocket.h:131
virtual DtU64 totalPacketsSent() const
Definition: vlInetSocket.h:497
DtThreadSafe< std::deque< DtInetBuffQueueEntry > > DtInetThreadSafeQueue
Socket state enumeration.
Definition: vlInetSocket.h:107
This file contains the declaration of the DtInetDevice class.
Definition: vlInetSocket.h:116
virtual DtU64 totalBytesSent() const
Get traffic statistics.
Definition: vlInetSocket.h:492
unsigned short DtU16
A 16 bit unsigned integer value.
Definition: vlMachineTypes.h:140
int send(int s, char *buf, int bufLen, int flags)
Definition: vlInetSocket.h:146
virtual DtU64 totalPacketsReceived() const
Definition: vlInetSocket.h:507
DtU64 myReceivedByteCount
Definition: vlInetSocket.h:484
Definition: vlInetSocket.h:157
unsigned long DtU32
A 32 bit unsigned integer value.
Definition: vlMachineTypes.h:153
a thread safe interface to a value.
Definition: vlThreadSafe.h:104
DtU64 myReceivedPacketCount
Definition: vlInetSocket.h:485
Instances of DtString are used to represent strings.
Definition: vlString.h:29
int(* DtInetSocketProcessingCallback)(DtInetSocket *sock, int size, DtInetBufferChain &buffChain, DtInetEndpoint &ep, void *usr)
Callbacks Pre- / Post- processing callback prototype.
Definition: vlInetSocket.h:172
Defines a thread-safe template class for variables.
DtInetEndpoint myRemoteEndpoint
Definition: vlInetSocket.h:458
#define DtDefaultSockOpts
Marks the Socket as Writable.
Definition: vlInetSocket.h:90
Definition: vlInetSocket.h:114
Definition: vlInetSocket.h:148
Definition: vlInetSocket.h:132
bool myAsync
Definition: vlInetSocket.h:465
bool myNonBlockingEnabled
Definition: vlInetSocket.h:466
This file declares useful defaults constants and static methods employed with the networking abstract...
DtU64 mySentByteCount
Definition: vlInetSocket.h:482
#define DT_DLL_VLUTIL
Definition: vlMachineTypes.h:109
DtInetSockCallbackList myPostRecvCallbacks
Definition: vlInetSocket.h:477
DtU64 mySentPacketCount
Definition: vlInetSocket.h:483
Definition: vlInetSocket.h:121

Document ID: Generated on Wed Mar 27 02:04:30 EDT 2024 from SVN revision 264570
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)