BioInt
1.02.00
BioInt: An integrative biological object-oriented application framework and interpreter
|
00001 #ifndef BIOSOCKETBASE_H 00002 #define BIOSOCKETBASE_H 00003 00004 #include "BioSocketAddress.h" 00005 00006 struct BioSocket 00007 { 00008 00010 enum format { 00011 pf_unspec = PF_UNSPEC, 00012 pf_unix = PF_UNIX, 00013 pf_inet = PF_INET, 00014 // pf_inet6 = PF_INET6, ///< IPv6 format 00015 pf_max = PF_MAX 00016 }; 00017 00019 enum type { 00020 sock_stream = SOCK_STREAM, 00021 sock_dgram = SOCK_DGRAM, 00022 sock_raw = SOCK_RAW, 00023 sock_rdm = SOCK_RDM, 00024 sock_seqpacket = SOCK_SEQPACKET 00025 }; 00026 00028 enum protocol { 00029 proto_ip = IPPROTO_IP, 00030 proto_icmp = IPPROTO_ICMP, 00031 proto_tcp = IPPROTO_TCP, 00032 proto_pup = IPPROTO_PUP, 00033 proto_udp = IPPROTO_UDP, 00034 proto_idp = IPPROTO_IDP, 00035 proto_raw = IPPROTO_RAW, 00036 proto_max = IPPROTO_MAX 00037 /* TODO (DanielKO#2#): Support IPv6 protocol options */ 00038 }; 00039 00040 enum { 00041 invalid_socket = INVALID_SOCKET 00042 }; 00043 00045 enum msgflag { 00046 msg_none = 0, 00047 msg_oob = MSG_OOB, 00048 msg_peek = MSG_PEEK, 00049 msg_dontroute = MSG_DONTROUTE 00050 }; 00051 00053 enum shuthow { 00054 shut_recv = 0, 00055 shut_send = 1, 00056 shut_both = 2 00057 }; 00058 00060 enum errors { 00061 socket_error = SOCKET_ERROR, 00062 00063 eintr = WSAEINTR, 00064 ebadf = WSAEBADF, 00065 eacces = WSAEACCES, 00066 efault = WSAEFAULT, 00067 einval = WSAEINVAL, 00068 emfile = WSAEMFILE, 00069 ewouldblock = WSAEWOULDBLOCK, 00070 einprogress = WSAEINPROGRESS, 00071 ealready = WSAEALREADY, 00072 enotsock = WSAENOTSOCK, 00073 edestaddrreq = WSAEDESTADDRREQ, 00074 emsgsize = WSAEMSGSIZE, 00075 eprototype = WSAEPROTOTYPE, 00076 enoprotoopt = WSAENOPROTOOPT, 00077 eprotonosupport = WSAEPROTONOSUPPORT, 00078 esocktnotsupport = WSAESOCKTNOSUPPORT, 00079 eopnotsupp = WSAEOPNOTSUPP, 00080 epfnosupport = WSAEPFNOSUPPORT, 00081 afnosupport = WSAEAFNOSUPPORT, 00082 eaddrinuse = WSAEADDRINUSE, 00083 eaddrnotavail = WSAEADDRNOTAVAIL, 00084 enetdown = WSAENETDOWN, 00085 enetunreach = WSAENETUNREACH, 00086 enetreset = WSAENETRESET, 00087 econnaborted = WSAECONNABORTED, 00088 econnreset = WSAECONNRESET, 00089 enobufs = WSAENOBUFS, 00090 eisconn = WSAEISCONN, 00091 enotconn = WSAENOTCONN, 00092 eshutdown = WSAESHUTDOWN, 00093 etoomanyrefs = WSAETOOMANYREFS, 00094 etimedout = WSAETIMEDOUT, 00095 econnrefused = WSAECONNREFUSED, 00096 eloop = WSAELOOP, 00097 enametoolong = WSAENAMETOOLONG, 00098 ehostdown = WSAEHOSTDOWN, 00099 ehostunreach = WSAEHOSTUNREACH, 00100 host_not_found = WSAHOST_NOT_FOUND, 00101 try_again = WSATRY_AGAIN, 00102 no_recovery = WSANO_RECOVERY, 00103 no_data = WSANO_DATA 00104 }; 00105 00107 enum ioctl_cmd { 00108 fionbio = FIONBIO, 00109 fionread = FIONREAD, 00110 siocatmark = SIOCATMARK 00111 }; 00112 00114 enum mode { 00115 passive, 00116 active 00117 }; 00118 00120 enum optlevel { 00121 sol_socket = SOL_SOCKET, 00122 ipproto_tcp = IPPROTO_TCP 00123 }; 00124 00126 enum optname { 00127 so_acceptconn = SO_ACCEPTCONN, 00128 so_broadcast = SO_BROADCAST, 00129 so_debug = SO_DEBUG, 00130 // so_dontlinger = SO_DONTLINGER, ///< (bool) If true, the so_linger option is disabled.. 00131 so_dontroute = SO_DONTROUTE, 00132 so_error = SO_ERROR, 00133 so_keepalive = SO_KEEPALIVE, 00134 so_linger = SO_LINGER, 00135 so_oobinline = SO_OOBINLINE, 00136 so_rcvbuf = SO_RCVBUF, 00137 so_reuseaddr = SO_REUSEADDR, 00138 so_sndbuf = SO_SNDBUF, 00139 so_type = SO_TYPE, 00140 tcp_nodelay = TCP_NODELAY 00141 }; 00142 00144 enum selectstate { 00145 readable = 1, 00146 writable = 2, 00147 exceptable = 4 00148 }; 00149 00150 00151 // Here are the two data members of socklibpp::sock 00152 00161 socket_type fd; 00162 00163 00171 mutable int error; 00172 00173 00180 BioSocket() throw() : fd(invalid_socket), error(0) {} 00181 00185 BioSocket(socket_type fd_) throw() : fd(fd_), error(0) {} 00186 00187 00189 BioSocket(format, type, protocol = proto_ip) throw(); 00190 00191 00193 BioSocket accept() throw(); 00194 00197 BioSocket accept(BioSocketAddress&) throw(); 00198 00199 00201 bool bind(const BioSocketAddress&) throw(); 00202 00203 00205 bool close() throw(); 00206 00207 00209 bool connect(const BioSocketAddress&) throw(); 00210 00211 00213 bool peername(BioSocketAddress&) const throw(); 00214 00215 00217 bool sockname(BioSocketAddress&) throw(); 00218 00219 00221 bool getsockopt(optlevel, optname, int&) const throw(); 00223 bool getsockopt(optlevel, optname, bool&) const throw(); 00224 00225 00227 bool ioctl(ioctl_cmd, unsigned long&) throw(); 00228 00229 00231 bool listen(int=3) throw(); 00232 00233 00235 int recv(char *, int, msgflag=msg_none) throw(); 00236 00237 00239 int recvfrom(BioSocketAddress&, char*, int, msgflag=msg_none) throw(); 00240 00241 00242 // for select() 00244 bool can_read(long=0, long=0) const throw(); 00245 00247 bool wait_read(long&, long&) const throw(); 00248 00250 bool wait_read() const throw(); 00251 00253 bool can_write(int=0, int=0) throw(); 00255 bool can_except(int=0, int=0) throw(); 00256 00258 unsigned select(int=0, int=0) throw(); 00259 00261 int send(const char*, int, msgflag=msg_none) throw(); 00262 00264 int sendto(const BioSocketAddress&, const char*, int, msgflag=msg_none) throw(); 00265 00267 bool setsockopt(optlevel, optname, int) throw(); 00268 00269 00271 bool shutdown(shuthow) throw(); 00272 00273 00275 bool create(format, type, protocol = proto_ip) throw(); 00276 00278 inline bool socket(format fmt, type tp, protocol pr = proto_ip) throw() 00279 { return create(fmt, tp, pr); } 00280 00281 00282 00283 00284 // ****************** Convenience functions ********************** 00285 00286 00288 bool sendall(const char*, int, msgflag=msg_none) throw(); 00289 00290 00292 bool recvall(char *, int, msgflag=msg_none) throw(); 00293 00294 00296 bool block(bool) throw(); 00297 00298 00300 bool reuseaddr() const throw(); 00301 00303 bool reuseaddr(bool) throw(); 00304 00305 00307 int sendbuf() const throw(); 00308 00310 void sendbuf(int) throw(); 00311 00312 00314 int recvbuf() const throw(); 00315 00317 void recvbuf(int) throw(); 00318 00319 00320 }; 00321 #endif