2017-02-12 13:38:56 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#define IPC_MAX_RECONNECTS 3
|
|
|
|
#define IPC_COMMAND_SIZE 100
|
|
|
|
#define IPC_BUFFER_SIZE 0x2000
|
|
|
|
|
|
|
|
#define IPC_PIPE_NAME_SERVER "IW4x-Server"
|
|
|
|
#define IPC_PIPE_NAME_CLIENT "IW4x-Client"
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
class Pipe
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Packet
|
|
|
|
{
|
|
|
|
char command[IPC_COMMAND_SIZE];
|
|
|
|
char buffer[IPC_BUFFER_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
IPCTYPE_NONE,
|
|
|
|
IPCTYPE_SERVER,
|
|
|
|
IPCTYPE_CLIENT
|
|
|
|
};
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
typedef void(__cdecl PacketCallback)(const std::string& data);
|
2017-02-12 13:38:56 -05:00
|
|
|
typedef void(__cdecl Callback)();
|
|
|
|
|
|
|
|
Pipe();
|
|
|
|
~Pipe();
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
bool connect(const std::string& name);
|
|
|
|
bool create(const std::string& name);
|
2017-02-12 13:38:56 -05:00
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
bool write(const std::string& command, const std::string& data);
|
|
|
|
void setCallback(const std::string& command, Utils::Slot<PacketCallback> callback);
|
2017-02-12 13:38:56 -05:00
|
|
|
void onConnect(Callback callback);
|
|
|
|
|
|
|
|
void destroy();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Utils::Slot<void()> connectCallback;
|
|
|
|
std::map<std::string, Utils::Slot<PacketCallback>> packetCallbacks;
|
|
|
|
|
|
|
|
HANDLE pipe;
|
|
|
|
std::thread thread;
|
|
|
|
bool threadAttached;
|
|
|
|
|
|
|
|
Type type;
|
|
|
|
Packet packet;
|
|
|
|
|
|
|
|
char pipeName[MAX_PATH];
|
|
|
|
char pipeFile[MAX_PATH];
|
|
|
|
unsigned int reconnectAttempt;
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
void setName(const std::string& name);
|
2017-02-12 13:38:56 -05:00
|
|
|
|
|
|
|
static void ReceiveThread(Pipe* pipe);
|
|
|
|
};
|
|
|
|
|
|
|
|
class IPCPipe : public Component
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IPCPipe();
|
|
|
|
|
|
|
|
void preDestroy() override;
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
static bool Write(const std::string& command, const std::string& data);
|
|
|
|
static void On(const std::string& command, Utils::Slot<Pipe::PacketCallback> callback);
|
2017-02-12 13:38:56 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
static Pipe ServerPipe;
|
|
|
|
static Pipe ClientPipe;
|
|
|
|
|
|
|
|
static void ConnectClient();
|
|
|
|
};
|
|
|
|
}
|