usocket.hpp

00001 /* ==================================================== ======== ======= *
00002 *
00003 *  usocket.hpp : simple sockets
00004 *  Ubit Project
00005 *  Part of the Ubit Toolkit: A Brick Construction Game Model for Creating GUIs
00006 *  (C) 1999-2006 EricLecolinet / ENST Paris / http://www.enst.fr/~elc/ubit
00007 *
00008 * ***********************************************************************
00009 * COPYRIGHT NOTICE :
00010 * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
00011 * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00012 * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
00013 * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
00014 * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
00015 * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
00016 * ***********************************************************************
00017 * ==================================================== [(c)Elc] ======= *
00018 * ==================================================== ======== ======= */
00019 
00020 #ifndef _usocket_hpp_
00021 #define _usocket_hpp_
00022 //pragma ident  "@(#)usocket.hpp        ubit:05.00.00"
00023 #include <ubit/ustr.hpp>
00024 
00025 extern "C" {
00026   struct sockaddr_in;
00027 }
00028 
00029 namespace ubit {
00030 
00031   class UInput;
00032   class UOutbuf;
00033   class UInbuf;
00034 
00035 /* ==================================================== ===== ======= */
00068 class USocket {
00069 public:
00070   USocket();
00071   USocket(const char* remote_host, int remote_port);
00072   USocket(const UStr& remote_host, int remote_port);
00073   virtual ~USocket();
00074 
00075   virtual int connect(const char* remote_host, int remote_port);
00076   virtual void close();
00077 
00078   virtual void onInput(UCall&);
00080 
00081   //bool isBound() const;
00082   // returns the binding state of the Socket
00083   
00084   //bool isClosed() const;
00085   // returns the closed state of the Socket
00086   
00087   bool isConnected() const {return sock >= 0;}
00088   // returns the connection state of the Socket
00089 
00090   int  getRemotePort() const {return remport;}
00091   int  getDescriptor() const {return sock;}
00092 
00093   bool sendBlock(const char* buffer, unsigned short size);
00094   bool sendBlock(UOutbuf&);
00095   bool receiveBlock(UInbuf&);
00116   bool sendBytes(const char* buffer, unsigned int size);
00117   bool receiveBytes(char* buffer, unsigned int size);
00129   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00130 protected:
00131   friend class UServerSocket;
00132   int remport, sock;
00133   struct sockaddr_in* sin;
00134   UInput* input;
00135 };
00136 
00137 /* ==================================================== [Elc] ======= */
00138 /* ==================================================== ===== ======= */
00157 class UServerSocket {
00158 public:
00159   UServerSocket();
00161   
00162   UServerSocket(int port);
00164 
00165   virtual ~UServerSocket();
00167 
00168   virtual void onInput(UCall&);
00173   virtual USocket* accept();
00183   bool bind(int port, int backlog = 0, bool reuse_address = true);
00184   /* binds this socket.
00185     * there is no need to call this function if the constructor
00186     * UServerSocket(int port) was used. bind() must only be called to bind
00187     * an unbound socket created by UServerSocket() [without an arg].
00188     *
00189     * Args:
00190     * - 'backlog' specifies how many pending connections the queue will hold
00191     *   (a default value will be used if this arg is <= 0)
00192     * - 'reuse_address' specifies if the same address can be reused
00193     */  
00194 
00195   virtual void close();
00197 
00198   virtual USocket* createSocket() const {return new USocket();}
00200 
00201   bool isClosed() const {return listen_sock < 0;}
00203   
00204   //bool isBound() const;
00205   // returns the binding state of the ServerSocket
00206   
00207   int getLocalPort()  const {return listen_port;}
00208   //< returns the port on which the server is listening.
00209   
00210   int getDescriptor() const {return listen_sock;}
00211   //< returns the socket descriptor on which the server is listening.
00212 
00213 protected:
00214   int listen_port, listen_sock;
00215   struct sockaddr_in* sin;
00216   UInput* input;
00217 };
00218 
00219 /* ==================================================== [Elc] ======= */
00220 /* ==================================================== ===== ======= */
00223 struct UIObuf {
00224   UIObuf();
00225   virtual ~UIObuf();
00226 
00227   const char* data() const;
00228   char* data();
00234   unsigned int size() const;
00235   unsigned int consumed() const;
00236 
00237   bool resize(unsigned short);
00238   bool augment(unsigned short);
00239 
00240 protected:
00241   friend class USocket;
00242   enum {DEFAULT_BUFSIZE = 512, AUGMENT_QUANTUM = 2048};
00243   char* buffer;
00244   char  default_buffer[DEFAULT_BUFSIZE];
00245   unsigned int inpos, outpos;  // current pos of the input and output indexes
00246   unsigned int bufsize;        // total memory size (int not short!)
00247 };
00248 
00249 /* ==================================================== ======== ======= */
00252 struct UOutbuf : public UIObuf {  
00253   void writeChar(char);
00254   void writeChar(unsigned char);
00255   void writeShort(short);
00256   void writeLong(long);
00257   void writeString(const UStr&);
00258   void writeString(const char*);
00259   void writeString(const char* s, unsigned int len);
00260   void writeEvent(unsigned char event_type, unsigned char event_flow,
00261                   long x, long y, unsigned long detail);
00262 };
00263 
00264 /* ==================================================== ======== ======= */
00267 struct UInbuf : public UIObuf {
00268   void readChar(char&);
00269   void readChar(unsigned char&);
00270   void readShort(short&);
00271   void readLong(long&);
00272   void readString(UStr&);
00273   void readEvent(unsigned char& event_type, unsigned char& event_flow,
00274                  long& x, long& y, unsigned long& detail);
00275 };
00276 }
00277 #endif
00278 /* ==================================================== [TheEnd] ======= */
00279 /* ==================================================== [(c)Elc] ======= */
00280 
00281 

Generated on Mon Jan 29 00:20:40 2007 for Ubit by  doxygen 1.4.7