diff --git a/server/wsproxy/GCListener.cc b/server/wsproxy/GCListener.cc index 9d120b3..d0f7ad7 100644 --- a/server/wsproxy/GCListener.cc +++ b/server/wsproxy/GCListener.cc @@ -23,7 +23,7 @@ public: bool warning = false; while (buflen - offset >= sizeof(PackHead)) { PackHead* p = (PackHead*)&buf[offset]; - if (p->magiccode == MAGIC_CODE) { + if (p->magic_code == MAGIC_CODE) { if (buflen - offset < sizeof(PackHead) + p->packlen) { break; } @@ -31,6 +31,7 @@ public: socket_handle, saddr, p->msgid, + p->seqid, &buf[offset + sizeof(PackHead)], p->packlen); offset += sizeof(PackHead) + p->packlen; @@ -98,17 +99,6 @@ void GCListener::SendText(unsigned short sockhandle, const std::string& text) tcp_listener_->SendClientMsg(sockhandle, text.data(), text.size()); } -void GCListener::SendErrorMsg(unsigned short sockhandle, int msgid, - int error_code, const std::string& error_msg) -{ - #if 0 - ss::SS_Error msg; - msg.mutable_result()->set_error_code(error_code); - msg.mutable_result()->set_error_msg(error_msg); - Net_SendMsg(tcp_listener_, sockhandle, msgid, msg); - #endif -} - void GCListener::ForceCloseClient(unsigned short sockhandle) { tcp_listener_->ForceCloseClient(sockhandle); diff --git a/server/wsproxy/GCListener.h b/server/wsproxy/GCListener.h index 7f084ba..26516cb 100644 --- a/server/wsproxy/GCListener.h +++ b/server/wsproxy/GCListener.h @@ -32,9 +32,6 @@ class GCListener : public a8::Singleton } void SendText(unsigned short sockhandle, const std::string& text); - void SendErrorMsg(unsigned short sockhandle, int msgid, - int error_code, const std::string& error_msg); - void ForceCloseClient(unsigned short sockhandle); void MarkClient(unsigned short sockhandle, bool is_active); diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index 1531b10..91159ce 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -15,12 +15,16 @@ #include "GCListener.h" #include "jsondatamgr.h" #include "handlermgr.h" +#include "target_conn.h" +#include "gameclient.h" +#include "gameclientmgr.h" struct MsgNode { SocketFrom_e sockfrom; int sockhandle; unsigned short msgid; + unsigned int seqid; long ip_saddr; char* buf; int buflen; @@ -82,6 +86,7 @@ void App::Init(int argc, char* argv[]) JsonDataMgr::Instance()->Init(); GCListener::Instance()->Init(); uuid.SetMachineId(instance_id); + GameClientMgr::Instance()->Init(); a8::UdpLog::Instance()->Info("masterserver starting instance_id:%d pid:%d", {instance_id, getpid()}); { @@ -103,6 +108,7 @@ void App::UnInit() if (terminated) { return; } + GameClientMgr::Instance()->UnInit(); GCListener::Instance()->UnInit(); JsonDataMgr::Instance()->UnInit(); a8::Timer::Instance()->UnInit(); @@ -143,6 +149,7 @@ void App::AddSocketMsg(SocketFrom_e sockfrom, int sockhandle, long ip_saddr, unsigned short msgid, + unsigned int seqid, const char *msgbody, int bodylen) { @@ -152,6 +159,7 @@ void App::AddSocketMsg(SocketFrom_e sockfrom, p->ip_saddr = ip_saddr; p->sockhandle = sockhandle; p->msgid = msgid; + p->seqid = seqid; p->buf = nullptr; p->buflen = bodylen; if (bodylen > 0) { @@ -279,9 +287,9 @@ void App::DispatchMsg() ProcessClientMsg(hdr); } break; - case SF_RoomServer: + case SF_TargetServer: { - ProcessRoomServerMsg(hdr); + ProcessTargetServerMsg(hdr); } break; } @@ -302,24 +310,16 @@ void App::DispatchMsg() void App::ProcessClientMsg(MsgHdr& hdr) { - NetMsgHandler* handler = GetNetMsgHandler(&HandlerMgr::Instance()->gcmsghandler, - hdr.msgid); - if (handler) { - #if 0 - switch (handler->handlerid) { - } - #endif - } else { - #if 0 - Player* hum = PlayerMgr::Instance()->GetPlayerBySocket(hdr.socket_handle); - if (hum) { - hum->ForwardClientMsg(hdr); - } - #endif + if (hdr.msgid < 100) { + return; + } + GameClient* client = GameClientMgr::Instance()->GetGameClientBySocket(hdr.socket_handle); + if (client && client->conn) { + client->conn->ForwardClientMsg(hdr); } } -void App::ProcessRoomServerMsg(MsgHdr& hdr) +void App::ProcessTargetServerMsg(MsgHdr& hdr) { NetMsgHandler* handler = GetNetMsgHandler(&HandlerMgr::Instance()->rsmsghandler, hdr.msgid); diff --git a/server/wsproxy/app.h b/server/wsproxy/app.h index 97da542..7201637 100644 --- a/server/wsproxy/app.h +++ b/server/wsproxy/app.h @@ -21,6 +21,7 @@ class App : public a8::Singleton int sockhandle, long ip_saddr, unsigned short msgid, + unsigned int seqid, const char *msgbody, int bodylen); void AddIMMsg(unsigned short imcmd, a8::XParams params); @@ -41,7 +42,7 @@ private: void ProcessIMMsg(); void ProcessClientMsg(MsgHdr& hdr); - void ProcessRoomServerMsg(MsgHdr& hdr); + void ProcessTargetServerMsg(MsgHdr& hdr); void InitLog(); void UnInitLog(); diff --git a/server/wsproxy/constant.h b/server/wsproxy/constant.h index b73c6ba..0e601e0 100644 --- a/server/wsproxy/constant.h +++ b/server/wsproxy/constant.h @@ -3,7 +3,7 @@ enum SocketFrom_e { SF_Client, - SF_RoomServer, + SF_TargetServer, }; enum InnerMesssage_e @@ -11,7 +11,7 @@ enum InnerMesssage_e IM_ClientSocketDisconnect = 100, IM_PlayerOffline, IM_ExecGM, - IM_RSConnDisconnect + IM_TargetConnDisconnect }; //网络处理对象 diff --git a/server/wsproxy/gameclient.cc b/server/wsproxy/gameclient.cc new file mode 100644 index 0000000..80b4132 --- /dev/null +++ b/server/wsproxy/gameclient.cc @@ -0,0 +1,3 @@ +#include "precompile.h" + +#include "gameclient.h" diff --git a/server/wsproxy/gameclient.h b/server/wsproxy/gameclient.h new file mode 100644 index 0000000..61ad8b8 --- /dev/null +++ b/server/wsproxy/gameclient.h @@ -0,0 +1,9 @@ +#pragma once + +class TargetConn; +class GameClient +{ + public: + TargetConn* conn = nullptr; + +}; diff --git a/server/wsproxy/gameclientmgr.cc b/server/wsproxy/gameclientmgr.cc new file mode 100644 index 0000000..acddbbd --- /dev/null +++ b/server/wsproxy/gameclientmgr.cc @@ -0,0 +1,17 @@ +#include "precompile.h" + +#include "gameclientmgr.h" + +void GameClientMgr::Init() +{ +} + +void GameClientMgr::UnInit() +{ +} + +GameClient* GameClientMgr::GetGameClientBySocket(int sockhandle) +{ + auto itr = socket_hash_.find(sockhandle); + return itr != socket_hash_.end() ? itr->second : nullptr; +} diff --git a/server/wsproxy/gameclientmgr.h b/server/wsproxy/gameclientmgr.h new file mode 100644 index 0000000..9a3bd50 --- /dev/null +++ b/server/wsproxy/gameclientmgr.h @@ -0,0 +1,19 @@ +#pragma once + +class GameClient; +class GameClientMgr : public a8::Singleton +{ + private: + GameClientMgr() {}; + friend class a8::Singleton; + + public: + + void Init(); + void UnInit(); + + GameClient* GetGameClientBySocket(int sockhande); + + private: + std::map socket_hash_; +}; diff --git a/server/wsproxy/target_conn.cc b/server/wsproxy/target_conn.cc index a52e0e7..9476e27 100644 --- a/server/wsproxy/target_conn.cc +++ b/server/wsproxy/target_conn.cc @@ -12,7 +12,7 @@ const int PACK_MAX = 1024 * 64; -void RSConn::Init(int instance_id, const std::string& remote_ip, int remote_port) +void TargetConn::Init(int instance_id, const std::string& remote_ip, int remote_port) { this->instance_id = instance_id; this->remote_ip = remote_ip; @@ -24,20 +24,20 @@ void RSConn::Init(int instance_id, const std::string& remote_ip, int remote_port tcp_client_ = new a8::TcpClient(); tcp_client_->remote_address = remote_ip; tcp_client_->remote_port = remote_port; - tcp_client_->on_error = std::bind(&RSConn::on_error, this, std::placeholders::_1, std::placeholders::_2); - tcp_client_->on_connect = std::bind(&RSConn::on_connect, this, std::placeholders::_1); - tcp_client_->on_disconnect = std::bind(&RSConn::on_disconnect, this, std::placeholders::_1); - tcp_client_->on_socketread = std::bind(&RSConn::on_socketread, this ,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); + tcp_client_->on_error = std::bind(&TargetConn::on_error, this, std::placeholders::_1, std::placeholders::_2); + tcp_client_->on_connect = std::bind(&TargetConn::on_connect, this, std::placeholders::_1); + tcp_client_->on_disconnect = std::bind(&TargetConn::on_disconnect, this, std::placeholders::_1); + tcp_client_->on_socketread = std::bind(&TargetConn::on_socketread, this ,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); timer_ = a8::Timer::Instance()->AddRepeatTimer(1000 * 9 + a8::RandEx(500, 150), a8::XParams().SetSender(this), [] (const a8::XParams& param) { - RSConn* conn = (RSConn*)param.sender.GetUserData(); + TargetConn* conn = (TargetConn*)param.sender.GetUserData(); conn->CheckAlive(); }); } -void RSConn::UnInit() +void TargetConn::UnInit() { a8::Timer::Instance()->DeleteTimer(timer_); timer_ = nullptr; @@ -49,43 +49,61 @@ void RSConn::UnInit() recv_buff_ = nullptr; } -void RSConn::Open() +void TargetConn::Open() { tcp_client_->Open(); } -void RSConn::Close() +void TargetConn::Close() { tcp_client_->Close(); } -bool RSConn::Connected() +bool TargetConn::Connected() { return tcp_client_->Connected(); } -void RSConn::on_error(a8::TcpClient* sender, int errorId) +void TargetConn::ForwardClientMsg(MsgHdr& hdr) { - a8::UdpLog::Instance()->Error("RSConn errorid=%d", {errorId}); + char* buff = (char*)malloc(sizeof(WSProxyPackHead_C) + hdr.buflen); + WSProxyPackHead_C* head = (WSProxyPackHead_C*)buff; + head->packlen = hdr.buflen; + head->msgid = hdr.msgid; + head->seqid = hdr.seqid; + head->magic_code = MAGIC_CODE; + #if 0 + head->rpc_error_code = 0; + #endif + head->socket_handle = hdr.socket_handle; + head->ip_saddr = hdr.ip_saddr; + + tcp_client_->SendBuff(buff, sizeof(WSProxyPackHead_C*) + head->packlen); + free(buff); } -void RSConn::on_connect(a8::TcpClient* sender) +void TargetConn::on_error(a8::TcpClient* sender, int errorId) +{ + a8::UdpLog::Instance()->Error("TargetConn errorid=%d", {errorId}); +} + +void TargetConn::on_connect(a8::TcpClient* sender) { recv_bufflen_ = 0; a8::UdpLog::Instance()->Info("room server connected", {}); } -void RSConn::on_disconnect(a8::TcpClient* sender) +void TargetConn::on_disconnect(a8::TcpClient* sender) { recv_bufflen_ = 0; a8::UdpLog::Instance()->Info("room server %d disconnected after 10s later reconnect", {instance_id}); - App::Instance()->AddIMMsg(IM_RSConnDisconnect, + App::Instance()->AddIMMsg(IM_TargetConnDisconnect, a8::XParams() .SetSender(instance_id) ); } -void RSConn::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len) +void TargetConn::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len) { #if 0 ++App::Instance()->perf.read_count; @@ -103,14 +121,15 @@ void RSConn::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len) unsigned int offset = 0; while (recv_bufflen_ - offset > sizeof(PackHead)) { PackHead* p = (PackHead*) &recv_buff_[offset]; - if (p->magiccode == MAGIC_CODE) { + if (p->magic_code == MAGIC_CODE) { if (recv_bufflen_ - offset < sizeof(PackHead) + p->packlen) { break; } - App::Instance()->AddSocketMsg(SF_RoomServer, + App::Instance()->AddSocketMsg(SF_TargetServer, instance_id, 0, p->msgid, + p->seqid, &recv_buff_[offset + sizeof(PackHead)], p->packlen); offset += sizeof(PackHead) + p->packlen; @@ -130,7 +149,7 @@ void RSConn::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len) recv_bufflen_ -= offset; } -void RSConn::CheckAlive() +void TargetConn::CheckAlive() { if (!Connected()) { Open(); @@ -139,8 +158,10 @@ void RSConn::CheckAlive() last_pong_tick = a8::XGetTickCount(); Open(); } else { + #if 0 ss::SS_Ping msg; SendToRoomServer(msg); + #endif } } } diff --git a/server/wsproxy/target_conn.h b/server/wsproxy/target_conn.h index bfa1382..3901462 100644 --- a/server/wsproxy/target_conn.h +++ b/server/wsproxy/target_conn.h @@ -6,7 +6,7 @@ namespace a8 } struct timer_list; -class RSConn +class TargetConn { public: int instance_id = 0; @@ -23,12 +23,7 @@ class RSConn void Close(); bool Connected(); - template - void SendToRoomServer(T& msg) - { - static int msgid = ::Net_GetMessageId(msg); - Net_SendMsg(tcp_client_, msgid, msg); - } + void ForwardClientMsg(MsgHdr& hdr); private: void on_error(a8::TcpClient* sender, int errorId); diff --git a/server/wsproxy/target_conn_mgr.cc b/server/wsproxy/target_conn_mgr.cc new file mode 100644 index 0000000..59ce089 --- /dev/null +++ b/server/wsproxy/target_conn_mgr.cc @@ -0,0 +1,17 @@ +#include "precompile.h" + +#include "target_conn_mgr.h" + +void TargetConnMgr::Init() +{ +} + +void TargetConnMgr::UnInit() +{ +} + +TargetConn* TargetConnMgr::GetConnBySocket(int instance_id) +{ + auto itr = target_conn_hash_.find(instance_id); + return itr != target_conn_hash_.end() ? itr->second : nullptr; +} diff --git a/server/wsproxy/target_conn_mgr.h b/server/wsproxy/target_conn_mgr.h new file mode 100644 index 0000000..652ea04 --- /dev/null +++ b/server/wsproxy/target_conn_mgr.h @@ -0,0 +1,19 @@ +#pragma once + +class TargetConn; +class TargetConnMgr : public a8::Singleton +{ + private: + TargetConnMgr() {}; + friend class a8::Singleton; + + public: + + void Init(); + void UnInit(); + + TargetConn* GetConnBySocket(int instance_id); + + private: + std::map target_conn_hash_; +};