Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/PacketGenerator.cc @ 1432

Last change on this file since 1432 was 1432, checked in by scheusso, 16 years ago

found a bug in gamestate diffing (in some cases the gamestate diffed was
filled with zeros at the end)

File size: 8.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Dumeni Manatschal, (C) 2007
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/*
30* Class generates packets that can be send by enet
31* ->don't read this without the class PacketDecoder, since they belong together
32*
33* Autor: Dumeni Manatschal
34*
35*/
36
37#include "PacketManager.h"
38#include "PacketTypes.h"
39
40#include <iostream>
41#include <list>
42#include <string>
43#include <cstring>
44
45
46namespace network
47{
48  void calcCRCBit(uint32_t &crc32, int bit){
49    int hbit;
50 
51    hbit=(crc32 & 0x80000000) ? 1 : 0;
52    if (hbit != bit)
53      crc32=(crc32<<1) ^ NETWORK_CRC32POLY;
54    else
55      crc32=crc32<<1;
56  }
57 
58  uint32_t calcCRC(unsigned char *data, unsigned int dataLength){
59    uint32_t crc32=0;
60    for(unsigned int i=0; i<dataLength; i++){
61      calcCRCBit(crc32, (data[i]&0x1)>>0); // 1st bit
62      calcCRCBit(crc32, (data[i]&0x2)>>1); // 2nd bit
63      calcCRCBit(crc32, (data[i]&0x3)>>2); // 3rd bit
64      calcCRCBit(crc32, (data[i]&0x4)>>3); // 4th bit
65      calcCRCBit(crc32, (data[i]&0x5)>>4); // 5th bit
66      calcCRCBit(crc32, (data[i]&0x6)>>5); // 6th bit
67      calcCRCBit(crc32, (data[i]&0x7)>>6); // 7th bit
68      calcCRCBit(crc32, (data[i]&0x8)>>7); // 8th bit
69    }
70    return crc32;
71  }
72 
73  PacketGenerator::PacketGenerator() { }
74
75  //following functions create a packet in form of bytestream
76
77  ENetPacket* PacketGenerator::acknowledgement( int state, int reliable )
78  {
79    COUT(4) << "PacketGenerator: generating new acknowledgement, id: " << state << std::endl;
80    ack* ackreq = new ack;
81    ackreq->id = ACK;
82    ackreq->a = state;
83
84    ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable );
85    delete ackreq;
86    return packet;
87  }
88 
89  ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE )
90  {
91    if(dataLength==0)
92      return NULL;
93    unsigned char *stream = new unsigned char[dataLength + 2*sizeof(int)];
94    if(!stream)
95      return NULL;
96    packet_id a = COMMAND;
97    memcpy(stream, (void*)&a, sizeof(int));
98    memcpy((unsigned char *)stream+sizeof(int), (void*)&dataLength, sizeof(int));
99    memcpy((unsigned char *)stream+2*sizeof(int), data, dataLength);
100    ENetPacket *packet = enet_packet_create(stream, dataLength+2*sizeof(int), reliable);
101    delete[] stream; // TODO: we could also tell enet not to copy the data, but to use the exisiting memory
102    return packet;
103  }
104
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  }
131
132  /*### chat messages packet */
133  ENetPacket* PacketGenerator::chatMessage( const char* message, int reliable )
134  {
135    int* trans = new int[sizeof(int) + strlen(message) + 1];
136    *trans = CHAT;
137    //be carefull here, don't forget to allocate the space before using it ;-)
138    memcpy( &trans[1], (const void*)message, strlen( message ) + 1);
139    ENetPacket *packet = enet_packet_create( trans , sizeof( int ) + strlen( message ) + 1, reliable );
140    delete[] trans;
141    return packet;
142  }
143
144  /*### gamestate packet */
145  ENetPacket* PacketGenerator::gstate( GameStateCompressed* states, int reliable )
146  {
147    //std::cout << "packetgenerator" << std::endl;
148    //std::cout << "states->normsize " << states->normsize << std::endl;
149    //std::cout << "states->compsize " << states->compsize << std::endl;
150    int gid = GAMESTATE; //first assign the correct enet id
151    int totalLen = 5*sizeof( int ) + 2*sizeof(bool) + states->compsize; //calculate the total size of the datastream memory
152    //std::cout << "totalLen " << totalLen << std::endl;
153    //unsigned char *data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream
154    if(totalLen==0)
155      return NULL;
156    unsigned char *data = new unsigned char[totalLen];
157    memcpy( (void*)(data), (const void*)&gid, sizeof( int ) ); //this is the enet id
158    memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id
159    memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->compsize), sizeof(int));
160    memcpy( (void*)(data+3*sizeof(int)), (const void*)&(states->normsize), sizeof(int));
161    memcpy( (void*)(data+4*sizeof(int)), (const void*)&(states->base_id), sizeof(int));
162    memcpy( (void*)(data+5*sizeof(int)), (const void*)&(states->diffed), sizeof(bool));
163    //place the GameStateCompressed data at the end of the enet datastream
164    memcpy( (void*)(data+5*sizeof( int ) + sizeof(bool)), (const void*)&(states->complete), sizeof(bool) );
165    memcpy( (void*)(data+5*sizeof( int ) + 2*sizeof(bool)), (const void*)states->data, states->compsize );
166   
167    //create an enet packet with the generated bytestream
168    COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl;
169    ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
170    delete[] data;
171    if(!addCRC(packet))
172      COUT(3) << "could not add crc to gamestate packet" << std::endl;
173    return packet;
174  }
175
176  ENetPacket* PacketGenerator::clid( int classid, std::string classname, int reliable )
177  {
178    //unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1);
179    if(classname.length()==0)
180      return NULL;
181    unsigned char *data = new unsigned char[3*sizeof(int)+classname.length()+1];
182    std::cout << "PacketGenerator: classid: " << classid << ", name: " << classname << std::endl;
183    *(int *)data = CLASSID;
184    *((int *)data+1) = classname.length()+1;
185    *((int *)data+2) = classid;
186    memcpy( (void *)(data+3*sizeof(int)), classname.c_str(), classname.length()+1);
187    ENetPacket *packet = enet_packet_create( data , 3*sizeof(int)+classname.length()+1, reliable );
188    delete[] data;
189    return packet;
190  }
191 
192  ENetPacket* PacketGenerator::generateWelcome( int clientID,int shipID, bool allowed , int reliable ){
193    welcome *wc = new welcome;
194    wc->id = WELCOME;
195    wc->clientID = clientID;
196    wc->shipID = shipID;
197    wc->allowed = true;
198    ENetPacket *packet = enet_packet_create( wc, sizeof(welcome), reliable);
199    delete wc;
200    return packet;
201  }
202 
203  ENetPacket* PacketGenerator::generateConnectRequest( int reliable ){
204    connectRequest *con = new connectRequest;
205    con->id=CONNECT;
206    ENetPacket *packet = enet_packet_create( con, sizeof(connectRequest), reliable);
207    delete con;
208    return packet;
209  }
210 
211 
212  bool PacketGenerator::addCRC( ENetPacket *packet){
213    unsigned char *data = packet->data;
214    uint32_t crc32=calcCRC(data, packet->dataLength);
215    // 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){
218      memcpy(&packet->data[oldlength], &crc32, sizeof(uint32_t));
219      return true;
220    }else{
221      COUT(3) << "could not add crc to gamestate" << std::endl;
222      return false;
223    }
224  }
225
226}
Note: See TracBrowser for help on using the repository browser.