Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages | Examples

clientpacket.cpp

00001 /***************************************************************************
00002  * The contents of this file are subject to the Mozilla Public             *
00003  * License Version 1.1 (the "License"); you may not use this file          *
00004  * except in compliance with the License. You may obtain a copy of         *
00005  * the License at http://www.mozilla.org/MPL/                              *
00006  *                                                                         *
00007  * Software distributed under the License is distributed on an "AS         *
00008  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
00009  * implied. See the License for the specific language governing            *
00010  * rights and limitations under the License.                               *
00011  *                                                                         *
00012  * The Original Code is Game Network Framework (GaNeF).                    *
00013  *                                                                         *
00014  * The Initial Developers of the Original Code are                         *
00015  * Lars Langer and Emanuel Greisen                                         *
00016  * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
00017  * All Rights Reserved.                                                    *
00018  *                                                                         *
00019  * Contributor(s):                                                         *
00020  *   none yet....                                                          *
00021  *                                                                         *
00022  ***************************************************************************/
00023 #include "clientpacket.h"
00024 
00025 #include "clientdata.h"
00026 
00027 #include "../common/plugin.h"
00028 
00029 ClientPacket::ClientPacket(const ClientData * fdata, unsigned char type, unsigned char priority, bool reliable) :
00030    framework(fdata->getFramework()),
00031    FrameworkPacket(fdata->getFrameworkId(),type,priority,INITIAL_BODY_SIZE,reliable)
00032 {
00033    // Check type is not system r plugin
00034    if(type < 4)
00035       throw new PacketError("Type is below 4, user defined messages must be type: 4-255");
00036 }
00037 
00038 ClientPacket::ClientPacket(ClientFramework * f, unsigned int receiver, unsigned char type, unsigned char priority, unsigned int bodysize, bool reliable ) :
00039       framework(f),
00040       FrameworkPacket(receiver,type,priority,bodysize,reliable)
00041 {
00042 }
00043 
00044 ClientPacket::ClientPacket( char * data, unsigned int size) :
00045       FrameworkPacket(data,size)
00046 {
00047 }
00048 
00049 
00050 
00051 ClientPacket::~ClientPacket()
00052 {
00053 }
00054 
00055 ClientPacket * ClientPacket::createDummyPacket( )
00056 {
00057    return createSystemPacket(NULL, DUMMY_PACKET );
00058 }
00059 
00060 ClientPacket * ClientPacket::createSystemPacket(ClientFramework * f, FrameworkPacket::SpecialReceivers receiver )
00061 {
00062    ClientPacket * packet = new ClientPacket(f, receiver, 0);
00063    packet->header->reliable = 1;
00064    return packet;
00065 }
00066 
00067 ClientPacket * ClientPacket::createAckPacket( ClientPacket * packet )
00068 {
00069    // Create a packet of type 0(system), with receiver PACKET_ACK, the same priority, a bodysize of 5 and unreliable.
00070    ClientPacket * ack = new ClientPacket(NULL, PACKET_ACK, 0, packet->getPriority(), 5, false);
00071    ack->peer = packet->peer;
00072    ack->writeUInt32(packet->getSequenceNumber());
00073    ack->writeUInt8(packet->getPriority());
00074    return ack;
00075 }
00076 
00077 ClientPacket * ClientPacket::createPluginPacket( const Plugin<ClientPacket> * plugin, const ClientPacket * _packet  )
00078 {
00079    ClientPacket * packet = new ClientPacket(NULL, plugin->getUniqueId(),1,0,INITIAL_BODY_SIZE,true);
00080    packet->peer = _packet->peer;
00081    return packet;
00082 }
00083 
00084 ClientPacket * ClientPacket::readPacket( unsigned int psize )
00085 {
00086    before_read_check(psize);
00087    ClientPacket * pack = new ClientPacket(&data[pos], psize);
00088    pack->peer = peer;
00089    pos += psize;
00090    return pack;
00091 }
00092 
00093 
00094 void ClientPacket::makeSendReady( )
00095 {
00096    if(!packet_ready_to_transmit)
00097    {
00098       if(!header->reliable && !isAckPacket())
00099       {
00100          // Get the next sequence number
00101          header->packet_seq_number = framework->getNextUnreliablePacketSeqNumber(this);
00102       }
00103       header->receiver = htonl(header->receiver);
00104       header->packet_seq_number = htonl(header->packet_seq_number);
00105       packet_ready_to_transmit = true;
00106    }
00107 }

Generated on Mon Feb 6 12:24:50 2006 for Ganef by  doxygen 1.4.4