00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _usocket_hpp_
00021 #define _usocket_hpp_
00022
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
00082
00083
00084
00085
00086
00087 bool isConnected() const {return sock >= 0;}
00088
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
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
00185
00186
00187
00188
00189
00190
00191
00192
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
00205
00206
00207 int getLocalPort() const {return listen_port;}
00208
00209
00210 int getDescriptor() const {return listen_sock;}
00211
00212
00213 protected:
00214 int listen_port, listen_sock;
00215 struct sockaddr_in* sin;
00216 UInput* input;
00217 };
00218
00219
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;
00246 unsigned int bufsize;
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
00279
00280
00281