Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/network_stream.h @ 9248

Last change on this file since 9248 was 9248, checked in by patrick, 18 years ago

starting switch to multiple server states (proxy, master, client,..)

File size: 3.6 KB
Line 
1/*!
2 * @file network_stream.h
3 *  implementation of a network pipe
4 */
5
6#ifndef _NETWORK_STREAM
7#define _NETWORK_STREAM
8
9#include <vector>
10#include <list>
11#include <map>
12
13#include "data_stream.h"
14#include "server_socket.h"
15#include "handshake.h"
16#include "connection_monitor.h"
17#include "udp_server_socket.h"
18
19class Synchronizeable;
20class NetworkSocket;
21class ServerSocket;
22class NetworkGameManager;
23
24//!< this structure contains informations about the network node
25class PeerInfo
26{
27  public:
28    PeerInfo() { clear(); }
29    void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; connectionMonitor = NULL; }
30
31
32  public:
33    int                 userId;
34    bool                isServer;
35    NetworkSocket *     socket;
36    Handshake *         handshake;
37    ConnectionMonitor * connectionMonitor;
38    int                 lastAckedState;
39    int                 lastRecvedState;
40};
41
42typedef std::list<Synchronizeable*>  SynchronizeableList;
43typedef std::map<int,PeerInfo>       PeerList;
44
45
46class NetworkStream : public DataStream
47{
48
49  public:
50    NetworkStream();
51    NetworkStream( std::string host, int port);
52    NetworkStream( int port );
53
54    virtual ~NetworkStream();
55    void init();
56
57    void createNetworkGameManager();
58    void startHandshake();
59
60    void connectSynchronizeable(Synchronizeable& sync);
61    void disconnectSynchronizeable(Synchronizeable& sync);
62
63    inline bool isMasterServer() const { return (this->type == NET_MASTER_SERVER)? true:false; }
64    inline bool isProxyServer() const { return (this->type == NET_PROXY_SERVER)? true:false; }
65    inline bool isClient() const { return (this->type == NET_CLIENT)? true:false; }
66    inline bool isActive() const { return this->bActive; }
67
68    inline int getMaxConnections(){ return MAX_CONNECTIONS; }
69
70    virtual void processData();
71
72    inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); }
73    inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); }
74    int getSyncCount();
75
76    inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); }
77    inline bool isUserServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isServer; }
78
79    void debug();
80
81    inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; }
82
83
84  private:
85    void updateConnectionList();
86    void handleHandshakes();
87    void handleUpstream( int tick );
88    void handleDownstream(int tick );
89    void handleNewClient( int userId );
90    void cleanUpOldSyncList();
91
92    void writeToNewDict( byte * data, int length, bool upstream );
93
94
95  private:
96    SynchronizeableList        synchronizeables;
97    PeerList                   peers;
98    ServerSocket*              serverSocket;
99    int                        type;
100    bool                       bActive;
101    std::list<int>             freeSocketSlots;
102
103    int                        myHostId;
104
105    int                        currentState;                //!< current state id
106
107    NetworkGameManager*        networkGameManager;
108
109    std::map<int,int>          oldSynchronizeables;         //!< used to save recently deleted sync ids to not recreate them
110
111    byte                       buf[UDP_PACKET_SIZE];        //!< used by handleUp/Downstream
112    byte                       compBuf[UDP_PACKET_SIZE];    //!< used by handleUp/Downstream
113
114    int                        remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict
115
116    int dictServer;
117    int dictClient;
118};
119#endif /* _NETWORK_STREAM */
Note: See TracBrowser for help on using the repository browser.