frameType:数据帧类型和data区域类型统一放到协议头中。
This commit is contained in:
parent
9e4230f696
commit
af648087bf
@ -30,8 +30,7 @@ void ClientCore::Disconnect()
|
|||||||
bool ClientCore::GetOnlineList(InfoClientVec& infoClientVec)
|
bool ClientCore::GetOnlineList(InfoClientVec& infoClientVec)
|
||||||
{
|
{
|
||||||
InfoCommunicate infoCommunicate;
|
InfoCommunicate infoCommunicate;
|
||||||
infoCommunicate.type = MSG_TYPE_ASK_CLIENTS;
|
if (!Send<InfoCommunicate>(infoCommunicate, FBT_SER_MSG_ASKCLIENTS)) {
|
||||||
if (!Send<InfoCommunicate>(infoCommunicate)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -80,7 +79,7 @@ void ClientCore::UseFrame(FrameBuffer* buf)
|
|||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
switch (buf->dataType) {
|
switch (buf->dataType) {
|
||||||
case FRAME_TYPE_INFO_CLIENT: {
|
case FBT_SER_MSG_ASKCLIENTS: {
|
||||||
InfoClientVec vec;
|
InfoClientVec vec;
|
||||||
ZeroCopyInput input(buf->dataMut, buf->len);
|
ZeroCopyInput input(buf->dataMut, buf->len);
|
||||||
input.archive() >> vec;
|
input.archive() >> vec;
|
||||||
|
@ -10,8 +10,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <wx/socket.h>
|
#include <wx/socket.h>
|
||||||
|
|
||||||
#include <Util.h>
|
|
||||||
#include <Communicate.h>
|
#include <Communicate.h>
|
||||||
|
#include <Util.h>
|
||||||
|
|
||||||
class ClientCore : public wxEvtHandler
|
class ClientCore : public wxEvtHandler
|
||||||
{
|
{
|
||||||
@ -34,7 +34,7 @@ private:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void HeartBeat();
|
void HeartBeat();
|
||||||
template <typename T> bool Send(const T& info)
|
template <typename T> bool Send(const T& info, FrameBufferType type)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
cereal::BinaryOutputArchive archive(ss);
|
cereal::BinaryOutputArchive archive(ss);
|
||||||
@ -43,10 +43,11 @@ private:
|
|||||||
auto buf = std::make_shared<FrameBuffer>();
|
auto buf = std::make_shared<FrameBuffer>();
|
||||||
buf->dataConst = ss.view().data();
|
buf->dataConst = ss.view().data();
|
||||||
buf->len = ss.str().size();
|
buf->len = ss.str().size();
|
||||||
|
buf->dataType = type;
|
||||||
|
|
||||||
return Send(buf.get());
|
return Send(buf.get());
|
||||||
}
|
}
|
||||||
bool Send(FrameBuffer* buf);
|
bool Send(FrameBuffer* buf);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxString id_;
|
wxString id_;
|
||||||
|
@ -8,14 +8,7 @@
|
|||||||
|
|
||||||
constexpr int GBUFFER_SIZE = 256;
|
constexpr int GBUFFER_SIZE = 256;
|
||||||
|
|
||||||
enum MessageType {
|
|
||||||
MSG_TYPE_ASK_CLIENTS = 1,
|
|
||||||
MSG_TYPE_FORWORD_FAILED,
|
|
||||||
MSG_TYPE_HEARTBEAT,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct InfoCommunicate {
|
struct InfoCommunicate {
|
||||||
MessageType type;
|
|
||||||
std::string fromID;
|
std::string fromID;
|
||||||
std::string toID;
|
std::string toID;
|
||||||
std::string UUID;
|
std::string UUID;
|
||||||
@ -23,7 +16,7 @@ struct InfoCommunicate {
|
|||||||
uint8_t mark{};
|
uint8_t mark{};
|
||||||
template <class Archive> void serialize(Archive& archive)
|
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));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4,12 +4,15 @@
|
|||||||
#include <Util.h>
|
#include <Util.h>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
// 这里规定,前 30 个内容(包含)为与服务端交流用。
|
||||||
|
// 大于 30 的内容仅做转发。
|
||||||
enum FrameBufferType : uint16_t {
|
enum FrameBufferType : uint16_t {
|
||||||
FRAME_TYPE_MSG_FILEDATA,
|
FBT_SER_MSG_ASKCLIENTS = 0,
|
||||||
FRAME_TYPE_INFO_COMMUNICATE,
|
FBT_SER_MSG_YOURID,
|
||||||
FRAME_TYPE_INFO_CLIENT,
|
FBT_SER_MSG_RESPONSE,
|
||||||
FRAME_TYPE_INFO_DIRFILE,
|
FBT_CLI_BIN_FILEDATA = 31,
|
||||||
FRAME_TYPE_MSG_YOURID
|
FBT_CLI_MSG_COMMUNICATE,
|
||||||
|
FBT_CLI_INFO_DIRFILE
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FrameBuffer {
|
struct FrameBuffer {
|
||||||
|
@ -101,7 +101,6 @@ void RelayServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, co
|
|||||||
}
|
}
|
||||||
|
|
||||||
client->wxSock->SetFlags(wxSOCKET_BLOCK);
|
client->wxSock->SetFlags(wxSOCKET_BLOCK);
|
||||||
InfoCommunicate info;
|
|
||||||
while (thRun_) {
|
while (thRun_) {
|
||||||
wxSock->Read(client->buf.data(), GBUFFER_SIZE);
|
wxSock->Read(client->buf.data(), GBUFFER_SIZE);
|
||||||
auto br = wxSock->LastCount();
|
auto br = wxSock->LastCount();
|
||||||
@ -118,12 +117,8 @@ void RelayServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, co
|
|||||||
if (!frame) {
|
if (!frame) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (frame->dataType != 0) {
|
if (frame->dataType <= 30) {
|
||||||
std::stringstream ss;
|
ReplyRequest(client->wxSock, frame);
|
||||||
ss.write(frame->dataMut, frame->len);
|
|
||||||
cereal::BinaryInputArchive inputArchive(ss);
|
|
||||||
inputArchive(info);
|
|
||||||
Reply(client->wxSock, info);
|
|
||||||
delete frame;
|
delete frame;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -161,10 +156,10 @@ bool RelayServer::Forword(const sockPtr& wxSock, FrameBuffer* buf)
|
|||||||
return farward;
|
return farward;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RelayServer::Reply(const sockPtr& wxSock, InfoCommunicate& info)
|
bool RelayServer::Reply(const sockPtr& wxSock, InfoCommunicate& info, FrameBufferType type)
|
||||||
{
|
{
|
||||||
switch (info.type) {
|
switch (type) {
|
||||||
case MSG_TYPE_ASK_CLIENTS: {
|
case FBT_SER_MSG_ASKCLIENTS: {
|
||||||
std::swap(info.fromID, info.toID);
|
std::swap(info.fromID, info.toID);
|
||||||
info.fromID = strID_;
|
info.fromID = strID_;
|
||||||
RpyOnline(wxSock);
|
RpyOnline(wxSock);
|
||||||
@ -173,10 +168,27 @@ bool RelayServer::Reply(const sockPtr& wxSock, InfoCommunicate& info)
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
wxLogWarning(_("Unknow message type: %d"), info.type);
|
wxLogWarning(_("Unknow message type: %d"), type);
|
||||||
return false;
|
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)
|
bool RelayServer::RpyOnline(const sockPtr& wxSock)
|
||||||
{
|
{
|
||||||
InfoClientVec infoClients;
|
InfoClientVec infoClients;
|
||||||
@ -192,14 +204,13 @@ bool RelayServer::RpyOnline(const sockPtr& wxSock)
|
|||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
cereal::BinaryOutputArchive archive(ss);
|
cereal::BinaryOutputArchive archive(ss);
|
||||||
archive(infoClients);
|
archive(infoClients);
|
||||||
return Send<InfoClientVec>(wxSock, infoClients);
|
return Send<InfoClientVec>(wxSock, infoClients, FBT_SER_MSG_ASKCLIENTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RelayServer::RpyForwordFailed(const sockPtr& wxSock)
|
bool RelayServer::RpyForwordFailed(const sockPtr& wxSock)
|
||||||
{
|
{
|
||||||
InfoCommunicate info;
|
InfoCommunicate info;
|
||||||
info.type = MSG_TYPE_FORWORD_FAILED;
|
return Send<InfoCommunicate>(wxSock, info, FBT_SER_MSG_RESPONSE);
|
||||||
return Send<InfoCommunicate>(wxSock, info);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RelayServer::Send(const sockPtr& wxSock, FrameBuffer* buf)
|
bool RelayServer::Send(const sockPtr& wxSock, FrameBuffer* buf)
|
||||||
|
@ -41,14 +41,15 @@ private:
|
|||||||
void OnServerEvent(wxSocketEvent& event);
|
void OnServerEvent(wxSocketEvent& event);
|
||||||
void thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, const wxString& id);
|
void thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, const wxString& id);
|
||||||
bool Forword(const sockPtr& wxSock, FrameBuffer* buf);
|
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:
|
private:
|
||||||
bool RpyOnline(const sockPtr& wxSock);
|
bool RpyOnline(const sockPtr& wxSock);
|
||||||
bool RpyForwordFailed(const sockPtr& wxSock);
|
bool RpyForwordFailed(const sockPtr& wxSock);
|
||||||
|
|
||||||
private:
|
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;
|
std::stringstream ss;
|
||||||
cereal::BinaryOutputArchive archive(ss);
|
cereal::BinaryOutputArchive archive(ss);
|
||||||
@ -57,6 +58,7 @@ private:
|
|||||||
auto buf = std::make_shared<FrameBuffer>();
|
auto buf = std::make_shared<FrameBuffer>();
|
||||||
buf->dataConst = ss.view().data();
|
buf->dataConst = ss.view().data();
|
||||||
buf->len = ss.str().size();
|
buf->len = ss.str().size();
|
||||||
|
buf->dataType = type;
|
||||||
|
|
||||||
return Send(wxSock, buf.get());
|
return Send(wxSock, buf.get());
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,7 @@ void HeaderControl::OnConnect(wxCommandEvent& event)
|
|||||||
wxString ip = textIP_->GetValue();
|
wxString ip = textIP_->GetValue();
|
||||||
unsigned int port{};
|
unsigned int port{};
|
||||||
if (!textPort_->GetValue().ToUInt(&port)) {
|
if (!textPort_->GetValue().ToUInt(&port)) {
|
||||||
|
logControl_->AddLog(_("Invalid port."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto uPort = static_cast<unsigned short>(port);
|
auto uPort = static_cast<unsigned short>(port);
|
||||||
|
Reference in New Issue
Block a user