NptSockets.h

00001 /*****************************************************************
00002 |
00003 |   Neptune - Network Sockets
00004 |
00005 |   (c) 2001-2006 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _NPT_SOCKETS_H_
00011 #define _NPT_SOCKETS_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptTypes.h"
00017 #include "NptConstants.h"
00018 #include "NptStreams.h"
00019 #include "NptStrings.h"
00020 #include "NptDataBuffer.h"
00021 #include "NptNetwork.h"
00022 
00023 /*----------------------------------------------------------------------
00024 |   constants
00025 +---------------------------------------------------------------------*/
00026 const int NPT_ERROR_CONNECTION_RESET      = NPT_ERROR_BASE_SOCKET - 0;
00027 const int NPT_ERROR_CONNECTION_ABORTED    = NPT_ERROR_BASE_SOCKET - 1;
00028 const int NPT_ERROR_CONNECTION_REFUSED    = NPT_ERROR_BASE_SOCKET - 2;
00029 const int NPT_ERROR_CONNECTION_FAILED     = NPT_ERROR_BASE_SOCKET - 3;
00030 const int NPT_ERROR_HOST_UNKNOWN          = NPT_ERROR_BASE_SOCKET - 4;
00031 const int NPT_ERROR_SOCKET_FAILED         = NPT_ERROR_BASE_SOCKET - 5;
00032 const int NPT_ERROR_GETSOCKOPT_FAILED     = NPT_ERROR_BASE_SOCKET - 6;
00033 const int NPT_ERROR_SETSOCKOPT_FAILED     = NPT_ERROR_BASE_SOCKET - 7;
00034 const int NPT_ERROR_SOCKET_CONTROL_FAILED = NPT_ERROR_BASE_SOCKET - 8;
00035 const int NPT_ERROR_BIND_FAILED           = NPT_ERROR_BASE_SOCKET - 9;
00036 const int NPT_ERROR_LISTEN_FAILED         = NPT_ERROR_BASE_SOCKET - 10;
00037 const int NPT_ERROR_ACCEPT_FAILED         = NPT_ERROR_BASE_SOCKET - 11;
00038 const int NPT_ERROR_ADDRESS_IN_USE        = NPT_ERROR_BASE_SOCKET - 12;
00039 const int NPT_ERROR_NETWORK_DOWN          = NPT_ERROR_BASE_SOCKET - 13;
00040 const int NPT_ERROR_NETWORK_UNREACHABLE   = NPT_ERROR_BASE_SOCKET - 14;
00041 
00042 /*----------------------------------------------------------------------
00043 |   forward references
00044 +---------------------------------------------------------------------*/
00045 class NPT_Socket;
00046 
00047 /*----------------------------------------------------------------------
00048 |   NPT_SocketAddress
00049 +---------------------------------------------------------------------*/
00050 class NPT_SocketAddress 
00051 {
00052 public:
00053     // constructors and destructor
00054     NPT_SocketAddress() : m_Port(0) {}
00055     NPT_SocketAddress(const NPT_IpAddress& address, NPT_IpPort port) :
00056         m_IpAddress(address),
00057         m_Port(port) {}
00058 
00059     // methods
00060     NPT_Result SetIpAddress(const NPT_IpAddress& address) {
00061         m_IpAddress = address;
00062         return NPT_SUCCESS;
00063     }
00064     const NPT_IpAddress& GetIpAddress() const { 
00065         return m_IpAddress; 
00066     }
00067     NPT_Result SetPort(NPT_IpPort port) { 
00068         m_Port = port; 
00069         return NPT_SUCCESS; 
00070     }
00071     NPT_IpPort GetPort() const { 
00072         return m_Port; 
00073     }
00074     NPT_String ToString() const;
00075 
00076 private:
00077     // members
00078     NPT_IpAddress m_IpAddress;
00079     NPT_IpPort    m_Port;
00080 };
00081 
00082 /*----------------------------------------------------------------------
00083 |   NPT_SocketInfo
00084 +---------------------------------------------------------------------*/
00085 typedef struct {
00086     NPT_SocketAddress local_address;
00087     NPT_SocketAddress remote_address;
00088 } NPT_SocketInfo;
00089 
00090 /*----------------------------------------------------------------------
00091 |   NPT_SocketInterface
00092 +---------------------------------------------------------------------*/
00093 class NPT_SocketInterface
00094 {
00095  public:
00096     virtual ~NPT_SocketInterface() {}
00097 
00098     // interface methods
00099     virtual NPT_Result Bind(const NPT_SocketAddress& address, bool reuse_address = true) = 0;
00100     virtual NPT_Result Connect(const NPT_SocketAddress& address, NPT_Timeout timeout) = 0;
00101     virtual NPT_Result WaitForConnection(NPT_Timeout timeout) = 0;
00102     virtual NPT_Result GetInputStream(NPT_InputStreamReference& stream) = 0;
00103     virtual NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) = 0;
00104     virtual NPT_Result GetInfo(NPT_SocketInfo& info) = 0;
00105     virtual NPT_Result SetBlockingMode(bool blocking) = 0;
00106     virtual NPT_Result SetReadTimeout(NPT_Timeout timeout) = 0;
00107     virtual NPT_Result SetWriteTimeout(NPT_Timeout timeout) = 0;
00108 };
00109 
00110 /*----------------------------------------------------------------------
00111 |   NPT_UdpSocketInterface
00112 +---------------------------------------------------------------------*/
00113 class NPT_UdpSocketInterface
00114 {
00115  public:
00116     virtual ~NPT_UdpSocketInterface() {}
00117 
00118     // methods
00119     virtual NPT_Result Send(const NPT_DataBuffer&    packet, 
00120                             const NPT_SocketAddress* address = NULL) = 0;
00121     virtual NPT_Result Receive(NPT_DataBuffer&     packet, 
00122                                NPT_SocketAddress*  address = NULL) = 0;
00123 };
00124 
00125 /*----------------------------------------------------------------------
00126 |   NPT_UdpMulticastSocketInterface
00127 +---------------------------------------------------------------------*/
00128 class NPT_UdpMulticastSocketInterface
00129 {
00130  public:
00131     virtual ~NPT_UdpMulticastSocketInterface() {}
00132 
00133     // methods
00134     virtual NPT_Result JoinGroup(const NPT_IpAddress& group, 
00135                                  const NPT_IpAddress& iface) = 0;
00136     virtual NPT_Result LeaveGroup(const NPT_IpAddress& group,
00137                                   const NPT_IpAddress& iface) = 0;
00138     virtual NPT_Result SetTimeToLive(unsigned char ttl) = 0;
00139     virtual NPT_Result SetInterface(const NPT_IpAddress& iface) = 0;
00140 };
00141 
00142 /*----------------------------------------------------------------------
00143 |   NPT_TcpServerSocketInterface
00144 +---------------------------------------------------------------------*/
00145 class NPT_TcpServerSocketInterface
00146 {
00147  public:
00148     virtual ~NPT_TcpServerSocketInterface() {}
00149 
00150     // interface methods
00151     virtual NPT_Result Listen(unsigned int max_clients) = 0;
00152     virtual NPT_Result WaitForNewClient(NPT_Socket*& client, 
00153                                         NPT_Timeout  timeout) = 0;
00154 };
00155 
00156 /*----------------------------------------------------------------------
00157 |   NPT_Socket
00158 +---------------------------------------------------------------------*/
00159 class NPT_Socket : public NPT_SocketInterface
00160 {
00161 public:
00162     // constructor and destructor
00163     NPT_Socket(NPT_SocketInterface* delegate) :
00164         m_SocketDelegate(delegate) {}
00165     virtual ~NPT_Socket();
00166 
00167     // delegate NPT_SocketInterface methods
00168     NPT_Result Bind(const NPT_SocketAddress& address, bool reuse_address = true) {             
00169         return m_SocketDelegate->Bind(address, reuse_address);                            
00170     }                                                               
00171     NPT_Result Connect(const NPT_SocketAddress& address,            
00172                        NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) {
00173        return m_SocketDelegate->Connect(address, timeout);                 
00174     }                                                               
00175     NPT_Result WaitForConnection(NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) {
00176         return m_SocketDelegate->WaitForConnection(timeout);                 
00177     } 
00178     NPT_Result GetInputStream(NPT_InputStreamReference& stream) {   
00179         return m_SocketDelegate->GetInputStream(stream);                   
00180     }                                                               
00181     NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) { 
00182     return m_SocketDelegate->GetOutputStream(stream);                      
00183     }                                                               
00184     NPT_Result GetInfo(NPT_SocketInfo& info) {                      
00185         return m_SocketDelegate->GetInfo(info);                            
00186     }                                                               
00187     NPT_Result SetBlockingMode(bool blocking) {                      
00188         return m_SocketDelegate->SetBlockingMode(blocking);                            
00189     }                                                          
00190     NPT_Result SetReadTimeout(NPT_Timeout timeout) {                      
00191         return m_SocketDelegate->SetReadTimeout(timeout);                            
00192     }                                                          
00193     NPT_Result SetWriteTimeout(NPT_Timeout timeout) {                      
00194         return m_SocketDelegate->SetWriteTimeout(timeout);                            
00195     }                                                          
00196 
00197 protected:
00198     // constructor
00199     NPT_Socket() {}
00200 
00201     // members
00202     NPT_SocketInterface* m_SocketDelegate;
00203 };
00204 
00205 /*----------------------------------------------------------------------
00206 |   NPT_UdpSocket
00207 +---------------------------------------------------------------------*/
00208 class NPT_UdpSocket : public NPT_Socket,
00209                       public NPT_UdpSocketInterface
00210 {
00211  public:
00212     // constructor and destructor
00213              NPT_UdpSocket();
00214     virtual ~NPT_UdpSocket();
00215 
00216     // delegate NPT_UdpSocketInterface methods
00217     NPT_Result Send(const NPT_DataBuffer&    packet,           
00218                     const NPT_SocketAddress* address = NULL) {
00219         return m_UdpSocketDelegate->Send(packet, address);              
00220     }                                                         
00221     NPT_Result Receive(NPT_DataBuffer&     packet,            
00222                        NPT_SocketAddress*  address = NULL) {  
00223         return m_UdpSocketDelegate->Receive(packet, address);           
00224     }
00225 
00226 protected:
00227     // constructor
00228     NPT_UdpSocket(NPT_UdpSocketInterface* delegate);
00229 
00230     // members
00231     NPT_UdpSocketInterface* m_UdpSocketDelegate;
00232 };
00233 
00234 /*----------------------------------------------------------------------
00235 |   NPT_UdpMulticastSocket
00236 +---------------------------------------------------------------------*/
00237 class NPT_UdpMulticastSocket : public NPT_UdpSocket, 
00238                                public NPT_UdpMulticastSocketInterface
00239 {
00240 public:
00241     // constructor and destructor
00242              NPT_UdpMulticastSocket();
00243     virtual ~NPT_UdpMulticastSocket();
00244 
00245     // delegate NPT_UdpMulticastSocketInterface methods
00246     NPT_Result JoinGroup(const NPT_IpAddress& group,            
00247                          const NPT_IpAddress& iface =           
00248                          NPT_IpAddress::Any) {                  
00249         return m_UdpMulticastSocketDelegate->JoinGroup(group, iface);
00250     }                                                           
00251     NPT_Result LeaveGroup(const NPT_IpAddress& group,           
00252                           const NPT_IpAddress& iface =          
00253                           NPT_IpAddress::Any) {                 
00254         return m_UdpMulticastSocketDelegate->LeaveGroup(group, iface);
00255     }                                                          
00256     NPT_Result SetTimeToLive(unsigned char ttl) {     
00257         return m_UdpMulticastSocketDelegate->SetTimeToLive(ttl); 
00258     }
00259     NPT_Result SetInterface(const NPT_IpAddress& iface) {
00260         return m_UdpMulticastSocketDelegate->SetInterface(iface);
00261     }
00262 
00263 protected:
00264     // members
00265     NPT_UdpMulticastSocketInterface* m_UdpMulticastSocketDelegate;
00266 };
00267 
00268 /*----------------------------------------------------------------------
00269 |   NPT_TcpClientSocket
00270 +---------------------------------------------------------------------*/
00271 class NPT_TcpClientSocket : public NPT_Socket
00272 {
00273 public:
00274     // constructors and destructor
00275              NPT_TcpClientSocket();
00276     virtual ~NPT_TcpClientSocket();
00277 };
00278 
00279 /*----------------------------------------------------------------------
00280 |   NPT_TcpServerSocket
00281 +---------------------------------------------------------------------*/
00282 class NPT_TcpServerSocket : public NPT_Socket,
00283                             public NPT_TcpServerSocketInterface
00284 {
00285 public:
00286     // constructors and destructor
00287              NPT_TcpServerSocket();
00288     virtual ~NPT_TcpServerSocket();
00289 
00290     // delegate NPT_TcpServerSocketInterface methods
00291     NPT_Result Listen(unsigned int max_clients) {   
00292         return m_TcpServerSocketDelegate->Listen(max_clients);
00293     }
00294     NPT_Result WaitForNewClient(NPT_Socket*& client, 
00295                                 NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) {
00296         return m_TcpServerSocketDelegate->WaitForNewClient(client, timeout);
00297     }
00298 
00299 protected:
00300     // members
00301     NPT_TcpServerSocketInterface* m_TcpServerSocketDelegate;
00302 };
00303 
00304 #endif // _NPT_SOCKETS_H_