Changeset 369 for code/branches/FICN/src/network
- Timestamp:
- Dec 3, 2007, 12:28:19 AM (17 years ago)
- Location:
- code/branches/FICN/src/network
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/network/Client.cc
r337 r369 21 21 Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ 22 22 // set server address to localhost 23 //server_address="127.0.0.1";24 //port = NETWORK_PORT;25 23 isConnected=false; 24 pck_gen = PacketGenerator(); 25 gamestate = GameStateManager(); 26 26 } 27 27 … … 32 32 */ 33 33 Client::Client(std::string address, int port) : client_connection(port, address){ 34 //server_address=address.c_str();35 //this->port = port;36 34 isConnected=false; 35 pck_gen = PacketGenerator(); 36 gamestate = GameStateManager(); 37 37 } 38 38 … … 43 43 */ 44 44 Client::Client(const char *address, int port) : client_connection(port, address){ 45 //server_address = address;46 //this->port = port;47 45 isConnected=false; 46 pck_gen = PacketGenerator(); 47 gamestate = GameStateManager(); 48 48 } 49 49 … … 78 78 return false; 79 79 // send packets 80 client_connection.sendPackets(&event); 81 if(event.type==ENET_EVENT_TYPE_NONE) 82 return true; 83 else 84 return false; 80 return client_connection.sendPackets(); 85 81 } 86 82 … … 96 92 return false; 97 93 // send packets 98 client_connection.sendPackets(&event); 99 if(event.type==ENET_EVENT_TYPE_NONE) 100 return true; 101 else 102 return false; 94 return client_connection.sendPackets(); 103 95 } 104 96 … … 131 123 } 132 124 125 /** 126 * Sends out all the packets queued by addXXX 127 */ 128 bool Client::sendPackets(){ 129 ENetEvent event; 130 // send packets 131 client_connection.sendPackets(&event); 132 if(event.type==ENET_EVENT_TYPE_NONE) 133 return true; 134 else 135 return false; 136 } 137 133 138 /** 134 139 * Performs a GameState update 135 140 */ 136 141 void Client::update(){ 137 // to be implemented ================== 138 139 142 ENetPacket *packet; 143 // stop if the packet queue is empty 144 while(!client_connection.queueEmpty()){ 145 packet = client_connection.getPacket(); 146 elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) 147 } 148 return; 140 149 } 141 150 151 void Client::processGamestate( GameState *data){ 152 gamestate.loadSnapshot( *data ); 153 return; 154 } 155 142 156 } -
code/branches/FICN/src/network/Client.h
r337 r369 18 18 #include "ClientConnection.h" 19 19 #include "PacketManager.h" 20 #include "GameStateManager.h" 20 21 21 22 … … 43 44 bool addKeyboard(char key_code); 44 45 46 bool sendPackets(); 47 45 48 void update(); 46 49 … … 48 51 ClientConnection client_connection; 49 52 PacketGenerator pck_gen; 50 51 //const char *server_address; 52 //int port; 53 GameStateManager gamestate; 53 54 bool isConnected; 55 56 // implement data processing functions of PacketDecoder 57 void processGamestate( GameState *data); 54 58 }; 55 59 -
code/branches/FICN/src/network/ClientConnection.cc
r355 r369 54 54 } 55 55 56 ENetPacket *ClientConnection::getPacket(){ 57 ENetAddress address; 58 return getPacket(address); 59 } 60 56 61 bool ClientConnection::queueEmpty(){ 57 62 return buffer.isEmpty(); … … 110 115 quit=true; 111 116 //connect to the server 112 if(!establishConnection()) 117 if(!establishConnection()){ 113 118 quit=true; 119 return; 120 } 114 121 //main loop 115 122 while(!quit){ … … 126 133 break; 127 134 case ENET_EVENT_TYPE_DISCONNECT: 128 // add some error/log handling here 129 // extend ===================== 135 quit=true; 136 // server closed the connection 137 return; 130 138 break; 131 139 case ENET_EVENT_TYPE_NONE: … … 143 151 bool ClientConnection::disconnectConnection(){ 144 152 ENetEvent event; 145 // enet_peer_disconnect(server);146 153 enet_peer_disconnect(server, 0); 147 154 while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){ -
code/branches/FICN/src/network/ClientConnection.h
r337 r369 38 38 ClientConnection(int port, const char* address); 39 39 ENetPacket *getPacket(ENetAddress &address); // thread1 40 ENetPacket *getPacket(); // thread1 40 41 // check wheter the packet queue is empty 41 42 bool queueEmpty(); -
code/branches/FICN/src/network/ConnectionManager.cc
r352 r369 106 106 return false; 107 107 if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) 108 return true; 109 else 110 return false; 111 } 112 113 bool ConnectionManager::sendPackets(){ 114 ENetEvent event; 115 if(server==NULL) 116 return false; 117 if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0) 108 118 return true; 109 119 else … … 117 127 ENetEvent event; 118 128 server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); 119 if(server==NULL) 129 if(server==NULL){ 120 130 // add some error handling here ========================== 121 131 quit=true; 132 return; 133 } 122 134 123 135 while(!quit){ … … 143 155 } 144 156 } 157 disconnectClients(); 145 158 // if we're finishied, destroy server 146 159 enet_host_destroy(server); 160 } 161 162 void ConnectionManager::disconnectClients(){ 163 bool disconnected=false; 164 ENetEvent event; 165 ClientList *temp=client; 166 while(temp!=NULL){ 167 enet_peer_disconnect(temp->event->peer, 0); 168 while( !disconnected && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ 169 switch (event.type) 170 { 171 case ENET_EVENT_TYPE_NONE: 172 case ENET_EVENT_TYPE_CONNECT: 173 case ENET_EVENT_TYPE_RECEIVE: 174 enet_packet_destroy(event.packet); 175 break; 176 case ENET_EVENT_TYPE_DISCONNECT: 177 disconnected=true; 178 break; 179 } 180 } 181 temp = temp->next; 182 disconnected=false; 183 } 184 return; 147 185 } 148 186 -
code/branches/FICN/src/network/ConnectionManager.h
r337 r369 61 61 // send out all queued packets 62 62 bool sendPackets(ENetEvent *event); 63 bool sendPackets(); 63 64 private: 64 65 bool clientDisconnect(ENetPeer *peer); … … 67 68 // implementation of the listener 68 69 void receiverThread(); //thread2 70 void disconnectClients(); 69 71 //packetbuffer 70 72 PacketBuffer buffer; -
code/branches/FICN/src/network/GameStateManager.cc
r346 r369 95 95 } 96 96 if(it==0){ // add the new object 97 97 // =================== factory command to add object 98 // can we be sure the object really was added? 99 it=orxonox::ObjectList<Synchronisable>::end(); 100 it->objectID=sync.objectID; 101 it->classID=sync.classID; 98 102 } 99 103 } else { -
code/branches/FICN/src/network/PacketDecoder.cc
r341 r369 100 100 enet_packet_destroy( packet ); 101 101 102 pr intChat( chatting ); //debug info102 processChat( chatting ); //debug info 103 103 104 104 } … … 118 118 //copy the gamestate data 119 119 memcpy( (void*)(currentState->data), (const void*)(data+3*sizeof( int )), currentState->size ); 120 120 121 121 //clean memory 122 122 enet_packet_destroy( packet ); 123 //run processGamestate 124 processGamestate(currentState); 125 } 126 127 // now the data processing functions: 128 129 void PacketDecoder::processChat( chat *data){ 130 printChat(data); 123 131 } 124 132 -
code/branches/FICN/src/network/PacketManager.h
r341 r369 92 92 void gstate( ENetPacket* packet ); 93 93 94 //process data 95 virtual void processGamestate(GameState *state); 96 virtual void processChat( chat *data); 97 virtual void processAck( ack *data); 98 94 99 //print functions 95 100 void printAck( ack* data ); -
code/branches/FICN/src/network/Server.cc
r337 r369 21 21 */ 22 22 Server::Server(){ 23 24 23 connection = ConnectionManager(); 24 gamestates = GameStateManager(); 25 packet_gen = PacketGenerator(); 25 26 } 26 27 … … 30 31 * @param bindAddress Address to listen on 31 32 */ 32 Server::Server(int port, std::string bindAddress) : connection(port, bindAddress){ 33 34 33 Server::Server(int port, std::string bindAddress){ 34 connection = ConnectionManager(port, bindAddress); 35 gamestates = GameStateManager(); 36 packet_gen = PacketGenerator(); 35 37 } 36 38 … … 40 42 * @param bindAddress Address to listen on 41 43 */ 42 Server::Server(int port, const char *bindAddress) : connection(port, bindAddress){ 43 44 45 46 44 Server::Server(int port, const char *bindAddress){ 45 connection = ConnectionManager(port, bindAddress); 46 gamestates = GameStateManager(); 47 packet_gen = PacketGenerator(); 47 48 } 48 49 50 51 52 49 50 /** 51 * This function opens the server by creating the listener thread 52 */ 53 void Server::open(){ 54 connection.createListener(); 55 return; 56 } 57 58 /** 59 * This function closes the server 60 */ 61 void Server::close(){ 62 connection.quitListener(); 63 return; 64 } 65 66 /** 67 * This function sends out a message to all clients 68 * @param msg message 69 * @return true/false 70 */ 71 bool Server::sendMSG(std::string msg){ 72 ENetPacket *packet = packet_gen.chatMessage(msg.c_str()); 73 connection.addPacketAll(packet); 74 return connection.sendPackets(); 75 } 76 /** 77 * This function sends out a message to all clients 78 * @param msg message 79 * @return true/false 80 */ 81 bool Server::sendMSG(const char *msg){ 82 ENetPacket *packet = packet_gen.chatMessage(msg); 83 connection.addPacketAll(packet); 84 return connection.sendPackets(); 85 } 86 87 void Server::tick(){ 88 } 89 53 90 } -
code/branches/FICN/src/network/Server.h
r337 r369 18 18 #include "ConnectionManager.h" 19 19 #include "PacketManager.h" 20 20 #include "enet/enet.h" 21 21 22 22 namespace network{ … … 31 31 Server(int port, std::string bindAddress); 32 32 Server(int port, const char *bindAddress); 33 33 void open(); 34 void close(); 35 bool sendMSG(std::string msg); 36 bool sendMSG(const char *msg); 37 void tick(); 34 38 private: 35 39 ConnectionManager connection; 36 37 38 39 40 41 40 GameStateManager gamestates; 41 PacketGenerator packet_gen; 42 42 43 43 };
Note: See TracChangeset
for help on using the changeset viewer.