Skip to content

Commit

Permalink
OpcodeMgr class, will be used to register opcode handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
DDuarte committed Jul 5, 2012
1 parent 01cb5d4 commit 04734ab
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 44 deletions.
22 changes: 0 additions & 22 deletions src/LeEngine/Defines.h
Expand Up @@ -6,8 +6,6 @@
#include <boost/integer.hpp>
#include <boost/lexical_cast.hpp>

#define INVALID_OGL_VALUE 0xFFFFFFFF

// Helper typedefs

typedef boost::int8_t int8;
Expand All @@ -19,26 +17,6 @@ typedef boost::uint16_t uint16;
typedef boost::uint32_t uint32;
typedef boost::uint64_t uint64;

template<typename T>
class Vector2
{
public:
Vector2() : X(static_cast<T>(0)), Y(static_cast<T>(0)) { }
Vector2(T x, T y) : X(x), Y(y) { }
T X, Y;
};

template<typename T>
class Vector3
{
public:
Vector3() : X(static_cast<T>(0)), Y(static_cast<T>(0)), Z(static_cast<T>(0)) { }
Vector3(T x, T y, T z) : X(x), Y(y), Z(z) { }
T X, Y, Z;
};

typedef Vector2<uint32> Size2;

template<typename T>
inline void SAFE_DELETE(T* p)
{
Expand Down
3 changes: 2 additions & 1 deletion src/LeEngine/LeEngine.vcxproj
Expand Up @@ -25,7 +25,7 @@
<ClInclude Include="Log.h" />
<ClInclude Include="DMath.h" />
<ClInclude Include="Matrix.h" />
<ClInclude Include="Opcode.h" />
<ClInclude Include="OpcodeMgr.h" />
<ClInclude Include="Packet.h" />
<ClInclude Include="Plane.h" />
<ClInclude Include="Quaternion.h" />
Expand All @@ -52,6 +52,7 @@
<ClCompile Include="Kernel.cpp" />
<ClCompile Include="Line.cpp" />
<ClCompile Include="Log.cpp" />
<ClCompile Include="OpcodeMgr.cpp" />
<ClCompile Include="Plane.cpp" />
<ClCompile Include="Random.cpp" />
<ClCompile Include="Scene.cpp" />
Expand Down
5 changes: 4 additions & 1 deletion src/LeEngine/LeEngine.vcxproj.filters
Expand Up @@ -93,7 +93,7 @@
<ClInclude Include="Packet.h">
<Filter>Networking</Filter>
</ClInclude>
<ClInclude Include="Opcode.h">
<ClInclude Include="OpcodeMgr.h">
<Filter>Networking</Filter>
</ClInclude>
</ItemGroup>
Expand Down Expand Up @@ -147,6 +147,9 @@
<ClCompile Include="ByteBuffer.cpp">
<Filter>Networking</Filter>
</ClCompile>
<ClCompile Include="OpcodeMgr.cpp">
<Filter>Networking</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Logging">
Expand Down
16 changes: 0 additions & 16 deletions src/LeEngine/Opcode.h

This file was deleted.

38 changes: 38 additions & 0 deletions src/LeEngine/OpcodeMgr.cpp
@@ -0,0 +1,38 @@
#include "OpcodeMgr.h"
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include "Defines.h"
#include "Log.h"
#include "Packet.h"

void OpcodeMgr::AddHandler(uint16 opcode, OpcHandler handler)
{
#ifdef _DEBUG
std::map<uint16, OpcHandler>::iterator itr = _handlers.find(opcode);
if (itr != _handlers.end())
LeLog.WriteP(LOG_APP, "Handler for opcode %u already registered.", opcode);
#endif

_handlers[opcode] = handler;
}

void OpcodeMgr::BindAndCall(Packet* packet)
{
OpcHandler* handler = GetHandler(packet->GetOpcode());
if (!handler)
{
LeLog.WriteP(LOG_APP, "Handler for opcode %u not found.", packet->GetOpcode());
return;
}

boost::bind(*handler, packet)();
}

OpcHandler* OpcodeMgr::GetHandler(uint16 opcode)
{
std::map<uint16, OpcHandler>::iterator itr = _handlers.find(opcode);
if (itr == _handlers.end())
return NULL;

return &_handlers[opcode];
}
30 changes: 30 additions & 0 deletions src/LeEngine/OpcodeMgr.h
@@ -0,0 +1,30 @@
#ifndef OPCODEMGR_H
#define OPCODEMGR_H

#include <map>
#include <boost/function.hpp>
#include "Defines.h"

class Packet;

enum Direction
{
ClientToServer = 0, // CMSG
ServerToClient = 1, // SMSG
};

typedef boost::function<void (Packet* packet)> OpcHandler;

class OpcodeMgr
{
public:
void AddHandler(uint16 opcode, OpcHandler handler);

void BindAndCall(Packet* packet);

private:
std::map<uint16, OpcHandler> _handlers;
OpcHandler* GetHandler(uint16 opcode);
};

#endif // OPCODEMGR_H
7 changes: 3 additions & 4 deletions src/LeEngine/Packet.h
Expand Up @@ -2,7 +2,6 @@
#define PACKET_H

#include "ByteBuffer.h"
#include "Opcode.h"
#include "Defines.h"

class Packet : public ByteBuffer
Expand All @@ -15,11 +14,11 @@ class Packet : public ByteBuffer
// Byte* _data;

public:
Packet(Opcode opcode, uint32 reserveSize) : ByteBuffer(reserveSize), _opcode((uint16)opcode) {}
Packet(uint16 opcode, uint32 reserveSize) : ByteBuffer(reserveSize), _opcode(opcode) {}
Packet(const Packet& other) : ByteBuffer(other), _opcode(other._opcode) {}

Opcode GetOpcode() const { _return (Opcode)_opcode; }
void SetOpcode(Opcode opcode) { _opcode = opcode; }
uint16 GetOpcode() const { return _opcode; }
void SetOpcode(uint16 opcode) { _opcode = opcode; }

uint32 GetDataSize() const { return _buffer.size(); }

Expand Down

0 comments on commit 04734ab

Please sign in to comment.