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