Leviathan  0.8.0.0
Leviathan game engine
Leviathan::GameSpecificPacketHandler Class Reference

Handles construction of all game specific packets. More...

#include <GameSpecificPacketHandler.h>

Public Member Functions

DLLEXPORT GameSpecificPacketHandler (NetworkInterface *usetoreport)
 
DLLEXPORT ~GameSpecificPacketHandler ()
 
DLLEXPORT void PassGameSpecificDataToPacket (GameSpecificPacketData *datatosend, sf::Packet &packet)
 
DLLEXPORT std::shared_ptr< GameSpecificPacketDataReadGameSpecificPacketFromPacket (bool responsepacket, sf::Packet &packet)
 
DLLEXPORT void RegisterNewTypeFactory (BaseGameSpecificPacketFactory *newdfactoryobject)
 Adds a new type that can be handled. More...
 

Static Public Member Functions

static DLLEXPORT GameSpecificPacketHandlerGet ()
 

Protected Member Functions

std::shared_ptr< BaseGameSpecificPacketFactory_FindFactoryForType (int typenumber, bool requesttype)
 
void _CheckVectorSorting ()
 

Protected Attributes

std::vector< std::shared_ptr< BaseGameSpecificPacketFactory > > AllPacketFactories
 Stores a list of custom type factories. More...
 
bool IsVectorSorted
 Marks whether or not the vector is indexed. More...
 

Static Protected Attributes

static GameSpecificPacketHandlerStaticaccess = NULL
 

Detailed Description

Handles construction of all game specific packets.

Definition at line 80 of file GameSpecificPacketHandler.h.

Constructor & Destructor Documentation

◆ GameSpecificPacketHandler()

DLLEXPORT Leviathan::GameSpecificPacketHandler::GameSpecificPacketHandler ( NetworkInterface usetoreport)

Definition at line 9 of file GameSpecificPacketHandler.cpp.

9  :
10  IsVectorSorted(false)
11 {
12  Staticaccess = this;
13 }
static GameSpecificPacketHandler * Staticaccess
bool IsVectorSorted
Marks whether or not the vector is indexed.

◆ ~GameSpecificPacketHandler()

DLLEXPORT Leviathan::GameSpecificPacketHandler::~GameSpecificPacketHandler ( )

Definition at line 15 of file GameSpecificPacketHandler.cpp.

15  {
16  Staticaccess = NULL;
17 }
static GameSpecificPacketHandler * Staticaccess

Member Function Documentation

◆ _CheckVectorSorting()

void Leviathan::GameSpecificPacketHandler::_CheckVectorSorting ( )
protected

Definition at line 148 of file GameSpecificPacketHandler.cpp.

148  {
149  // Return if already sorted //
150  if(IsVectorSorted)
151  return;
152 
153  // Sort the vector //
155 
156 
157  // Mark as sorted //
158  IsVectorSorted = true;
159 }
bool SharedPtrVecSortComparison(const std::shared_ptr< BaseGameSpecificPacketFactory > &first, const std::shared_ptr< BaseGameSpecificPacketFactory > &second)
std::vector< std::shared_ptr< BaseGameSpecificPacketFactory > > AllPacketFactories
Stores a list of custom type factories.
bool IsVectorSorted
Marks whether or not the vector is indexed.

◆ _FindFactoryForType()

shared_ptr< BaseGameSpecificPacketFactory > Leviathan::GameSpecificPacketHandler::_FindFactoryForType ( int  typenumber,
bool  requesttype 
)
protected

Definition at line 103 of file GameSpecificPacketHandler.cpp.

105 {
106  // We can do some optimization if it is sorted //
107  if(!IsVectorSorted){
108  // Just search the old fashion way //
109 
110  for(size_t i = 0; i < AllPacketFactories.size(); i++){
111  // Check does it match //
112  if(AllPacketFactories[i]->TypeIDNumber == typenumber &&
113  AllPacketFactories[i]->HandlesRequests == requesttype)
114 
115  return AllPacketFactories[i];
116  }
117 
118  // Nothing found //
119  return NULL;
120  }
121 
122  // The elements are sorted so that the smallest ID is first so search until the id is higher
123  // than the wanted one
124  for(size_t i = 0; i < AllPacketFactories.size(); i++){
125  // Store some data for checking after it doesn't match //
126  auto tmpobj = AllPacketFactories[i].get();
127  int currenttype = tmpobj->TypeIDNumber;
128 
129  // Check does it match //
130  if(currenttype == typenumber && tmpobj->HandlesRequests == requesttype)
131  return AllPacketFactories[i];
132 
133  // Check can we end now //
134  if(currenttype > typenumber)
135  return NULL;
136  }
137 
138  // Nothing found //
139  return NULL;
140 }
std::vector< std::shared_ptr< BaseGameSpecificPacketFactory > > AllPacketFactories
Stores a list of custom type factories.
bool IsVectorSorted
Marks whether or not the vector is indexed.

◆ Get()

DLLEXPORT GameSpecificPacketHandler * Leviathan::GameSpecificPacketHandler::Get ( )
static

Definition at line 19 of file GameSpecificPacketHandler.cpp.

19  {
20  return Staticaccess;
21 }
static GameSpecificPacketHandler * Staticaccess

◆ PassGameSpecificDataToPacket()

DLLEXPORT void Leviathan::GameSpecificPacketHandler::PassGameSpecificDataToPacket ( GameSpecificPacketData datatosend,
sf::Packet &  packet 
)

Definition at line 25 of file GameSpecificPacketHandler.cpp.

27 {
28  // Try to find a handler for this //
29  auto handlerobject = _FindFactoryForType(datatosend->TypeIDNumber, datatosend->IsRequest);
30 
31  // Now that there is a factory it's safe to start putting it together by adding the TypeID first //
32  packet << datatosend->TypeIDNumber << datatosend->IsRequest;
33 
34  // Now the actual data //
35 
36  // Factory already knows what it does so let it do it //
37  if(!handlerobject->SerializeToPacket(datatosend, packet)){
38 
39  throw InvalidArgument("invalid custom request packet for writing to packet");
40  }
41 
42 
43  // The packet should now be fine //
44 }
std::shared_ptr< BaseGameSpecificPacketFactory > _FindFactoryForType(int typenumber, bool requesttype)

◆ ReadGameSpecificPacketFromPacket()

DLLEXPORT std::shared_ptr< GameSpecificPacketData > Leviathan::GameSpecificPacketHandler::ReadGameSpecificPacketFromPacket ( bool  responsepacket,
sf::Packet &  packet 
)

Definition at line 46 of file GameSpecificPacketHandler.cpp.

48 {
49  // Get the basic data from the packet //
50  int typeidnumber = -1;
51  bool packetisrequest;
52 
53  if(!(packet >> typeidnumber)){
54 
55  throw InvalidAccess("invalid received custom packet base format");
56  }
57 
58  if(!(packet >> packetisrequest)){
59 
60  throw InvalidAccess("invalid received custom packet base format");
61  }
62 
63  if(packetisrequest != !responsepacket){
64 
65  // The packet might be corrupted or something //
66  Logger::Get()->Warning("GameSpecificPacketHandler: packet has inconsistent request identifier, expected "+
67  Convert::ToString(responsepacket)+" but packet was "+
68  Convert::ToString(packetisrequest));
69  return NULL;
70  }
71 
72  // Try to find a handler for this //
73  auto handlerobject = _FindFactoryForType(typeidnumber, packetisrequest);
74 
75  if(!handlerobject){
76 
77  Logger::Get()->Warning("GameSpecificPacketHandler: couldn't find a handler for a packet of type "+
78  Convert::ToString(typeidnumber)+" and"
79  " request state: "+Convert::ToString(packetisrequest));
80  return NULL;
81  }
82 
83  // Factory already knows what it does so let it do it //
84  return handlerobject->UnSerializeObjectFromPacket(packet);
85 }
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
std::shared_ptr< BaseGameSpecificPacketFactory > _FindFactoryForType(int typenumber, bool requesttype)

◆ RegisterNewTypeFactory()

DLLEXPORT void Leviathan::GameSpecificPacketHandler::RegisterNewTypeFactory ( BaseGameSpecificPacketFactory newdfactoryobject)

Adds a new type that can be handled.

Definition at line 87 of file GameSpecificPacketHandler.cpp.

89 {
90  // Check does it exist already //
91  if(_FindFactoryForType(newdfactoryobject->TypeIDNumber, newdfactoryobject->HandlesRequests)){
92 
93  assert(0 && "Trying to register a type that is already registered for custom packages");
94  }
95 
96  // Add //
97  AllPacketFactories.push_back(shared_ptr<BaseGameSpecificPacketFactory>(newdfactoryobject));
98 
99  // Mark as unsorted //
100  IsVectorSorted = false;
101 }
bool HandlesRequests
Specifies are the TypeIDNumbers corresponding to request or response packets.
int TypeIDNumber
The integer identifying when this factory needs to be used.
std::shared_ptr< BaseGameSpecificPacketFactory > _FindFactoryForType(int typenumber, bool requesttype)
std::vector< std::shared_ptr< BaseGameSpecificPacketFactory > > AllPacketFactories
Stores a list of custom type factories.
bool IsVectorSorted
Marks whether or not the vector is indexed.

Member Data Documentation

◆ AllPacketFactories

std::vector<std::shared_ptr<BaseGameSpecificPacketFactory> > Leviathan::GameSpecificPacketHandler::AllPacketFactories
protected

Stores a list of custom type factories.

Note
Map to easily map the ID to a handler

Definition at line 107 of file GameSpecificPacketHandler.h.

◆ IsVectorSorted

bool Leviathan::GameSpecificPacketHandler::IsVectorSorted
protected

Marks whether or not the vector is indexed.

Definition at line 110 of file GameSpecificPacketHandler.h.

◆ Staticaccess

GameSpecificPacketHandler * Leviathan::GameSpecificPacketHandler::Staticaccess = NULL
staticprotected

Definition at line 113 of file GameSpecificPacketHandler.h.


The documentation for this class was generated from the following files: