Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/PacketGenerator.cc @ 636

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

synchronising of classid↔classname works now

File size: 5.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Dumeni Manatschal, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/*
29 *Class generates packets that can be send by enet
30 * ->don't read this without the class PacketDecoder, since they belong together
31 *
32 * Autor: Dumeni Manatschal
33 *
34*/
35
36#include "PacketManager.h"
37#include <enet/enet.h>
38#include <iostream>
39#include <list>
40#include <string>
41#include <cstring>
42
43using namespace network;
44
45PacketGenerator::PacketGenerator() {}
46
47//following functions create a packet in form of bytestream
48
49ENetPacket* PacketGenerator::acknowledgement( int state, int reliable )
50{
51    std::cout << "generating new acknowledgement, id: " << state << std::endl;
52        ack* ackreq = new ack;
53        ackreq->id = ACK;
54        ackreq->a = state;
55
56        ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable );
57
58        return packet;
59}
60/*### mouseupdates */
61ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )
62{
63        std::cout << "generating new mouse" << std::endl;
64        mouse* mousemove = new mouse;
65        mousemove->id = MOUSE;
66        mousemove->x = x;
67        mousemove->y = y;
68
69        ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable );
70
71        return packet;
72}
73/*### keystrikes updates */
74ENetPacket* PacketGenerator::keystrike( char press, int reliable )
75{
76        std::cout << "generating new keyboard" << std::endl;
77        keyboard* key = new keyboard;
78        key->id = KEYBOARD;
79        key->press = press;
80
81        ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable );
82
83        return packet;
84}
85/*### chat messages packet */
86ENetPacket* PacketGenerator::chatMessage( const char* message, int reliable )
87{
88        int* trans = new int[sizeof(int) + strlen(message) + 1];
89        *trans = CHAT;
90        //be carefull here, don't forget to allocate the space before using it ;-)
91        memcpy( &trans[1], (const void*)message, strlen( message ) + 1);
92        ENetPacket *packet = enet_packet_create( trans , sizeof( int ) + strlen( message ) + 1, reliable );
93
94        return packet;
95}
96
97/*### gamestate packet */
98ENetPacket* PacketGenerator::gstate( GameStateCompressed* states, int reliable )
99{
100  //std::cout << "packetgenerator" << std::endl;
101  //std::cout << "states->normsize " << states->normsize << std::endl;
102  //std::cout << "states->compsize " << states->compsize << std::endl;
103  int gid = GAMESTATE; //first assign the correct enet id
104  int totalLen = 4*sizeof( int ) + sizeof(bool) + states->compsize; //calculate the total size of the datastream memory
105  //std::cout << "totalLen " << totalLen << std::endl;
106  unsigned char *data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream
107  memcpy( (void*)(data), (const void*)&gid, sizeof( int ) ); //this is the enet id
108  memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id
109  memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->compsize), sizeof(int));
110  memcpy( (void*)(data+3*sizeof(int)), (const void*)&(states->normsize), sizeof(int));
111  memcpy( (void*)(data+4*sizeof(int)), (const void*)&(states->diffed), sizeof(bool));
112  /*(int)*(data) = gid;
113  (int)*(data+sizeof(int)) = states->id;
114  //this is the compressed size of the GameStateCompressed data, place at 3th position of the enet datastream
115  (int)*(data+2*sizeof(int)) = states->compsize;
116  //this is the uncompressed size of GameStateCompressed data
117  (int)*(data+3*sizeof(int)) = states->normsize;
118  //since there is a new parameter inside GameStateCompressed, change this function to create packet
119  (bool)*(data+4*sizeof(int)) = states->diffed;*/
120  //place the GameStateCompressed data at the end of the enet datastream
121  memcpy( (void*)(data+4*sizeof( int ) + sizeof(bool)), (const void*)states->data, states->compsize );
122  //create an enet packet with the generated bytestream
123  ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
124  //delete data;
125  return packet;
126}
127
128ENetPacket* PacketGenerator::clid( int classid, std::string classname, int reliable ){
129        unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1);
130        std::cout << "classid: " << classid << ", name: " << classname << std::endl;
131        *(int *)data = CLASSID;
132        *((int *)data+1) = classname.length()+1;
133        *((int *)data+2) = classid;
134        memcpy( (void *)(data+3*sizeof(int)), classname.c_str(), classname.length()+1);
135        ENetPacket *packet = enet_packet_create( data , 3*sizeof(int)+classname.length()+1, reliable );
136        return packet;
137}
138
139
Note: See TracBrowser for help on using the repository browser.