Pol  Revision:3cfda13
packethelper.h
Go to the documentation of this file.
1 #ifndef __PACKETHELPER_H
2 #define __PACKETHELPER_H
3 
4 #include "../globals/network.h"
5 #include "client.h"
6 #include "clienttransmit.h"
7 #include "packets.h"
8 
9 namespace Pol
10 {
11 namespace Network
12 {
13 namespace PktHelper
14 {
15 using namespace PacketWriterDefs;
16 // creates new packets
17 PacketInterface* GetPacket( u8 id, u16 sub = 0 );
18 
19 template <class T>
20 inline T* RequestPacket( u8 id, u16 sub = 0 )
21 {
22  return static_cast<T*>( Core::networkManager.packetsSingleton->getPacket( id, sub ) );
23 };
24 
25 inline void ReAddPacket( PacketInterface* msg )
26 {
27  Core::networkManager.packetsSingleton->ReAddPacket( msg );
28 };
29 
30 template <class T>
31 class PacketOut
32 {
33 private:
34  T* pkt;
35 
36 public:
37  PacketOut();
38  ~PacketOut();
39  void Release();
40  void Send( Client* client, int len = -1 ) const;
41  // be really really careful with this function
42  // needs PolLock
43  void SendDirect( Client* client, int len = -1 ) const;
44  T* operator->(void)const;
45  T* Get();
46 };
47 
48 template <class T>
50 {
51  pkt = RequestPacket<T>( T::ID, T::SUB );
52 }
53 
54 template <class T>
56 {
57  if ( pkt != 0 )
58  ReAddPacket( pkt );
59 }
60 
61 template <class T>
63 {
64  ReAddPacket( pkt );
65  pkt = 0;
66 }
67 
68 template <class T>
69 void PacketOut<T>::Send( Client* client, int len ) const
70 {
71  if ( pkt == 0 )
72  return;
73  if ( len == -1 )
74  len = pkt->offset;
75  Core::networkManager.clientTransmit->AddToQueue( client, &pkt->buffer, len );
76 }
77 
78 template <class T>
79 void PacketOut<T>::SendDirect( Client* client, int len ) const
80 {
81  if ( pkt == 0 )
82  return;
83  if ( len == -1 )
84  len = pkt->offset;
85  client->transmit( &pkt->buffer, len );
86 }
87 
88 template <class T>
90 {
91  return pkt;
92 }
93 
94 template <class T>
96 {
97  return pkt;
98 }
99 }
100 }
101 }
102 #endif
unsigned char u8
Definition: rawtypes.h:25
std::unique_ptr< Network::PacketsSingleton > packetsSingleton
Definition: network.h:95
T * RequestPacket(u8 id, u16 sub=0)
Definition: packethelper.h:20
void SendDirect(Client *client, int len=-1) const
Definition: packethelper.h:79
void transmit(const void *data, int len, bool needslock=false)
Definition: clientio.cpp:189
unsigned short u16
Definition: rawtypes.h:26
std::unique_ptr< Network::ClientTransmit > clientTransmit
Definition: network.h:97
NetworkManager networkManager
Definition: network.cpp:28
void Send(Client *client, int len=-1) const
Definition: packethelper.h:69
void ReAddPacket(PacketInterface *msg)
Definition: packethelper.h:25
unsigned char buffer[MAXBUFFER]
Definition: client.h:208
PacketInterface * GetPacket(u8 id, u16 sub=0)
Definition: packets.cpp:302
Definition: berror.cpp:12