This commit is contained in:
azw 2023-05-05 23:22:34 +00:00
parent 5d69655f43
commit b064aed353
5 changed files with 37 additions and 22 deletions

View File

@ -2,7 +2,11 @@
#include <string.h>
#ifdef USE_ASIO
#include <a8/asiotcpclient.h>
#else
#include <a8/tcpclient.h>
#endif
#include <f8/udplog.h>
#include <f8/timer.h>
@ -16,12 +20,6 @@
const int PACK_MAX = 1024 * 64 * 2;
#ifdef USE_ASIO
UpStream::UpStream(asio::io_context& io_context):socket_(io_context)
{
}
#endif
void UpStream::Init(int instance_id, const std::string& remote_ip, int remote_port)
{
if (remote_ip.empty()) {
@ -34,7 +32,11 @@ void UpStream::Init(int instance_id, const std::string& remote_ip, int remote_po
recv_bufflen_ = 0;
last_pong_tick = a8::XGetTickCount();
recv_buff_ = (char*) malloc(PACK_MAX * 2);
#ifdef USE_ASIO
tcp_client_ = std::make_shared<a8::AsioTcpClient>();
#else
tcp_client_ = std::make_shared<a8::TcpClient>();
#endif
tcp_client_->remote_address = remote_ip;
tcp_client_->remote_port = remote_port;
tcp_client_->on_error = std::bind(&UpStream::on_error, this, std::placeholders::_1, std::placeholders::_2);
@ -154,7 +156,11 @@ void UpStream::ForwardClientMsgEx(f8::MsgHdr* hdr)
}
}
#ifdef USE_ASIO
void UpStream::on_error(a8::AsioTcpClient* sender, int errorId)
#else
void UpStream::on_error(a8::TcpClient* sender, int errorId)
#endif
{
f8::UdpLog::Instance()->Error("target server errorid=%d remote_ip:%s remote_port:%d",
{
@ -164,7 +170,11 @@ void UpStream::on_error(a8::TcpClient* sender, int errorId)
});
}
#ifdef USE_ASIO
void UpStream::on_connect(a8::AsioTcpClient* sender)
#else
void UpStream::on_connect(a8::TcpClient* sender)
#endif
{
recv_bufflen_ = 0;
f8::UdpLog::Instance()->Info("target server connected remote_ip:%s remote_port:%d",
@ -184,7 +194,11 @@ void UpStream::on_connect(a8::TcpClient* sender)
);
}
#ifdef USE_ASIO
void UpStream::on_disconnect(a8::AsioTcpClient* sender)
#else
void UpStream::on_disconnect(a8::TcpClient* sender)
#endif
{
recv_bufflen_ = 0;
f8::UdpLog::Instance()->Info("target server %d disconnected after 10s later reconnect "
@ -206,7 +220,11 @@ void UpStream::on_disconnect(a8::TcpClient* sender)
);
}
#ifdef USE_ASIO
void UpStream::on_socketread(a8::AsioTcpClient* sender, char* buf, unsigned int len)
#else
void UpStream::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len)
#endif
{
#if 0
++App::Instance()->perf.read_count;

View File

@ -2,13 +2,10 @@
#include <f8/protoutils.h>
#ifdef USE_ASIO
#include <asio.hpp>
#endif
namespace a8
{
class TcpClient;
class AsioTcpClient;
}
struct UpStreamMsgNode
@ -30,9 +27,6 @@ class UpStream
a8::tick_t last_pong_tick = 0;
public:
#ifdef USE_ASIO
UpStream(asio::io_context& io_context);
#endif
void Init(int instance_id, const std::string& remote_ip, int remote_port);
void UnInit();
@ -62,10 +56,17 @@ class UpStream
void ForwardClientMsgEx(f8::MsgHdr* hdr);
private:
#ifdef USE_ASIO
void on_error(a8::AsioTcpClient* sender, int errorId);
void on_connect(a8::AsioTcpClient* sender);
void on_disconnect(a8::AsioTcpClient* sender);
void on_socketread(a8::AsioTcpClient* 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,
@ -74,9 +75,10 @@ class UpStream
private:
char *recv_buff_ = nullptr;
unsigned int recv_bufflen_ = 0;
std::shared_ptr<a8::TcpClient> tcp_client_;
#ifdef USE_ASIO
asio::ip::tcp::socket socket_;
std::shared_ptr<a8::AsioTcpClient> tcp_client_;
#else
std::shared_ptr<a8::TcpClient> tcp_client_;
#endif
f8::TimerWp timer_wp_;
f8::Attacher attacher_;

View File

@ -55,12 +55,7 @@ std::weak_ptr<UpStream> UpStreamMgr::RecreateUpStream(const std::string& host, i
int instance_id = curr_id_;
std::string remote_ip = host;
int remote_port = port;
#ifdef USE_ASIO
asio::io_context io_context;
std::shared_ptr<UpStream> conn = std::make_shared<UpStream>(io_context);
#else
std::shared_ptr<UpStream> conn = std::make_shared<UpStream>();
#endif
conn->Init(instance_id, remote_ip, remote_port);
id_hash_[conn->instance_id] = conn;
key_hash_[key] = conn;

2
third_party/a8 vendored

@ -1 +1 @@
Subproject commit 54fe50edf11dda774dcd6fc480134c9e4ce449cd
Subproject commit c5e180a96a7ba0d89f41f28821b0eb081f7ee880

2
third_party/f8 vendored

@ -1 +1 @@
Subproject commit ae5c84ae98a314a6f29a2e29e9a2937801324c15
Subproject commit e8e6a85b8f00a248eea7aea80276c6f1afd98ef9