Socket.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_Socket_hxx
00024 #define __ccxx_Socket_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/SocketException.h++>
00028 #include <commonc++/Network.h++>
00029 #include <commonc++/SocketAddress.h++>
00030
00031 #ifdef CCXX_OS_WINDOWS
00032 typedef int socklen_t;
00033 typedef char FAR* sockbufptr_t;
00034 #endif
00035
00036 #ifdef CCXX_OS_POSIX
00037 #include <netinet/in.h>
00038 #include <sys/socket.h>
00039 typedef void * sockbufptr_t;
00040 #endif
00041
00042 #ifndef MSG_NOSIGNAL
00043 #define MSG_NOSIGNAL 0
00044 #endif // MSG_NOSIGNAL
00045
00046 namespace ccxx {
00047
00048 #ifdef CCXX_OS_WINDOWS
00049 typedef SOCKET SocketHandle;
00050 #define INVALID_SOCKET_HANDLE INVALID_SOCKET
00051 #else
00052 typedef int SocketHandle;
00053 #define INVALID_SOCKET_HANDLE (-1)
00054 #endif
00055
00061 class COMMONCPP_API Socket
00062 {
00063 friend class ServerSocket;
00064 friend class SocketMuxer;
00065
00066 protected:
00067
00073 Socket(NetProtocol type = ProtoTCP) throw();
00074
00075 public:
00076
00078 virtual ~Socket() throw();
00079
00085 virtual void init() throw(SocketException);
00086
00096 virtual void connect(const String &addr, uint16_t port) throw(IOException);
00097
00105 virtual void connect(const SocketAddress &addr) throw(IOException);
00106
00110 virtual void shutdown() throw();
00111
00118 void setTimeout(timespan_ms_t timeout) throw(SocketException);
00119
00125 void setReceiveBufSize(size_t size) throw(SocketException);
00126
00132 size_t getReceiveBufSize() const throw(SocketException);
00133
00139 void setSendBufSize(size_t size) throw(SocketException);
00140
00146 size_t getSendBufSize() const throw(SocketException);
00147
00154 void setLingerTime(timespan_s_t timeout) throw(SocketException);
00155
00162 timespan_s_t getLingerTime() const throw(SocketException);
00163
00173 void setReuseAddress(bool enable) throw(SocketException);
00174
00180 bool getReuseAddress() const throw(SocketException);
00181
00188 void setKeepAlive(bool enable) throw(SocketException);
00189
00195 bool getKeepAlive() throw(SocketException);
00196
00203 void setTCPDelay(bool enable) throw(SocketException);
00204
00211 bool getTCPDelay() throw(SocketException);
00212
00214 inline bool isInitialized() const throw()
00215 { return(_socket != INVALID_SOCKET_HANDLE); }
00216
00218 inline bool isConnected() const throw()
00219 { return(_connected); }
00220
00222 inline NetProtocol getType() const throw()
00223 { return(_type); }
00224
00226 inline const SocketAddress& getLocalAddress() const throw()
00227 { return(_laddr); }
00228
00230 inline const SocketAddress& getRemoteAddress() const throw()
00231 { return(_raddr); }
00232
00233 protected:
00234
00235 enum IOWaitMode { WaitWrite, WaitRead };
00236
00238 inline SocketHandle getSocketHandle() const throw()
00239 { return(_socket); }
00240
00246 void waitForIO(IOWaitMode mode) throw(IOException);
00247
00249 NetProtocol _type;
00251 SocketHandle _socket;
00253 SocketAddress _raddr;
00255 SocketAddress _laddr;
00257 int _sotimeout;
00258
00260 bool _connected;
00261 bool _reuseAddr;
00264 private:
00265
00266 CCXX_COPY_DECLS(Socket);
00267 };
00268
00269 };
00270
00271 #endif // __ccxx_Socket_hxx
00272
00273