com:尝试基本的通信。

This commit is contained in:
taynpg 2025-05-11 00:00:10 +08:00
parent 113fbee659
commit 9e4230f696
12 changed files with 120 additions and 25 deletions

View File

@ -20,9 +20,9 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
include_directories(3rd) include_directories(3rd)
include_directories(Information) include_directories(Information)
add_subdirectory(ClientCore)
add_subdirectory(Util) add_subdirectory(Util)
add_subdirectory(Protocol) add_subdirectory(Protocol)
add_subdirectory(UserInterface) add_subdirectory(UserInterface)
add_subdirectory(RelayServer) add_subdirectory(RelayServer)
add_subdirectory(ClientCore)
add_subdirectory(Test) add_subdirectory(Test)

View File

@ -12,4 +12,5 @@ ClientCore.cxx
) )
add_library(ClientCore STATIC ${MSOURCES}) add_library(ClientCore STATIC ${MSOURCES})
target_link_libraries(ClientCore PRIVATE wx::base wx::core Protocol Util) target_link_libraries(ClientCore PRIVATE wx::base wx::core Protocol Util)
target_include_directories(ClientCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -29,6 +29,11 @@ void ClientCore::Disconnect()
bool ClientCore::GetOnlineList(InfoClientVec& infoClientVec) bool ClientCore::GetOnlineList(InfoClientVec& infoClientVec)
{ {
InfoCommunicate infoCommunicate;
infoCommunicate.type = MSG_TYPE_ASK_CLIENTS;
if (!Send<InfoCommunicate>(infoCommunicate)) {
return false;
}
return false; return false;
} }

View File

@ -4,7 +4,6 @@
#include <InfoClient.hpp> #include <InfoClient.hpp>
#include <InfoCommunicate.hpp> #include <InfoCommunicate.hpp>
#include <InfoDirFile.hpp> #include <InfoDirFile.hpp>
#include <Communicate.h>
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
@ -12,6 +11,7 @@
#include <wx/socket.h> #include <wx/socket.h>
#include <Util.h> #include <Util.h>
#include <Communicate.h>
class ClientCore : public wxEvtHandler class ClientCore : public wxEvtHandler
{ {
@ -44,7 +44,7 @@ private:
buf->dataConst = ss.view().data(); buf->dataConst = ss.view().data();
buf->len = ss.str().size(); buf->len = ss.str().size();
return Send(wxSock, buf.get()); return Send(buf.get());
} }
bool Send(FrameBuffer* buf); bool Send(FrameBuffer* buf);

View File

@ -1,11 +1,11 @@
#include "RelayServer.h" #include "RelayServer.h"
#include <InfoClient.hpp> #include <InfoClient.hpp>
RemoteServer::RemoteServer() RelayServer::RelayServer()
{ {
} }
bool RemoteServer::Init(const wxString& ip, unsigned short port) bool RelayServer::Init(const wxString& ip, unsigned short port)
{ {
thRun_ = true; thRun_ = true;
wxIPV4address addr; wxIPV4address addr;
@ -29,23 +29,23 @@ bool RemoteServer::Init(const wxString& ip, unsigned short port)
// wxLogInfo(wxT("Server socket created on %s:%d"), addr.IPAddress(), addr.Service()); // wxLogInfo(wxT("Server socket created on %s:%d"), addr.IPAddress(), addr.Service());
serverId_ = wxNewId(); serverId_ = wxNewId();
//server_->SetFlags(wxSOCKET_NOWAIT); // server_->SetFlags(wxSOCKET_NOWAIT);
server_->SetEventHandler(*this, serverId_); server_->SetEventHandler(*this, serverId_);
server_->SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_LOST_FLAG); server_->SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_LOST_FLAG);
server_->Notify(true); server_->Notify(true);
Bind(wxEVT_SOCKET, &RemoteServer::OnServerEvent, this, serverId_); Bind(wxEVT_SOCKET, &RelayServer::OnServerEvent, this, serverId_);
return true; return true;
} }
int RemoteServer::Run() int RelayServer::Run()
{ {
wxEventLoop loop; wxEventLoop loop;
return loop.Run(); return loop.Run();
} }
void RemoteServer::OnServerEvent(wxSocketEvent& event) void RelayServer::OnServerEvent(wxSocketEvent& event)
{ {
auto* sock = event.GetSocket(); auto* sock = event.GetSocket();
switch (event.GetSocketEvent()) { switch (event.GetSocketEvent()) {
@ -67,7 +67,7 @@ void RemoteServer::OnServerEvent(wxSocketEvent& event)
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
client->lastRecvTime = std::chrono::high_resolution_clock::now(); client->lastRecvTime = std::chrono::high_resolution_clock::now();
clients_[id] = client; clients_[id] = client;
threads_[id] = std::thread(&RemoteServer::thClientThread, this, newer, id); threads_[id] = std::thread(&RelayServer::thClientThread, this, newer, id);
break; break;
} }
case wxSOCKET_LOST: { case wxSOCKET_LOST: {
@ -90,7 +90,7 @@ void RemoteServer::OnServerEvent(wxSocketEvent& event)
} }
} }
void RemoteServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, const wxString& id) void RelayServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, const wxString& id)
{ {
wxLogMessage(wxT("Client thread started: %s"), id); wxLogMessage(wxT("Client thread started: %s"), id);
std::shared_ptr<TranClient> client = nullptr; std::shared_ptr<TranClient> client = nullptr;
@ -133,7 +133,7 @@ void RemoteServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, c
} }
} }
bool RemoteServer::Forword(const sockPtr& wxSock, FrameBuffer* buf) bool RelayServer::Forword(const sockPtr& wxSock, FrameBuffer* buf)
{ {
std::shared_ptr<TranClient> fcl = nullptr; std::shared_ptr<TranClient> fcl = nullptr;
std::shared_ptr<TranClient> tcl = nullptr; std::shared_ptr<TranClient> tcl = nullptr;
@ -161,7 +161,7 @@ bool RemoteServer::Forword(const sockPtr& wxSock, FrameBuffer* buf)
return farward; return farward;
} }
bool RemoteServer::Reply(const sockPtr& wxSock, InfoCommunicate& info) bool RelayServer::Reply(const sockPtr& wxSock, InfoCommunicate& info)
{ {
switch (info.type) { switch (info.type) {
case MSG_TYPE_ASK_CLIENTS: { case MSG_TYPE_ASK_CLIENTS: {
@ -177,7 +177,7 @@ bool RemoteServer::Reply(const sockPtr& wxSock, InfoCommunicate& info)
return false; return false;
} }
bool RemoteServer::RpyOnline(const sockPtr& wxSock) bool RelayServer::RpyOnline(const sockPtr& wxSock)
{ {
InfoClientVec infoClients; InfoClientVec infoClients;
{ {
@ -195,14 +195,14 @@ bool RemoteServer::RpyOnline(const sockPtr& wxSock)
return Send<InfoClientVec>(wxSock, infoClients); return Send<InfoClientVec>(wxSock, infoClients);
} }
bool RemoteServer::RpyForwordFailed(const sockPtr& wxSock) bool RelayServer::RpyForwordFailed(const sockPtr& wxSock)
{ {
InfoCommunicate info; InfoCommunicate info;
info.type = MSG_TYPE_FORWORD_FAILED; info.type = MSG_TYPE_FORWORD_FAILED;
return Send<InfoCommunicate>(wxSock, info); return Send<InfoCommunicate>(wxSock, info);
} }
bool RemoteServer::Send(const sockPtr& wxSock, FrameBuffer* buf) bool RelayServer::Send(const sockPtr& wxSock, FrameBuffer* buf)
{ {
if (buf == nullptr) { if (buf == nullptr) {
return false; return false;

View File

@ -28,10 +28,10 @@ struct TranClient {
std::array<char, GBUFFER_SIZE> buf; std::array<char, GBUFFER_SIZE> buf;
}; };
class RemoteServer : public wxEvtHandler class RelayServer : public wxEvtHandler
{ {
public: public:
RemoteServer(); RelayServer();
public: public:
bool Init(const wxString& ip, unsigned short port); bool Init(const wxString& ip, unsigned short port);

View File

@ -1,13 +1,18 @@
#include "RelayServer.h" #include "RelayServer.h"
#include <Util.h>
#include <wx/init.h>
#include <wx/log.h>
int main() int main(int argc, char** argv)
{ {
wxInitializer initializer; wxInitializer initializer(argc, argv);
if (!initializer.IsOk()) { if (!initializer.IsOk()) {
fprintf(stderr, "Failed to initialize the wxWidgets library, aborting."); fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
return -1; return -1;
} }
auto server_ = std::make_unique<RemoteServer>(); auto* dl = wxLog::GetActiveTarget();
dl->SetFormatter(new MLogFormatter());
auto server_ = std::make_unique<RelayServer>();
if (server_->Init("127.0.0.1", 8080)) { if (server_->Init("127.0.0.1", 8080)) {
server_->Run(); server_->Run();
} }

View File

@ -29,5 +29,5 @@ ControlManager.cxx
) )
add_executable(RelayFile ${MSOURCES}) add_executable(RelayFile ${MSOURCES})
target_link_libraries(RelayFile PRIVATE wx::base wx::core wx::aui Util) target_link_libraries(RelayFile PRIVATE wx::base wx::core wx::aui wx::net Util ClientCore Protocol)
set_target_properties(RelayFile PROPERTIES WIN32_EXECUTABLE TRUE) set_target_properties(RelayFile PROPERTIES WIN32_EXECUTABLE TRUE)

View File

@ -7,10 +7,12 @@ ControlManager::ControlManager(wxWindow* parent) : parent_(parent), header_(null
void ControlManager::Init() void ControlManager::Init()
{ {
log_ = new LogControl(parent_);
header_ = new HeaderControl(parent_); header_ = new HeaderControl(parent_);
local_ = new LocalControl(parent_); local_ = new LocalControl(parent_);
remote_ = new RemoteControl(parent_); remote_ = new RemoteControl(parent_);
task_ = new TaskControl(parent_); task_ = new TaskControl(parent_);
log_ = new LogControl(parent_);
online_ = new OnlineControl(parent_); online_ = new OnlineControl(parent_);
header_->SetLogControl(log_);
} }

View File

@ -1,5 +1,6 @@
#include "HeaderControl.h" #include "HeaderControl.h"
#include "InterfaceDefine.hpp" #include "InterfaceDefine.hpp"
#include "LogControl.h"
HeaderControl::HeaderControl(wxWindow* parent) : wxPanel(parent) HeaderControl::HeaderControl(wxWindow* parent) : wxPanel(parent)
{ {
@ -10,6 +11,11 @@ HeaderControl::~HeaderControl()
{ {
} }
void HeaderControl::SetLogControl(LogControl* logControl)
{
logControl_ = logControl;
}
void HeaderControl::Init() void HeaderControl::Init()
{ {
auto* topSizer = new wxBoxSizer(wxHORIZONTAL); auto* topSizer = new wxBoxSizer(wxHORIZONTAL);
@ -26,4 +32,25 @@ void HeaderControl::Init()
SetSizer(topSizer); SetSizer(topSizer);
Layout(); Layout();
}
Bind(wxEVT_BUTTON, &HeaderControl::OnConnect, this, btnConnect_->GetId());
}
void HeaderControl::OnConnect(wxCommandEvent& event)
{
wxString ip = textIP_->GetValue();
unsigned int port{};
if (!textPort_->GetValue().ToUInt(&port)) {
return;
}
auto uPort = static_cast<unsigned short>(port);
if (!clientCore_.Connect(ip, uPort)) {
logControl_->AddLog(wxString::Format(_("Connect to %s:%d failed."), ip, uPort));
return;
}
logControl_->AddLog(wxString::Format(_("Connect to %s:%d Success."), ip, uPort));
}
void HeaderControl::OnDisconnect(wxCommandEvent& event)
{
}

View File

@ -1,22 +1,35 @@
#ifndef HEADERCONTROL_H #ifndef HEADERCONTROL_H
#define HEADERCONTROL_H #define HEADERCONTROL_H
#include <ClientCore.h>
#include <wx/wx.h> #include <wx/wx.h>
class LogControl;
class HeaderControl : public wxPanel class HeaderControl : public wxPanel
{ {
public: public:
HeaderControl(wxWindow* parent); HeaderControl(wxWindow* parent);
~HeaderControl() override; ~HeaderControl() override;
public:
void SetLogControl(LogControl* logControl);
private: private:
void Init(); void Init();
private:
void OnConnect(wxCommandEvent& event);
void OnDisconnect(wxCommandEvent& event);
public: public:
wxTextCtrl* textIP_; wxTextCtrl* textIP_;
wxTextCtrl* textPort_; wxTextCtrl* textPort_;
wxButton* btnConnect_; wxButton* btnConnect_;
wxButton* btnDisconnect_; wxButton* btnDisconnect_;
ClientCore clientCore_;
private:
LogControl* logControl_;
}; };
#endif // HEADERCONTROL_H #endif // HEADERCONTROL_H

View File

@ -13,6 +13,48 @@ public:
TranUtil(); TranUtil();
}; };
class MLogFormatter : public wxLogFormatter
{
public:
wxString Format(wxLogLevel level, const wxString& msg, const wxLogRecordInfo& info) const override
{
wxDateTime time(info.timestampMS / 1000);
time.SetMillisecond(info.timestampMS % 1000);
auto timeStr = time.Format(wxT("%H:%M:%S.%l"));
wxString levelStr;
switch (level) {
case wxLOG_FatalError:
levelStr = "FATAL";
break;
case wxLOG_Error:
levelStr = "ERROR";
break;
case wxLOG_Warning:
levelStr = "WARN";
break;
case wxLOG_Message:
levelStr = "INFO";
break;
case wxLOG_Status:
levelStr = "STATUS";
break;
case wxLOG_Info:
levelStr = "INFO";
break;
case wxLOG_Debug:
levelStr = "DEBUG";
break;
case wxLOG_Trace:
levelStr = "TRACE";
break;
default:
levelStr = "OTHER";
break;
}
return wxString::Format("[%s][%s] %s", timeStr, levelStr, msg);
}
};
class MutBuffer class MutBuffer
{ {
public: public: