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
#include <vector>
namespace a8
{

View File

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

View File

@ -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();

View File

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

View File

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

View File

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

View File

@ -21,6 +21,8 @@ namespace f8
std::function<void (f8::TcpClient*, int)> on_disconnect;
std::function<void (f8::TcpClient*, char*, int)> on_read;
public:
TcpClient(const std::string& remote_ip, int remote_port);
virtual ~TcpClient();
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;
};
enum TCPLISTENER_E
{
TE_CREATE_ERR,
TE_SETSOCKOPT_ERR,
TE_BIND_ERR,
TE_LISTEN_ERR,
};
}

View File

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