Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

started implementation for soft connections

File size: 5.1 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 "monitor/connection_monitor.h"
17#include "udp_server_socket.h"
18#include "peer_info.h"
19
20#include "shared_network_data.h"
21
22class Synchronizeable;
23class NetworkSocket;
24class ServerSocket;
25class NetworkGameManager;
26class NetworkMonitor;
27
28
29typedef std::list<Synchronizeable*>  SynchronizeableList;
30typedef std::map<int,PeerInfo>       PeerList;
31
32
33class NetworkStream : public DataStream
34{
35
36  public:
37    NetworkStream();
38    NetworkStream(int nodeType);
39    virtual ~NetworkStream();
40
41    void init();
42
43    /* network interface controls */
44    void connectToMasterServer(std::string host, int port);
45    void connectToProxyServer(int proxyId, std::string host, int port);
46    void createServer(int clientPort, int proxyPort, int clientSoftPort);
47
48    void createNetworkGameManager();
49    void startHandshake(int userId = NET_ID_MASTER_SERVER);
50
51    void reconnectToServer(IP address);
52    void softReconnectToServer(IP address);
53
54
55    /* synchronizeable interface */
56    void connectSynchronizeable(Synchronizeable& sync);
57    void disconnectSynchronizeable(Synchronizeable& sync);
58
59    inline int getMaxConnections(){ return SharedNetworkData::getInstance()->getMaxPlayer(); }
60
61    /* functions for the peerInfo information retreival */
62    /** @returns true if this userId is activated at this client false if not*/
63    inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); }
64    /** @returns true if this userId is a local client */
65    inline bool isUserLocal( int userID) { return this->isUserIdActive(userID); }
66    /** @returns true if this user is a master server */
67    inline bool isUserMasterServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isMasterServer(); }
68    /** @returns true if this user is a proxy server */
69    inline bool isUserProxyServerActive( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isProxyServerActive(); }
70    /** @returns true if this user is a client */
71    inline bool isUserClient( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isClient(); }
72
73    /* peering interface */
74    inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; }
75    inline PeerInfo* getPeerInfo() { return this->pInfo; }
76    inline PeerList getPeerInfoList() { return this->peers; }
77
78    /* data processing*/
79    virtual void processData();
80
81    /* debugging */
82    void debug();
83
84
85  private:
86
87    inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); }
88    inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); }
89    void cleanUpOldSyncList();
90    int getSyncCount();
91
92    void updateConnectionList();
93
94    /* handle processes */
95    void handleHandshakes();
96    void handleUpstream( int tick );
97    void handleDownstream(int tick );
98
99    /* handle events*/
100    void handleNewClient( int userId );
101
102    void handleConnect( int userId);
103    void handleSoftConnect( int userId);
104    void handleReconnect( int userId );
105    void handleDisconnect( int userId );
106
107    void writeToNewDict( byte * data, int length, bool upstream );
108
109
110  private:
111    PeerList                   peers;                       //!< list of the network node informations
112
113    PeerInfo*                  pInfo;                       //!< the info about the local peer node (not in the PeerList)
114
115    std::list<int>             freeSocketSlots;             //!< list of free sockets (to ensure not to resycle sockets)
116    int                        currentState;                //!< current state id
117
118    NetworkMonitor*            networkMonitor;              //!< the network monitor
119    NetworkGameManager*        networkGameManager;          //!< reference to the network game manager
120    ServerSocket*              clientSocket;                //!< the listening socket for clients of the server
121    ServerSocket*              clientSoftSocket;            //!< the listening socket for soft connections to the server
122    ServerSocket*              proxySocket;                 //!< socket for proxy connections
123
124    std::map<int,int>          oldSynchronizeables;         //!< used to save recently deleted sync ids to not recreate them
125    SynchronizeableList        synchronizeables;            //!< list of the synchronizeables
126
127    byte                       buf[UDP_PACKET_SIZE];        //!< used by handleUp/Downstream
128    byte                       compBuf[UDP_PACKET_SIZE];    //!< used by handleUp/Downstream
129    int                        remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict
130
131    int                        dictServer;                  //!< the zip dict for the server
132    int                        dictClient;                  //!< the zip dict for the client
133
134    bool                       bRedirect;                   //!< true if the master server sent a redirect command
135};
136#endif /* _NETWORK_STREAM */
Note: See TracBrowser for help on using the repository browser.