Danbias/Code/Network/NetworkAPI/NetworkCallbackHelper.h

80 lines
2.5 KiB
C
Raw Normal View History

#ifndef NETWORK_API_NETWORK_CALLBACK_HELPER_H
#define NETWORK_API_NETWORK_CALLBACK_HELPER_H
/////////////////////////////////////
// Created by Dennis Andersen 2013 //
/////////////////////////////////////
2013-12-12 10:36:55 +01:00
#include <memory>
2014-01-07 10:26:09 +01:00
namespace Oyster
{
namespace Network
{
enum NetworkProtocolCallbackType
{
NetworkProtocolCallbackType_Function,
NetworkProtocolCallbackType_Object,
NetworkProtocolCallbackType_Unknown = -1,
};
enum NetworkClientCallbackType
{
NetworkClientCallbackType_Function,
NetworkClientCallbackType_Object,
NetworkClientCallbackType_Unknown = -1,
};
class NetworkClient;
class CustomNetProtocol;
2014-01-07 10:26:09 +01:00
template<typename Param>
struct NetRecieverObject
{
2014-01-07 10:26:09 +01:00
virtual void NetworkCallback(Param) = 0;
};
2014-01-07 10:26:09 +01:00
struct NetClientEvent :public NetRecieverObject<CustomNetProtocol&>
{
2014-01-07 10:26:09 +01:00
virtual void NetworkCallback(CustomNetProtocol& protocol) = 0;
virtual void Disconnected() { };
};
2014-01-07 10:26:09 +01:00
typedef NetRecieverObject<NetworkClient*> ClientConnectedObject ;
typedef NetClientEvent ProtocolRecieverObject;
2014-01-07 10:26:09 +01:00
template <typename Param>
struct NetCallbackFunction
{
typedef void (*FNC)(Param);
};
typedef NetCallbackFunction<NetworkClient*>::FNC ClientConnectCallbackMethod;
typedef NetCallbackFunction<CustomNetProtocol&>::FNC ProtocolRecFunction;
struct NetClientMethods
{
typedef void(*Dissconnected)(void);
ProtocolRecFunction recieved;
Dissconnected dissconnected;
void operator()(CustomNetProtocol& obj) { if(recieved) recieved(obj); }
void operator()() { if(dissconnected) dissconnected(); }
};
typedef NetClientMethods ProtocolRecieverFunction;
union RecieverObject
{
2013-12-12 10:36:55 +01:00
ClientConnectCallbackMethod clientConnectFnc; // !< A function pointer for sending or recieving NetworkClient
ProtocolRecieverFunction protocolRecieverFnc; // !< A function pointer for sending or recieving CustomNetProtocol
2014-01-07 10:26:09 +01:00
2013-12-12 10:36:55 +01:00
ClientConnectedObject *clientConnectObject; // !< An object for sending or recieving NetworkClient
2014-01-07 10:26:09 +01:00
ProtocolRecieverObject *protocolRecievedObject; // !< An object with collected client events methods.
2013-12-12 10:36:55 +01:00
RecieverObject() { memset(this, 0, sizeof(RecieverObject)); }
RecieverObject(ClientConnectCallbackMethod o) { clientConnectFnc = o; }
RecieverObject(ProtocolRecieverFunction o) { protocolRecieverFnc = o; }
RecieverObject(ClientConnectedObject* o) { clientConnectObject = o; }
RecieverObject(ProtocolRecieverObject* o) { protocolRecievedObject = o; }
};
}
}
#endif