From 759b58b04c797ebe0ac914d99741e7dff063183f Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 9 Jan 2020 18:45:20 +0800 Subject: [PATCH] add asynctcpclient --- server/wsproxy/target_conn.cc | 26 ++++++++++++++++++++++++++ server/wsproxy/target_conn.h | 12 ++++++++++++ 2 files changed, 38 insertions(+) diff --git a/server/wsproxy/target_conn.cc b/server/wsproxy/target_conn.cc index 2c73435..ec8e407 100644 --- a/server/wsproxy/target_conn.cc +++ b/server/wsproxy/target_conn.cc @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include "app.h" const int PACK_MAX = 1024 * 64 * 2; @@ -24,7 +26,11 @@ void TargetConn::Init(int instance_id, const std::string& remote_ip, int remote_ recv_bufflen_ = 0; last_pong_tick = a8::XGetTickCount(); recv_buff_ = (char*) malloc(PACK_MAX * 2); +#if ASYNC_TCPCLIENT && GAME_ID == 2002 + tcp_client_ = a8::IoLoop::Instance()->CreateAsyncTcpClient(); +#else tcp_client_ = new a8::TcpClient(); +#endif tcp_client_->remote_address = remote_ip; tcp_client_->remote_port = remote_port; tcp_client_->on_error = std::bind(&TargetConn::on_error, this, std::placeholders::_1, std::placeholders::_2); @@ -56,7 +62,11 @@ void TargetConn::UnInit() a8::Timer::Instance()->DeleteTimer(timer_); timer_ = nullptr; tcp_client_->Close(); +#if ASYNC_TCPCLIENT && GAME_ID == 2002 + a8::IoLoop::Instance()->DestoryAsyncTcpClient(tcp_client_); +#else delete tcp_client_; +#endif tcp_client_ = nullptr; recv_bufflen_ = 0; free(recv_buff_); @@ -142,7 +152,11 @@ void TargetConn::ForwardClientMsgEx(f8::MsgHdr* hdr) } } +#if ASYNC_TCPCLIENT && GAME_ID == 2002 +void TargetConn::on_error(a8::AsyncTcpClient* sender, int errorId) +#else void TargetConn::on_error(a8::TcpClient* sender, int errorId) +#endif { a8::UdpLog::Instance()->Error("target server errorid=%d remote_ip:%s remote_port:%d", { @@ -152,7 +166,11 @@ void TargetConn::on_error(a8::TcpClient* sender, int errorId) }); } +#if ASYNC_TCPCLIENT && GAME_ID == 2002 +void TargetConn::on_connect(a8::AsyncTcpClient* sender) +#else void TargetConn::on_connect(a8::TcpClient* sender) +#endif { recv_bufflen_ = 0; a8::UdpLog::Instance()->Info("target server connected remote_ip:%s remote_port:%d", @@ -166,7 +184,11 @@ void TargetConn::on_connect(a8::TcpClient* sender) ); } +#if ASYNC_TCPCLIENT && GAME_ID == 2002 +void TargetConn::on_disconnect(a8::AsyncTcpClient* sender) +#else void TargetConn::on_disconnect(a8::TcpClient* sender) +#endif { recv_bufflen_ = 0; a8::UdpLog::Instance()->Info("target server %d disconnected after 10s later reconnect " @@ -182,7 +204,11 @@ void TargetConn::on_disconnect(a8::TcpClient* sender) ); } +#if ASYNC_TCPCLIENT && GAME_ID == 2002 +void TargetConn::on_socketread(a8::AsyncTcpClient* sender, char* buf, unsigned int len) +#else void TargetConn::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len) +#endif { #if 0 ++App::Instance()->perf.read_count; diff --git a/server/wsproxy/target_conn.h b/server/wsproxy/target_conn.h index 8624dd9..4e2d8ab 100644 --- a/server/wsproxy/target_conn.h +++ b/server/wsproxy/target_conn.h @@ -5,6 +5,7 @@ namespace a8 { class TcpClient; + class AsyncTcpClient; } struct TargetConnMsgNode @@ -55,10 +56,17 @@ class TargetConn void ForwardClientMsgEx(f8::MsgHdr* hdr); private: +#if ASYNC_TCPCLIENT && GAME_ID == 2002 + 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(); void AddStockMsg(unsigned short socket_handle, int msgid, ::google::protobuf::Message* msg, @@ -67,7 +75,11 @@ class TargetConn private: char *recv_buff_ = nullptr; unsigned int recv_bufflen_ = 0; +#if ASYNC_TCPCLIENT && GAME_ID == 2002 + a8::AsyncTcpClient* tcp_client_ = nullptr; +#else a8::TcpClient* tcp_client_ = nullptr; +#endif timer_list* timer_ = nullptr; TargetConnMsgNode* top_node_ = nullptr;