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

framework.h

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 #ifndef FRAMEWORK_H
00024 #define FRAMEWORK_H
00025 
00026 #include <string>
00027 #include <ptime.h>
00028 #include <pinet.h>
00029 
00030 #include "pendingacklist.h"
00031 #include "queue.h"
00032 
00033 template<class PacketType, class SocketType>
00034 class PacketSender;
00035 template<class PacketType, class SocketType>
00036 class PacketReceiver;
00037 template<class PacketType>
00038 class Plugin;
00039 
00040 /**
00041  * @ingroup Common
00042  * @brief This struct is used when throwing exceptions.
00043  */
00044 struct FrameworkError
00045 {
00046    const std::string msg;
00047    FrameworkError(const std::string &msg) : msg(msg){};
00048 };
00049 
00050 class FrameworkData;
00051 
00052 /**
00053  * @ingroup Common
00054  * @brief The general interface for a framework.
00055  * @author Lars Langer and Emanuel Greisen
00056 */
00057 template<class PacketType, class SocketType>
00058 class Framework
00059 {
00060 protected:
00061    /// The port the framework listens on.
00062    int listen_port;
00063    /// The socket used for sending/receiving.
00064    SocketType socket;
00065    /// The receiving object, enqueues incomming packets.
00066    PacketReceiver<PacketType, SocketType> * receiver;
00067    /// All the incomming unprocessessed packets.
00068    Queue<PacketType> *inqueue;
00069    /// The queue of packets on their way out of the server.
00070    Queue<PacketType> *outqueue;
00071    /// The Sending object, takes/waits from the outqueue.
00072    PacketSender<PacketType, SocketType> * sender;
00073    /// A list of pending acknowledgements.
00074    PendingAckList<PacketType, SocketType> * pending_acks;
00075 
00076 public:
00077    Framework(const int listen_port);
00078    virtual ~Framework();
00079 
00080 protected:
00081    unsigned int handleAckPacket(PacketType * packet, unsigned int userid);
00082 
00083 public:
00084    /// Will be called by the pendingacklist when ever we want to retransmit a packet
00085    virtual void retransmitPacket(PacketType * packet);
00086    /// This should send an AckPacket based on the packet.
00087    void ackPacket(PacketType * p);
00088    /// Called when we want to insert a pending ack into the pendingacklist.
00089    virtual void addPendingAck(PacketType * packet, bool haslock) = 0;
00090    /// This method should deliver the packet and return any buffered packets, waiting for this packet.
00091    virtual PacketType * deliverPacket(PacketType * p) = 0;
00092    /// This method should test if a packet is in order, and if not buffer the packet for later delivery.
00093    virtual bool isPacketReadyForDelivery(PacketType * packet) = 0;
00094    /// This method will be called by the sender when ever it successfully sends a reliable packet to the socket.
00095    virtual void sentReliablePacket(PacketType * packet) = 0;
00096    /// This method should add a sequence number to the pluginpacket if it does not already have one.
00097    virtual void addPluginSequenceNumber( PacketType * packet) = 0;
00098 
00099 public:
00100    static inline unsigned int currentTimeMillis()
00101    {
00102       //struct timeval cur;
00103       //gettimeofday(&cur, 0);
00104       //return cur.tv_sec * 1000 + cur.tv_usec / 1000;
00105       return msecs(now());
00106    };
00107 
00108 };
00109 
00110 
00111 template<class PacketType, class SocketType>
00112 Framework<PacketType, SocketType>::Framework( const int listen_port )
00113    : listen_port(listen_port),
00114       socket(ipaddress(0,0,0,0), listen_port)
00115 {
00116    pending_acks = new PendingAckList<PacketType, SocketType>(this);
00117    receiver = new PacketReceiver<PacketType, SocketType>(&socket, inqueue, this);
00118    inqueue = new Queue<PacketType>();
00119    outqueue = new Queue<PacketType>();
00120    sender = new PacketSender<PacketType, SocketType>(&socket, outqueue, *pending_acks, this);
00121 }
00122 
00123 
00124 template<class PacketType, class SocketType>
00125 Framework<PacketType, SocketType>::~Framework()
00126 {
00127    delete pending_acks;
00128    delete receiver;
00129    delete inqueue;
00130    delete outqueue;
00131    delete sender;
00132 }
00133 
00134 template<class PacketType, class SocketType>
00135 unsigned int Framework<PacketType, SocketType>::handleAckPacket( PacketType * packet, unsigned int userid)
00136 {
00137    unsigned int obj_seq = packet->readUInt32();
00138    unsigned char obj_pri = packet->readUInt8();
00139    return pending_acks->registerAck( obj_seq, obj_pri, userid );
00140 }
00141 
00142 template<class PacketType, class SocketType>
00143 void Framework<PacketType, SocketType>::retransmitPacket( PacketType * packet )
00144 {
00145    outqueue->insert(packet);
00146 }
00147 
00148 template<class PacketType, class SocketType>
00149 void Framework<PacketType, SocketType>::ackPacket( PacketType * p )
00150 {
00151    PacketType * _p = PacketType::createAckPacket(p);
00152    outqueue->insert(_p);
00153 }
00154 
00155 #endif

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