Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 909 was 790, checked in by nicolasc, 17 years ago

merged FICN back into trunk
awaiting release.

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