Socket.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 }; // namespace ccxx
00270 
00271 #endif // __ccxx_Socket_hxx
00272 
00273 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3