aozhiwei a181513c83 1
2020-09-24 11:51:10 +08:00

57 lines
1.3 KiB
C++

#pragma once
#include "framework/cpp/protoutils.h"
#include "framework/cpp/netmsghandler.h"
namespace a8
{
class TcpClient;
class AsyncTcpClient;
}
struct timer_list;
class IMConn
{
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(const T& msg)
{
static int msgid = f8::Net_GetMessageId(msg);
SendMsg(msgid, msg);
}
void SendMsg(int msgid, const ::google::protobuf::Message& msg)
{
f8::Net_SendMsg(tcp_client_, 0, msgid, msg);
#ifdef DEBUG
f8::DumpMsgToLog(msg, "<<<<<<<IMC ");
#endif
}
private:
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);
void CheckAlive();
private:
char *recv_buff_ = nullptr;
unsigned int recv_bufflen_ = 0;
a8::AsyncTcpClient* tcp_client_ = nullptr;
timer_list* timer_ = nullptr;
};