From 6c39a6480dba31424ae8abfe83b88e1363c126a5 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 22 Dec 2024 21:18:03 +0800 Subject: [PATCH] 1 --- third_party/a8/a8/magicenum.h | 2 - third_party/f8/f8/app.cc | 40 +++++++++---------- third_party/f8/f8/app.h | 16 ++++---- third_party/f8/f8/internal/asiotcpclient.h | 3 +- third_party/f8/f8/protoutils.cc | 8 ++++ third_party/f8/f8/protoutils.h | 15 +++----- third_party/f8/f8/tcpclient.h | 2 + third_party/f8/f8/tcplistener.h | 45 ++++++++++++++++++++++ third_party/f8/f8/tcpsession.h | 26 +++++++++++++ third_party/f8/f8/types.h | 8 ++++ third_party/f8/f8/userapp.h | 1 + 11 files changed, 124 insertions(+), 42 deletions(-) create mode 100644 third_party/f8/f8/tcplistener.h create mode 100644 third_party/f8/f8/tcpsession.h diff --git a/third_party/a8/a8/magicenum.h b/third_party/a8/a8/magicenum.h index e57b178..506089d 100644 --- a/third_party/a8/a8/magicenum.h +++ b/third_party/a8/a8/magicenum.h @@ -1,7 +1,5 @@ #pragma once -#include - namespace a8 { diff --git a/third_party/f8/f8/app.cc b/third_party/f8/f8/app.cc index 3bf30e0..c3c3054 100644 --- a/third_party/f8/f8/app.cc +++ b/third_party/f8/f8/app.cc @@ -199,27 +199,25 @@ namespace f8 } } - void AddSocketMsg(int sockfrom, - int sockhandle, - long ip_saddr, - unsigned short msgid, - unsigned int seqid, - const char *msgbody, - int bodylen, - int tag) + void AddSocketMsg(unsigned short sockfrom, + long long socket_handle, + int msgid, + int seqid, + const char *msgbody, + int bodylen, + unsigned short tag, + std::any* user_data) { char *p = (char*)malloc(sizeof(MsgHdr) + bodylen); MsgHdr* hdr = (MsgHdr*)p; hdr->sockfrom = sockfrom; hdr->seqid = seqid; hdr->msgid = msgid; - hdr->socket_handle = sockhandle; - hdr->ip_saddr = ip_saddr; + hdr->socket_handle = socket_handle; hdr->buf = p + sizeof(MsgHdr); hdr->buflen = bodylen; hdr->offset = 0; - hdr->hum = nullptr; - hdr->user_data = nullptr; + hdr->user_data = user_data; hdr->tag = tag; if (bodylen > 0) { memmove((void*)hdr->buf, msgbody, bodylen); @@ -400,23 +398,23 @@ namespace f8 return impl_->GetInstanceId(); } - void App::AddSocketMsg(int sockfrom, - int sockhandle, - long ip_saddr, - unsigned short msgid, - unsigned int seqid, + void App::AddSocketMsg(unsigned short sockfrom, + long long socket_handle, + int msgid, + int seqid, const char *msgbody, int bodylen, - int tag) + unsigned short tag, + std::any* user_data) { impl_->AddSocketMsg(sockfrom, - sockhandle, - ip_saddr, + socket_handle, msgid, seqid, msgbody, bodylen, - tag); + tag, + user_data); } } diff --git a/third_party/f8/f8/app.h b/third_party/f8/f8/app.h index a92a453..6b3bfdb 100644 --- a/third_party/f8/f8/app.h +++ b/third_party/f8/f8/app.h @@ -23,14 +23,14 @@ namespace f8 int GetPid(); bool Terminated(); void Terminate(); - void AddSocketMsg(int sockfrom, - int sockhandle, - long ip_saddr, - unsigned short msgid, - unsigned int seqid, - const char *msgbody, - int bodylen, - int tag); + void AddSocketMsg(unsigned short sockfrom, + long long socket_handle, + int msgid, + int seqid, + const char *buf, + int buflen, + unsigned short tag, + std::any* user_data); char** GetArgv(); int GetArgc(); long long GetMsgNodeSize(); diff --git a/third_party/f8/f8/internal/asiotcpclient.h b/third_party/f8/f8/internal/asiotcpclient.h index 64a4339..5e15b9f 100644 --- a/third_party/f8/f8/internal/asiotcpclient.h +++ b/third_party/f8/f8/internal/asiotcpclient.h @@ -2,10 +2,9 @@ #include -using asio::ip::tcp; - namespace f8 { + namespace internal { diff --git a/third_party/f8/f8/protoutils.cc b/third_party/f8/f8/protoutils.cc index d34b1c7..2a61a1d 100644 --- a/third_party/f8/f8/protoutils.cc +++ b/third_party/f8/f8/protoutils.cc @@ -308,11 +308,19 @@ namespace f8 MsgHdr* hdr = (MsgHdr*)malloc(sizeof(MsgHdr) + buflen); memmove((void*)hdr, (void*)this, sizeof(MsgHdr) + buflen); hdr->buf = ((char*)hdr) + sizeof(MsgHdr); + if (user_data) { + hdr->user_data = new(std::any); + *hdr->user_data = *user_data; + } return hdr; } void MsgHdr::Destroy(MsgHdr* hdr) { + if (hdr->user_data) { + delete hdr->user_data; + hdr->user_data = nullptr; + } free((void*)hdr); } diff --git a/third_party/f8/f8/protoutils.h b/third_party/f8/f8/protoutils.h index 89c613b..22a39a7 100644 --- a/third_party/f8/f8/protoutils.h +++ b/third_party/f8/f8/protoutils.h @@ -7,24 +7,21 @@ namespace a8 class AsioTcpClient; } -class Player; namespace f8 { struct MsgHdr { - int sockfrom; - unsigned int seqid; - unsigned short msgid; - int socket_handle; - unsigned long ip_saddr; + unsigned short sockfrom; + unsigned short tag; + int seqid; + int msgid; + long long socket_handle; const char* buf; int buflen; int offset; - Player *hum = nullptr; - const void* user_data = nullptr; - int tag; list_head entry; + std::any* user_data = nullptr; MsgHdr* Clone(); static void Destroy(MsgHdr* hdr); diff --git a/third_party/f8/f8/tcpclient.h b/third_party/f8/f8/tcpclient.h index 2cb492f..82c0b4f 100644 --- a/third_party/f8/f8/tcpclient.h +++ b/third_party/f8/f8/tcpclient.h @@ -21,6 +21,8 @@ namespace f8 std::function on_disconnect; std::function on_read; + public: + TcpClient(const std::string& remote_ip, int remote_port); virtual ~TcpClient(); const std::string& GetRemoteAddress(); diff --git a/third_party/f8/f8/tcplistener.h b/third_party/f8/f8/tcplistener.h new file mode 100644 index 0000000..1c38a54 --- /dev/null +++ b/third_party/f8/f8/tcplistener.h @@ -0,0 +1,45 @@ +#pragma once + +namespace f8 +{ + namespace internal + { + class AsioTcpListener; + } +} + +namespace f8 +{ + + using TcpListenerImpl = f8::internal::AsioTcpListener; + + class TcpSession; + class TcpListener + { + public: + std::function on_error; + + public: + TcpListener(int max_client_cnt=0xEFFF); + virtual ~TcpListener(); + + void Open(); + void Close(); + template + void RegisterSessionClass() + { + on_create_client_socket_ = + [] (f8::TcpSession** p) + { + *p = new T; + }; + } + + private: + f8::TcpListenerImpl* impl_ = nullptr; + + friend class f8::TcpSession; + friend class f8::TcpListenerImpl; + }; + +} diff --git a/third_party/f8/f8/tcpsession.h b/third_party/f8/f8/tcpsession.h new file mode 100644 index 0000000..cecb685 --- /dev/null +++ b/third_party/f8/f8/tcpsession.h @@ -0,0 +1,26 @@ +#pragma once + +namespace f8 +{ + + class TcpListener; + class TcpSession + { + public: + TcpSession(); + virtual ~TcpSession(); + + void SetMaxPacketLen(int max_packet_len); + virtual void SendBuff(const char* buff, unsigned int bufflen); + + protected: + virtual void OnError(int); + virtual void OnConnect(); + virtual void OnDisConnect(); + virtual void OnRead(char* buf, unsigned int buflen); + virtual void DecodePacket(char* buf, int& offset, unsigned int buflen); + virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) = 0; + + }; + +} diff --git a/third_party/f8/f8/types.h b/third_party/f8/f8/types.h index 3011237..b1dbb3e 100644 --- a/third_party/f8/f8/types.h +++ b/third_party/f8/f8/types.h @@ -43,4 +43,12 @@ namespace f8 friend class Timer; }; + enum TCPLISTENER_E + { + TE_CREATE_ERR, + TE_SETSOCKOPT_ERR, + TE_BIND_ERR, + TE_LISTEN_ERR, + }; + } diff --git a/third_party/f8/f8/userapp.h b/third_party/f8/f8/userapp.h index e41f8f8..25ec329 100644 --- a/third_party/f8/f8/userapp.h +++ b/third_party/f8/f8/userapp.h @@ -3,6 +3,7 @@ namespace f8 { + struct MsgHdr; class UserApp { public: