cisst-saw
Loading...
Searching...
No Matches
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
13This software is provided "as is" under an open source license, with
14no warranty. The complete license can be found in license.txt and
15http://www.cisst.org/cisst/license.txt.
16
17--- end cisst license ---
18*/
19
55
56#ifndef _osaSocket_h
57#define _osaSocket_h
58
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
76class osaSocket;
77
78template <class _element, class _trait = std::char_traits<_element> >
79class 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
119template <class _element, class _trait>
120int osaSocketStreambuf<_element, _trait>::sync(void)
121{
122 // do nothing, flush on this->socket?
123 return 0;
124}
125
126
127template <class _element, class _trait>
128std::streamsize
129osaSocketStreambuf<_element, _trait>::xsputn(const _element *s, std::streamsize n)
130{
131 return this->Socket->Send(s, n);
132}
133
134
135template <class _element, class _trait>
136std::streamsize
137osaSocketStreambuf<_element, _trait>::xsgetn(_element * s, std::streamsize n)
138{
139 return this->Socket->Receive(s, n);
140}
141
142
143template <class _element, class _trait>
144typename osaSocketStreambuf<_element, _trait>::int_type
145osaSocketStreambuf<_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{
176 CMN_DECLARE_SERVICES(CMN_NO_DYNAMIC_CREATION, CMN_LOG_ALLOW_DEFAULT);
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
194
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
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
356
357 friend class osaSocketServer;
358};
359
361
362#endif // _osaSocket_h
Base class for high level objects.
Definition cmnGenericObject.h:53
Definition osaSocket.h:175
bool Connect(const std::string &host, unsigned short port)
Connect to the server; required for TCP sockets; includes call to SetDestination().
bool Connect(void)
Connect to the server; required for TCP sockets and should be used after SetDestination().
bool GetDestination(osaIPandPort &ip_port) const
Get the destination address for UDP or TCP socket. This is particularly useful for programs using UDP...
static int GetLocalhostIP(std::vector< std::string > &IPaddress)
Retrieve IP address of the localhost as string from each network interface (which may be more than tw...
void SetDestination(const std::string &host, unsigned short port)
Set the destination address for UDP or TCP socket.
static std::string GetLocalhostIP(void)
int SendAsPackets(const char *bufsend, unsigned int msglen, unsigned int packetSize, double timeoutSec=0.0)
Send a byte array via the socket, possibly in multiple packets based on the specified maximum packet_...
int Receive(char *bufrecv, unsigned int maxlen, double timeoutSec=0.0)
Receive a byte array via the socket.
bool GetDestination(std::string &host, unsigned short &port) const
Get the destination address for UDP or TCP socket. This is particularly useful for programs using UDP...
~osaSocket(void)
Destructor.
bool Connect(const osaIPandPort &ip_port)
Connect to the server; required for TCP sockets; includes call to SetDestination().
osaSocket(SocketTypes type=TCP)
Default constructor.
int SocketFD
Definition osaSocket.h:354
bool IsConnected(void)
bool Connected
Definition osaSocket.h:355
unsigned long GetIP(const std::string &host) const
int GetIdentifier(void) const
Definition osaSocket.h:199
friend class osaSocketServer
Definition osaSocket.h:357
int Send(const char *bufsend, unsigned int msglen, double timeoutSec=0.0)
Send a byte array via the socket.
bool AssignPort(unsigned short port)
Sets the port of a UDP server.
int SendAsPackets(const std::string &bufsend, unsigned int packetSize, double timeoutSec=0.0)
Send a string via the socket, possibly in multiple packets based on the specified maximum packet_size...
void SetDestination(const osaIPandPort &ip_port)
Set the destination address for UDP or TCP socket.
SocketTypes SocketType
Definition osaSocket.h:353
bool Close(void)
Close the socket.
osaSocket(void *socketFDPtr)
osaSocketServer constructor (for use by osaSocketServer)
SocketTypes
Definition osaSocket.h:190
@ TCP
Definition osaSocket.h:190
@ UDP
Definition osaSocket.h:190
int Send(const std::string &bufsend, double timeoutSec=0.0)
Send a string via the socket.
int ReceiveAsPackets(std::string &bufrecv, char *packetBuffer, unsigned int packetSize, double timeoutStartSec=0.0, double timeoutNextSec=0.0)
Receive a string via the socket, possibly in multiple packets. This method can be used with both UDP ...
Assert macros definitions.
#define CMN_ASSERT(expr)
Definition cmnAssert.h:97
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition cmnClassRegisterMacros.h:199
const int CMN_NO_DYNAMIC_CREATION
Definition cmnClassRegisterMacros.h:325
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Defines cmnGenericObject.
#define CMN_LOG_ALLOW_DEFAULT
Definition cmnLogLoD.h:76
Declaration of cmnLogger amd macros for human readable logging.
Portability across compilers and operating systems tools.
Macros to export the symbols of cisstOSAbstraction (in a Dll).
Definition osaSocket.h:156
osaIPandPort()
Definition osaSocket.h:160
osaIPandPort(const std::string &ip, short port)
Definition osaSocket.h:161
std::string IP
Definition osaSocket.h:157
~osaIPandPort()
Definition osaSocket.h:162
unsigned short Port
Definition osaSocket.h:158
bool operator==(const vctDynamicConstMatrixBase< __matrixOwnerType, _elementType > &otherMatrix) const
Definition vctDynamicConstMatrixBase.h:715
bool operator!=(const vctDynamicConstMatrixBase< __matrixOwnerType, _elementType > &otherMatrix) const
Definition vctDynamicConstMatrixBase.h:743