Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/PacketGenerator.cc @ 1298

Last change on this file since 1298 was 1293, checked in by scheusso, 17 years ago

merged changes from input & camera & network branch into trunk

File size: 6.8 KB
RevLine 
[1168]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 */
[1180]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  PacketGenerator::PacketGenerator() { }
49
50  //following functions create a packet in form of bytestream
51
52  ENetPacket* PacketGenerator::acknowledgement( int state, int reliable )
53  {
54    COUT(4) << "PacketGenerator: generating new acknowledgement, id: " << state << std::endl;
55    ack* ackreq = new ack;
56    ackreq->id = ACK;
57    ackreq->a = state;
58
59    ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable );
[1248]60    delete ackreq;
[1180]61    return packet;
62  }
[1232]63 
64  ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE )
65  {
66    void *stream = new char[dataLength + 2*sizeof(int)];
67    if(!stream)
68      return NULL;
69    packet_id a = COMMAND;
70    memcpy(stream, (void*)&a, sizeof(int));
71    memcpy((unsigned char *)stream+sizeof(int), (void*)&dataLength, sizeof(int));
72    memcpy((unsigned char *)stream+2*sizeof(int), data, dataLength);
[1248]73    ENetPacket *packet = enet_packet_create(stream, dataLength+2*sizeof(int), reliable);
74    delete[] stream; // TODO: we could also tell enet not to copy the data, but to use the exisiting memory
75    return packet;
[1232]76  }
[1180]77
78  /*### mouseupdates */
79  ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )
80  {
81    COUT(4) << "PacketGenerator: generating new mouse" << std::endl;
82    mouse* mousemove = new mouse;
83    mousemove->id = MOUSE;
84    mousemove->x = x;
85    mousemove->y = y;
86
87    ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable );
[1248]88    delete mousemove;
[1180]89    return packet;
90  }
91
92  /*### keystrikes updates */
93  ENetPacket* PacketGenerator::keystrike( char press, int reliable )
94  {
95    COUT(4) << "PacketGenerator: generating new keyboard" << std::endl;
96    keyboard* key = new keyboard;
97    key->id = KEYBOARD;
98    key->press = press;
99
100    ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable );
[1248]101    delete key;
[1180]102    return packet;
103  }
104
105  /*### chat messages packet */
106  ENetPacket* PacketGenerator::chatMessage( const char* message, int reliable )
107  {
108    int* trans = new int[sizeof(int) + strlen(message) + 1];
109    *trans = CHAT;
110    //be carefull here, don't forget to allocate the space before using it ;-)
111    memcpy( &trans[1], (const void*)message, strlen( message ) + 1);
112    ENetPacket *packet = enet_packet_create( trans , sizeof( int ) + strlen( message ) + 1, reliable );
[1248]113    delete[] trans;
[1180]114    return packet;
115  }
116
117  /*### gamestate packet */
118  ENetPacket* PacketGenerator::gstate( GameStateCompressed* states, int reliable )
119  {
120    //std::cout << "packetgenerator" << std::endl;
121    //std::cout << "states->normsize " << states->normsize << std::endl;
122    //std::cout << "states->compsize " << states->compsize << std::endl;
123    int gid = GAMESTATE; //first assign the correct enet id
[1232]124    int totalLen = 5*sizeof( int ) + 2*sizeof(bool) + states->compsize; //calculate the total size of the datastream memory
[1180]125    //std::cout << "totalLen " << totalLen << std::endl;
[1232]126    //unsigned char *data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream
127    unsigned char *data = new unsigned char[totalLen];
[1180]128    memcpy( (void*)(data), (const void*)&gid, sizeof( int ) ); //this is the enet id
129    memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id
130    memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->compsize), sizeof(int));
131    memcpy( (void*)(data+3*sizeof(int)), (const void*)&(states->normsize), sizeof(int));
132    memcpy( (void*)(data+4*sizeof(int)), (const void*)&(states->base_id), sizeof(int));
133    memcpy( (void*)(data+5*sizeof(int)), (const void*)&(states->diffed), sizeof(bool));
134    //place the GameStateCompressed data at the end of the enet datastream
[1232]135    memcpy( (void*)(data+5*sizeof( int ) + sizeof(bool)), (const void*)&(states->complete), sizeof(bool) );
136    memcpy( (void*)(data+5*sizeof( int ) + 2*sizeof(bool)), (const void*)states->data, states->compsize );
[1180]137    //create an enet packet with the generated bytestream
138    COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl;
139    ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
[1248]140    delete[] data;
[1180]141    return packet;
142  }
143
144  ENetPacket* PacketGenerator::clid( int classid, std::string classname, int reliable )
145  {
[1232]146    //unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1);
147    unsigned char *data = new unsigned char[3*sizeof(int)+classname.length()+1];
[1180]148    std::cout << "PacketGenerator: classid: " << classid << ", name: " << classname << std::endl;
149    *(int *)data = CLASSID;
150    *((int *)data+1) = classname.length()+1;
151    *((int *)data+2) = classid;
152    memcpy( (void *)(data+3*sizeof(int)), classname.c_str(), classname.length()+1);
153    ENetPacket *packet = enet_packet_create( data , 3*sizeof(int)+classname.length()+1, reliable );
[1248]154    delete[] data;
[1180]155    return packet;
156  }
[1232]157 
158  ENetPacket* PacketGenerator::generateWelcome( int clientID,int shipID, bool allowed , int reliable ){
159    welcome *wc = new welcome;
160    wc->id = WELCOME;
161    wc->clientID = clientID;
162    wc->shipID = shipID;
163    wc->allowed = true;
164    ENetPacket *packet = enet_packet_create( wc, sizeof(welcome), reliable);
[1248]165    delete wc;
[1232]166    return packet;
167  }
168 
169  ENetPacket* PacketGenerator::generateConnectRequest( int reliable ){
170    connectRequest *con = new connectRequest;
171    con->id=CONNECT;
[1248]172    ENetPacket *packet = enet_packet_create( con, sizeof(connectRequest), reliable);
173    delete con;
174    return packet;
[1232]175  }
[1180]176
177}
Note: See TracBrowser for help on using the repository browser.