AGXDynamics  2.31.1.0
Socket.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 /* The MIT License:
18 
19 Copyright (c) 2008 Ivan Gagis
20 
21 Permission is hereby granted, free of charge, to any person obtaining a copy
22 of this software and associated documentation files (the "Software"), to deal
23 in the Software without restriction, including without limitation the rights
24 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 copies of the Software, and to permit persons to whom the Software is
26 furnished to do so, subject to the following conditions:
27 
28 The above copyright notice and this permission notice shall be included in
29 all copies or substantial portions of the Software.
30 
31 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 THE SOFTWARE. */
38 
39 // (c) Ivan Gagis 2008
40 // e-mail: igagis\gmail.com
41 
42 // Description:
43 // cross platform C++ Sockets wrapper
44 //
45 
46 
47 #ifndef AGXNET_SOCKET_H
48 #define AGXNET_SOCKET_H
49 
50 
51 #ifdef _MSC_VER
52 # pragma warning( disable : 4290)
53 # pragma warning( disable: 4275 ) // warning C4275: non dll-interface class
54 #endif
55 
56 
57 #include <exception>
58 #include <stdexcept>
59 #include <new>
60 #include <agxNet/FramePacket.h>
61 #include <agx/stdint.h>
62 #include <agx/agxCore_export.h>
63 #include <agx/Singleton.h>
64 #include <agx/Referenced.h>
65 #include <agx/Vector.h>
66 #include <agx/String.h>
67 #include <iosfwd>
68 
73 namespace agxNet{
74 
75  //forward declarations
76  class SocketSet;
77  class IPAddress;
78  class Socket;
79 
84  class AGXCORE_EXPORT NetError : public std::runtime_error {
85  public:
90  NetError(const char* message) : std::runtime_error(message), m_socket(nullptr) {}
91  NetError(Socket *socket) : std::runtime_error("Connection closed"), m_socket(socket) {}
92  virtual ~NetError() throw() {}
93 
94  Socket *getSocket() { return m_socket; }
95  private:
96  Socket *m_socket;
97  };
98 
103 
108  {
109  public:
110 
112  IPAddress();
113 
120  m_host(h),
121  m_port(p)
122  {}
123 
137  m_host(agx::UInt32(h1) + (agx::UInt32(h2)<<8) + (agx::UInt32(h3)<<16) + (agx::UInt32(h4)<<24)),
138  m_port(p)
139  {
140  }
141 
147  IPAddress(const agx::String& ip, agx::UInt16 p);
148 
155  inline bool operator==(const IPAddress& ip){
156  return (this->m_host == ip.m_host) && (this->m_port == ip.m_port);
157  }
158 
160  const agx::UInt32& host() const { return m_host; }
161 
163  agx::UInt32& host() { return m_host; }
164 
166  const agx::UInt16& port() const { return m_port; }
167 
169  agx::UInt16& port() { return m_port; }
170 
171  agx::String string() const;
172  private:
173  //parse IP address from string
174  static agx::UInt32 parseString(const char* ip);
175 
176  agx::UInt32 m_host;
177  agx::UInt16 m_port;
178 
179  };
180 
181  std::ostream& operator<<(std::ostream& stream, const IPAddress& adr);
182 
192 
193  public:
194  Library();
195 
199  static Library *instance();
200 
205  virtual void shutdown() override;
206 
214  IPAddress getHostByName(const char *hostName, agx::UInt16 port);
215 
216  SINGLETON_CLASSNAME_METHOD();
217 
218  private:
219 
220  virtual ~Library();
221 
222  void initSockets();
223  void deinitSockets();
224 
225  static Library *s_instance;
226  bool m_initialized;
227  };
228 
236  friend class SocketSet;
237 
238  public:
239  //this type will hold system specific socket handle.
240  //this buffer should be large enough to hold socket handle in different systems.
241  //sizeof(u64) looks enough so far (need to work on 64-bit platforms too).
242 #ifndef DOC_DONT_EXTRACT //direction to doxygen not to extract this class
244  agx::UInt8 buffer[sizeof(uint64_t)];
245  };
246 #endif//~DOC_DONT_EXTRACT
247 
248  public:
249 
255  bool isValid()const;
256 
257  void setBlocking(bool flag);
258  bool isBlocking() const { return m_blocking; }
259 
268  inline bool isReady()const
269  {
270  return m_readyToReceive;
271  }
272 
273  void select(agx::UInt32 timeoutMillis = 0);
274 
275  inline bool readyToReceive() const
276  {
277  return m_readyToReceive;
278  }
279 
280  inline bool readyToSend() const
281  {
282  return m_readyToSend;
283  }
284 
285  inline const IPAddress& adr() const { return m_adr; }
286 
290  void close();
291 
292  protected:
293 
295  virtual ~Socket(){
296  this->close();
297  }
298 
299  Socket(const IPAddress& adr = IPAddress());
300 
301  Socket& operator=(const Socket& s);
302 
303  protected:
304  // bool m_isReady;
309 
311  };
312 
313 
319  class AGXCORE_EXPORT TCPSocket : public Socket {
320  friend class TCPServerSocket;
321  public:
326 
334  //copy constructor
335  TCPSocket(const TCPSocket& s, const IPAddress& adr = IPAddress()) : Socket(adr) {
336  //NOTE: that operator= calls destructor, so this->socket should be invalid, base class constructor takes care about it.
337  this->operator=(s);//same as auto_ptr
338  }
339 
347  TCPSocket(const IPAddress& ip, bool disableNaggle = false) : Socket(ip) {
348  this->open(ip, disableNaggle);
349  }
350 
358  this->Socket::operator=(s);
359  return *this;
360  }
361 
368  void open(const IPAddress& ip, bool disableNaggle = false);
369 
377  agx::UInt32 send(const agx::UInt8* data, agx::UInt32 size);
378 
388  //returns 0 if connection was closed by peer
389  size_t recv(agx::UInt8* buf, size_t maxSize);
390 
391  void sendBlocking(const agx::UInt8 *data, agx::UInt32 size);
392  void recvBlocking(agx::UInt8 *buf, agx::UInt32 size);
393 
394  void sendBlocking(const StructuredMessage *message);
395 
396  private:
397  void disableNaggle();
398  };
399 
400 
401 
409  public:
413  TCPServerSocket() : m_disableNaggle(false) {}
414 
422  //copy constructor
423  TCPServerSocket(const TCPServerSocket& s) : Socket(), m_disableNaggle(s.m_disableNaggle)
424  {
425  //NOTE: that operator= calls destructor, so this->socket should be invalid, base class constructor takes care about it.
426  this->operator=(s);//same as auto_ptr
427  }
428 
436  this->Socket::operator=(s);
437  return *this;
438  }
439 
447  TCPServerSocket(agx::UInt16 port, bool disableNaggle = false){
448  this->open(port, disableNaggle);
449  }
450 
457  void open(agx::UInt16 port, bool disableNaggle = false);
458 
468  TCPSocket* accept();
469  private:
470  bool m_disableNaggle;//this flag indicates if accepted sockets should be created with disabled Naggle
471 
472  };
473 
476  public:
478 
489  void open(agx::UInt16 port);
490 
491 
492  inline void open(){
493  this->open(0);
494  }
495 
496  //returns number of bytes sent, should be less or equal to size.
497  size_t send(const agx::UInt8* buf, agx::UInt16 size, IPAddress destinationIP);
498 
499  //returns number of bytes received, 0 if connection was gracefully closed (???).
500  agx::UInt32 recv(agx::UInt8* buf, agx::UInt16 maxSize, IPAddress &out_SenderIP);
501 
502  protected:
503  virtual ~UDPSocket(){
504  this->close();
505  }
506  };
507 
508 
519 
520  public:
521 
526  SocketSet();
527 
532  inline size_t getNumSockets()const{return m_set.size(); }
533 
538  size_t getMaxSockets()const;
539 
545  bool addSocket(Socket *sock);
546 
552  bool removeSocket(Socket *sock);
553 
564  //This function checks to see if data is available for reading on the
565  //given set of sockets. If 'timeout' is 0, it performs a quick poll,
566  //otherwise the function returns when either data is available for
567  //reading, or the timeout in milliseconds has elapsed, which ever occurs
568  //first. This function returns true if there are any sockets ready for reading,
569  //or false if there was an error with the select() system call.
570  bool checkSockets(agx::UInt32 timeoutMillis);
571 
572  const SocketRefVector& getSockets() const { return m_set; }
573 
574  protected:
575 
580  virtual ~SocketSet(){
581  m_set.clear();
582  }
583 
584  private:
585 
586  SocketRefVector m_set;
587 
588  };
589 }
590 #endif
a structure which holds IP address
Definition: Socket.h:107
Name space containing classes for sending/reading data over sockets.
Definition: MacUtil.h:22
TCPServerSocket & operator=(const TCPServerSocket &s)
Assignment operator, works similar to std::auto_ptr::operator=().
Definition: Socket.h:435
std::ostream & operator<<(std::ostream &stream, const IPAddress &adr)
void AGXPHYSICS_EXPORT shutdown()
Shutdown agx.
IPAddress(agx::UInt32 h, agx::UInt16 p)
Create IP address specifying exact ip address and port number.
Definition: Socket.h:119
bool m_readyToSend
Definition: Socket.h:306
static agx::String parseString(agx::UInt8 *&buffer, agx::UInt8 *)
const agx::UInt32 & host() const
Definition: Socket.h:160
bool operator==(const IPAddress &ip)
compares two IP addresses for equality.
Definition: Socket.h:155
virtual ~NetError()
Definition: Socket.h:92
uint8_t UInt8
Definition: Integer.h:38
STL namespace.
virtual ~SocketSet()
Destroys the socket set.
Definition: Socket.h:580
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
size_t getNumSockets() const
Returns number of sockets the socket set currently holds.
Definition: Socket.h:532
virtual ~UDPSocket()
Definition: Socket.h:503
Base class providing referencing counted objects.
Definition: Referenced.h:113
SystemIndependentSocketHandle m_socket
Definition: Socket.h:310
Socket & operator=(const Socket &s)
Library singleton class.
Definition: Socket.h:191
const SocketRefVector & getSockets() const
Definition: Socket.h:572
TCPServerSocket(agx::UInt16 port, bool disableNaggle=false)
A constructor which automatically calls TCPServerSocket::Open() method.
Definition: Socket.h:447
TCPServerSocket()
Creates an invalid (unopened) TCP server socket.
Definition: Socket.h:413
IPAddress(agx::UInt8 h1, agx::UInt8 h2, agx::UInt8 h3, agx::UInt8 h4, agx::UInt16 p)
Create IP address specifying exact ip address as 4 bytes and port number.
Definition: Socket.h:136
agx::UInt16 AGXCORE_EXPORT getAvailablePort()
bool readyToSend() const
Definition: Socket.h:280
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
TCPServerSocket(const TCPServerSocket &s)
A copy constructor.
Definition: Socket.h:423
TCPSocket & operator=(const TCPSocket &s)
Assignment operator, works similar to std::auto_ptr::operator=().
Definition: Socket.h:357
bool readyToReceive() const
Definition: Socket.h:275
bool isBlocking() const
Definition: Socket.h:258
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: gl.h:5539
IPAddress m_adr
Definition: Socket.h:308
virtual ~Socket()
Destructor.
Definition: Socket.h:295
const agx::UInt16 & port() const
Definition: Socket.h:166
const IPAddress & adr() const
Definition: Socket.h:285
bool isReady() const
Tells whether there is some activity on the socket or not.
Definition: Socket.h:268
agx::UInt32 & host()
Definition: Socket.h:163
bool m_blocking
Definition: Socket.h:307
AGX_DECLARE_VECTOR_TYPES(Socket)
TCPSocket()
Constructs an invalid TCP socket object.
Definition: Socket.h:325
NetError(const char *message)
Exception constructor.
Definition: Socket.h:90
Socket * getSocket()
Definition: Socket.h:94
TCPSocket(const TCPSocket &s, const IPAddress &adr=IPAddress())
A copy constructor.
Definition: Socket.h:335
bool m_readyToReceive
Definition: Socket.h:305
agx::UInt16 & port()
Definition: Socket.h:169
uint16_t UInt16
Definition: Integer.h:39
NetError(Socket *socket)
Definition: Socket.h:91
TCPSocket(const IPAddress &ip, bool disableNaggle=false)
A constructor which automatically calls TCPSocket::Open() method.
Definition: Socket.h:347
Basic exception class.
Definition: Socket.h:84
uint32_t UInt32
Definition: Integer.h:40
#define AGXCORE_EXPORT
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
Definition: Singleton.h:30