From af648087bfab86d5c0e931f51bee19457f625349 Mon Sep 17 00:00:00 2001 From: taynpg Date: Sun, 11 May 2025 00:32:52 +0800 Subject: [PATCH] =?UTF-8?q?frameType=EF=BC=9A=E6=95=B0=E6=8D=AE=E5=B8=A7?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=92=8Cdata=E5=8C=BA=E5=9F=9F=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=BB=9F=E4=B8=80=E6=94=BE=E5=88=B0=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E5=A4=B4=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ClientCore/ClientCore.cxx | 5 ++--- ClientCore/ClientCore.h | 7 +++--- Information/InfoCommunicate.hpp | 9 +------- Protocol/Communicate.h | 13 ++++++----- RelayServer/RelayServer.cxx | 39 +++++++++++++++++++++------------ RelayServer/RelayServer.h | 6 +++-- UserInterface/HeaderControl.cxx | 1 + 7 files changed, 45 insertions(+), 35 deletions(-) diff --git a/ClientCore/ClientCore.cxx b/ClientCore/ClientCore.cxx index a28bba1..04a06f9 100644 --- a/ClientCore/ClientCore.cxx +++ b/ClientCore/ClientCore.cxx @@ -30,8 +30,7 @@ void ClientCore::Disconnect() bool ClientCore::GetOnlineList(InfoClientVec& infoClientVec) { InfoCommunicate infoCommunicate; - infoCommunicate.type = MSG_TYPE_ASK_CLIENTS; - if (!Send(infoCommunicate)) { + if (!Send(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; diff --git a/ClientCore/ClientCore.h b/ClientCore/ClientCore.h index 6852424..8d58bc8 100644 --- a/ClientCore/ClientCore.h +++ b/ClientCore/ClientCore.h @@ -10,8 +10,8 @@ #include #include -#include #include +#include class ClientCore : public wxEvtHandler { @@ -34,7 +34,7 @@ private: private: void HeartBeat(); - template bool Send(const T& info) + template bool Send(const T& info, FrameBufferType type) { std::stringstream ss; cereal::BinaryOutputArchive archive(ss); @@ -43,10 +43,11 @@ private: auto buf = std::make_shared(); 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_; diff --git a/Information/InfoCommunicate.hpp b/Information/InfoCommunicate.hpp index 9721314..6d1a147 100644 --- a/Information/InfoCommunicate.hpp +++ b/Information/InfoCommunicate.hpp @@ -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 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)); } }; diff --git a/Protocol/Communicate.h b/Protocol/Communicate.h index 628c007..a4f1a41 100644 --- a/Protocol/Communicate.h +++ b/Protocol/Communicate.h @@ -4,12 +4,15 @@ #include #include +// 这里规定,前 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 { diff --git a/RelayServer/RelayServer.cxx b/RelayServer/RelayServer.cxx index 9223f03..b60b19d 100644 --- a/RelayServer/RelayServer.cxx +++ b/RelayServer/RelayServer.cxx @@ -101,7 +101,6 @@ void RelayServer::thClientThread(const std::shared_ptr& 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& 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(wxSock, infoClients); + return Send(wxSock, infoClients, FBT_SER_MSG_ASKCLIENTS); } bool RelayServer::RpyForwordFailed(const sockPtr& wxSock) { InfoCommunicate info; - info.type = MSG_TYPE_FORWORD_FAILED; - return Send(wxSock, info); + return Send(wxSock, info, FBT_SER_MSG_RESPONSE); } bool RelayServer::Send(const sockPtr& wxSock, FrameBuffer* buf) diff --git a/RelayServer/RelayServer.h b/RelayServer/RelayServer.h index 0ba22a8..670c9e7 100644 --- a/RelayServer/RelayServer.h +++ b/RelayServer/RelayServer.h @@ -41,14 +41,15 @@ private: void OnServerEvent(wxSocketEvent& event); void thClientThread(const std::shared_ptr& 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 bool Send(const sockPtr& wxSock, const T& info) + template 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(); buf->dataConst = ss.view().data(); buf->len = ss.str().size(); + buf->dataType = type; return Send(wxSock, buf.get()); } diff --git a/UserInterface/HeaderControl.cxx b/UserInterface/HeaderControl.cxx index e404138..c4f9ace 100644 --- a/UserInterface/HeaderControl.cxx +++ b/UserInterface/HeaderControl.cxx @@ -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(port);