00001 /** 00002 * @file scim_socket.h 00003 * @brief Socket interfaces. 00004 */ 00005 00006 /* 00007 * Smart Common Input Method 00008 * 00009 * Copyright (c) 2004 James Su <suzhe@turbolinux.com.cn> 00010 * Copyright (c) 2003 James Su <suzhe@turbolinux.com.cn> 00011 * Copyright (c) 2002 James Su <suzhe@turbolinux.com.cn> 00012 * 00013 * 00014 * This library is free software; you can redistribute it and/or 00015 * modify it under the terms of the GNU Lesser General Public 00016 * License as published by the Free Software Foundation; either 00017 * version 2 of the License, or (at your option) any later version. 00018 * 00019 * This library is distributed in the hope that it will be useful, 00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 * GNU Lesser General Public License for more details. 00023 * 00024 * You should have received a copy of the GNU Lesser General Public 00025 * License along with this program; if not, write to the 00026 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 00027 * Boston, MA 02111-1307 USA 00028 * 00029 * $Id: scim_socket.h,v 1.15 2004/02/06 07:53:15 suzhe Exp $ 00030 */ 00031 00032 #ifndef __SCIM_SOCKET_H 00033 #define __SCIM_SOCKET_H 00034 00035 namespace scim { 00036 00037 /** 00038 * @addtogroup Helper 00039 * @{ 00040 */ 00041 00042 class Socket; 00043 class SocketAddress; 00044 class SocketServer; 00045 class SocketClient; 00046 00047 typedef Slot2<void, SocketServer *, const Socket &> 00048 SocketServerSlotSocket; 00049 00050 typedef Signal2<void, SocketServer *, const Socket &> 00051 SocketServerSignalSocket; 00052 00053 class SocketError: public Exception 00054 { 00055 public: 00056 SocketError (const String& what_arg) 00057 : Exception (String("scim::Socket: ") + what_arg) { } 00058 }; 00059 00060 /** 00061 * The vaild Socket address/protocol family, 00062 * corresponding to libc PF_LOCAL/AF_LOCAL and PF_INET/AF_INET 00063 */ 00064 enum SocketFamily 00065 { 00066 SCIM_SOCKET_UNKNOWN, 00067 SCIM_SOCKET_LOCAL, 00068 SCIM_SOCKET_INET 00069 }; 00070 00071 /** 00072 * class SocketAddress encapsulates the details of 00073 * socket address, like socketaddr_un and socketaddr_in. 00074 */ 00075 class SocketAddress 00076 { 00077 class SocketAddressImpl; 00078 SocketAddressImpl *m_impl; 00079 00080 public: 00081 /** 00082 * constructor. 00083 * 00084 * @param addr the string format of the address. 00085 */ 00086 SocketAddress (const String &addr = String ()); 00087 00088 /** 00089 * copy constructor. 00090 */ 00091 SocketAddress (const SocketAddress &addr); 00092 00093 /** 00094 * destructor. 00095 */ 00096 ~SocketAddress (); 00097 00098 /** 00099 * copy operator. 00100 */ 00101 const SocketAddress& operator = (const SocketAddress &addr); 00102 00103 /** 00104 * check if this address is valid. 00105 */ 00106 bool valid () const; 00107 00108 /** 00109 * get the family of this socket address. 00110 */ 00111 SocketFamily get_family () const; 00112 00113 /** 00114 * set a new address. 00115 * 00116 * @param addr the string format of the address. 00117 */ 00118 bool set_address (const String &addr); 00119 00120 /** 00121 * get the string format of the address. 00122 */ 00123 String get_address () const; 00124 00125 /** 00126 * get the data of socket address, 00127 * used by class Socket 00128 * 00129 * @return the pointer to the data, usually a sockaddr struct. 00130 */ 00131 const void * get_data () const; 00132 00133 /** 00134 * get the length of the data. 00135 */ 00136 int get_data_length () const; 00137 }; 00138 00139 /** 00140 * class Socket provides basic operation of socket, 00141 * such as bind connect, read, write etc. 00142 * 00143 * This class cannot be created by user, it can only 00144 * be created by class SocketClient and SocketServer. 00145 */ 00146 class Socket 00147 { 00148 class SocketImpl; 00149 00150 SocketImpl *m_impl; 00151 00152 /** 00153 * null declaration of copy constructor and operator, 00154 * to prevent from being constructed by user. 00155 */ 00156 Socket (const Socket&); 00157 const Socket& operator = (const Socket&); 00158 00159 public: 00160 /** 00161 * create a Socket object from an already created socket_id. 00162 */ 00163 Socket (int id = -1); 00164 00165 /** 00166 * destructor, call close. 00167 */ 00168 ~Socket (); 00169 00170 /** 00171 * check if the socket is valid. 00172 */ 00173 bool valid () const; 00174 00175 /** 00176 * read data from socket. 00177 * 00178 * @param buf the buffer to store the data. 00179 * @param size size of the buffer. 00180 * 00181 * @return the amount of data actually read, -1 means error occurred. 00182 */ 00183 int read (void *buf, size_t size) const; 00184 00185 /** 00186 * read data from socket with a timeout. 00187 * 00188 * @param buf the buffer to store the data. 00189 * @param size size of the buffer, and the amount of data to be read. 00190 * @param timeout time out in millisecond (1/1000 second), -1 means infinity. 00191 * 00192 * @return the amount of data actually read, 00193 * 0 means the connection is closed, 00194 * -1 means error occurred. 00195 */ 00196 int read_with_timeout (void *buf, size_t size, int timeout) const; 00197 00198 /** 00199 * write data to socket. 00200 * 00201 * @param buf the buffer stores the data. 00202 * @param size size of the data to be sent. 00203 * 00204 * @return the amount of data acutally sent, or -1 if an error occurred. 00205 */ 00206 int write (const void *buf, size_t size) const; 00207 00208 /** 00209 * wait for data is ready to read. 00210 * 00211 * @param timeout time out in millisecond (1/1000 second), -1 means infinity. 00212 * 00213 * @return > 0 if data is OK, == 0 if time is out, < 0 if an error occurred. 00214 */ 00215 int wait_for_data (int timeout = -1) const; 00216 00217 /** 00218 * get the number of the last occurred error. 00219 */ 00220 int get_error_number () const; 00221 00222 /** 00223 * get the message of the last occurred error. 00224 */ 00225 String get_error_message () const; 00226 00227 /** 00228 * get the socket id. 00229 */ 00230 int get_id () const; 00231 00232 protected: 00233 00234 /** 00235 * initiate a connection on a socket. 00236 * 00237 * @param addr the address to be connected. 00238 */ 00239 bool connect (const SocketAddress &addr) const; 00240 00241 /** 00242 * bind a socket to an address, used by SocketServer. 00243 */ 00244 bool bind (const SocketAddress &addr) const; 00245 00246 /** 00247 * listen for connections on a socket. 00248 * 00249 * @param queue_length the length of the waiting queue. 00250 */ 00251 bool listen (int queue_length = 5) const; 00252 00253 /** 00254 * accept a connection on the socket, used by SocketServer. 00255 * 00256 * @return the id of the accepted socket, or -1 if an error is occurred. 00257 */ 00258 int accept () const; 00259 00260 /** 00261 * create a socket for specific family. 00262 */ 00263 bool create (SocketFamily family); 00264 00265 /** 00266 * close the socket. 00267 */ 00268 void close (); 00269 }; 00270 00271 /** 00272 * class SocketServer provides basic operations to create a Socket Server, 00273 * such as create, run etc. 00274 */ 00275 class SocketServer : private Socket 00276 { 00277 class SocketServerImpl; 00278 00279 SocketServerImpl *m_impl; 00280 00281 public: 00282 /** 00283 * default constructor, do nothing. 00284 */ 00285 SocketServer (int max_clients = -1); 00286 00287 /** 00288 * constructor. 00289 * 00290 * @param address create a server on this address. 00291 */ 00292 SocketServer (const SocketAddress &address, int max_clients = -1); 00293 00294 /** 00295 * destructor. 00296 */ 00297 ~SocketServer (); 00298 00299 /** 00300 * test if the server is valid. 00301 */ 00302 bool valid (); 00303 00304 /** 00305 * create a socket on the address. 00306 * 00307 * @param address the address to be listen. 00308 * 00309 * @return true if OK. 00310 */ 00311 bool create (const SocketAddress &address); 00312 00313 /** 00314 * run the server. 00315 */ 00316 bool run (); 00317 00318 /** 00319 * check if the server is running. 00320 */ 00321 bool is_running () const; 00322 00323 /** 00324 * shutdown the server. 00325 */ 00326 void shutdown (); 00327 00328 /** 00329 * close a connection. 00330 */ 00331 void close_connection (const Socket &socket); 00332 00333 /** 00334 * get the number of the last occurred error. 00335 */ 00336 int get_error_number () const; 00337 00338 /** 00339 * get the message of the last occurred error. 00340 */ 00341 String get_error_message () const; 00342 00343 /** 00344 * get the max number of clients. 00345 */ 00346 int get_max_clients () const; 00347 00348 /** 00349 * set the max number of clients. 00350 */ 00351 void set_max_clients (int max_clients); 00352 00353 public: 00354 /** 00355 * connect a slot to accept signal, if a client connection is accepted, 00356 * this signal will be emitted. 00357 */ 00358 Connection signal_connect_accept (SocketServerSlotSocket *slot); 00359 00360 /** 00361 * connect a slot to receive signal, if a client send data to server, 00362 * this signal will be emitted. 00363 */ 00364 Connection signal_connect_receive (SocketServerSlotSocket *slot); 00365 00366 /** 00367 * connect a slot to exception signal, if a exception was occurred 00368 * to a connection, this signal will be emitted. 00369 */ 00370 Connection signal_connect_exception (SocketServerSlotSocket *slot); 00371 }; 00372 00373 /** 00374 * class SocketClient provides basic operations to create a Socket Client, 00375 * such as connect, read, write, etc. 00376 */ 00377 class SocketClient : public Socket 00378 { 00379 bool m_connected; 00380 00381 public: 00382 /** 00383 * constructor. 00384 */ 00385 SocketClient (); 00386 00387 /** 00388 * constructor. 00389 * 00390 * @param address the server address to be connected. 00391 */ 00392 SocketClient (const SocketAddress &address); 00393 00394 /** 00395 * destructor. 00396 */ 00397 ~SocketClient (); 00398 00399 /** 00400 * check if the socket is connected. 00401 */ 00402 bool is_connected () const; 00403 00404 /** 00405 * connect to a server. 00406 * 00407 * @param address the server address. 00408 */ 00409 bool connect (const SocketAddress &address); 00410 00411 /** 00412 * close the client. 00413 */ 00414 void close (); 00415 }; 00416 00417 /** @} */ 00418 00419 } // namespace scim 00420 00421 #endif //__SCIM_SOCKET_H 00422 00423 /* 00424 vi:ts=4:nowrap:ai:expandtab 00425 */ 00426