This commit is contained in:
aozhiwei 2024-12-22 21:18:03 +08:00
parent f658c698a0
commit 6c39a6480d
11 changed files with 124 additions and 42 deletions

View File

@ -1,7 +1,5 @@
#pragma once #pragma once
#include <vector>
namespace a8 namespace a8
{ {

View File

@ -199,27 +199,25 @@ namespace f8
} }
} }
void AddSocketMsg(int sockfrom, void AddSocketMsg(unsigned short sockfrom,
int sockhandle, long long socket_handle,
long ip_saddr, int msgid,
unsigned short msgid, int seqid,
unsigned int seqid, const char *msgbody,
const char *msgbody, int bodylen,
int bodylen, unsigned short tag,
int tag) std::any* user_data)
{ {
char *p = (char*)malloc(sizeof(MsgHdr) + bodylen); char *p = (char*)malloc(sizeof(MsgHdr) + bodylen);
MsgHdr* hdr = (MsgHdr*)p; MsgHdr* hdr = (MsgHdr*)p;
hdr->sockfrom = sockfrom; hdr->sockfrom = sockfrom;
hdr->seqid = seqid; hdr->seqid = seqid;
hdr->msgid = msgid; hdr->msgid = msgid;
hdr->socket_handle = sockhandle; hdr->socket_handle = socket_handle;
hdr->ip_saddr = ip_saddr;
hdr->buf = p + sizeof(MsgHdr); hdr->buf = p + sizeof(MsgHdr);
hdr->buflen = bodylen; hdr->buflen = bodylen;
hdr->offset = 0; hdr->offset = 0;
hdr->hum = nullptr; hdr->user_data = user_data;
hdr->user_data = nullptr;
hdr->tag = tag; hdr->tag = tag;
if (bodylen > 0) { if (bodylen > 0) {
memmove((void*)hdr->buf, msgbody, bodylen); memmove((void*)hdr->buf, msgbody, bodylen);
@ -400,23 +398,23 @@ namespace f8
return impl_->GetInstanceId(); return impl_->GetInstanceId();
} }
void App::AddSocketMsg(int sockfrom, void App::AddSocketMsg(unsigned short sockfrom,
int sockhandle, long long socket_handle,
long ip_saddr, int msgid,
unsigned short msgid, int seqid,
unsigned int seqid,
const char *msgbody, const char *msgbody,
int bodylen, int bodylen,
int tag) unsigned short tag,
std::any* user_data)
{ {
impl_->AddSocketMsg(sockfrom, impl_->AddSocketMsg(sockfrom,
sockhandle, socket_handle,
ip_saddr,
msgid, msgid,
seqid, seqid,
msgbody, msgbody,
bodylen, bodylen,
tag); tag,
user_data);
} }
} }

View File

@ -23,14 +23,14 @@ namespace f8
int GetPid(); int GetPid();
bool Terminated(); bool Terminated();
void Terminate(); void Terminate();
void AddSocketMsg(int sockfrom, void AddSocketMsg(unsigned short sockfrom,
int sockhandle, long long socket_handle,
long ip_saddr, int msgid,
unsigned short msgid, int seqid,
unsigned int seqid, const char *buf,
const char *msgbody, int buflen,
int bodylen, unsigned short tag,
int tag); std::any* user_data);
char** GetArgv(); char** GetArgv();
int GetArgc(); int GetArgc();
long long GetMsgNodeSize(); long long GetMsgNodeSize();

View File

@ -2,10 +2,9 @@
#include <asio.hpp> #include <asio.hpp>
using asio::ip::tcp;
namespace f8 namespace f8
{ {
namespace internal namespace internal
{ {

View File

@ -308,11 +308,19 @@ namespace f8
MsgHdr* hdr = (MsgHdr*)malloc(sizeof(MsgHdr) + buflen); MsgHdr* hdr = (MsgHdr*)malloc(sizeof(MsgHdr) + buflen);
memmove((void*)hdr, (void*)this, sizeof(MsgHdr) + buflen); memmove((void*)hdr, (void*)this, sizeof(MsgHdr) + buflen);
hdr->buf = ((char*)hdr) + sizeof(MsgHdr); hdr->buf = ((char*)hdr) + sizeof(MsgHdr);
if (user_data) {
hdr->user_data = new(std::any);
*hdr->user_data = *user_data;
}
return hdr; return hdr;
} }
void MsgHdr::Destroy(MsgHdr* hdr) void MsgHdr::Destroy(MsgHdr* hdr)
{ {
if (hdr->user_data) {
delete hdr->user_data;
hdr->user_data = nullptr;
}
free((void*)hdr); free((void*)hdr);
} }

View File

@ -7,24 +7,21 @@ namespace a8
class AsioTcpClient; class AsioTcpClient;
} }
class Player;
namespace f8 namespace f8
{ {
struct MsgHdr struct MsgHdr
{ {
int sockfrom; unsigned short sockfrom;
unsigned int seqid; unsigned short tag;
unsigned short msgid; int seqid;
int socket_handle; int msgid;
unsigned long ip_saddr; long long socket_handle;
const char* buf; const char* buf;
int buflen; int buflen;
int offset; int offset;
Player *hum = nullptr;
const void* user_data = nullptr;
int tag;
list_head entry; list_head entry;
std::any* user_data = nullptr;
MsgHdr* Clone(); MsgHdr* Clone();
static void Destroy(MsgHdr* hdr); static void Destroy(MsgHdr* hdr);

View File

@ -21,6 +21,8 @@ namespace f8
std::function<void (f8::TcpClient*, int)> on_disconnect; std::function<void (f8::TcpClient*, int)> on_disconnect;
std::function<void (f8::TcpClient*, char*, int)> on_read; std::function<void (f8::TcpClient*, char*, int)> on_read;
public:
TcpClient(const std::string& remote_ip, int remote_port); TcpClient(const std::string& remote_ip, int remote_port);
virtual ~TcpClient(); virtual ~TcpClient();
const std::string& GetRemoteAddress(); const std::string& GetRemoteAddress();

45
third_party/f8/f8/tcplistener.h vendored Normal file
View File

@ -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<void (f8::TcpListener*, f8::TCPLISTENER_E, int)> on_error;
public:
TcpListener(int max_client_cnt=0xEFFF);
virtual ~TcpListener();
void Open();
void Close();
template <typename T>
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;
};
}

26
third_party/f8/f8/tcpsession.h vendored Normal file
View File

@ -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;
};
}

View File

@ -43,4 +43,12 @@ namespace f8
friend class Timer; friend class Timer;
}; };
enum TCPLISTENER_E
{
TE_CREATE_ERR,
TE_SETSOCKOPT_ERR,
TE_BIND_ERR,
TE_LISTEN_ERR,
};
} }

View File

@ -3,6 +3,7 @@
namespace f8 namespace f8
{ {
struct MsgHdr;
class UserApp class UserApp
{ {
public: public: