00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00042
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
00054
00055
00056
00057 template<class PacketType, class SocketType>
00058 class Framework
00059 {
00060 protected:
00061
00062 int listen_port;
00063
00064 SocketType socket;
00065
00066 PacketReceiver<PacketType, SocketType> * receiver;
00067
00068 Queue<PacketType> *inqueue;
00069
00070 Queue<PacketType> *outqueue;
00071
00072 PacketSender<PacketType, SocketType> * sender;
00073
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
00085 virtual void retransmitPacket(PacketType * packet);
00086
00087 void ackPacket(PacketType * p);
00088
00089 virtual void addPendingAck(PacketType * packet, bool haslock) = 0;
00090
00091 virtual PacketType * deliverPacket(PacketType * p) = 0;
00092
00093 virtual bool isPacketReadyForDelivery(PacketType * packet) = 0;
00094
00095 virtual void sentReliablePacket(PacketType * packet) = 0;
00096
00097 virtual void addPluginSequenceNumber( PacketType * packet) = 0;
00098
00099 public:
00100 static inline unsigned int currentTimeMillis()
00101 {
00102
00103
00104
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