00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef NETSOCKET_H
00023 #define NETSOCKET_H
00024
00025 #include "SDL/SDL_net.h"
00026 #include "basicobject.h"
00027
00034
00035 #define FILTER_SERVER 1
00037 #define MAX_DATALEN 0xFFFE
00038
00042 #define E_NET_NONE 0
00043
00047 #define E_NET_NO_HOST -16
00048
00052 #define E_NET_DISCONNECTED -17
00053
00057 #define E_NET_RAW_DATA -32
00058
00062 #define E_NET_BUFTOOSMALL -33
00063
00067 #define E_NET_OTHER -64
00068
00069 class NetBasicManager;
00070
00083 class DECLSPEC NetSocket: public BasicObject
00084 {
00085 public:
00091 DECLSPEC NetSocket( const char *name, NetBasicManager *p, TCPsocket connection = 0 );
00098 DECLSPEC NetSocket( const char *name, NetBasicManager *p, IPaddress ip );
00108 DECLSPEC NetSocket( TiXmlElement *el, NetBasicManager *p );
00109 virtual DECLSPEC ~NetSocket();
00110
00111 virtual bool isNetSocket() const { return true; }
00112
00114 bool isValid() const { return bValid; }
00115
00123 DECLSPEC bool connect( const char *host, int port );
00130 DECLSPEC bool connect( IPaddress &ip );
00134 DECLSPEC void disconnect();
00136 virtual bool connected() const { return sock != 0; }
00141 virtual bool deleteOnDisconnected() const { return bDelOnDisconn; }
00142
00147 virtual DECLSPEC bool wait4data( Uint32 time );
00148
00156 virtual bool sendData( char *data, unsigned int size );
00165 virtual bool sendUDP( char *data, unsigned int size );
00174 virtual int readData( char *buffer, int maxsize );
00181 virtual char *readData( int *size ) = 0;
00182
00188 virtual bool isActive() const { return bActive; }
00190 TCPsocket getSocket() { return sock; }
00194 const IPaddress *getAddress() const { if(!sock) return NULL; return SDLNet_TCP_GetPeerAddress( sock ); }
00200 const char *getHostName() const { if(!sock) return NULL; return SDLNet_ResolveIP( SDLNet_TCP_GetPeerAddress( sock ) ); }
00201
00202 protected:
00203 virtual DECLSPEC void onInit();
00209 virtual DECLSPEC bool onReceive();
00216 virtual DECLSPEC void onReceiveUDP(UDPpacket &udp);
00222 virtual void onConnect() {}
00228 virtual void onDisconnect() {}
00230 TCPsocket sock;
00232 bool bValid;
00234 bool bActive;
00236 bool bDelOnDisconn;
00237 private:
00238 bool init();
00239 friend class NetBasicManager;
00240 };
00241
00242
00243 class NetSimpleManager;
00244
00257 class DECLSPEC NetStandardSocket : public NetSocket
00258 {
00259 public:
00265 DECLSPEC NetStandardSocket( const char *name, NetBasicManager *p, TCPsocket connection = 0 );
00274 DECLSPEC NetStandardSocket( const char *name, NetBasicManager *p, IPaddress ip, int filter = 0 );
00281 DECLSPEC NetStandardSocket( TiXmlElement *el, NetBasicManager *p );
00282
00283 virtual DECLSPEC ~NetStandardSocket();
00284
00285 virtual const char *getClassName() { return "NetStandardSocket"; }
00286
00293 virtual bool sendData( char *data, unsigned int size );
00305 virtual DECLSPEC bool sendData( char *data, unsigned int size, Uint32 condition, void *buffer = NULL );
00313 virtual DECLSPEC int readData( char *data, int maxsize );
00322 virtual DECLSPEC char *readData( int *size );
00323
00329 void setReceiveCallback(BasicObject::Callback cb, BasicObject *o) {cbRecieve.func = cb; cbRecieve.obj = o;}
00330
00335 Uint32 filter;
00336
00337 protected:
00338 virtual DECLSPEC void onInit();
00346 virtual DECLSPEC bool onReceive();
00347 virtual DECLSPEC void onConnect();
00348
00349 private:
00350 BasicObject::FunctionData cbRecieve;
00351
00352 unsigned int nBytesUnread;
00353 };
00354
00355
00356 #endif