simple-socket library 1.1.5

SimpleSocket.h

Go to the documentation of this file.
00001 #ifndef NET_SimpleSocket_h__
00002 #define NET_SimpleSocket_h__
00003 
00004 #include <sys/socket.h>
00005 #include <string>
00006 #include <exception>
00007 
00008 namespace NET
00009 {
00011     class SocketException : public std::exception
00012     {
00013     public:
00020         SocketException( const std::string& message, bool inclSysMsg = true) throw();
00021 
00023         ~SocketException() throw() {};
00024 
00026         const char* what() const throw() { return m_message.c_str(); }
00027 
00029         int errorCode() const throw() { return m_errorcode; }
00030 
00031     private:
00032         std::string m_message;
00033         int m_errorcode;
00034     };
00035 
00038     typedef void raw_type;
00040 
00042 
00046     class SimpleSocket
00047     {
00048     public:
00049         enum ShutdownDirection
00050         {
00051             STOP_SEND = SHUT_WR,     
00052             STOP_RECEIVE = SHUT_RD,  
00053             STOP_BOTH = SHUT_RDWR    
00054         };
00055 
00056         ~SimpleSocket();
00057 
00059 
00079         int send( const void* buffer, size_t len);
00080 
00082 
00105         int receive( void* buffer, size_t len);
00106 
00108 
00132         int timedReceive( void* buffer, size_t len, int timeout);
00133 
00138         void disconnect();
00139 
00141 
00153         void shutdown( ShutdownDirection type);
00154 
00156 
00163         bool peerDisconnected() const;
00164 
00165     protected:
00166         enum SocketDomain
00167         {
00168             INTERNET = PF_INET,
00169             UNIX = PF_LOCAL
00170         };
00171 
00172         enum SocketType
00173         {
00174             STREAM = SOCK_STREAM,
00175             DATAGRAM = SOCK_DGRAM
00176         };
00177 
00179         SimpleSocket( int sockfd);
00180 
00182         SimpleSocket( int domain, int type, int protocol);
00183 
00184         // socket descriptor
00185         int m_socket;
00186         bool m_peerDisconnected;
00187 
00188     private:
00189         // dont' allow
00190         SimpleSocket( const SimpleSocket&);
00191         const SimpleSocket& operator=( const SimpleSocket&);
00192     };
00193 
00194 } // namespace NET
00195 
00196 #endif // NET_SimpleSocket_h__