FFL
1.0
Finfly Foundation Library
|
00001 /* $Id */ 00002 00003 #ifndef __FFL_IPC_SOCKET__ 00004 #define __FFL_IPC_SOCKET__ 00005 00006 #include <iostream> 00007 #include <string> 00008 #include <list> 00009 00010 #ifdef _WIN32 00011 # include <winsock2.h> 00012 # include <WS2tcpip.h> 00013 #else 00014 # include <sys/socket.h> 00015 # include <unistd.h> 00016 # include <netinet/in.h> 00017 # include <arpa/inet.h> 00018 # include <netdb.h> 00019 # include <sys/time.h> 00020 #endif // _WIN32 00021 00022 #include <co.h> 00023 00024 namespace ffl 00025 { 00026 00027 namespace ipc 00028 { 00029 00031 class socket 00032 { 00033 public: 00034 00036 class failure:public std::exception 00037 { 00038 private: 00039 00040 int code; //<! Код ошибки. 00041 00042 public: 00043 00047 failure(int _errno) throw(); 00048 00054 failure() throw(); 00055 00059 const char * what() const throw(); 00060 00061 }; 00062 00064 class connection_gracefully_closed:public std::exception 00065 { 00066 public: 00067 00071 const char* what() const throw(){return "Connection gracefully closed";} 00072 00073 }; 00074 00076 class io_timeout:public std::exception 00077 { 00078 public: 00079 00083 const char* what() const throw(){return "IO operation timeout";} 00084 00085 }; 00086 00087 class address; 00088 00090 class address 00091 { 00092 friend class socket; 00093 00094 sockaddr_storage Address; 00095 00096 socklen_t Length; 00097 00102 address(struct sockaddr* _a, socklen_t _l) throw(); 00103 00104 public: 00105 00107 enum family 00108 { 00109 UNSPEC = AF_UNSPEC, 00110 INET4 = AF_INET, 00111 INET6 = AF_INET6 00112 }; 00113 00115 address() throw(); 00116 00120 address(const address& _a) throw():Address(_a.Address), Length(_a.Length){} 00121 00129 enum hint 00130 { 00131 LOCAL, 00132 REMOTE, 00133 }; 00134 00146 address(const socket& _s, hint _h = LOCAL) throw(...); 00147 00149 struct info 00150 { 00152 enum hint 00153 { 00154 NOFQDN = NI_NOFQDN, 00155 NUMERICHOST = NI_NUMERICHOST, 00156 NAMEREQD = NI_NAMEREQD, 00157 NUMERICSERV = NI_NUMERICSERV, 00158 DGRAM = NI_DGRAM 00159 }; 00160 00162 typedef int hints; 00163 00164 family Family; 00165 std::string Host; 00166 std::string Service; 00167 00173 info(const address& _a, hints _h = 0) throw(...); 00174 00175 }; 00176 00177 }; 00178 00180 enum type 00181 { 00182 ANYTYPE = 0, 00183 STREAM = SOCK_STREAM, 00184 DGRAM = SOCK_DGRAM, 00185 }; 00186 00188 enum protocol 00189 { 00190 ANYPROTO = 0, 00191 ICMP = IPPROTO_ICMP, 00192 IGMP = IPPROTO_IGMP, 00193 TCP = IPPROTO_TCP, 00194 UDP = IPPROTO_UDP, 00195 ICMPV6 = IPPROTO_ICMPV6, 00196 #if(_WIN32_WINNT >= 0x0600) 00197 RM = IPPROTO_PGM, 00198 #endif 00199 }; 00200 00207 class environment 00208 { 00209 public: 00210 00214 environment() throw(...); 00215 00218 ~environment() throw(); 00219 00224 const char* spell(address::family _f) throw(); 00225 00230 const char* spell(type _t) throw(); 00231 00236 const char* spell(protocol _p) throw(); 00237 00238 }; 00239 00241 enum hint 00242 { 00249 PASSIVE = AI_PASSIVE, 00254 CANONNAME = AI_CANONNAME, 00255 NUMERICHOST = AI_NUMERICHOST, 00256 ADDRCONFIG = AI_ADDRCONFIG, 00257 NON_AUTHORITATIVE = AI_NON_AUTHORITATIVE, 00258 SECURE = AI_SECURE, 00259 RETURN_PREFERRED_NAMES = AI_RETURN_PREFERRED_NAMES 00260 }; 00261 00262 typedef int hints; 00263 00265 struct info 00266 { 00267 address::family Family; 00268 type Type; 00269 protocol Protocol; 00270 address Address; 00271 std::string Name; 00272 00274 info() throw() {} 00275 00283 info 00284 ( 00285 address::family _f, 00286 type _t, 00287 protocol _p, 00288 address _a, 00289 const char* _n 00290 ) throw() :Family(_f), Type(_t), Protocol(_p), Address(_a), Name(_n ? _n:""){} 00291 00293 info(const info& _info) throw() :Family(_info.Family), Type(_info.Type), Protocol(_info.Protocol), Address(_info.Address), Name(_info.Name){} 00294 00295 }; 00296 00317 static void find 00318 ( 00319 environment& _e, 00320 std::list<info>& _r, 00321 const char* _n, 00322 const char* _s, 00323 hints _h = 0, 00324 address::family _f = address::UNSPEC, 00325 type _t = ANYTYPE, 00326 protocol _p = ANYPROTO 00327 ) throw(...); 00328 00333 class set 00334 { 00335 00336 friend class socket; 00337 00338 fd_set Set; 00339 00340 #ifndef _WIN32 00341 int maxfd; 00342 #endif 00343 00344 public: 00345 00347 set() throw() 00348 { 00349 FD_ZERO(&Set); 00350 #ifndef _WIN32 00351 maxfd = 0; 00352 #endif 00353 } 00354 00356 void zero() throw() 00357 { 00358 FD_ZERO(&Set); 00359 #ifndef _WIN32 00360 maxfd = 0; 00361 #endif 00362 } 00363 00364 set& operator +=(socket* _s) throw() 00365 { 00366 FD_SET(_s->Socket, &Set); 00367 #ifndef _WIN32 00368 if(_s->Socket > maxfd) 00369 maxfd = _s->Socket; 00370 #endif 00371 return *this; 00372 } 00373 00377 bool contains(socket* _s) throw(){return FD_ISSET(_s->Socket, &Set) != 0;} 00378 00379 }; 00380 00384 socket(environment& _e) throw(); 00385 00393 socket(environment& _e, address::family _f, type _t, protocol _p = ANYPROTO) throw(...); 00394 00405 socket(environment& _e, socket& _s) throw(...); 00406 00415 socket(const socket& _s) throw():Socket(_s.Socket){} 00416 00422 virtual ~socket(); 00423 00434 unsigned char get() throw(...); 00435 00436 unsigned char get(long _t) throw(...); 00437 00450 size_t read(char* _b, size_t _l) throw(...); 00451 00464 void read(char* _b, size_t _l, long _t) throw(...); 00465 00475 void put(unsigned char _c) throw(...); 00476 00477 void put(unsigned char _c, long _t) throw(...); 00478 00490 size_t write(const char* _b, size_t _l) throw(...); 00491 00502 void write(const char* _b, size_t _l, long _timeout) throw(...); 00503 00504 void write(const co_table& _t) throw(...); 00505 00506 void write(const co_table& _t, long _timeout) throw(...); 00507 00508 void read(co_table& _t) throw(...); 00509 00510 void read(co_table& _t, long _timeout) throw(...); 00511 00521 void blocking(bool _flag = true) throw(...); 00522 00527 void bind(const address& _a) throw(...); 00528 00534 static const int MaxBackLog; 00535 00546 void listen(int _backlog = MaxBackLog) throw(...); 00547 00552 void connect(const address& _a) throw(...); 00553 00589 static size_t wait(socket::set* _r, socket::set* _w, socket::set* _e, long _t) throw(...); 00590 00592 enum parameter 00593 { 00594 RCVBUF = SO_RCVBUF, 00595 SNDBUF = SO_SNDBUF, 00596 }; 00597 00603 int option(parameter _p) throw(...); 00604 00605 void option(parameter _p, int _v) throw(...); 00606 00607 protected: 00608 00610 #ifdef _WIN32 00611 SOCKET Socket; 00612 #else 00613 int Socket; 00614 #endif 00615 00616 }; 00617 00618 } 00619 00620 } 00621 00622 #endif // __FFL_IPC_SOCKET__ 00623 00624 00625 00626 00627 00628