diff --git a/server/textserver/GCListener.cc b/server/textserver/GCListener.cc new file mode 100644 index 0000000..8343949 --- /dev/null +++ b/server/textserver/GCListener.cc @@ -0,0 +1,79 @@ +#include "precompile.h" + +#include +#include +#include + +#include "framework/cpp/netmsghandler.h" + +#include "app.h" +#include "GCListener.h" +#include "jsondatamgr.h" +#include "handlermgr.h" + +class GCClientSession: public a8::HttpSession +{ +public: + + virtual void OnRawHttpGet(const std::string& url, const std::string& querystr, + std::string& response) override + { + App::Instance()->AddIMMsg(IM_ExecGM, + a8::XParams() + .SetSender(socket_handle) + .SetParam1(url) + .SetParam2(querystr) + .SetParam3(saddr)); + } + + virtual void OnDisConnect() override + { + App::Instance()->AddIMMsg(IM_ClientSocketDisconnect, + a8::XParams() + .SetSender(socket_handle) + .SetParam1(1)); + } + +}; + +static void CreateGameClientSocket(a8::TcpSession **p) +{ + *p = new GCClientSession(); +} + +static void GSListeneron_error(a8::TcpListener*, int type, int errorid) +{ + a8::UdpLog::Instance()->Debug("GCListeneron_error %d %d", {type, errorid}); +} + +void GCListener::Init() +{ + tcp_listener_ = new a8::TcpListener(); + tcp_listener_->on_create_client_socket = CreateGameClientSocket; + tcp_listener_->on_error = GSListeneron_error; + + tcp_listener_->bind_address = "0.0.0.0"; + tcp_listener_->bind_port = JsonDataMgr::Instance()->GetConf()->At("listen_port")->AsXValue(); + tcp_listener_->Open(); +} + +void GCListener::UnInit() +{ + delete tcp_listener_; + tcp_listener_ = nullptr; +} + +void GCListener::SendText(unsigned short sockhandle, const std::string& text) +{ + tcp_listener_->SendClientMsg(sockhandle, text.data(), text.size()); +} + +void GCListener::ForceCloseClient(unsigned short sockhandle) +{ + tcp_listener_->ForceCloseClient(sockhandle); +} + +void GCListener::MarkClient(unsigned short sockhandle, bool is_active) +{ + tcp_listener_->MarkClient(sockhandle, is_active); +} diff --git a/server/textserver/GCListener.h b/server/textserver/GCListener.h new file mode 100644 index 0000000..5de98fb --- /dev/null +++ b/server/textserver/GCListener.h @@ -0,0 +1,36 @@ +#pragma once + +//game client listener +namespace a8 +{ + class TcpListener; +} + +class GCListener : public a8::Singleton +{ + private: + GCListener() {}; + friend class a8::Singleton; + + public: + enum { HID = HID_GCListener }; + + public: + void Init(); + void UnInit(); + + template + void SendMsg(unsigned short socket_handle, T& msg) + { + static int msgid = f8::Net_GetMessageId(msg); + f8::Net_SendMsg(tcp_listener_, socket_handle, 0, msgid, msg); + } + + void SendText(unsigned short sockhandle, const std::string& text); + + void ForceCloseClient(unsigned short sockhandle); + void MarkClient(unsigned short sockhandle, bool is_active); + + private: + a8::TcpListener *tcp_listener_ = nullptr; +}; diff --git a/server/textserver/constant.h b/server/textserver/constant.h index 5d8717d..f5318af 100755 --- a/server/textserver/constant.h +++ b/server/textserver/constant.h @@ -16,7 +16,7 @@ enum InnerMesssage_e //网络处理对象 enum NetHandler_e { - HID_IMSMgr, + HID_GCListener, }; const char* const PROJ_NAME_FMT = "textserver";