wsproxy/server/wsproxy/mastersvr.h
2020-01-09 18:51:58 +08:00

60 lines
1.4 KiB
C++

#pragma once
#include "framework/cpp/protoutils.h"
namespace a8
{
class TcpClient;
class AsyncTcpClient;
}
struct timer_list;
class MasterSvr
{
public:
int instance_id = 0;
std::string remote_ip;
int remote_port = 0;
a8::tick_t last_pong_tick = 0;
public:
void Init(int instance_id, const std::string& remote_ip, int remote_port);
void UnInit();
void Open();
void Close();
bool Connected();
template <typename T>
void SendMsg(T& msg)
{
static int msgid = f8::Net_GetMessageId(msg);
f8::Net_SendMsg(tcp_client_, 0, msgid, msg);
}
private:
#if ASYNC_TCPCLIENT
void on_error(a8::AsyncTcpClient* sender, int errorId);
void on_connect(a8::AsyncTcpClient* sender);
void on_disconnect(a8::AsyncTcpClient* sender);
void on_socketread(a8::AsyncTcpClient* sender, char* buf, unsigned int len);
#else
void on_error(a8::TcpClient* sender, int errorId);
void on_connect(a8::TcpClient* sender);
void on_disconnect(a8::TcpClient* sender);
void on_socketread(a8::TcpClient* sender, char* buf, unsigned int len);
#endif
void CheckAlive();
private:
char *recv_buff_ = nullptr;
unsigned int recv_bufflen_ = 0;
#if ASYNC_TCPCLIENT
a8::AsyncTcpClient* tcp_client_ = nullptr;
#else
a8::TcpClient* tcp_client_ = nullptr;
#endif
timer_list* timer_ = nullptr;
};