zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Net.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
95 #ifndef ZENI_NET_H
96 #define ZENI_NET_H
97 
98 #include <Zeni/Singleton.h>
99 #include <Zeni/VLUID.h>
100 
101 #include <SDL/SDL_net.h>
102 
103 #include <cassert>
104 #include <Zeni/String.h>
105 #include <list>
106 #include <vector>
107 
108 #include <Zeni/Define.h>
109 
110 struct SDL_Thread;
111 
112 namespace Zeni {
113 
114  class ZENI_NET_DLL Net;
115 
116 #ifdef _WINDOWS
117  ZENI_NET_EXT template class ZENI_NET_DLL Singleton<Net>;
118 #endif
119 
120  class ZENI_NET_DLL Net : public Singleton<Net> {
121  friend class Singleton<Net>;
122 
123  static Net * create();
124 
125 #ifdef _WINDOWS
126 #pragma warning( push )
127 #pragma warning( disable : 4251 )
128 #endif
129  static Uninit g_uninit;
130  static Reinit g_reinit;
131 #ifdef _WINDOWS
132 #pragma warning( pop )
133 #endif
134 
135  Net();
136  ~Net();
137 
138  // Undefined
139  Net(const Net &);
140  Net & operator=(const Net &);
141 
142  public:
144  IPaddress resolve_host(const String &host, const Uint16 &port = 0);
146  String reverse_lookup(IPaddress ip);
147  };
148 
149  ZENI_NET_DLL Net & get_Net();
150 
151  class ZENI_NET_DLL TCP_Socket {
152  TCP_Socket(const TCP_Socket &);
153  TCP_Socket & operator=(const TCP_Socket &);
154 
155  public:
156  TCP_Socket(IPaddress ip);
158  ~TCP_Socket();
159 
160  IPaddress peer_address() const;
161  int try_check_socket(); // return 0 if open, -1 on socket closed
162  int check_socket(); // return 0 if open, throw Socket_Closed() on socket closed
163 
165  int try_send(const void * const &data, const Uint16 &num_bytes); // send, returning 0 on success, -1 on socket closed
166  int try_send(const String &data); // send, returning 0 on success, -1 on socket closed
167  void send(const void * const &data, const Uint16 &num_bytes); // send, returning 0 on success, throw Socket_Closed() on socket closed
168  void send(const String &data); // send, returning 0 on success, throw Socket_Closed() on socket closed
169 
171  int try_receive(void * const &data, const Uint16 &num_bytes); // receive, returning 0 on success, -1 on socket closed
172  int try_receive(String &data, const Uint16 &num_bytes); // receive, returning 0 on success, -1 on socket closed
173  int receive(void * const &data, const Uint16 &num_bytes); // receive, returning 0 on success, throw Socket_Closed() on socket closed
174  int receive(String &data, const Uint16 &num_bytes); // receive, returning 0 on success, throw Socket_Closed() on socket closed
175 
176  private:
177  TCPsocket sock;
178  SDLNet_SocketSet sockset;
179 
180  class ZENI_NET_DLL Uninit : public Event::Handler {
181  void operator()();
182 
183  Uninit * duplicate() const {
184  return new Uninit(m_sock);
185  }
186 
187  // Undefined
188  Uninit(const Uninit &);
189  Uninit operator=(const Uninit &);
190 
191  public:
192  Uninit(TCP_Socket &sock_)
193  : m_sock(sock_)
194  {
195  }
196 
197  private:
198  TCP_Socket &m_sock;
199  } m_uninit;
200  };
201 
202  class ZENI_NET_DLL TCP_Listener {
203  TCP_Listener(const TCP_Listener &);
204  TCP_Listener & operator=(const TCP_Listener &);
205 
206  public:
207  TCP_Listener(const Uint16 &port);
208  ~TCP_Listener();
209 
210  TCPsocket accept();
211 
212  private:
213  TCPsocket sock;
214 
215  class ZENI_NET_DLL Uninit : public Event::Handler {
216  void operator()();
217 
218  Uninit * duplicate() const {
219  return new Uninit(m_sock);
220  }
221 
222  // Undefined
223  Uninit(const Uninit &);
224  Uninit operator=(const Uninit &);
225 
226  public:
227  Uninit(TCP_Listener &sock_)
228  : m_sock(sock_)
229  {
230  }
231 
232  private:
233  TCP_Listener &m_sock;
234  } m_uninit;
235  };
236 
237  class ZENI_NET_DLL UDP_Socket {
238  UDP_Socket(const UDP_Socket &);
239  UDP_Socket & operator=(const UDP_Socket &);
240 
241  public:
242  UDP_Socket(const Uint16 &port);
243  virtual ~UDP_Socket();
244 
245  IPaddress peer_address() const;
246 
248  virtual void send(const IPaddress &ip, const void * const &data, const Uint16 &num_bytes);
249  virtual void send(const IPaddress &ip, const String &data);
250 
252  virtual int receive(IPaddress &ip, const void * const &data, const Uint16 &num_bytes);
253  virtual int receive(IPaddress &ip, String &data);
254 
255  private:
256  UDPsocket sock;
257 
258  class ZENI_NET_DLL Uninit : public Event::Handler {
259  void operator()();
260 
261  Uninit * duplicate() const {
262  return new Uninit(m_sock);
263  }
264 
265  // Undefined
266  Uninit(const Uninit &);
267  Uninit operator=(const Uninit &);
268 
269  public:
270  Uninit(UDP_Socket &sock_)
271  : m_sock(sock_)
272  {
273  }
274 
275  private:
276  UDP_Socket &m_sock;
277  } m_uninit;
278  };
279 
280  class ZENI_NET_DLL Split_UDP_Socket : public UDP_Socket {
282  Split_UDP_Socket & operator=(const Split_UDP_Socket &);
283 
284  struct ZENI_NET_DLL Chunk {
285  Chunk() : size(0), data(0) {}
286  ~Chunk() {delete [] data;}
287 
288  Chunk(const Chunk &rhs)
289  : size(rhs.size),
290  data(rhs.data)
291  {
292  rhs.size = 0;
293  rhs.data = 0;
294  }
295 
296  Chunk & operator=(const Chunk &rhs) {
297  size = rhs.size;
298  data = rhs.data;
299 
300  rhs.size = 0;
301  rhs.data = 0;
302 
303  return *this;
304  }
305 
306  mutable size_t size;
307  mutable char * data;
308  };
309 
310  class ZENI_NET_DLL Chunk_Set {
311  Chunk_Set(const Chunk_Set &);
312  Chunk_Set operator=(const Chunk_Set &);
313 
314  public:
315  IPaddress ip;
316  Nonce nonce;
317 
318 #ifdef _WINDOWS
319 #pragma warning( push )
320 #pragma warning( disable : 4251 )
321 #endif
322  std::vector<Chunk> chunks;
323 #ifdef _WINDOWS
324 #pragma warning( pop )
325 #endif
326  Uint16 chunks_arrived;
327 
328  Chunk_Set() : chunks_arrived(0u) {}
329 
330  Chunk_Set(const IPaddress &sender, const Nonce &incoming, const Uint16 &num_chunks, const Uint16 &which, Chunk &chunk);
331 
332  bool add_chunk(const IPaddress &sender, const Nonce &incoming, const Uint16 &num_chunks, const Uint16 &which, Chunk &chunk);
333  bool complete() const;
334 
335  Chunk receive() const;
336  };
337 
338  class ZENI_NET_DLL Chunk_Collector {
339  Chunk_Collector(const Chunk_Collector &);
340  Chunk_Collector operator=(const Chunk_Collector &);
341 
342  public:
343  Chunk_Collector(const Uint16 &size = ZENI_DEFAULT_CHUNK_SIZE)
344  : m_size(size)
345  {
346  assert(m_size);
347  }
348 
349  ~Chunk_Collector() {
350  for(std::list<Chunk_Set *>::iterator it = chunk_sets.begin(); it != chunk_sets.end(); ++it)
351  delete *it;
352  }
353 
354  const Chunk_Set * add_chunk(const IPaddress &sender, const Nonce &incoming, const Uint16 &num_chunks, const Uint16 &which, Chunk &chunk);
355 
356  private:
357 #ifdef _WINDOWS
358 #pragma warning( push )
359 #pragma warning( disable : 4251 )
360 #endif
361  std::list<Chunk_Set *> chunk_sets;
362 #ifdef _WINDOWS
363 #pragma warning( pop )
364 #endif
365  Uint16 m_size;
366  };
367 
368  public:
369  Split_UDP_Socket(const Uint16 &port, const Uint16 &chunk_sets = ZENI_DEFAULT_CHUNK_SETS, const Uint16 &chunk_size = ZENI_DEFAULT_CHUNK_SIZE);
370 
372  virtual void send(const IPaddress &ip, const void * const &data, const Uint16 &num_bytes);
373  virtual void send(const IPaddress &ip, const String &data);
374 
376  virtual int receive(IPaddress &ip, const void * const &data, const Uint16 &num_bytes);
377  virtual int receive(IPaddress &ip, String &data);
378 
379  private:
380  Uint16 m_chunk_size;
381 
382  Chunk_Collector m_chunk_collector;
383 
384  Nonce m_nonce_send;
385  };
386 
387  struct ZENI_NET_DLL Net_Init_Failure : public Error {
388  Net_Init_Failure() : Error("Zeni Net Failed to Initialize Correctly") {}
389  };
390 
391  struct ZENI_NET_DLL TCP_Socket_Init_Failure : public Error {
392  TCP_Socket_Init_Failure() : Error("Zeni TCP Socket Failed to Initialize Correctly") {}
393  };
394 
395  struct ZENI_NET_DLL UDP_Socket_Init_Failure : public Error {
396  UDP_Socket_Init_Failure() : Error("Zeni UDP Socket Failed to Initialize Correctly") {}
397  };
398 
399  struct ZENI_NET_DLL UDP_Packet_Overflow : public Error {
400  UDP_Packet_Overflow() : Error("Zeni UDP Packet Too Large") {}
401  };
402 
403  struct ZENI_NET_DLL Socket_Closed : public Error {
404  Socket_Closed() : Error("Zeni Socket Unexpectedly Closed") {}
405  };
406 
407 }
408 
409 #include <Zeni/Undefine.h>
410 
411 #endif
A higher level UDP_Socket.
Definition: Net.h:280
else Out of place iCCP chunk
Definition: pngrutil.c:1260
struct Chunk Chunk
struct _UDPsocket * UDPsocket
Definition: SDL_net.h:182
#define assert(x)
Definition: SDL_malloc.c:1234
Net & get_Net()
Get access to the singleton.
Definition: Net.cpp:72
#define ZENI_DEFAULT_CHUNK_SIZE
Definition: Define.h:50
The Net Singleton.
Definition: Net.h:120
A TCP Socket for sending and receiving data.
Definition: Net.h:151
struct _SDLNet_SocketSet * SDLNet_SocketSet
Definition: SDL_net.h:298
ALuint u
Definition: alMain.h:58
A UDP Socket for sending and receiving data.
Definition: Net.h:237
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
Variable Length Unique IDentifier.
Definition: VLUID.h:47
struct _TCPsocket * TCPsocket
Definition: SDL_net.h:131
TCPsocket sock
Definition: chatd.c:41
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:137
A Socket for accepting new TCP connections (in the form of TCP_Sockets)
Definition: Net.h:202
The Error Class.
Definition: Error.h:52
#define ZENI_DEFAULT_CHUNK_SETS
Definition: Define.h:51
GLsizei size
Definition: gl2ext.h:1467