1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
19 | */ |
---|
20 | #define DEBUG_MODULE_NETWORK |
---|
21 | |
---|
22 | #include "class_list.h" |
---|
23 | #include "debug.h" |
---|
24 | #include "shell_command.h" |
---|
25 | |
---|
26 | /* include your own header */ |
---|
27 | #include "network_manager.h" |
---|
28 | #include "shared_network_data.h" |
---|
29 | #include "network_stream.h" |
---|
30 | #include "preferences.h" |
---|
31 | #include "network_log.h" |
---|
32 | #include "network_game_manager.h" |
---|
33 | |
---|
34 | |
---|
35 | /* using namespace std is default, this needs to be here */ |
---|
36 | using namespace std; |
---|
37 | |
---|
38 | SHELL_COMMAND(debug, NetworkManager, debug); |
---|
39 | |
---|
40 | |
---|
41 | NetworkManager* NetworkManager::singletonRef = NULL; |
---|
42 | |
---|
43 | /** |
---|
44 | * standard constructor |
---|
45 | */ |
---|
46 | NetworkManager::NetworkManager() |
---|
47 | { |
---|
48 | /* set the class id for the base object */ |
---|
49 | this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); |
---|
50 | PRINTF(0)("START\n"); |
---|
51 | |
---|
52 | /* initialize the references */ |
---|
53 | this->netStreamList = NULL; |
---|
54 | this->syncList = NULL; |
---|
55 | this->defaultSyncStream = NULL; |
---|
56 | this->sharedNetworkData = SharedNetworkData::getInstance(); |
---|
57 | this->elapsedTime = 0.0f; |
---|
58 | |
---|
59 | |
---|
60 | int port = Preferences::getInstance()->getInt( "network", "telnetport", 0 ); |
---|
61 | |
---|
62 | if ( port > 0 ) |
---|
63 | NetworkLog::getInstance()->listen( port ); |
---|
64 | |
---|
65 | PRINTF(0)("NetworkManager created\n"); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | * standard deconstructor |
---|
71 | */ |
---|
72 | NetworkManager::~NetworkManager() |
---|
73 | {} |
---|
74 | |
---|
75 | |
---|
76 | /** |
---|
77 | * initializes the network manager |
---|
78 | */ |
---|
79 | void NetworkManager::initialize() |
---|
80 | { |
---|
81 | /* get the synchronizeable list from the class list */ |
---|
82 | this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE); |
---|
83 | PRINTF(0)("NetworkManager initzalized\n"); |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | /** |
---|
89 | * shutsdown the network manager |
---|
90 | */ |
---|
91 | void NetworkManager::shutdown() |
---|
92 | { |
---|
93 | |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | * creates a connection from one object to a host |
---|
99 | * @param hostName: the name of the destination host |
---|
100 | */ |
---|
101 | int NetworkManager::establishConnection(const std::string & name, unsigned int port) |
---|
102 | { |
---|
103 | this->defaultSyncStream = new NetworkStream( name, port ); |
---|
104 | this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream); |
---|
105 | this->defaultSyncStream->startHandshake(); |
---|
106 | return 1; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * creates a new NetworkStream of server type |
---|
112 | * @param port: number of the TCP port |
---|
113 | */ |
---|
114 | int NetworkManager::createServer(unsigned int port) |
---|
115 | { |
---|
116 | this->sharedNetworkData->setHostID(0); |
---|
117 | this->sharedNetworkData->setGameServer(true); |
---|
118 | this->defaultSyncStream = new NetworkStream(port); |
---|
119 | this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream); |
---|
120 | this->defaultSyncStream->createNetworkGameManager(); |
---|
121 | PRINTF(0)("CREATE SERVER\n"); |
---|
122 | SDL_Delay(20); |
---|
123 | return 1; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
---|
128 | { |
---|
129 | if( this->defaultSyncStream) |
---|
130 | this->defaultSyncStream->connectSynchronizeable(sync); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | * sync the network |
---|
136 | * @param dtS: the seceonds elapsed since the last synchronize call |
---|
137 | */ |
---|
138 | void NetworkManager::synchronize( float dtS) |
---|
139 | { |
---|
140 | this->elapsedTime += dtS; |
---|
141 | if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY)) |
---|
142 | return; |
---|
143 | this->elapsedTime = 0.0f; |
---|
144 | |
---|
145 | if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL) |
---|
146 | { |
---|
147 | std::list<BaseObject*>::const_iterator stream; |
---|
148 | for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) |
---|
149 | if( static_cast<NetworkStream*>(*stream)->isActive()) |
---|
150 | static_cast<NetworkStream*>(*stream)->processData(); |
---|
151 | } |
---|
152 | |
---|
153 | NetworkGameManager::getInstance()->tick( this->elapsedTime ); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | /** |
---|
159 | * debug output |
---|
160 | */ |
---|
161 | void NetworkManager::debug() |
---|
162 | { |
---|
163 | PRINT(0)("=================Network::debug()=========\n"); |
---|
164 | this->defaultSyncStream->debug(); |
---|
165 | PRINT(0)("===========================================\n"); |
---|
166 | } |
---|