Ginga  0.13.6
 All Classes Namespaces Functions Variables
PracticalSocket.h
1 /*
2  * C++ sockets on Unix and Windows
3  * Copyright (C) 2002
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef __PRACTICALSOCKET_INCLUDED__
21 #define __PRACTICALSOCKET_INCLUDED__
22 
23 #include <string> // For string
24 #include <exception> // For exception class
25 using namespace std;
26 
27 namespace br {
28 namespace pucrio {
29 namespace telemidia {
30 namespace ginga {
31 namespace core {
32 namespace system {
33 namespace compat {
34 
38 class SocketException : public exception {
39 public:
46  SocketException(const string &message, bool inclSysMsg = false) throw();
47 
51  ~SocketException() throw();
52 
57  const char *what() const throw();
58 
59 private:
60  string userMessage; // Exception message
61 };
62 
66 class Socket {
67 public:
71  ~Socket();
72 
78  string getLocalAddress() throw(SocketException);
79 
85  unsigned short getLocalPort() throw(SocketException);
86 
93  void setLocalPort(unsigned short localPort) throw(SocketException);
94 
103  void setLocalAddressAndPort(const string &localAddress,
104  unsigned short localPort = 0) throw(SocketException);
105 
119  static void cleanUp() throw(SocketException);
120 
127  static unsigned short resolveService(const string &service,
128  const string &protocol = "tcp");
129 
130 private:
131  // Prevent the user from trying to use value semantics on this object
132  Socket(const Socket &sock);
133  void operator=(const Socket &sock);
134 
135 protected:
136  int sockDesc; // Socket descriptor
137  Socket(int type, int protocol) throw(SocketException);
138  Socket(int sockDesc);
139 };
140 
144 class CommunicatingSocket : public Socket {
145 public:
153  void connect(const string &foreignAddress, unsigned short foreignPort)
154  throw(SocketException);
155 
163  void send(const void *buffer, int bufferLen) throw(SocketException);
164 
173  int recv(void *buffer, int bufferLen) throw(SocketException);
174 
180  string getForeignAddress() throw(SocketException);
181 
187  unsigned short getForeignPort() throw(SocketException);
188 
189 protected:
190  CommunicatingSocket(int type, int protocol) throw(SocketException);
191  CommunicatingSocket(int newConnSD);
192 };
193 
198 public:
203  TCPSocket() throw(SocketException);
204 
212  TCPSocket(const string &foreignAddress, unsigned short foreignPort)
213  throw(SocketException);
214 
215 private:
216  // Access for TCPServerSocket::accept() connection creation
217  friend class TCPServerSocket;
218  TCPSocket(int newConnSD);
219 };
220 
224 class TCPServerSocket : public Socket {
225 public:
235  TCPServerSocket(unsigned short localPort, int queueLen = 5)
236  throw(SocketException);
237 
247  TCPServerSocket(const string &localAddress, unsigned short localPort,
248  int queueLen = 5) throw(SocketException);
249 
255  TCPSocket *accept() throw(SocketException);
256 
257 private:
258  void setListen(int queueLen) throw(SocketException);
259 };
260 
265 public:
270  UDPSocket() throw(SocketException);
271 
277  UDPSocket(unsigned short localPort) throw(SocketException);
278 
285  UDPSocket(const string &localAddress, unsigned short localPort)
286  throw(SocketException);
287 
293  void disconnect() throw(SocketException);
294 
305  void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
306  unsigned short foreignPort) throw(SocketException);
307 
318  int recvFrom(void *buffer, int bufferLen, string &sourceAddress,
319  unsigned short &sourcePort) throw(SocketException);
320 
326  void setMulticastTTL(unsigned char multicastTTL) throw(SocketException);
327 
333  void joinGroup(const string &multicastGroup) throw(SocketException);
334 
340  void leaveGroup(const string &multicastGroup) throw(SocketException);
341 
345  string getBroadcastAddress() throw(SocketException);
346 
351  unsigned int getLocalIPAddress() throw(SocketException);
352 
358  void setReuseAddr(bool reuse) throw(SocketException);
359 
365  void setMulticastLoop(bool loop) throw(SocketException);
366 
373  int select_t(int sec, int usec);
374 
379  void setNonBlocking(bool nonblock);
380 
381 
382 
383 private:
384  void setBroadcast();
385  int BLOCKING_MODE;
386 };
387 
388 }
389 }
390 }
391 }
392 }
393 }
394 }
395 
396 #endif