add asynctcpclient mastersvr

This commit is contained in:
aozhiwei 2020-01-09 18:51:58 +08:00
parent 759b58b04c
commit 193aa76969
4 changed files with 40 additions and 2 deletions

View File

@ -6,6 +6,8 @@
#include "ss_msgid.pb.h" #include "ss_msgid.pb.h"
#include "mastersvr.h" #include "mastersvr.h"
#include <a8/tcpclient.h> #include <a8/tcpclient.h>
#include <a8/asynctcpclient.h>
#include <a8/ioloop.h>
#include <a8/udplog.h> #include <a8/udplog.h>
#include <a8/timer.h> #include <a8/timer.h>
#include "app.h" #include "app.h"
@ -21,7 +23,11 @@ void MasterSvr::Init(int instance_id, const std::string& remote_ip, int remote_p
recv_bufflen_ = 0; recv_bufflen_ = 0;
last_pong_tick = a8::XGetTickCount(); last_pong_tick = a8::XGetTickCount();
recv_buff_ = (char*) malloc(PACK_MAX * 2); recv_buff_ = (char*) malloc(PACK_MAX * 2);
#if ASYNC_TCPCLIENT
tcp_client_ = a8::IoLoop::Instance()->CreateAsyncTcpClient();
#else
tcp_client_ = new a8::TcpClient(); tcp_client_ = new a8::TcpClient();
#endif
tcp_client_->remote_address = remote_ip; tcp_client_->remote_address = remote_ip;
tcp_client_->remote_port = remote_port; tcp_client_->remote_port = remote_port;
tcp_client_->on_error = std::bind(&MasterSvr::on_error, this, std::placeholders::_1, std::placeholders::_2); tcp_client_->on_error = std::bind(&MasterSvr::on_error, this, std::placeholders::_1, std::placeholders::_2);
@ -42,7 +48,11 @@ void MasterSvr::UnInit()
a8::Timer::Instance()->DeleteTimer(timer_); a8::Timer::Instance()->DeleteTimer(timer_);
timer_ = nullptr; timer_ = nullptr;
tcp_client_->Close(); tcp_client_->Close();
#if ASYNC_TCPCLIENT
a8::IoLoop::Instance()->DestoryAsyncTcpClient(tcp_client_);
#else
delete tcp_client_; delete tcp_client_;
#endif
tcp_client_ = nullptr; tcp_client_ = nullptr;
recv_bufflen_ = 0; recv_bufflen_ = 0;
free(recv_buff_); free(recv_buff_);
@ -64,7 +74,11 @@ bool MasterSvr::Connected()
return tcp_client_->Connected(); return tcp_client_->Connected();
} }
#if ASYNC_TCPCLIENT
void MasterSvr::on_error(a8::AsyncTcpClient* sender, int errorId)
#else
void MasterSvr::on_error(a8::TcpClient* sender, int errorId) void MasterSvr::on_error(a8::TcpClient* sender, int errorId)
#endif
{ {
a8::UdpLog::Instance()->Error("MasterSvr errorid=%d remote_ip:%s remote_port:%d", a8::UdpLog::Instance()->Error("MasterSvr errorid=%d remote_ip:%s remote_port:%d",
{ {
@ -74,13 +88,21 @@ void MasterSvr::on_error(a8::TcpClient* sender, int errorId)
}); });
} }
#if ASYNC_TCPCLIENT
void MasterSvr::on_connect(a8::AsyncTcpClient* sender)
#else
void MasterSvr::on_connect(a8::TcpClient* sender) void MasterSvr::on_connect(a8::TcpClient* sender)
#endif
{ {
recv_bufflen_ = 0; recv_bufflen_ = 0;
a8::UdpLog::Instance()->Info("masterserver connected", {}); a8::UdpLog::Instance()->Info("masterserver connected", {});
} }
#if ASYNC_TCPCLIENT
void MasterSvr::on_disconnect(a8::AsyncTcpClient* sender)
#else
void MasterSvr::on_disconnect(a8::TcpClient* sender) void MasterSvr::on_disconnect(a8::TcpClient* sender)
#endif
{ {
recv_bufflen_ = 0; recv_bufflen_ = 0;
a8::UdpLog::Instance()->Info("masterserver %d disconnected after 10s later reconnect", {instance_id}); a8::UdpLog::Instance()->Info("masterserver %d disconnected after 10s later reconnect", {instance_id});
@ -90,7 +112,11 @@ void MasterSvr::on_disconnect(a8::TcpClient* sender)
); );
} }
#if ASYNC_TCPCLIENT
void MasterSvr::on_socketread(a8::AsyncTcpClient* sender, char* buf, unsigned int len)
#else
void MasterSvr::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len) void MasterSvr::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len)
#endif
{ {
#if 0 #if 0
++App::Instance()->perf.read_count; ++App::Instance()->perf.read_count;

View File

@ -5,6 +5,7 @@
namespace a8 namespace a8
{ {
class TcpClient; class TcpClient;
class AsyncTcpClient;
} }
struct timer_list; struct timer_list;
@ -32,16 +33,27 @@ class MasterSvr
} }
private: 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_error(a8::TcpClient* sender, int errorId);
void on_connect(a8::TcpClient* sender); void on_connect(a8::TcpClient* sender);
void on_disconnect(a8::TcpClient* sender); void on_disconnect(a8::TcpClient* sender);
void on_socketread(a8::TcpClient* sender, char* buf, unsigned int len); void on_socketread(a8::TcpClient* sender, char* buf, unsigned int len);
#endif
void CheckAlive(); void CheckAlive();
private: private:
char *recv_buff_ = nullptr; char *recv_buff_ = nullptr;
unsigned int recv_bufflen_ = 0; unsigned int recv_bufflen_ = 0;
#if ASYNC_TCPCLIENT
a8::AsyncTcpClient* tcp_client_ = nullptr;
#else
a8::TcpClient* tcp_client_ = nullptr; a8::TcpClient* tcp_client_ = nullptr;
#endif
timer_list* timer_ = nullptr; timer_list* timer_ = nullptr;
}; };

@ -1 +1 @@
Subproject commit f9f17a60fbef4174c2dd8ae406f742371b62421f Subproject commit 67358105a4e2301c48f351d02fde1c59ef8a4a3e

@ -1 +1 @@
Subproject commit 6210196bcda13489e2f691aa001de042ae61d9cb Subproject commit 9ba2696e52664c9f5b7a1e09a4a5516359d330b0