Changeset 413 for code/branches
- Timestamp:
- Dec 5, 2007, 5:59:25 PM (17 years ago)
- Location:
- code/branches/FICN/src/network
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/network/Client.cc
r405 r413 22 22 // set server address to localhost 23 23 isConnected=false; 24 pck_gen = PacketGenerator();25 gamestate = GameStateManager();26 24 } 27 25 … … 33 31 Client::Client(std::string address, int port) : client_connection(port, address){ 34 32 isConnected=false; 35 pck_gen = PacketGenerator();36 gamestate = GameStateManager();37 33 } 38 34 … … 44 40 Client::Client(const char *address, int port) : client_connection(port, address){ 45 41 isConnected=false; 46 pck_gen = PacketGenerator();47 gamestate = GameStateManager();48 42 } 49 43 … … 147 141 148 142 void Client::processGamestate( GameStateCompressed *data){ 149 gamestate. loadSnapshot( *data);143 gamestate.pushGameState(*data); 150 144 return; 151 145 } … … 153 147 void Client::processClassid(classid *clid){ 154 148 orxonox::Identifier *id; 155 orxonox::ID(std::string(clid->message))->setNetworkID(clid->classid); 149 id=orxonox::ID(std::string(clid->message)); 150 if(id!=NULL) 151 id->setNetworkID(clid->classid); 152 return; 156 153 } 157 154 -
code/branches/FICN/src/network/Client.h
r405 r413 18 18 #include "ClientConnection.h" 19 19 #include "PacketManager.h" 20 #include "GameState Manager.h"20 #include "GameStateClient.h" 21 21 #include "orxonox/core/IdentifierIncludes.h" 22 22 … … 52 52 ClientConnection client_connection; 53 53 PacketGenerator pck_gen; 54 GameState Managergamestate;54 GameStateClient gamestate; 55 55 bool isConnected; 56 56 -
code/branches/FICN/src/network/GameStateManager.cc
r407 r413 16 16 GameStateManager::GameStateManager() 17 17 { 18 id=0; 18 19 } 19 20 … … 22 23 } 23 24 25 void GameStateManager::update(){ 26 reference = getSnapshot(id++); 27 return; 28 } 29 30 GameStateCompressed GameStateManager::popGameState(int clientID){ 31 GameState *client = clientGameState[clientID]; 32 GameState *server = &reference; 33 return encode(client, server); 34 } 35 36 37 24 38 /** 25 39 * This function goes through the whole list of synchronisables and … … 27 41 * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list 28 42 */ 29 GameState CompressedGameStateManager::getSnapshot(int id)43 GameState GameStateManager::getSnapshot(int id) 30 44 { 31 45 //the size of the gamestate … … 63 77 } 64 78 retval.size=totalsize; 65 return compress(retval);79 return retval; 66 80 } 67 81 68 /** 69 * This function loads a Snapshort of the gamestate into the universe 70 * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot) 71 */ 72 bool GameStateManager::loadSnapshot(GameStateCompressed compstate) 73 { 74 GameState state = decompress(compstate); 75 unsigned char *data=state.data; 76 // get the start of the Synchronisable list 77 orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start(); 78 syncData sync; 79 // loop as long as we have some data ;) 80 while(data < state.data+state.size){ 81 // prepare the syncData struct 82 sync.length = *(int *)data; 83 data+=sizeof(int); 84 sync.objectID = *(int *)data; 85 data+=sizeof(int); 86 sync.classID = *(int *)data; 87 data+=sizeof(int); 88 sync.data = data; 89 data+=sync.length; 90 91 if(it->objectID!=sync.objectID){ 92 // bad luck ;) 93 // delete the synchronisable (obviously seems to be deleted on the server) 94 while(it != 0 && it->objectID!=sync.objectID){ 95 removeObject(it); 96 } 97 if(it==0){ // add the new object 98 // =================== factory command to add object 99 // can we be sure the object really was added? 100 it=orxonox::ObjectList<Synchronisable>::end(); 101 it->objectID=sync.objectID; 102 it->classID=sync.classID; 103 } 104 } else { 105 // we have our object 106 if(! it->updateData(sync)) 107 std::cout << "We couldn't update objectID: " \ 108 << sync.objectID << "; classID: " << sync.classID << std::endl; 109 } 110 111 } 112 113 return true; 82 83 84 GameStateCompressed GameStateManager::encode(GameState *a, GameState *b){ 85 GameState r = diff(a,b); 86 return compress_(r); 114 87 } 115 88 116 /**117 * This function removes a Synchronisable out of the universe118 * @param it iterator of the list pointing to the object119 * @return iterator pointing to the next object in the list120 */121 void GameStateManager::removeObject(orxonox::Iterator<Synchronisable> &it){122 orxonox::Iterator<Synchronisable> temp=it;123 ++it;124 delete *temp;125 // return it;126 }127 89 128 GameStateCompressed GameStateManager::encode(GameState a, GameState b){ 129 GameState r = diff(a,b); 130 return compress(r); 131 } 132 133 GameState GameStateManager::decode(GameState a, GameStateCompressed x){ 134 GameState t = decompress(x); 135 return diff(a, t); 136 } 137 138 GameState GameStateManager::diff(GameState a, GameState b){ 139 unsigned char *ap = a.data, *bp = b.data; 90 GameState GameStateManager::diff(GameState *a, GameState *b){ 91 unsigned char *ap = a->data, *bp = b->data; 140 92 int of=0; // pointers offset 141 93 int dest_length=0; 142 if(a .size>=b.size)143 dest_length=a .size;94 if(a->size>=b->size) 95 dest_length=a->size; 144 96 else 145 dest_length=b .size;97 dest_length=b->size; 146 98 unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 147 while(of<a .size && of<b.size){99 while(of<a->size && of<b->size){ 148 100 *(dp+of)=*(ap+of)^*(bp+of); // do the xor 149 101 ++of; 150 102 } 151 if(a .size!=b.size){ // do we have to fill up ?103 if(a->size!=b->size){ // do we have to fill up ? 152 104 unsigned char n=0; 153 if(a .size<b.size){105 if(a->size<b->size){ 154 106 while(of<dest_length){ 155 107 *(dp+of)=n^*(bp+of); … … 164 116 } 165 117 // should be finished now 166 GameState r = {b .id, dest_length, dp};118 GameState r = {b->id, dest_length, dp}; 167 119 return r; 168 120 } 169 121 170 GameStateCompressed GameStateManager::compress (GameState a) {122 GameStateCompressed GameStateManager::compress_(GameState a) { 171 123 int size = a.size; 172 124 uLongf buffer = (uLongf)((a.size + 12)*1.01)+1; … … 176 128 177 129 switch ( retval ) { 178 case Z_OK: cout << "successfully compressed" <<endl; break;179 case Z_MEM_ERROR: cout << "not enough memory available" <<endl; break;180 case Z_BUF_ERROR: cout << "not enough memory available in the buffer" <<endl; break;181 case Z_DATA_ERROR: cout << "data corrupted" <<endl; break;130 case Z_OK: std::cout << "successfully compressed" << std::endl; break; 131 case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break; 132 case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break; 133 case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break; 182 134 } 183 135 184 GameStateCompressed compressedGamestate = new GameStateCompressed;136 GameStateCompressed compressedGamestate; 185 137 compressedGamestate.compsize = buffer; 186 138 compressedGamestate.normsize = size; … … 191 143 } 192 144 193 GameState GameStateManager::decompress(GameStateCompressed a){ 194 int normsize = a.normsize; 195 int compsize = a.compsize; 196 unsigned char* dest = (unsigned char*)malloc( normsize ); 197 int retval; 198 retval = uncompress( dest, &normsize, a.data, compsize ); 199 200 switch ( retval ) { 201 case Z_OK: cout << "successfully compressed" << endl; break; 202 case Z_MEM_ERROR: cout << "not enough memory available" << endl; break; 203 case Z_BUF_ERROR: cout << "not enough memory available in the buffer" << endl; break; 204 case Z_DATA_ERROR: cout << "data corrupted" << endl; break; 205 } 206 207 GameState gamestate = new GameState; 208 gamestate.id = a.id; 209 gamestate.size = normsize; 210 gamestate.data = dest; 211 212 return gamestate; 213 } 145 214 146 215 147 } -
code/branches/FICN/src/network/GameStateManager.h
r405 r413 13 13 #define NETWORK_GAMESTATEMANAGER_H 14 14 15 #include <vector> 16 17 #include "zlib.h" 18 15 19 #include "Synchronisable.h" 16 20 #include "orxonox/core/IdentifierIncludes.h" 17 21 #include "orxonox/core/Iterator.h" 22 #include "PacketManager.h" 18 23 19 24 namespace network { … … 62 67 GameStateManager(); 63 68 ~GameStateManager(); 64 GameStateCompressed getSnapshot(int id); 65 bool loadSnapshot(GameStateCompressed state); 66 GameStateCompressed encode(GameState a, GameState b); 67 GameState decode(GameState a, GameStateCompressed x); 69 void update(); 70 GameStateCompressed popGameState(int clientID); 68 71 private: 69 void removeObject(orxonox::Iterator<Synchronisable> &it); 70 GameState diff(GameState a, GameState b); 71 GameStateCompressed compress(GameState a); 72 GameState decompress(GameStateCompressed a); 72 GameState getSnapshot(int id); 73 GameStateCompressed encode(GameState *a, GameState *b); 74 GameState diff(GameState *a, GameState *b); 75 GameStateCompressed compress_(GameState a); 76 77 std::vector<GameState *> clientGameState; 78 GameState reference; 79 int id; 73 80 }; 74 81 -
code/branches/FICN/src/network/PacketManager.h
r405 r413 7 7 8 8 //enum netowk generaly used to set the type ID of a packet 9 namespace network 10 { 9 namespace network{ 10 11 11 enum packet_id { 12 12 ACK, … … 32 32 ENetPacket* mousem( double x, double y, int reliable = ENET_PACKET_FLAG_RELIABLE ); 33 33 ENetPacket* keystrike( char press, int reliable = ENET_PACKET_FLAG_RELIABLE ); 34 35 ENetPacket* gstate( GameStateCompressed*states, int reliable = ENET_PACKET_FLAG_RELIABLE );34 ENetPacket* chatMessage( const char* message, int reliable = ENET_PACKET_FLAG_RELIABLE ); 35 ENetPacket* gstate( GameStateCompressed *states, int reliable = ENET_PACKET_FLAG_RELIABLE ); 36 36 ENetPacket* clid( int classid, std::string classname, int reliable = ENET_PACKET_FLAG_RELIABLE ); 37 37 private: … … 116 116 void printKey( keyboard* data ); 117 117 void printChat( chat* data ); 118 void printGamestate( GameStateCompressed *data );118 void printGamestate( GameStateCompressed *data ); 119 119 void printClassid( classid *cid); 120 120 }; -
code/branches/FICN/src/network/Server.h
r380 r413 18 18 #include "ConnectionManager.h" 19 19 #include "PacketManager.h" 20 #include "GameStateManager.h" 20 21 #include "enet/enet.h" 21 22
Note: See TracChangeset
for help on using the changeset viewer.