add asynctcpclient

This commit is contained in:
aozhiwei 2020-01-09 18:45:20 +08:00
parent 042ef86426
commit 759b58b04c
2 changed files with 38 additions and 0 deletions

View File

@ -8,6 +8,8 @@
#include <a8/tcpclient.h> #include <a8/tcpclient.h>
#include <a8/udplog.h> #include <a8/udplog.h>
#include <a8/timer.h> #include <a8/timer.h>
#include <a8/ioloop.h>
#include <a8/asynctcpclient.h>
#include "app.h" #include "app.h"
const int PACK_MAX = 1024 * 64 * 2; 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; 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 && GAME_ID == 2002
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(&TargetConn::on_error, this, std::placeholders::_1, std::placeholders::_2); 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_); a8::Timer::Instance()->DeleteTimer(timer_);
timer_ = nullptr; timer_ = nullptr;
tcp_client_->Close(); tcp_client_->Close();
#if ASYNC_TCPCLIENT && GAME_ID == 2002
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_);
@ -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) void TargetConn::on_error(a8::TcpClient* sender, int errorId)
#endif
{ {
a8::UdpLog::Instance()->Error("target server errorid=%d remote_ip:%s remote_port:%d", 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) void TargetConn::on_connect(a8::TcpClient* sender)
#endif
{ {
recv_bufflen_ = 0; recv_bufflen_ = 0;
a8::UdpLog::Instance()->Info("target server connected remote_ip:%s remote_port:%d", 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) void TargetConn::on_disconnect(a8::TcpClient* sender)
#endif
{ {
recv_bufflen_ = 0; recv_bufflen_ = 0;
a8::UdpLog::Instance()->Info("target server %d disconnected after 10s later reconnect " 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) void TargetConn::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 TargetConnMsgNode struct TargetConnMsgNode
@ -55,10 +56,17 @@ class TargetConn
void ForwardClientMsgEx(f8::MsgHdr* hdr); void ForwardClientMsgEx(f8::MsgHdr* hdr);
private: 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_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();
void AddStockMsg(unsigned short socket_handle, int msgid, ::google::protobuf::Message* msg, void AddStockMsg(unsigned short socket_handle, int msgid, ::google::protobuf::Message* msg,
@ -67,7 +75,11 @@ class TargetConn
private: private:
char *recv_buff_ = nullptr; char *recv_buff_ = nullptr;
unsigned int recv_bufflen_ = 0; unsigned int recv_bufflen_ = 0;
#if ASYNC_TCPCLIENT && GAME_ID == 2002
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;
TargetConnMsgNode* top_node_ = nullptr; TargetConnMsgNode* top_node_ = nullptr;