Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 28, 2007, 5:20:23 PM (17 years ago)
Author:
nicolasc
Message:

merge network

Location:
code/branches/merger/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/merger/src/network/CMakeLists.txt

    r319 r332  
    11PROJECT(Orxonox)
    22
    3 SET(SRC_FILES Client.cc ConnectionManager.cc PacketBuffer.cc PacketGenerator.cc Synchronisable.cc dummyclient2.cc ClientConnection.cc GameStateManager.cc PacketDecoder.cc Server.cc dummyclient.cc dummyserver.cc)
     3SET(SRC_FILES Client.cc ConnectionManager.cc PacketBuffer.cc PacketGenerator.cc Synchronisable.cc ClientConnection.cc GameStateManager.cc PacketDecoder.cc Server.cc)
    44
    5 ADD_LIBRARY(network ${SRC_FILES})
     5INCLUDE_DIRECTORIES(..)
     6
     7ADD_LIBRARY(network ${SRC_FILES} ${ENet_LIBRARY})
  • code/branches/merger/src/network/GameStateManager.cc

    r278 r332  
    2222}
    2323
    24 GameState GameStateManager::getSnapshot(){
     24/**
     25 * This function goes through the whole list of synchronisables and
     26 * saves all the synchronisables to a flat "list".
     27 * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list
     28 */
     29GameState GameStateManager::getSnapshot(int id)
     30{
     31  //the size of the gamestate
     32  int totalsize=0;
     33  //the size of one specific synchronisable
     34  int tempsize=0;
     35  // get the start of the Synchronisable list
     36  orxonox::Iterator<Synchronisable> it;
     37  // struct for return value of Synchronisable::getData()
     38  syncData sync;
     39 
     40  GameState retval; //return value
     41  retval.id=id;
     42  // reserve a little memory and increase it later on
     43  retval.data = (unsigned char*)malloc(1);
     44 
     45  // offset of memory functions
     46  int offset=0;
     47  // go through all Synchronisables
     48  for(it = orxonox::ObjectList<Synchronisable>::start(); it != 0; ++it){
     49    //get size of the synchronisable
     50    tempsize=it->getSize();
     51    // add place for data and 3 ints (length,classid,objectid)
     52    totalsize+=tempsize+3*sizeof(int);
     53    // allocate additional space
     54    retval.data = (unsigned char *)realloc((void *)retval.data, totalsize);
     55   
     56    // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length)
     57    sync=it->getData(retval.data+offset+3*sizeof(int));
     58    *(retval.data+offset)=sync.length;
     59    *(retval.data+offset+sizeof(int))=sync.objectID;
     60    *(retval.data+offset+2*sizeof(int))=sync.classID;
     61    // increase data pointer
     62    offset+=tempsize+3*sizeof(int);
     63  }
     64  retval.size=totalsize;
     65  return retval;
     66}
     67
     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 */
     72bool GameStateManager::loadSnapshot(GameState state)
     73{
     74  unsigned char *data=state.data;
     75  // get the start of the Synchronisable list
     76  orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
     77  syncData sync;
     78  // loop as long as we have some data ;)
     79  while(data < state.data+state.size){
     80    // prepare the syncData struct
     81    sync.length = *(int *)data;
     82    data+=sizeof(int);
     83    sync.objectID = *(int *)data;
     84    data+=sizeof(int);
     85    sync.classID = *(int *)data;
     86    data+=sizeof(int);
     87    sync.data = data;
     88    data+=sync.length;
     89   
     90    if(it->objectID!=sync.objectID){
     91      // bad luck ;)
     92      // delete the synchronisable (obviously seems to be deleted on the server)
     93      while(it != 0 && it->objectID!=sync.objectID){
     94        removeObject(it);
     95      }
     96      if(it==0){  // add the new object
     97       
     98      }
     99    } else {
     100      // we have our object
     101      if(! it->updateData(sync))
     102        std::cout << "We couldn't update objectID: " \
     103          << sync.objectID << "; classID: " << sync.classID << std::endl;
     104    }
     105   
     106  }
     107 
    25108 
    26109}
    27110
    28 bool GameStateManager::loadSnapshot(GameState state){
    29  
     111/**
     112 * This function removes a Synchronisable out of the universe
     113 * @param it iterator of the list pointing to the object
     114 * @return iterator pointing to the next object in the list
     115 */
     116// orxonox::Iterator<Synchronisable> removeObject(orxonox::Iterator<Synchronisable> it){
     117void removeObject(orxonox::Iterator<Synchronisable> &it){
     118  orxonox::Iterator<Synchronisable> temp=it;
     119  ++it;
     120  delete  *temp;
     121//   return it;
    30122}
    31123
     124
    32125}
     126
     127
  • code/branches/merger/src/network/GameStateManager.h

    r278 r332  
    1313#define NETWORK_GAMESTATEMANAGER_H
    1414
     15#include "Synchronisable.h"
     16#include "orxonox/core/IdentifierIncludes.h"
     17#include "orxonox/core/Iterator.h"
     18
    1519namespace network {
    1620
     
    2024 * data: pointer to the data allocated in the memory
    2125 */
    22 struct GameState{
     26  struct GameState{
     27  int id;
    2328  int size;
    2429  unsigned char *data;
     
    3641  GameStateManager();
    3742  ~GameStateManager();
    38   GameState getSnapshot();
     43  GameState getSnapshot(int id);
    3944  bool loadSnapshot(GameState state);
    4045private:
    41  
     46  bool removeObject(orxonox::Iterator<Synchronisable> it);
     47
    4248};
    4349
  • code/branches/merger/src/network/PacketDecoder.cc

    r278 r332  
    11/*
    22 * Class contains functions to determine and decode incomming packages
     3 * ->don't read this without the class PacketGenerator, since they belong together
    34 *
    45 * Autor: Dumeni Manatschal
    56 *
    67*/
    7 
    88
    99#include "enet/enet.h"
     
    2121{
    2222        int client = clientId;
    23         cout << "clientId: " << client << endl;
    24         int id = (int)*packet->data;
     23        cout << "clientId: " << client << endl; //control cout, not important, just debugging info
     24        int id = (int)*packet->data; //the first 4 bytes are always the enet packet id
    2525        switch( id ) {
    2626        case ACK:
     
    4040                return true;
    4141                break;
     42        case GAMESTATE:
     43                gstate( packet );
     44                return true;
     45                break;
    4246        }
    4347        return false;
     
    4953{
    5054        ack* a = new ack;
    51         *a = *(ack*)packet->data;
    52         printAck( a );
     55        *a = *(ack*)packet->data; //press pattern of ack on new data
     56       
     57        //clean memory
     58        enet_packet_destroy( packet );
     59       
     60        printAck( a ); //debug info
    5361}
    5462
     
    5664{
    5765        mouse* mouseMove = new mouse;
    58         *mouseMove = *(mouse*)packet->data;
    59         printMouse( mouseMove );
     66        //copy data of packet->data to new struct
     67        *mouseMove = *(mouse*)packet->data;
     68       
     69        //clean memory
     70        enet_packet_destroy( packet );
     71       
     72        printMouse( mouseMove ); //debug info
    6073}
    6174
     
    6376{
    6477        keyboard* key = new keyboard;
    65         *key = *(keyboard*)packet->data;
    66         printKey( key );
     78        *key = *(keyboard*)packet->data; //see above
     79       
     80        //clean memory
     81        enet_packet_destroy( packet );
     82       
     83        printKey( key ); //debug info
     84
    6785}
    6886
     
    7088{
    7189        chat* chatting = new chat;
    72         chatting->id = (int)*packet->data;
    73         char* reserve = new char[packet->dataLength-4];
    74         memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-4 );
     90        chatting->id = (int)*packet->data; //first copy id into new struct
     91        //since the chat message is a char*, allocate the memory needed
     92        char* reserve = new char[packet->dataLength-4];
     93        //copy the transmitted bytestream into the new generated char*,
     94        //note the lenght of the message is represented as "packet->dataLength-sizeof( int )"
     95        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) );
     96        //put pointer of chatting struct to the begining of the new generated char*
    7597        chatting->message = reserve;
    76         printChat( chatting );
     98       
     99        //clean memory
     100        enet_packet_destroy( packet );
     101       
     102        printChat( chatting ); //debug info
     103       
     104}
     105
     106void PacketDecoder::gstate( ENetPacket* packet )
     107{
     108        GameState* currentState = new GameState;
     109        //since it's not alowed to use void* for pointer arithmetic
     110        unsigned char* data = (unsigned char*)packet->data;
     111        //copy the gamestate id into the struct, which is located at second place data+sizeof( int )
     112        memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) );
     113        //copy the size of the gamestate data into the new gamestate struct, located at 3th
     114        //position of the data stream, data+2*sizeof( int )
     115        memcpy( (void*)&(currentState->size), (const void*)(data+2*sizeof( int )), sizeof( int) );
     116        //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
     117        currentState->data = (unsigned char*)(malloc( currentState->size ));
     118        //copy the gamestate data
     119        memcpy( (void*)(currentState->data), (const void*)(data+3*sizeof( int )), currentState->size );
     120       
     121        //clean memory
     122        enet_packet_destroy( packet );
    77123}
    78124
     
    100146{
    101147        cout << "data id: " << data->id << endl;
    102         cout << "blablabla" << endl;
    103148        cout << "data:    " << data->message << endl;
    104149}
     150
     151void PacketDecoder::printGamestate( GameState* data )
     152{
     153        cout << "id of gamestate:   " << data->id << endl;
     154        cout << "size of gamestate: " << data->size << endl;
     155}
  • code/branches/merger/src/network/PacketGenerator.cc

    r278 r332  
    11/*
    22 *Class generates packets that can be send by enet
     3 * ->don't read this without the class PacketDecoder, since they belong together
    34 *
    45 * Autor: Dumeni Manatschal
     
    6869}
    6970
     71/*### gamestate packet */
     72ENetPacket* PacketGenerator::gstate( GameState* states, int reliable )
     73{
     74        int* gid; *gid = GAMESTATE; //first assign the correct enet id
     75        int totalLen = 3*sizeof( int ) + states->size; //calculate the total size of the datastream memory
     76        unsigned char* data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream
     77        memcpy( (void*)(data), (const void*)gid, sizeof( int ) ); //this is the enet id
     78        memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the gamestate id
     79        //this is the size of the gamestate data, place at 3th position of the enet datastream
     80        memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->size), sizeof(int));
     81        //place the gamestate data at the end of the enet datastream
     82        memcpy( (void*)(data+3*sizeof( int )), (const void*)states->data, states->size );
     83        //create an enet packet with the generated bytestream
     84        ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
     85       
     86        return packet;
     87}
  • code/branches/merger/src/network/PacketManager.h

    r278 r332  
    33
    44#include <enet/enet.h>
     5#include <network/GameStateManager.h>
    56
    67//enum netowk generaly used to set the type ID of a packet
     
    1112        MOUSE,
    1213        KEYBOARD,
    13         CHAT
     14        CHAT,
     15        GAMESTATE
    1416};
    1517
     
    1719 * class to generate packets
    1820 *
    19  * Autor: Dumeni Manatschal
     21 * @autor: Dumeni Manatschal
    2022 *
    2123*/
     
    2931        ENetPacket* keystrike( char press, int reliable = ENET_PACKET_FLAG_RELIABLE );
    3032        ENetPacket* chatMessage( const char* message, int reliable = ENET_PACKET_FLAG_RELIABLE );
     33        ENetPacket* gstate( GameState* states, int reliable = ENET_PACKET_FLAG_RELIABLE );
    3134private:
    3235        //used to set the bytes in the right order
     
    5154 * class used to decode incoming packets
    5255 *
    53  * Autor: Dumeni Manatschal
     56 * @autor: Dumeni Manatschal
    5457 *
    5558*/
     
    8790        void keystrike( ENetPacket* packet );
    8891        void chatMessage( ENetPacket* packet );
     92        void gstate( ENetPacket* packet );
    8993       
    9094        //print functions
     
    9397        void printKey( keyboard* data );
    9498        void printChat( chat* data );
    95         void printPeer( ENetPeer* peer );
     99        void printGamestate( GameState* data );
    96100};
    97101}
  • code/branches/merger/src/network/Synchronisable.h

    r278 r332  
    1414
    1515#include <list>
     16#include <iostream>
     17
     18#include "orxonox/core/IdentifierIncludes.h"
    1619
    1720namespace network {
     
    2023struct syncData{
    2124  int length;
     25  int objectID;
    2226  int classID;
    23   int objectID;
    2427  unsigned char *data;
    2528};
     
    4043  Synchronisable();
    4144
    42   ~Synchronisable();
     45  virtual ~Synchronisable();
    4346  int objectID;
    4447  int classID;
     
    5255
    5356private:
     57/*  bool removeObject(Iterator<Synchronisable> it);*/
     58 
    5459  std::list<SYNCVAR> syncList;
    5560  int datasize;
  • code/branches/merger/src/orxonox/core/Iterator.h

    r258 r332  
    1 #ifndef _Iterator_H__
    2 #define _Iterator_H__
     1#ifndef _Iterator_H2__
     2#define _Iterator_H2__
    33
    44namespace orxonox
Note: See TracChangeset for help on using the changeset viewer.