Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1056 was 1056, checked in by landauf, 16 years ago

don't panic, no codechanges!
added a link to www.orxonox.net

File size: 5.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 <iostream>
38#include <list>
39#include <string>
40#include <cstring>
41
42#include "PacketTypes.h"
43#include "PacketManager.h"
44
45namespace network
46{
47  PacketGenerator::PacketGenerator() { }
48
49  //following functions create a packet in form of bytestream
50
51  ENetPacket* PacketGenerator::acknowledgement( int state, int reliable )
52  {
53    COUT(4) << "generating new acknowledgement, id: " << state << std::endl;
54    ack* ackreq = new ack;
55    ackreq->id = ACK;
56    ackreq->a = state;
57
58    ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable );
59
60    return packet;
61  }
62
63  /*### mouseupdates */
64  ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )
65  {
66    COUT(4) << "generating new mouse" << std::endl;
67    mouse* mousemove = new mouse;
68    mousemove->id = MOUSE;
69    mousemove->x = x;
70    mousemove->y = y;
71
72    ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable );
73
74    return packet;
75  }
76
77  /*### keystrikes updates */
78  ENetPacket* PacketGenerator::keystrike( char press, int reliable )
79  {
80    COUT(4) << "generating new keyboard" << std::endl;
81    keyboard* key = new keyboard;
82    key->id = KEYBOARD;
83    key->press = press;
84
85    ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable );
86
87    return packet;
88  }
89
90  /*### chat messages packet */
91  ENetPacket* PacketGenerator::chatMessage( const char* message, int reliable )
92  {
93    int* trans = new int[sizeof(int) + strlen(message) + 1];
94    *trans = CHAT;
95    //be carefull here, don't forget to allocate the space before using it ;-)
96    memcpy( &trans[1], (const void*)message, strlen( message ) + 1);
97    ENetPacket *packet = enet_packet_create( trans , sizeof( int ) + strlen( message ) + 1, reliable );
98
99    return packet;
100  }
101
102  /*### gamestate packet */
103  ENetPacket* PacketGenerator::gstate( GameStateCompressed* states, int reliable )
104  {
105    //std::cout << "packetgenerator" << std::endl;
106    //std::cout << "states->normsize " << states->normsize << std::endl;
107    //std::cout << "states->compsize " << states->compsize << std::endl;
108    int gid = GAMESTATE; //first assign the correct enet id
109    int totalLen = 4*sizeof( int ) + sizeof(bool) + states->compsize; //calculate the total size of the datastream memory
110    //std::cout << "totalLen " << totalLen << std::endl;
111    unsigned char *data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream
112    memcpy( (void*)(data), (const void*)&gid, sizeof( int ) ); //this is the enet id
113    memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id
114    memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->compsize), sizeof(int));
115    memcpy( (void*)(data+3*sizeof(int)), (const void*)&(states->normsize), sizeof(int));
116    memcpy( (void*)(data+4*sizeof(int)), (const void*)&(states->diffed), sizeof(bool));
117    /*(int)*(data) = gid;
118    (int)*(data+sizeof(int)) = states->id;
119    //this is the compressed size of the GameStateCompressed data, place at 3th position of the enet datastream
120    (int)*(data+2*sizeof(int)) = states->compsize;
121    //this is the uncompressed size of GameStateCompressed data
122    (int)*(data+3*sizeof(int)) = states->normsize;
123    //since there is a new parameter inside GameStateCompressed, change this function to create packet
124    (bool)*(data+4*sizeof(int)) = states->diffed;*/
125    //place the GameStateCompressed data at the end of the enet datastream
126    memcpy( (void*)(data+4*sizeof( int ) + sizeof(bool)), (const void*)states->data, states->compsize );
127    //create an enet packet with the generated bytestream
128    ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
129    //delete data;
130    return packet;
131  }
132
133  ENetPacket* PacketGenerator::clid( int classid, std::string classname, int reliable )
134  {
135    unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1);
136    std::cout << "classid: " << classid << ", name: " << classname << std::endl;
137    *(int *)data = CLASSID;
138    *((int *)data+1) = classname.length()+1;
139    *((int *)data+2) = classid;
140    memcpy( (void *)(data+3*sizeof(int)), classname.c_str(), classname.length()+1);
141    ENetPacket *packet = enet_packet_create( data , 3*sizeof(int)+classname.length()+1, reliable );
142    return packet;
143  }
144
145}
Note: See TracBrowser for help on using the repository browser.