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-2026 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 isTls() const;
392  virtual bool isUdp() const;
394 
397  virtual int getLastError() const;
398  virtual const DtString& getLastErrorString() const;
399  virtual bool isErrorWouldBlockOrTryAgain() const;
400  virtual void clearError();
402 
405  virtual int getTerminationCode() const;
406  virtual const DtString& getTerminationString() const;
408 
415  virtual DtU64 totalBytesSent() const;
416  virtual DtU64 totalPacketsSent() const;
417  virtual DtU64 totalBytesReceived() const;
418  virtual DtU64 totalPacketsReceived() const;
419 
421  virtual void clearStats();
422 
429  virtual bool addProcessingCallback( DtInetSockProcessingType kind,
430  DtInetSocketProcessingCallback cb, void* usr, int callSequence = -1 );
431 
432  virtual bool removeProcessingCallback( DtInetSockProcessingType kind,
434 
435  // Absolute time of arrival of the datagram just read
436  virtual DtTime receiveTime() { return myReceiveTime; }
437 
438 #ifdef _WIN32
439 
443  inline HANDLE getReadHandle() const { return myInetReadHandle; }
444 #endif
445 
446 protected:
447  virtual bool findAndRemoveCallback( DtInetSockCallbackList& cbList,
449 
450 private:
452  DtInetSocket( const DtInetSocket& orig );
453 
455  DtInetSocket& operator=(const DtInetSocket& orig);
456 
457 protected:
458 #ifdef _WIN32
459  HANDLE myInetReadHandle;
460 #endif
461 
464 
466 
468 
470  bool myAsync;
474 
477 
480 
485 
486  // For internal statistics only
491 
492  // Absolute time of arrival of the datagram just read
494 };
495 // end of class DtInetSocket
496 
498 {
499  return mySentByteCount;
500 }
501 
503 {
504  return mySentPacketCount;
505 }
506 
508 {
509  return myReceivedByteCount;
510 }
511 
513 {
514  return myReceivedPacketCount;
515 }
516 
518 {
519  mySentByteCount = 0;
520  mySentPacketCount = 0;
521  myReceivedByteCount = 0;
522  myReceivedPacketCount = 0;
523 }
524 
527 {
528  bool found = false;
529  DtInetSockCallbackList::iterator iter = cbList.begin();
530  while ( !found && iter != cbList.end() )
531  {
532  if ( cb == iter->second->myCb )
533  {
534  DtDELETE(iter->second);
535  cbList.erase( iter );
536  found = true;
537  }
538  else
539  {
540  ++iter;
541  }
542  }
543 
544  return found;
545 }
546 
547 #ifdef DtUSE_UTILITIES_NAMESPACE
548 }
549 #endif
550 
551 
552 
virtual DtU64 totalBytesReceived() const
Definition: vlInetSocket.h:507
int myCallOrder
Definition: vlInetSocket.h:190
bool myNoDelayEnabled
Definition: vlInetSocket.h:472
Definition: vlInetSocket.h:139
DtU32 myFlags
Definition: vlInetSocket.h:469
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:483
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:197
The DtInetSocket class provides a protocol independant, address family independant base class for TCP...
Definition: vlInetSocket.h:201
int myErrorCode
Definition: vlInetSocket.h:475
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:462
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:436
This file contains the declaration of the DtInetEndpoint class.
bool myStartImmediately
Definition: vlInetSocket.h:473
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:34
#define DtDELETE(x)
DT_FD_ZERO_USES_MEMSET.
Definition: vlUtil.h:95
DtInetSockCallbackList myPreRecvCallbacks
Definition: vlInetSocket.h:481
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:479
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:484
DtInetDevice * myHostIf
Definition: vlInetSocket.h:465
Definition: vlInetSocket.h:134
DtString myErrorString
Definition: vlInetSocket.h:476
int myTerminationCode
Definition: vlInetSocket.h:478
Definition: vlInetSocket.h:156
Definition: vlInetSocket.h:158
virtual bool findAndRemoveCallback(DtInetSockCallbackList &cbList, DtInetSocketProcessingCallback cb)
Definition: vlInetSocket.h:525
Definition: vlInetSocket.h:118
Definition: vlInetSocket.h:124
virtual void clearStats()
Reset all statistics.
Definition: vlInetSocket.h:517
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:467
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:493
Definition: vlInetSocket.h:155
Definition: vlInetSocket.h:131
virtual DtU64 totalPacketsSent() const
Definition: vlInetSocket.h:502
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:497
unsigned short DtU16
A 16 bit unsigned integer value.
Definition: vlMachineTypes.h:152
int send(int s, char *buf, int bufLen, int flags)
Definition: vlInetSocket.h:146
virtual DtU64 totalPacketsReceived() const
Definition: vlInetSocket.h:512
DtU64 myReceivedByteCount
Definition: vlInetSocket.h:489
Definition: vlInetSocket.h:157
unsigned long DtU32
A 32 bit unsigned integer value.
Definition: vlMachineTypes.h:165
a thread safe interface to a value.
Definition: vlThreadSafe.h:104
DtU64 myReceivedPacketCount
Definition: vlInetSocket.h:490
Instances of DtString are used to represent strings.
Definition: vlString.h:38
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:463
#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:470
bool myNonBlockingEnabled
Definition: vlInetSocket.h:471
This file declares useful defaults constants and static methods employed with the networking abstract...
DtU64 mySentByteCount
Definition: vlInetSocket.h:487
#define DT_DLL_VLUTIL
Definition: vlMachineTypes.h:121
DtInetSockCallbackList myPostRecvCallbacks
Definition: vlInetSocket.h:482
DtU64 mySentPacketCount
Definition: vlInetSocket.h:488
Definition: vlInetSocket.h:121

Document ID: Generated on Mon Sep 28 00:09:32 EDT 2026 from SVN revision 295353
Copyright © 1992-2025 MAK Technologies. All Rights Reserved (www.mak.com)