Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1440 for code/branches


Ignore:
Timestamp:
May 27, 2008, 11:23:32 AM (16 years ago)
Author:
scheusso
Message:

cleanup up packetgenerator/decoder and improved speed of gamestate and ack sending

Location:
code/branches/network/src/network
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network/src/network/Client.cc

    r1431 r1440  
    140140  }
    141141
    142   /**
    143   * submits a MouseAction to the server
    144   * @param x x Coordinate
    145   * @param y y Coordinate
    146   * @return true/false
    147   */
    148   bool Client::sendMouse(double x, double y){
    149     // generate packet and add it to the queue
    150     if(!isConnected)
    151       return false;
    152     if(!client_connection.addPacket(pck_gen.mousem(x, y)))
    153       return false;
    154     // send packets
    155     return client_connection.sendPackets();
    156   }
    157 
    158   /**
    159   * submits a Keystrike to the server
    160   * @param key_code code to submit
    161   * @return true/false
    162   */
    163   bool Client::sendKeyboard(char key_code){
    164     // generate packet and add it to queue
    165     if(!isConnected)
    166       return false;
    167     if(!client_connection.addPacket(pck_gen.keystrike(key_code)))
    168       return false;
    169     // send packets
    170     return client_connection.sendPackets();
    171   }
     142 
     143
     144 
    172145
    173146  /**
     
    186159  }
    187160
    188   /**
    189   * Adds a MouseAction to the PacketQueue
    190   * @param x x Coordinate
    191   * @param y y Coordinate
    192   * @return true/false
    193   */
    194   bool Client::addMouse(double x, double y){
    195     // generate packet and add it to the queue
    196     if(client_connection.addPacket(pck_gen.mousem(x, y)))
    197       return true;
    198     else
    199       return false;
    200   }
    201 
    202   /**
    203   * Adds a Keystrike to the PacketQueue
    204   * @param key_code
    205   * @return true/false
    206   */
    207   bool Client::addKeyboard(char key_code){
    208     // generate packet and add it to queue
    209     if(client_connection.addPacket(pck_gen.keystrike(key_code)))
    210       return true;
    211     else
    212       return false;
    213   }
    214161
    215162  /**
  • code/branches/network/src/network/Client.h

    r1431 r1440  
    7575    bool closeConnection();
    7676
    77     bool sendMouse(double x, double y);
    78     bool sendKeyboard(char key_code);
    7977    bool sendChat( std::string message );
    80 
    81     bool addMouse(double x, double y);
    82     bool addKeyboard(char key_code);
    83 
     78   
    8479    bool sendPackets();
    8580   
  • code/branches/network/src/network/PacketDecoder.cc

    r1432 r1440  
    6868    case COMMAND:
    6969      return command( packet, clientId );
    70     case MOUSE:
    71       mousem( packet, clientId );
    72       return true;
    73     case KEYBOARD:
    74       keystrike( packet, clientId );
    75       return true;
    7670    case CHAT:
    7771      chatMessage( packet, clientId );
     
    135129  }
    136130
    137   void PacketDecoder::mousem( ENetPacket* packet, int clientId )
    138   {
    139     mouse* mouseMove = new mouse;
    140     //copy data of packet->data to new struct
    141     *mouseMove = *(mouse*)packet->data;
    142 
    143     //clean memory
    144     enet_packet_destroy( packet );
    145     printMouse( mouseMove ); //debug info
    146   }
    147 
    148   void PacketDecoder::keystrike( ENetPacket* packet, int clientId )
    149   {
    150     keyboard* key = new keyboard;
    151     *key = *(keyboard*)packet->data; //see above
    152 
    153     //clean memory
    154     enet_packet_destroy( packet );
    155     printKey( key ); //debug info
    156 
    157   }
    158 
    159131  void PacketDecoder::chatMessage( ENetPacket* packet, int clientId )
    160132  {
     
    189161      return;
    190162    }
    191     //since it's not alowed to use void* for pointer arithmetic
    192     //FIXME: variable never used
    193     unsigned char* data = (unsigned char *)(packet->data);
    194163    //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int )
    195164    memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) );
     
    307276  }
    308277
    309   void PacketDecoder::printMouse( mouse* data )
    310   {
    311     COUT(5) << "data id: " << data->id << std::endl;
    312     COUT(5) << "data:    " << data->x << " " << data->y << std::endl;
    313   }
    314 
    315   void PacketDecoder::printKey( keyboard* data )
    316   {
    317     COUT(5) << "data id: " << data->id << std::endl;
    318     COUT(5) << "data:    " << (char)data->press << std::endl;
    319   }
    320278
    321279  void PacketDecoder::printChat( chat* data, int clientId )
  • code/branches/network/src/network/PacketGenerator.cc

    r1432 r1440  
    7878  {
    7979    COUT(4) << "PacketGenerator: generating new acknowledgement, id: " << state << std::endl;
    80     ack* ackreq = new ack;
     80   
     81    ENetPacket *packet = enet_packet_create( NULL , sizeof( ack ), reliable );
     82    ack* ackreq = (ack *)packet->data;
    8183    ackreq->id = ACK;
    8284    ackreq->a = state;
    83 
    84     ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable );
    85     delete ackreq;
     85    //delete ackreq;
    8686    return packet;
    8787  }
     
    103103  }
    104104
    105   /*### mouseupdates */
    106   ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )
    107   {
    108     COUT(4) << "PacketGenerator: generating new mouse" << std::endl;
    109     mouse* mousemove = new mouse;
    110     mousemove->id = MOUSE;
    111     mousemove->x = x;
    112     mousemove->y = y;
    113 
    114     ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable );
    115     delete mousemove;
    116     return packet;
    117   }
    118 
    119   /*### keystrikes updates */
    120   ENetPacket* PacketGenerator::keystrike( char press, int reliable )
    121   {
    122     COUT(4) << "PacketGenerator: generating new keyboard" << std::endl;
    123     keyboard* key = new keyboard;
    124     key->id = KEYBOARD;
    125     key->press = press;
    126 
    127     ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable );
    128     delete key;
    129     return packet;
    130   }
    131105
    132106  /*### chat messages packet */
     
    154128    if(totalLen==0)
    155129      return NULL;
    156     unsigned char *data = new unsigned char[totalLen];
     130    ENetPacket *packet = enet_packet_create( NULL , totalLen+sizeof(uint32_t), reliable );
     131    //unsigned char *data = new unsigned char[totalLen];
     132    unsigned char *data = packet->data;
    157133    memcpy( (void*)(data), (const void*)&gid, sizeof( int ) ); //this is the enet id
    158134    memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id
     
    167143    //create an enet packet with the generated bytestream
    168144    COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl;
    169     ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
    170     delete[] data;
     145    //delete[] data;
    171146    if(!addCRC(packet))
    172147      COUT(3) << "could not add crc to gamestate packet" << std::endl;
     
    212187  bool PacketGenerator::addCRC( ENetPacket *packet){
    213188    unsigned char *data = packet->data;
    214     uint32_t crc32=calcCRC(data, packet->dataLength);
     189    uint32_t crc32=calcCRC(data, packet->dataLength-sizeof(uint32_t));
    215190    // now append the crc to the packet data
    216     int oldlength = packet->dataLength;
    217     if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){
     191    int oldlength = packet->dataLength-sizeof(uint32_t);
     192    //if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){
    218193      memcpy(&packet->data[oldlength], &crc32, sizeof(uint32_t));
    219194      return true;
    220     }else{
    221       COUT(3) << "could not add crc to gamestate" << std::endl;
    222       return false;
    223     }
     195    //}else{
     196     // COUT(3) << "could not add crc to gamestate" << std::endl;
     197     // return false;
     198    //}
    224199  }
    225200
  • code/branches/network/src/network/PacketManager.h

    r1360 r1440  
    5959    ENetPacket* acknowledgement( int state, int reliable = 0 ); // we do not want reliability
    6060    ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE );
    61     ENetPacket* mousem( double x, double y, int reliable = ENET_PACKET_FLAG_RELIABLE );
    62     ENetPacket* keystrike( char press, int reliable = ENET_PACKET_FLAG_RELIABLE );
    6361    ENetPacket* chatMessage( const char* message, int reliable = ENET_PACKET_FLAG_RELIABLE );
    6462    ENetPacket* gstate( GameStateCompressed *states, int reliable = 0 ); // we do not want reliability of gamestates
     
    9492    void acknowledgement( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    9593    bool command( ENetPacket* packet, int clientId );
    96     void mousem( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    97     void keystrike( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    9894    void chatMessage( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    9995    void gstate( ENetPacket* packet, int clientID = CLIENTID_CLIENT );
     
    113109    //print functions
    114110    void printAck( ack* data );
    115     void printMouse( mouse* data );
    116     void printKey( keyboard* data );
    117111    void printChat( chat* data, int clientId );
    118112    void printGamestate( GameStateCompressed *data );
  • code/branches/network/src/network/PacketTypes.h

    r1293 r1440  
    4848  enum packet_id {
    4949    ACK,
    50     MOUSE,
    5150    COMMAND,
    52     KEYBOARD,
    5351    CHAT,
    5452    GAMESTATE ,
     
    104102  };
    105103
    106   struct mouse {
    107     int id;
    108     double x;
    109     double y;
    110   };
    111 
    112   struct keyboard {
    113     int id;
    114     char press;
    115   };
    116104  //only in this class, not PacketGenerator, used as pattern to put incoming
    117105  //bytes inside
Note: See TracChangeset for help on using the changeset viewer.