src/network/nSocket.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 
00024 ***************************************************************************
00025 
00026 */
00027 
00028 // nSocket.h
00029 
00030 // extremely losely based on the GNU Quake 1 base network code, so technicaly:
00031 // Copyright (C) 1996-1997 Id Software, Inc.
00032 // Modified for Armagetron by Manuel Moos (manuel@moosnet.de)
00033 
00034 #ifndef NET_SOCKET_H
00035 #define NET_SOCKET_H
00036 
00037 #include <iostream>
00038 #include "tConsole.h"
00039 #include "tException.h"
00040 #include <vector>
00041 
00042 class nScoket;
00043 struct nHostInfo;
00044 // struct addrinfo;
00045 struct hostentry;
00046 struct sockaddr;
00047 
00048 #ifdef WIN32
00049 #include  <winsock.h>
00050 #else
00051 #include <sys/socket.h>
00052 #include <netinet/in.h>
00053 #endif
00054 
00055 typedef  char int8;
00056 
00058 union nAddressBase
00059 {
00060     struct sockaddr     addr;       
00061     struct sockaddr_in  addr_in;    
00062     // struct sockaddr_in6 addr_in6;   //!< IPV6 address ( not supported yet )
00063 };
00064 
00066 class nAddress
00067 {
00068 public:
00069     nAddress();         
00070     ~nAddress();        
00071 
00072     // these functions handle address representation in the form hostname:port
00073     nAddress const &    ToString    ( tString & string ) const   ; 
00074     tString                     ToString    ()                   const   ; 
00075     int                                 FromString  ( const char * string )      ; 
00076 
00077     //  void                FromAddrInfo( const addrinfo & info )    ; //!< copy address from addrinfo
00078 
00079     void                FromHostent ( int l, const char * addr ) ; 
00080 
00081     nAddress &                  SetHostname     ( const char * hostname )    ; 
00082     tString                     GetHostname     ( void ) const               ; 
00083     nAddress const &    GetHostname     ( tString & hostname ) const ; 
00084 
00085     nAddress &                  SetAddress      ( const char * address )     ; 
00086     tString                     GetAddress      ( void ) const               ; 
00087     nAddress const &    GetAddress      ( tString & address ) const  ; 
00088 
00089     nAddress &                  SetPort         ( int port )                 ; 
00090     int                                 GetPort         ( void ) const               ; 
00091     nAddress const &    GetPort         ( int & port ) const         ; 
00092 
00093     bool                IsSet       () const                     ; 
00094 
00095     static int  Compare ( const nAddress & a1, const nAddress & a2 );   
00096 
00097     operator struct sockaddr *      ()       { return &addr_.addr; }   
00098     operator struct sockaddr const *() const { return &addr_.addr; }   
00099 
00101     bool operator == ( nAddress const & other ) const
00102     {
00103         return Compare( *this, other ) == 0;
00104     }
00105 
00107     bool operator != ( nAddress const & other ) const
00108     {
00109         return Compare( *this, other ) != 0;
00110     }
00111 
00112     enum{ size = sizeof( nAddressBase ) };
00113 
00114     unsigned int GetAddressLength( void ) const;        
00115 private:
00116     nAddressBase addr_;     
00117     unsigned int addrLen_;  
00118 };
00119 
00121 class nSocket
00122 {
00123 public:
00124     nSocket();          
00125     ~nSocket();         
00126 
00128 class PermanentError: public tException
00129     {
00130     public:
00131         PermanentError();                                       
00132         PermanentError( const tString & details );              
00133         ~PermanentError();                                      
00134     private:
00135         virtual tString DoGetName()         const;              
00136         virtual tString DoGetDescription()  const;              
00137 
00138         tString description_;
00139     };
00140 
00141     int Open ();                            
00142     int Open ( nAddress const & address );  
00143     int Open ( int port );                  
00144     //  int Open ( const addrinfo & addr );     //!< open socket for listening on the specified address info
00145     int Open ( const nHostInfo & addr );    
00146     int Close ();                           
00147     bool IsOpen () const;                   
00148     void Reset () const;                    
00149 
00150     int Connect ( const nAddress & addr );          
00151     const nSocket * CheckNewConnection() const;     
00152 
00153     int Read        ( int8 *buf, int len, nAddress & addr )             const; 
00154     int Write       ( const int8 *buf, int len, const nAddress & addr ) const; 
00155     int Broadcast   ( const char *buf, int len, unsigned int port )     const; 
00156 
00157     nAddress const &   GetAddress( void )                  const   ;    
00158     nSocket const &    GetAddress( nAddress & address )    const   ;    
00159 
00160     inline int             GetSocket( void )         const; 
00161     inline nSocket const & GetSocket( int & socket ) const;     
00162 
00163     // moving copy semantics
00164     void MoveFrom( const nSocket & other );       
00165     nSocket( const nSocket & other );             
00166     nSocket & operator=( const nSocket & other ); 
00167 private:
00168     int Create ();                            
00169     int Bind ( nAddress const & address );    
00170 
00171     int Write       ( const int8 *buf, int len, const sockaddr * addr, int addrlen ) const; 
00172 
00173     nSocket & SetAddress( nAddress const & address );  
00174     inline nSocket & SetSocket( int socket );          
00175 
00176     int socket_;            
00177     nAddress address_;      
00178     nAddress trueAddress_;  
00179     int family_, socktype_, protocol_;    
00180     mutable bool broadcast_;              
00181 };
00182 
00184 class nSocketListener
00185 {
00186 public:
00187     nSocketListener();      
00188     ~nSocketListener();     
00189 
00190     bool Listen ( bool state );                                 
00191 
00192     typedef std::vector< nSocket > SocketArray;                 
00193     typedef SocketArray::const_iterator const_iterator;         
00194     typedef SocketArray::const_iterator iterator;               
00195 
00196     // almost std:: compliant iterator functions
00197     iterator begin() const;                                    
00198     iterator end() const;                                      
00199 
00200     nSocketListener &       SetPort     ( unsigned int port );          
00201     unsigned int            GetPort     ( void ) const;                 
00202     nSocketListener const & GetPort     ( unsigned int & port ) const;  
00203     nSocketListener &       SetIpList   ( tString const & ipList );         
00204     tString const &         GetIpList   ( void ) const;                 
00205     nSocketListener const & GetIpList   ( tString & ipList ) const;         
00206     inline SocketArray const     & GetSockets ( void ) const;                   
00207     inline nSocketListener const & GetSockets ( SocketArray & sockets ) const;  
00208 private:
00209     SocketArray  sockets_;   
00210     unsigned int port_;      
00211     tString      ipList_;    
00212 
00213     // forbid copying
00214     nSocketListener( const nSocketListener & );
00215     nSocketListener & operator=( const nSocketListener & );
00216 
00217     inline nSocketListener & SetSockets( SocketArray const & sockets ); 
00218 };
00219 
00221 class nBasicNetworkSystem
00222 {
00223 public:
00224     nBasicNetworkSystem();              
00225     ~nBasicNetworkSystem();             
00226 
00227     nSocket*  Init (void);              
00228     void      Shutdown (void);          
00229 
00230     bool      Select (REAL dt);         
00231 
00232     nSocketListener &             AccessListener      ( void )                                ; 
00233     nBasicNetworkSystem &         SetListener         ( nSocketListener const & listener )    ; 
00234     nSocketListener const &       GetListener         ( void ) const                          ; 
00235     // nBasicNetworkSystem const &   GetListener         ( nSocketListener & listener ) const    ;      //!< Gets listening sockets
00236 
00237     // nBasicNetworkSystem &         SetControlSocket    ( nSocket const & controlSocket )       ;      //!< Sets network control socket
00238     nSocket const &               GetControlSocket    ( void ) const                          ; 
00239     // nBasicNetworkSystem const &   GetControlSocket    ( nSocket & controlSocket ) const       ;      //!< Gets network control socket
00240 private:
00241 
00242     nSocketListener listener_;          
00243     nSocket         controlSocket_;     
00244 
00245     // forbid copying
00246     nBasicNetworkSystem( const nBasicNetworkSystem & );
00247     nBasicNetworkSystem & operator=( const nBasicNetworkSystem & );
00248 
00249     nSocket &                     AccessControlSocket ( void )                                ; 
00250 };
00251 
00252 // *******************************************************************************
00253 // *
00254 // *    GetSockets
00255 // *
00256 // *******************************************************************************
00260 // *******************************************************************************
00261 
00262 nSocketListener::SocketArray const & nSocketListener::GetSockets( void ) const
00263 {
00264     return this->sockets_;
00265 }
00266 
00267 // *******************************************************************************
00268 // *
00269 // *    GetSockets
00270 // *
00271 // *******************************************************************************
00276 // *******************************************************************************
00277 
00278 nSocketListener const & nSocketListener::GetSockets( SocketArray & sockets ) const
00279 {
00280     sockets = this->sockets_;
00281     return *this;
00282 }
00283 
00284 // *******************************************************************************
00285 // *
00286 // *    SetSockets
00287 // *
00288 // *******************************************************************************
00293 // *******************************************************************************
00294 
00295 nSocketListener & nSocketListener::SetSockets( SocketArray const & sockets )
00296 {
00297     this->sockets_ = sockets;
00298     return *this;
00299 }
00300 
00301 // *******************************************************************************
00302 // *
00303 // *    GetSocket
00304 // *
00305 // *******************************************************************************
00309 // *******************************************************************************
00310 
00311 int nSocket::GetSocket( void ) const
00312 {
00313     return this->socket_;
00314 }
00315 
00316 // *******************************************************************************
00317 // *
00318 // *    GetSocket
00319 // *
00320 // *******************************************************************************
00325 // *******************************************************************************
00326 
00327 nSocket const & nSocket::GetSocket( int & socket ) const
00328 {
00329     socket = this->socket_;
00330     return *this;
00331 }
00332 
00333 // *******************************************************************************
00334 // *
00335 // *    SetSocket
00336 // *
00337 // *******************************************************************************
00342 // *******************************************************************************
00343 
00344 nSocket & nSocket::SetSocket( int socket )
00345 {
00346     this->socket_ = socket;
00347     return *this;
00348 }
00349 
00350 #endif

Generated on Sat Mar 15 22:55:52 2008 for Armagetron Advanced by  doxygen 1.5.4