cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
osaSocket.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Peter Kazanzides, Ali Uneri
7  Created on: 2009
8 
9  (C) Copyright 2007-2013 Johns Hopkins University (JHU), All Rights Reserved.
10 
11 --- begin cisst license - do not edit ---
12 
13 This software is provided "as is" under an open source license, with
14 no warranty. The complete license can be found in license.txt and
15 http://www.cisst.org/cisst/license.txt.
16 
17 --- end cisst license ---
18 */
19 
56 #ifndef _osaSocket_h
57 #define _osaSocket_h
58 
59 #include <cisstCommon/cmnAssert.h>
61 #include <cisstCommon/cmnLogger.h>
63 // Always include last
65 
66 #if (CISST_OS != CISST_WINDOWS)
67 #define INVALID_SOCKET -1
68 #define SOCKET_ERROR -1
69 #define SOCKET int
70 #endif
71 
72 //#define OSA_SOCKET_WITH_STREAM
73 
74 #ifdef OSA_SOCKET_WITH_STREAM
75 // forward declaration
76 class osaSocket;
77 
78 template <class _element, class _trait = std::char_traits<_element> >
79 class osaSocketStreambuf: public std::basic_streambuf<_element, _trait>
80 {
81  public:
82 
83  typedef std::basic_streambuf<_element, _trait> BaseClassType;
84 
85  osaSocketStreambuf(osaSocket * socket):
86  Socket(socket)
87  {
88  CMN_ASSERT(this->Socket);
89  }
90 
91  protected:
92  typedef typename std::basic_streambuf<_element, _trait>::int_type int_type;
93 
96  virtual int sync(void);
97 
100  virtual std::streamsize xsputn(const _element * s, std::streamsize n);
101 
104  virtual std::streamsize xsgetn(_element * s, std::streamsize n);
105 
111  virtual int_type overflow(int_type c = _trait::eof());
112 
113  private:
114  osaSocket * Socket;
115 };
116 
117 
118 
119 template <class _element, class _trait>
120 int osaSocketStreambuf<_element, _trait>::sync(void)
121 {
122  // do nothing, flush on this->socket?
123  return 0;
124 }
125 
126 
127 template <class _element, class _trait>
128 std::streamsize
129 osaSocketStreambuf<_element, _trait>::xsputn(const _element *s, std::streamsize n)
130 {
131  return this->Socket->Send(s, n);
132 }
133 
134 
135 template <class _element, class _trait>
136 std::streamsize
137 osaSocketStreambuf<_element, _trait>::xsgetn(_element * s, std::streamsize n)
138 {
139  return this->Socket->Receive(s, n);
140 }
141 
142 
143 template <class _element, class _trait>
144 typename osaSocketStreambuf<_element, _trait>::int_type
145 osaSocketStreambuf<_element, _trait>::overflow(int_type c)
146 {
147  // follow the basic_streambuf standard
148  if (_trait::eq_int_type(_trait::eof(), c))
149  return (_trait::not_eof(c));
150  char cCopy = _trait::to_char_type(c);
151  return this->Socket->Send(&cCopy, 1);
152 }
153 
154 #endif // OSA_SOCKET_WITH_STREAM
155 
157  std::string IP;
158  unsigned short Port;
159 
160  osaIPandPort() : IP(""), Port(0) {}
161  osaIPandPort(const std::string &ip, short port) : IP(ip), Port(port) {}
163 
164  bool operator == (const osaIPandPort &other) const;
165  bool operator != (const osaIPandPort &other) const;
166 
167  // For STL maps and sets
168  bool operator < (const osaIPandPort &other) const;
169 };
170 
172 #ifdef OSA_SOCKET_WITH_STREAM
173 , public std::iostream
174 #endif // OSA_SOCKET_WITH_STREAM
175 {
177 
178  enum { INTERNALS_SIZE = 16 };
179  char Internals[INTERNALS_SIZE];
180 
183  static unsigned int SizeOfInternals(void);
184 
185 #ifdef OSA_SOCKET_WITH_STREAM
186  osaSocketStreambuf<char> Streambuf;
187 #endif // OSA_SOCKET_WITH_STREAM
188 
189  public:
190  enum SocketTypes { UDP, TCP };
191 
193  osaSocket(SocketTypes type = TCP);
194 
196  ~osaSocket(void);
197 
199  int GetIdentifier(void) const {
200  return SocketFD;
201  };
202 
204  static std::string GetLocalhostIP(void);
205 
210  static int GetLocalhostIP(std::vector<std::string> & IPaddress);
211 
213  bool AssignPort(unsigned short port);
214 
218  void SetDestination(const std::string & host, unsigned short port);
219 
222  void SetDestination(const osaIPandPort & ip_port);
223 
231  bool GetDestination(std::string & host, unsigned short &port) const;
232 
239  bool GetDestination(osaIPandPort & ip_port) const;
240 
244  bool Connect(void);
245 
251  bool Connect(const std::string & host, unsigned short port);
252 
257  bool Connect(const osaIPandPort & ip_port);
258 
268  int Send(const char * bufsend, unsigned int msglen, double timeoutSec = 0.0 );
269 
274  int Send(const std::string & bufsend, double timeoutSec = 0.0 );
275 
285  int SendAsPackets(const char * bufsend, unsigned int msglen, unsigned int packetSize, double timeoutSec = 0.0);
286 
297  int SendAsPackets(const std::string & bufsend, unsigned int packetSize, double timeoutSec = 0.0);
298 
305  int Receive(char * bufrecv, unsigned int maxlen, double timeoutSec = 0.0);
306 
324  int ReceiveAsPackets(std::string & bufrecv, char *packetBuffer, unsigned int packetSize,
325  double timeoutStartSec = 0.0, double timeoutNextSec = 0.0);
326 
329  bool Close(void);
330 
333  bool IsConnected(void);
334 
335 #ifdef OSA_SOCKET_WITH_STREAM
336 
337  virtual std::basic_streambuf<char> * rdbuf(void) {
338  return &Streambuf;
339  }
340 #endif // OSA_SOCKET_WITH_STREAM
341 
342  protected:
343 
348  osaSocket(void * socketFDPtr);
349 
351  unsigned long GetIP(const std::string & host) const;
352 
354  int SocketFD;
355  bool Connected;
356 
357  friend class osaSocketServer;
358 };
359 
361 
362 #endif // _osaSocket_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
Assert macros definitions.
~osaIPandPort()
Definition: osaSocket.h:162
#define CMN_ASSERT(expr)
Definition: cmnAssert.h:90
SocketTypes SocketType
Definition: osaSocket.h:353
Portability across compilers and operating systems tools.
Base class for high level objects.
Definition: cmnGenericObject.h:51
Definition: osaSocketServer.h:52
int SocketFD
Definition: osaSocket.h:354
CMN_DECLARE_SERVICES_INSTANTIATION(osaSocket)
bool Connected
Definition: osaSocket.h:355
Declaration of cmnLogger amd macros for human readable logging.
unsigned short Port
Definition: osaSocket.h:158
Defines cmnGenericObject.
SocketTypes
Definition: osaSocket.h:190
Macros to export the symbols of cisstOSAbstraction (in a Dll).
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
osaIPandPort(const std::string &ip, short port)
Definition: osaSocket.h:161
std::string IP
Definition: osaSocket.h:157
osaIPandPort()
Definition: osaSocket.h:160
Definition: osaSocket.h:156
Definition: osaSocket.h:171
Definition: osaSocket.h:190
int Send(const char *bufsend, unsigned int msglen, double timeoutSec=0.0)
Send a byte array via the socket.
int GetIdentifier(void) const
Definition: osaSocket.h:199
const int CMN_NO_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:328
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76