Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 25, 2007, 11:06:13 PM (17 years ago)
Author:
scheusso
Message:

added GameStateManager class:

  • gets snapshots of the universe (gamestate) and returns them
  • processes snapshots and loads them to the universe

modified Synchronisable

  • added a function getData(usigned char *data), which puts the data to

the already given pointer (more efficient memory handling for the
GameState class)

  • added a function getSize(), which tells you the total size of the

current gamestate (so that you can allocate enough memory and give it to
getData

Location:
code/branches/network/src/network
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network/src/network/Makefile

    r237 r247  
    2929Synchronisable.o: Synchronisable.cc
    3030        ${CC} -c Synchronisable.cc -g ${INC}
     31GameStateManager.o: GameStateManager.cc
     32        ${CC} -c GameStateManager.cc -g ${INC}
    3133
    3234clean:
  • code/branches/network/src/network/Makefile.tardis

    r237 r247  
    2828PacketDecoder.o: PacketDecoder.cc
    2929        ${CC} -c PacketDecoder.cc -o PacketDecoder.o -g ${INC}
     30Synchronisable.o: Synchronisable.cc
     31        ${CC} -c Synchronisable.cc -g ${INC}
     32GameStateManager.o: GameStateManager.cc
     33        ${CC} -c GameStateManager.cc -g ${INC}
    3034
    3135clean:
  • code/branches/network/src/network/Synchronisable.cc

    r245 r247  
    55//
    66//
    7 // Author:  Oliver Scheuss, (C) 2007
     7// Author:  Dumeni, Oliver Scheuss, (C) 2007
    88//
    99// Copyright: See COPYING file that comes with this distribution
     
    1414namespace network {
    1515
     16/**
     17 * Constructor:
     18 * calls registarAllVariables, that has to be implemented by the inheriting classID
     19 */
    1620Synchronisable::Synchronisable()
    1721{
     22  datasize=0;
    1823  registerAllVariables();
    1924}
     
    2530}
    2631
     32/**
     33 * This function is used to register a variable to be synchronized
     34 * also counts the total datasize needed to save the variables
     35 * @param var pointer to the variable
     36 * @param size size of the datatype the variable consists of
     37 */
    2738void Synchronisable::registerVar(const void *var, int size){
     39  // create temporary synch.Var struct
    2840  synchronisableVariable temp={size, var};
     41  // increase datasize
     42  datasize+=sizeof(int)+size;
     43  // push temp to syncList (at the bottom)
    2944  syncList.push_back(temp);
    3045}
     46
     47/**
     48 * note: only use this function for debug use, because it's inefficient (in order to produce a gamestate, you have to copy the whole data again to another memory location after this process)
     49 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
     50 * structure of the bitstream:
     51 * (var1_size,var1,var2_size,var2,...)
     52 * varx_size: size = sizeof(int)
     53 * varx: size = varx_size
     54 * @return data containing all variables and their sizes
     55 */
    3156syncData Synchronisable::getData(){
    3257  std::list<synchronisableVariable>::iterator i;
     
    5681  return retVal;
    5782}
     83/**
     84 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
     85 * Difference to the above function:
     86 * takes a pointer to already allocated memory (must have at least getSize bytes length)
     87 * structure of the bitstream:
     88 * (var1_size,var1,var2_size,var2,...)
     89 * varx_size: size = sizeof(int)
     90 * varx: size = varx_size
     91 * @return data containing all variables and their sizes
     92 */
     93syncData Synchronisable::getData(unsigned char *mem){
     94  std::list<synchronisableVariable>::iterator i;
     95  syncData retVal;
     96  retVal.objectID=this->objectID;
     97  retVal.classID=this->classID;
     98  retVal.length=datasize;
     99  retVal.data=mem;
     100  // copy to location
     101  //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
     102  int n=0;
     103  for(i=syncList.begin(); n<datasize && i!=syncList.end(); i++){
     104        //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID* 
     105    std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int));
     106    n+=sizeof(int);
     107    //CHANGED: i->var TO (const void*)(&(i->var)) SINCE var IS A POINTER, NO & BEFORE i
     108    std::memcpy(retVal.data+n, (const void*)(i->var), i->size);
     109    n+=i->size;
     110  }
     111  return retVal;
     112}
     113
     114/**
     115 * This function takes a syncData struct and takes it to update the variables
     116 * @param vars data of the variables
     117 * @return true/false
     118 */
    58119bool Synchronisable::updateData(syncData vars){
    59120  unsigned char *data=vars.data;
     
    70131}
    71132
     133/**
     134 * This function returns the total amount of bytes needed by getData to save the whole content of the variables
     135 * @return amount of bytes
     136 */
     137int Synchronisable::getSize(){
     138  return datasize;
    72139}
     140
     141}
  • code/branches/network/src/network/Synchronisable.h

    r237 r247  
    1010//
    1111//
    12 #ifndef NETWORKSYNCHRONISABLE_H
    13 #define NETWORKSYNCHRONISABLE_H
     12#ifndef NETWORK_SYNCHRONISABLE_H
     13#define NETWORK_SYNCHRONISABLE_H
    1414
    1515#include <list>
     
    4646  void registerVar(const void *var, int size);
    4747  syncData getData();
     48  syncData getData(unsigned char *mem);
     49  int getSize();
    4850  bool updateData(syncData vars);
    4951  virtual void registerAllVariables();
     
    5153private:
    5254  std::list<SYNCVAR> syncList;
     55  int datasize;
    5356};
    5457
Note: See TracChangeset for help on using the changeset viewer.