frameType:数据帧类型和data区域类型统一放到协议头中。

This commit is contained in:
taynpg 2025-05-11 00:32:52 +08:00
parent 9e4230f696
commit af648087bf
7 changed files with 45 additions and 35 deletions

View File

@ -30,8 +30,7 @@ void ClientCore::Disconnect()
bool ClientCore::GetOnlineList(InfoClientVec& infoClientVec)
{
InfoCommunicate infoCommunicate;
infoCommunicate.type = MSG_TYPE_ASK_CLIENTS;
if (!Send<InfoCommunicate>(infoCommunicate)) {
if (!Send<InfoCommunicate>(infoCommunicate, FBT_SER_MSG_ASKCLIENTS)) {
return false;
}
return false;
@ -80,7 +79,7 @@ void ClientCore::UseFrame(FrameBuffer* buf)
{
std::stringstream ss;
switch (buf->dataType) {
case FRAME_TYPE_INFO_CLIENT: {
case FBT_SER_MSG_ASKCLIENTS: {
InfoClientVec vec;
ZeroCopyInput input(buf->dataMut, buf->len);
input.archive() >> vec;

View File

@ -10,8 +10,8 @@
#include <vector>
#include <wx/socket.h>
#include <Util.h>
#include <Communicate.h>
#include <Util.h>
class ClientCore : public wxEvtHandler
{
@ -34,7 +34,7 @@ private:
private:
void HeartBeat();
template <typename T> bool Send(const T& info)
template <typename T> bool Send(const T& info, FrameBufferType type)
{
std::stringstream ss;
cereal::BinaryOutputArchive archive(ss);
@ -43,10 +43,11 @@ private:
auto buf = std::make_shared<FrameBuffer>();
buf->dataConst = ss.view().data();
buf->len = ss.str().size();
buf->dataType = type;
return Send(buf.get());
}
bool Send(FrameBuffer* buf);
bool Send(FrameBuffer* buf);
private:
wxString id_;

View File

@ -8,14 +8,7 @@
constexpr int GBUFFER_SIZE = 256;
enum MessageType {
MSG_TYPE_ASK_CLIENTS = 1,
MSG_TYPE_FORWORD_FAILED,
MSG_TYPE_HEARTBEAT,
};
struct InfoCommunicate {
MessageType type;
std::string fromID;
std::string toID;
std::string UUID;
@ -23,7 +16,7 @@ struct InfoCommunicate {
uint8_t mark{};
template <class Archive> void serialize(Archive& archive)
{
archive(CEREAL_NVP(type), CEREAL_NVP(fromID), CEREAL_NVP(toID), CEREAL_NVP(UUID), CEREAL_NVP(data), CEREAL_NVP(mark));
archive(CEREAL_NVP(fromID), CEREAL_NVP(toID), CEREAL_NVP(UUID), CEREAL_NVP(data), CEREAL_NVP(mark));
}
};

View File

@ -4,12 +4,15 @@
#include <Util.h>
#include <cstdint>
// 这里规定,前 30 个内容(包含)为与服务端交流用。
// 大于 30 的内容仅做转发。
enum FrameBufferType : uint16_t {
FRAME_TYPE_MSG_FILEDATA,
FRAME_TYPE_INFO_COMMUNICATE,
FRAME_TYPE_INFO_CLIENT,
FRAME_TYPE_INFO_DIRFILE,
FRAME_TYPE_MSG_YOURID
FBT_SER_MSG_ASKCLIENTS = 0,
FBT_SER_MSG_YOURID,
FBT_SER_MSG_RESPONSE,
FBT_CLI_BIN_FILEDATA = 31,
FBT_CLI_MSG_COMMUNICATE,
FBT_CLI_INFO_DIRFILE
};
struct FrameBuffer {

View File

@ -101,7 +101,6 @@ void RelayServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, co
}
client->wxSock->SetFlags(wxSOCKET_BLOCK);
InfoCommunicate info;
while (thRun_) {
wxSock->Read(client->buf.data(), GBUFFER_SIZE);
auto br = wxSock->LastCount();
@ -118,12 +117,8 @@ void RelayServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, co
if (!frame) {
break;
}
if (frame->dataType != 0) {
std::stringstream ss;
ss.write(frame->dataMut, frame->len);
cereal::BinaryInputArchive inputArchive(ss);
inputArchive(info);
Reply(client->wxSock, info);
if (frame->dataType <= 30) {
ReplyRequest(client->wxSock, frame);
delete frame;
continue;
}
@ -161,10 +156,10 @@ bool RelayServer::Forword(const sockPtr& wxSock, FrameBuffer* buf)
return farward;
}
bool RelayServer::Reply(const sockPtr& wxSock, InfoCommunicate& info)
bool RelayServer::Reply(const sockPtr& wxSock, InfoCommunicate& info, FrameBufferType type)
{
switch (info.type) {
case MSG_TYPE_ASK_CLIENTS: {
switch (type) {
case FBT_SER_MSG_ASKCLIENTS: {
std::swap(info.fromID, info.toID);
info.fromID = strID_;
RpyOnline(wxSock);
@ -173,10 +168,27 @@ bool RelayServer::Reply(const sockPtr& wxSock, InfoCommunicate& info)
default:
break;
}
wxLogWarning(_("Unknow message type: %d"), info.type);
wxLogWarning(_("Unknow message type: %d"), type);
return false;
}
void RelayServer::ReplyRequest(const sockPtr& wxSock, FrameBuffer* frame)
{
InfoCommunicate info;
switch (frame->dataType) {
case FBT_SER_MSG_ASKCLIENTS: {
std::stringstream ss;
ss.write(frame->dataMut, frame->len);
cereal::BinaryInputArchive inputArchive(ss);
inputArchive(info);
Reply(wxSock, info, FBT_SER_MSG_ASKCLIENTS);
break;
}
default:
break;
}
}
bool RelayServer::RpyOnline(const sockPtr& wxSock)
{
InfoClientVec infoClients;
@ -192,14 +204,13 @@ bool RelayServer::RpyOnline(const sockPtr& wxSock)
std::stringstream ss;
cereal::BinaryOutputArchive archive(ss);
archive(infoClients);
return Send<InfoClientVec>(wxSock, infoClients);
return Send<InfoClientVec>(wxSock, infoClients, FBT_SER_MSG_ASKCLIENTS);
}
bool RelayServer::RpyForwordFailed(const sockPtr& wxSock)
{
InfoCommunicate info;
info.type = MSG_TYPE_FORWORD_FAILED;
return Send<InfoCommunicate>(wxSock, info);
return Send<InfoCommunicate>(wxSock, info, FBT_SER_MSG_RESPONSE);
}
bool RelayServer::Send(const sockPtr& wxSock, FrameBuffer* buf)

View File

@ -41,14 +41,15 @@ private:
void OnServerEvent(wxSocketEvent& event);
void thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, const wxString& id);
bool Forword(const sockPtr& wxSock, FrameBuffer* buf);
bool Reply(const sockPtr& wxSock, InfoCommunicate& info);
bool Reply(const sockPtr& wxSock, InfoCommunicate& info, FrameBufferType type);
void ReplyRequest(const sockPtr& wxSock, FrameBuffer* frame);
private:
bool RpyOnline(const sockPtr& wxSock);
bool RpyForwordFailed(const sockPtr& wxSock);
private:
template <typename T> bool Send(const sockPtr& wxSock, const T& info)
template <typename T> bool Send(const sockPtr& wxSock, const T& info, FrameBufferType type)
{
std::stringstream ss;
cereal::BinaryOutputArchive archive(ss);
@ -57,6 +58,7 @@ private:
auto buf = std::make_shared<FrameBuffer>();
buf->dataConst = ss.view().data();
buf->len = ss.str().size();
buf->dataType = type;
return Send(wxSock, buf.get());
}

View File

@ -41,6 +41,7 @@ void HeaderControl::OnConnect(wxCommandEvent& event)
wxString ip = textIP_->GetValue();
unsigned int port{};
if (!textPort_->GetValue().ToUInt(&port)) {
logControl_->AddLog(_("Invalid port."));
return;
}
auto uPort = static_cast<unsigned short>(port);