ESA JPIP server  0.1
socket.h
Go to the documentation of this file.
1 #ifndef _NET_SOCKET_H__
2 #define _NET_SOCKET_H__
3 
4 
5 #include <sys/socket.h>
6 #include <sys/types.h>
7 #include <sys/time.h>
8 #include <sys/times.h>
9 #include <unistd.h>
10 #include <netinet/in.h>
11 #include <netinet/tcp.h>
12 #include <arpa/inet.h>
13 #include <netdb.h>
14 #include <string>
15 #include "address.h"
16 
17 
18 namespace net
19 {
20 
21  using namespace std;
22 
23 
28  class Socket
29  {
30  protected:
31  int sid;
32 
33  public:
38  {
39  sid = -1;
40  }
41 
45  Socket(int s)
46  {
47  sid = s;
48  }
49 
53  Socket(const Socket& xs)
54  {
55  sid = xs.sid;
56  }
57 
61  operator int() const
62  {
63  return sid;
64  }
65 
69  bool IsValid() const
70  {
71  return (sid != -1);
72  }
73 
77  Socket& operator=(int nsid)
78  {
79  sid = nsid;
80  return *this;
81  }
82 
89  bool OpenInet(int type = SOCK_STREAM)
90  {
91  return ((sid = socket(PF_INET, type, 0)) != -1);
92  }
93 
100  bool OpenUnix(int type = SOCK_STREAM)
101  {
102  return ((sid = socket(PF_UNIX, type, 0)) != -1);
103  }
104 
111  bool ListenAt(const Address& address, int nstack = 10)
112  {
113  int flags = 1;
114  setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
115  if(::bind(sid, address.GetSockAddr(), address.GetSize()) != 0) return false;
116  if(listen(sid, nstack) != 0) return false;
117  return true;
118  }
119 
125  bool ConnectTo(const Address& to_address)
126  {
127  return (connect(sid, to_address.GetSockAddr(), to_address.GetSize()) == 0);
128  }
129 
135  bool BindTo(const Address& address)
136  {
137  return !::bind(sid, address.GetSockAddr(), address.GetSize());
138  }
139 
145  int Accept(Address *from_address)
146  {
147  socklen_t len = from_address->GetSize();
148  return accept(sid, from_address->GetSockAddr(), &len);
149  }
150 
157  Socket& SetBlockingMode(bool state = true);
158 
162  bool IsBlockingMode();
163 
173  int Receive(void *buf, int len, bool prevent_block = false);
174 
185  int ReceiveFrom(Address *address, void *buf, int len,
186  bool prevent_block = false);
187 
197  int Send(void *buf, int len, bool prevent_block = false);
198 
209  int SendTo(const Address& address, void *buf, int len,
210  bool prevent_block = false);
211 
219  bool SendDescriptor(const Address& address, int fd, int aux = 0);
220 
225  bool IsValid();
226 
232  int WaitForInput(int time_out = -1);
233 
239  int WaitForOutput(int time_out = -1);
240 
247  bool SetNoDelay(int val = 1)
248  {
249  return !setsockopt(sid, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
250  }
251 
258  bool ReceiveDescriptor(int *fd, int *aux = NULL);
259 
263  void Close()
264  {
265  if(sid != -1) close(sid);
266  sid = -1;
267  }
268 
273  {
274  }
275  };
276 
277 }
278 
279 
280 #endif
281 
Socket & operator=(int nsid)
Copy asignment.
Definition: socket.h:77
bool IsValid() const
Definition: socket.h:69
void Close()
Closes the socket.
Definition: socket.h:263
bool BindTo(const Address &address)
Binds the socket to the specified address.
Definition: socket.h:135
bool OpenInet(int type=SOCK_STREAM)
This method creates a new Internet socket, storing its identifier in the object.
Definition: socket.h:89
bool OpenUnix(int type=SOCK_STREAM)
This method creates a new UNIX socket, storing its identifier in the object.
Definition: socket.h:100
bool SetNoDelay(int val=1)
Configures the parameter TCP_NODELAY of the socket.
Definition: socket.h:247
STL namespace.
int sid
Socket id.
Definition: socket.h:31
Socket()
Initializes the socket id with an invalid value.
Definition: socket.h:37
Contains classes to easy the utilization of sockets, specially implemented for UNIX systems...
Definition: address.h:15
~Socket()
The destructor does not closes the socket!.
Definition: socket.h:272
Socket(const Socket &xs)
Copy constructor.
Definition: socket.h:53
virtual int GetSize() const =0
Returns the size in bytes of the sockaddr structure returned by the previous method.
int Accept(Address *from_address)
If it is a server socket, it accepts a new connection.
Definition: socket.h:145
virtual sockaddr * GetSockAddr() const =0
Returns a pointer to a sockaddr structure.
Abstract base class to wrap the sockaddr derived structures.
Definition: address.h:28
bool ListenAt(const Address &address, int nstack=10)
Configures the socket for listening incoming connections.
Definition: socket.h:111
Socket(int s)
Initializes the socket id with an integer value.
Definition: socket.h:45
bool ConnectTo(const Address &to_address)
Connects the socket to a server.
Definition: socket.h:125
This class has been designed to work with UNIX sockets in an easy and object oriented way...
Definition: socket.h:28