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