Socket.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2009  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 #include <winsock2.h>
00033 typedef int socklen_t;
00034 typedef char FAR* sockbufptr_t;
00035 #endif
00036 
00037 #ifdef CCXX_OS_POSIX
00038 #include <netinet/in.h>
00039 #include <sys/socket.h>
00040 typedef void * sockbufptr_t;
00041 #endif
00042 
00043 #ifndef MSG_NOSIGNAL
00044 #define MSG_NOSIGNAL 0
00045 #endif // MSG_NOSIGNAL
00046 
00047 namespace ccxx {
00048 
00049 #ifdef CCXX_OS_WINDOWS
00050 typedef SOCKET SocketHandle;
00051 #define INVALID_SOCKET_HANDLE INVALID_SOCKET
00052 #else
00053 typedef int SocketHandle;
00054 #define INVALID_SOCKET_HANDLE (-1)
00055 #endif
00056 
00062 class COMMONCPP_API Socket
00063 {
00064   friend class ServerSocket;
00065   friend class SocketMuxer;
00066 
00067   protected:
00068 
00074   Socket(NetProtocol type = ProtoTCP) throw();
00075 
00076   public:
00077 
00079   virtual ~Socket() throw();
00080 
00086   virtual void init() throw(SocketException);
00087 
00097   virtual void connect(const String &addr, uint16_t port) throw(IOException);
00098 
00106   virtual void connect(const SocketAddress &addr) throw(IOException);
00107 
00111   virtual void shutdown() throw();
00112 
00119   void setTimeout(int msec) throw(SocketException);
00120 
00126   void setReceiveBufSize(size_t size) throw(SocketException);
00127 
00133   size_t getReceiveBufSize() const throw(SocketException);
00134 
00140   void setSendBufSize(size_t size) throw(SocketException);
00141 
00147   size_t getSendBufSize() const throw(SocketException);
00148 
00155   void setLingerTime(int msec) throw(SocketException);
00156 
00163   int getLingerTime() const throw(SocketException);
00164 
00174   void setReuseAddress(bool enable) throw(SocketException);
00175 
00181   bool getReuseAddress() const throw(SocketException);
00182 
00189   void setKeepAlive(bool enable) throw(SocketException);
00190 
00196   bool getKeepAlive() throw(SocketException);
00197 
00204   void setTCPDelay(bool enable) throw(SocketException);
00205 
00212   bool getTCPDelay() throw(SocketException);
00213 
00215   inline bool isInitialized() const throw()
00216   { return(_socket != INVALID_SOCKET_HANDLE); }
00217 
00219   inline bool isConnected() const throw()
00220   { return(_connected); }
00221 
00223   inline NetProtocol getType() const throw()
00224   { return(_type); }
00225 
00227   inline const SocketAddress& getLocalAddress() const throw()
00228   { return(_laddr); }
00229 
00231   inline const SocketAddress& getRemoteAddress() const throw()
00232   { return(_raddr); }
00233 
00234   protected:
00235 
00236   enum IOWaitMode { WaitWrite, WaitRead };
00237 
00239   inline SocketHandle getSocketHandle() const throw()
00240   { return(_socket); }
00241 
00247   void waitForIO(IOWaitMode mode) throw(IOException);
00248 
00250   NetProtocol _type;
00252   SocketHandle _socket;
00254   SocketAddress _raddr;
00256   SocketAddress _laddr;
00258   int _sotimeout;
00259   bool _connected;
00260   bool _reuseAddr;
00261 
00262   private:
00263 
00264   CCXX_COPY_DECLS(Socket);
00265 };
00266 
00267 }; // namespace ccxx
00268 
00269 #endif // __ccxx_Socket_hxx
00270 
00271 /* end of header file */
Generated on Fri Sep 3 23:47:34 2010 for libcommonc++ by  doxygen 1.6.3