00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00043 #ifndef COMMONCPP_SOCKET_H_
00044 #define COMMONCPP_SOCKET_H_
00045
00046 #include <cstdio>
00047
00048 #ifndef COMMONCPP_CONFIG_H_
00049 #include <commoncpp/config.h>
00050 #endif
00051
00052 #ifndef COMMONCPP_STRING_H_
00053 #include <commoncpp/string.h>
00054 #endif
00055
00056 #ifndef COMMONCPP_ADDRESS_H_
00057 #include <commoncpp/address.h>
00058 #endif
00059
00060 #ifndef COMMONCPP_EXCEPTION_H_
00061 #include <commoncpp/exception.h>
00062 #endif
00063
00064 #ifndef MSG_DONTWAIT
00065 #define MSG_DONTWAIT 0
00066 #endif
00067
00068 #ifndef MSG_NOSIGNAL
00069 #define MSG_NOSIGNAL 0
00070 #endif
00071
00072 #ifndef SOCK_DCCP
00073 #define SOCK_DCCP 6
00074 #endif
00075 #ifndef IPPROTO_DCCP
00076 #define IPPROTO_DCCP 33
00077 #endif
00078 #ifndef SOL_DCCP
00079 #define SOL_DCCP 269
00080 #endif
00081 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
00082 #define DCCP_SOCKOPT_CCID 13
00083 #define DCCP_SOCKOPT_TX_CCID 14
00084 #define DCCP_SOCKOPT_RX_CCID 15
00085
00086 NAMESPACE_COMMONCPP
00087
00088 typedef socket_t SOCKET;
00089
00090 class __EXPORT Socket : protected ucommon::Socket
00091 {
00092 public:
00093 enum State {
00094 INITIAL,
00095 AVAILABLE,
00096 BOUND,
00097 CONNECTED,
00098 CONNECTING,
00099 STREAM
00100 };
00101 typedef enum State State;
00102
00103 enum Family {
00104 #ifdef CCXX_IPV6
00105 IPV6 = AF_INET6,
00106 #endif
00107 IPV4 = AF_INET
00108 };
00109
00110 typedef enum Family Family;
00111
00112 enum Error {
00113 errSuccess = 0,
00114 errCreateFailed,
00115 errCopyFailed,
00116 errInput,
00117 errInputInterrupt,
00118 errResourceFailure,
00119 errOutput,
00120 errOutputInterrupt,
00121 errNotConnected,
00122 errConnectRefused,
00123 errConnectRejected,
00124 errConnectTimeout,
00125 errConnectFailed,
00126 errConnectInvalid,
00127 errConnectBusy,
00128 errConnectNoRoute,
00129 errBindingFailed,
00130 errBroadcastDenied,
00131 errRoutingDenied,
00132 errKeepaliveDenied,
00133 errServiceDenied,
00134 errServiceUnavailable,
00135 errMulticastDisabled,
00136 errTimeout,
00137 errNoDelay,
00138 errExtended,
00139 errLookupFail,
00140 errSearchErr,
00141 errInvalidValue
00142 };
00143
00144 typedef enum Error Error;
00145
00146 enum Tos {
00147 tosLowDelay = 0,
00148 tosThroughput,
00149 tosReliability,
00150 tosMinCost,
00151 tosInvalid
00152 };
00153 typedef enum Tos Tos;
00154
00155 enum Pending {
00156 pendingInput,
00157 pendingOutput,
00158 pendingError
00159 };
00160 typedef enum Pending Pending;
00161
00162 private:
00163
00164 mutable Error errid;
00165 mutable const char *errstr;
00166 mutable long syserr;
00167
00168 void setSocket(void);
00169
00170 protected:
00171 static socket_t dupSocket(socket_t s,Socket::State state);
00172
00173 static Mutex mutex;
00174
00175 mutable struct {
00176 bool thrown: 1;
00177 bool broadcast: 1;
00178 bool route: 1;
00179 bool keepalive: 1;
00180 bool loopback: 1;
00181 bool multicast: 1;
00182 bool completion: 1;
00183 bool linger: 1;
00184 unsigned ttl: 8;
00185 } flags;
00186
00187 State volatile state;
00188
00197 Error error(Error error, const char *err = NULL, long systemError = 0) const;
00198
00205 inline void error(const char *err) const
00206 {error(errExtended, err);};
00207
00214 inline void setError(bool enable)
00215 {flags.thrown = !enable;};
00216
00222 void endSocket(void);
00223
00229 Error connectError(void);
00230
00234 Error sendLimit(int limit = 2048);
00235
00239 Error receiveLimit(int limit = 1);
00240
00247 Error sendTimeout(timeout_t timer);
00248
00255 Error receiveTimeout(timeout_t timer);
00256
00264 Error sendBuffer(unsigned size);
00265
00273 Error receiveBuffer(unsigned size);
00274
00282 Error bufferSize(unsigned size);
00283
00292 Error setBroadcast(bool enable);
00293
00305 Error setMulticastByFamily(bool enable, Family family = IPV4);
00306
00315 Error setLoopbackByFamily(bool enable, Family family = IPV4);
00316
00324 Error setTimeToLiveByFamily(unsigned char ttl, Family fam = IPV4);
00325
00332 Error join(const IPV4Multicast &ia);
00333 #ifdef CCXX_IPV6
00334 Error join(const IPV6Multicast &ia);
00335 #endif
00336
00343 Error drop(const IPV4Multicast &ia);
00344 #ifdef CCXX_IPV6
00345 Error drop(const IPV6Multicast &ia);
00346 #endif
00347
00355 Error setRouting(bool enable);
00356
00363 Error setNoDelay(bool enable);
00364
00376 Socket(int domain, int type, int protocol = 0);
00377
00385 Socket(socket_t fd);
00386
00390 Socket();
00391
00399 Socket(const Socket &source);
00400
00410 ssize_t readLine(char *buf, size_t len, timeout_t timeout = 0);
00411
00423 virtual ssize_t readData(void * buf,size_t len,char separator=0,timeout_t t=0);
00424
00433 virtual ssize_t writeData(const void* buf,size_t len,timeout_t t=0);
00434
00435 public:
00436 ~Socket();
00437
00444 inline Error getErrorNumber(void) const {return errid;}
00445
00452 inline const char *getErrorString(void) const {return errstr;}
00453
00454 inline long getSystemError(void) const {return syserr;}
00455
00456 const char *getSystemErrorString(void) const;
00457
00467 virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
00468
00475 static bool check(Family fam);
00476
00481 bool operator!() const;
00482
00483 operator bool() const;
00484
00488 Socket &operator=(const Socket &from);
00489
00499 virtual IPV4Host getIPV4Sender(tpport_t *port = NULL) const;
00500
00501 inline IPV4Host getSender(tpport_t *port = NULL) const
00502 {return getIPV4Sender(port);}
00503
00504 #ifdef CCXX_IPV6
00505 virtual IPV6Host getIPV6Sender(tpport_t *port = NULL) const;
00506 #endif
00507
00517 IPV4Host getIPV4Peer(tpport_t *port = NULL) const;
00518
00519 inline IPV4Host getPeer(tpport_t *port = NULL) const
00520 {return getIPV4Peer(port);}
00521
00522 #ifdef CCXX_IPV6
00523 IPV6Host getIPV6Peer(tpport_t *port = NULL) const;
00524 #endif
00525
00533 IPV4Host getIPV4Local(tpport_t *port = NULL) const;
00534
00535 inline IPV4Host getLocal(tpport_t *port = NULL) const
00536 {return getIPV4Local(port);}
00537
00538 #ifdef CCXX_IPV6
00539 IPV6Host getIPV6Local(tpport_t *port = NULL) const;
00540 #endif
00541
00552 void setCompletion(bool immediate);
00553
00559 Error setLinger(bool linger);
00560
00568 Error setKeepAlive(bool enable);
00569
00578 Error setTypeOfService(Tos service);
00579
00588 bool isConnected(void) const;
00589
00597 bool isActive(void) const;
00598
00605 inline bool isBroadcast(void) const
00606 {return flags.broadcast;};
00607
00613 inline bool isRouted(void) const
00614 {return flags.route;};
00615
00616
00617 inline struct in_addr getaddress(const IPV4Address &ia)
00618 {return ia.getAddress();}
00619
00620 #ifdef CCXX_IPV6
00621 inline struct in6_addr getaddress(const IPV6Address &ia)
00622 {return ia.getAddress();}
00623 #endif
00624
00625 };
00626
00627 #if defined(CCXX_EXCEPTIONS)
00628
00629 class __EXPORT SockException : public IOException
00630 {
00631 private:
00632 Socket::Error _socketError;
00633
00634 public:
00635 inline SockException(const String &str, Socket::Error socketError, long systemError = 0) :
00636 IOException(str, systemError), _socketError(socketError) {};
00637
00638 inline Socket::Error getSocketError() const
00639 { return _socketError; }
00640 };
00641
00642 #endif
00643
00644 END_NAMESPACE
00645
00646 #endif