netmanager.h

Go to the documentation of this file.
00001 /********************************************************************************/
00002 /*                                                                              */
00003 /*  Netrinjo-Engine - a library for game-development                            */
00004 /*  Copyright (C) 2005-2006 by Tobias Nadler                                    */
00005 /*                                                                              */
00006 /*  This library is free software; you can redistribute it and/or               */
00007 /*  modify it under the terms of the GNU Lesser General Public                  */
00008 /*  License as published by the Free Software Foundation; either                */
00009 /*  version 2.1 of the License, or (at your option) any later version.          */
00010 /*                                                                              */
00011 /*  This library is distributed in the hope that it will be useful,             */
00012 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of              */
00013 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           */
00014 /*  Lesser General Public License for more details.                             */
00015 /*                                                                              */
00016 /*  You should have received a copy of the GNU Lesser General Public            */
00017 /*  License along with this library; if not, write to the Free Software         */
00018 /*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   */
00019 /*                                                                              */
00020 /********************************************************************************/
00021 
00022 #ifndef NETMANAGER_H
00023 #define NETMANAGER_H
00024 
00025 #include "SDL/SDL_net.h"
00026 #include <list>
00027 #include "basicobject.h"
00028 
00034 
00035 #define MAX_PEERS           127
00037 #define NETWORK_PORT        8457
00038 
00042 #define NETWORK_UDPPORT     (NETWORK_PORT+1)
00044 #define NETWORK_SERVERPORT  8455
00045 
00046 using namespace std;
00047 
00048 class NetSocket;
00049 struct NetBasicData;
00050 
00106 class DECLSPEC NetBasicManager: public BasicObject
00107 {
00108 public:
00109     DECLSPEC NetBasicManager(const char *name, BasicObject *p);
00110     DECLSPEC NetBasicManager(TiXmlElement *el, BasicObject *p);
00111     virtual DECLSPEC ~NetBasicManager();
00112 
00129     DECLSPEC bool init( unsigned int port = NETWORK_PORT, unsigned int udpport = NETWORK_UDPPORT,
00130                         int udpmaxsize = 1024 );
00134     void destroy()  {onDestroy();}
00135 
00143     DECLSPEC unsigned int send( char *data, int size );
00151     DECLSPEC bool sendUDP( char *data, int size, const IPaddress &ip );
00161     virtual DECLSPEC bool checkPeers();
00174     virtual DECLSPEC bool checkServer();
00175 
00182     DECLSPEC NetSocket *getSocket( const IPaddress &ip );
00187     DECLSPEC void deleteDisconnected();
00188 
00192     int numConnections()    { return nPeersConnected; }
00198     void setConnectCallback( BasicObject::Callback func, BasicObject *pData )   { cbServer.func = func; cbServer.obj = pData; }
00204     void setPeerCallback( BasicObject::Callback func, BasicObject *pData )  { cbPeer.func = func; cbPeer.obj = pData; }
00210     void setUDPCallback( BasicObject::Callback func, BasicObject *pData )   { cbUDP.func = func; cbUDP.obj = pData; }
00211 
00217     int localPort() { return nListenPort; }
00224     DECLSPEC const char *localIP();
00225 
00232     DECLSPEC static bool initSDL();
00237     DECLSPEC static void quitSDL();
00241     static NetBasicManager *the;
00242 protected:
00251     DECLSPEC static NetBasicManager *getThe();
00252     
00253     virtual DECLSPEC void onInit();
00254     virtual DECLSPEC void onDestroy();
00262     virtual DECLSPEC void onReceiveUDP(UDPpacket *udp);
00263 
00269     virtual NetSocket *createSocket( TCPsocket socket ) = 0;
00273     bool bInited;
00279     int nListenPort;
00283     unsigned int nUDPPort;
00284 
00285 private:
00286     DECLSPEC bool registerSocket( TCPsocket sock );
00287     DECLSPEC void unregisterSocket( TCPsocket sock );
00288 
00289     int nPeersConnected;
00290 
00291     SDLNet_SocketSet socksPeers, socksServer;
00292     TCPsocket sockServer;
00293     UDPsocket sockUDP;
00294     // used to receive/send incomming/outgoing messages
00295     UDPpacket *ppackUDPrecv, *ppackUDPsend;
00296     // max. size of a UDP-packet's content
00297     unsigned int nUDPMaxSize;
00298 
00299     BasicObject::FunctionData cbServer, cbPeer, cbUDP;
00300 
00301 //  char *chLocalName;
00302     // reference counter for usage of SDL-network functions
00303     static unsigned int nSDLNetRefCount;
00304 
00305     friend class NetSocket;
00306 };
00307 
00308 class NetStandardSocket;
00309 
00321 class DECLSPEC NetSimpleManager: public NetBasicManager
00322 {
00323 public:
00324     DECLSPEC NetSimpleManager(const char *name, BasicObject *p);
00325     DECLSPEC NetSimpleManager(TiXmlElement *el, BasicObject *p);
00326     virtual DECLSPEC ~NetSimpleManager();
00327     virtual const char *getClassName()  { return "NetSimpleManager"; }
00328 
00336     static NetSimpleManager *getThe()   { return (NetSimpleManager*)NetBasicManager::getThe(); }
00337 protected:
00338     virtual DECLSPEC NetSocket *createSocket( TCPsocket socket );
00339 
00340 private:
00341 };
00342 
00343 
00344 #endif

Generated on Wed May 9 17:35:55 2007 for netrinjo by  doxygen 1.5.1