ubit
usocket.hpp
1 /************************************************************************
2 *
3 * usocket.hpp: simple sockets
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 | Eric Lecolinet | TELECOM ParisTech | http://www.enst.fr/~elc/ubit
6 *
7 * ***********************************************************************
8 * COPYRIGHT NOTICE :
9 * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10 * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11 * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12 * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13 * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14 * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15 * ***********************************************************************/
16 
17 #ifndef _usocket_hpp_
18 #define _usocket_hpp_ 1
19 #include <ubit/udefs.hpp>
20 #include <ubit/ustr.hpp>
21 
22 extern "C" {
23  struct sockaddr_in;
24 }
25 
26 namespace ubit {
27  class UOutbuf;
28  class UInbuf;
29 
30 /* ==================================================== ===== ======= */
64 class USocket {
65 public:
66  USocket();
67  USocket(const char* remote_host, int remote_port);
68  USocket(const UStr& remote_host, int remote_port);
69  virtual ~USocket();
70 
71  virtual int connect(const char* remote_host, int remote_port);
72  virtual void close();
73 
74  virtual void onInput(UCall&);
76 
77  bool isConnected() const {return sock >= 0;}
78  // returns the connection state of the Socket
79 
80  int getRemotePort() const {return remport;}
81  int getDescriptor() const {return sock;}
82 
83  bool sendBlock(const char* buffer, unsigned short size);
85 
86  bool sendBlock(UOutbuf&);
88 
89  bool receiveBlock(UInbuf&);
110  bool sendBytes(const char* buffer, unsigned int size);
112 
113  bool receiveBytes(char* buffer, unsigned int size);
125 protected:
126  friend class UServerSocket;
127  int remport, sock;
128  struct sockaddr_in* sin;
129  USource* input;
130 };
131 
132 /* ==================================================== [Elc] ======= */
157 public:
158  UServerSocket();
160 
161  UServerSocket(int port);
163 
164  virtual ~UServerSocket();
166 
167  virtual void onInput(UCall&);
172  virtual USocket* accept();
182  bool bind(int port, int backlog = 0, bool reuse_address = true);
183  /* binds this socket.
184  * there is no need to call this function if the constructor
185  * UServerSocket(int port) was used. bind() must only be called to bind
186  * an unbound socket created by UServerSocket() [without an arg].
187  *
188  * Args:
189  * - 'backlog' specifies how many pending connections the queue will hold
190  * (a default value will be used if this arg is <= 0)
191  * - 'reuse_address' specifies if the same address can be reused
192  */
193 
194  virtual void close();
196 
197  virtual USocket* createSocket() const {return new USocket();}
199 
200  bool isClosed() const {return listen_sock < 0;}
202 
203  int getLocalPort() const {return listen_port;}
204  //< returns the port on which the server is listening.
205 
206  int getDescriptor() const {return listen_sock;}
207  //< returns the socket descriptor on which the server is listening.
208 
209 protected:
210  int listen_port, listen_sock;
211  struct sockaddr_in* sin;
212  USource* input;
213 };
214 
215 /* ==================================================== [Elc] ======= */
218 class UIObuf {
219 public:
220  UIObuf();
221  virtual ~UIObuf();
222 
223  const char* data() const;
224  char* data();
230  unsigned int size() const;
231  unsigned int consumed() const;
232 
233  bool resize(unsigned short);
234  bool augment(unsigned short);
235 
236 protected:
237  friend class USocket;
238  enum {DEFAULT_BUFSIZE = 512, AUGMENT_QUANTUM = 2048};
239  char* buffer;
240  char default_buffer[DEFAULT_BUFSIZE];
241  unsigned int inpos, outpos; // current pos of the input and output indexes
242  unsigned int bufsize; // total memory size (int not short!)
243 };
244 
245 /* ==================================================== [Elc] ======= */
248 class UOutbuf : public UIObuf {
249 public:
250  void writeChar(char);
251  void writeChar(unsigned char);
252  void writeShort(short);
253  void writeLong(long);
254  void writeString(const UStr&);
255  void writeString(const char*);
256  void writeString(const char* s, unsigned int len);
257  void writeEvent(unsigned char event_type, unsigned char event_flow,
258  long x, long y, unsigned long detail);
259 };
260 
261  /* ==================================================== [Elc] ======= */
264 class UInbuf : public UIObuf {
265 public:
266  void readChar(char&);
267  void readChar(unsigned char&);
268  void readShort(short&);
269  void readLong(long&);
270  void readString(UStr&);
271  void readEvent(unsigned char& event_type, unsigned char& event_flow,
272  long& x, long& y, unsigned long& detail);
273 };
274 
275 }
276 #endif
277 
278 
279 
bool sendBytes(const char *buffer, unsigned int size)
see receiveBytes().
Definition: usocket.cpp:228
virtual void onInput(UCall &)
adds a callback that is fired when data is received on the socket.
Definition: usocket.cpp:177
bool sendBlock(const char *buffer, unsigned short size)
see receiveBlock().
Definition: usocket.cpp:241
UInbuf (.
Definition: usocket.hpp:264
UIObuf (.
Definition: usocket.hpp:218
bool receiveBytes(char *buffer, unsigned int size)
byte oriented I/O.
Definition: usocket.cpp:261
UOutbuf (.
Definition: usocket.hpp:248
UServerSocket.
Definition: usocket.hpp:156
virtual USocket * createSocket() const
called by accept() to create the new socket (see accept() for details).
Definition: usocket.hpp:197
bool isClosed() const
returns the closed state of the ServerSocket
Definition: usocket.hpp:200
bool receiveBlock(UInbuf &)
simplified block oriented I/O.
Definition: usocket.cpp:301
base class of callback objects for firing functions or methods.
Definition: ucall.hpp:144
a USource object fires callbacks when a file or a socket gets data.
Definition: usource.hpp:35
Ubit Simple Sockets.
Definition: usocket.hpp:64
Definition: uhardfont.hpp:31
Ubit String.
Definition: ustr.hpp:72