From 418d57a295b66116739072297a27f9d05549a8b4 Mon Sep 17 00:00:00 2001 From: taynpg Date: Sun, 11 May 2025 23:20:22 +0800 Subject: [PATCH] =?UTF-8?q?apr=EF=BC=9A=E9=80=BB=E8=BE=91=E5=81=A5?= =?UTF-8?q?=E5=A3=AE=E6=80=A7=E8=A1=A5=E5=85=85=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ClientCore/ClientCore.cxx | 18 +++++++++++++++++- ClientCore/ClientCore.h | 2 +- UserInterface/ControlManager.cxx | 1 + UserInterface/HeaderControl.cxx | 16 +++++++++++++++- UserInterface/HeaderControl.h | 3 +++ UserInterface/LogControl.cxx | 4 ++-- UserInterface/LogControl.h | 7 ++++++- UserInterface/OnLineControl.cxx | 28 ++++++++++++++++++++++++---- UserInterface/OnLineControl.h | 3 +++ 9 files changed, 72 insertions(+), 10 deletions(-) diff --git a/ClientCore/ClientCore.cxx b/ClientCore/ClientCore.cxx index ee257a3..73e58d8 100644 --- a/ClientCore/ClientCore.cxx +++ b/ClientCore/ClientCore.cxx @@ -26,17 +26,25 @@ bool ClientCore::Connect(const wxString& host, uint16_t port) socket_->SetFlags(wxSOCKET_BLOCK); socket_->Notify(true); + if (socket_->IsConnected()) { + return true; + } + if (!socket_->Connect(addr)) { return false; } Bind(wxEVT_SOCKET, &ClientCore::OnSocketEvent, this); + + if (recvThread_.joinable()) { + recvThread_.join(); + } recvThread_ = std::thread(&ClientCore::Recv, this); return true; } void ClientCore::Disconnect() { - socket_->Destroy(); + socket_->Close(); } wxString ClientCore::GetErr() const @@ -44,6 +52,11 @@ wxString ClientCore::GetErr() const return err_; } +bool ClientCore::IsOk() +{ + return socket_->IsConnected(); +} + void ClientCore::SetLogCallback(const std::function& callback) { logCall_ = callback; @@ -122,6 +135,9 @@ void ClientCore::Recv() while (thRun_) { socket_->Read(buf_.data(), GBUFFER_SIZE); auto len = socket_->LastCount(); + if (len == 0) { + break; + } buffer_.Push(buf_.data(), len); while (true) { auto* frame = Communicate::ParseBuffer(buffer_); diff --git a/ClientCore/ClientCore.h b/ClientCore/ClientCore.h index bc66399..1f785f3 100644 --- a/ClientCore/ClientCore.h +++ b/ClientCore/ClientCore.h @@ -27,7 +27,7 @@ public: public: wxString GetErr() const; - + bool IsOk(); void SetLogCallback(const std::function& callback); bool ReqOnline(); void ReqOnlineCallback(const std::function& callback); diff --git a/UserInterface/ControlManager.cxx b/UserInterface/ControlManager.cxx index fcf3ae5..bf2de16 100644 --- a/UserInterface/ControlManager.cxx +++ b/UserInterface/ControlManager.cxx @@ -16,6 +16,7 @@ void ControlManager::Init(std::shared_ptr& clientCore) header_->SetLogControl(log_); online_->SetLogControl(log_); + header_->SetOnlineControl(online_); clientCore->SetLogCallback([this](const wxString& msg) { log_->AddLog(msg); }); } \ No newline at end of file diff --git a/UserInterface/HeaderControl.cxx b/UserInterface/HeaderControl.cxx index 1aecd03..aeaefee 100644 --- a/UserInterface/HeaderControl.cxx +++ b/UserInterface/HeaderControl.cxx @@ -1,6 +1,7 @@ #include "HeaderControl.h" #include "InterfaceDefine.hpp" #include "LogControl.h" +#include "OnLineControl.h" #include HeaderControl::HeaderControl(wxWindow* parent, std::shared_ptr& clientCore) : wxPanel(parent), clientCore_(clientCore) @@ -17,6 +18,11 @@ void HeaderControl::SetLogControl(LogControl* logControl) logControl_ = logControl; } +void HeaderControl::SetOnlineControl(OnlineControl* onlineControl) +{ + onlineControl_ = onlineControl; +} + void HeaderControl::Init() { auto* topSizer = new wxBoxSizer(wxHORIZONTAL); @@ -35,6 +41,7 @@ void HeaderControl::Init() Layout(); Bind(wxEVT_BUTTON, &HeaderControl::OnConnect, this, btnConnect_->GetId()); + Bind(wxEVT_BUTTON, &HeaderControl::OnDisconnect, this, btnDisconnect_->GetId()); textIP_->SetValue(wxT("127.0.0.1")); textPort_->SetValue(wxT("8080")); @@ -53,9 +60,16 @@ void HeaderControl::OnConnect(wxCommandEvent& event) logControl_->AddLog(wxString::Format(_("Connect to %s:%d failed."), ip, uPort)); return; } - logControl_->AddLog(wxString::Format(_("Connect to %s:%d Success."), ip, uPort)); + logControl_->AddLog(_("Connect to %s:%d Success."), ip, uPort); + onlineControl_->SetConnectState(_("Connected")); + onlineControl_->SetConnectServer(wxString::Format(_("Connected to %s:%d"), ip, uPort)); } void HeaderControl::OnDisconnect(wxCommandEvent& event) { + clientCore_->Disconnect(); + onlineControl_->SetConnectState(_("Disconnected")); + onlineControl_->SetConnectServer(_("None")); + onlineControl_->ClearClientsShow(); + } diff --git a/UserInterface/HeaderControl.h b/UserInterface/HeaderControl.h index e2df5de..324fa24 100644 --- a/UserInterface/HeaderControl.h +++ b/UserInterface/HeaderControl.h @@ -5,6 +5,7 @@ class LogControl; class ClientCore; +class OnlineControl; class HeaderControl : public wxPanel { public: @@ -13,6 +14,7 @@ public: public: void SetLogControl(LogControl* logControl); + void SetOnlineControl(OnlineControl* onlineControl); private: void Init(); @@ -30,6 +32,7 @@ public: private: LogControl* logControl_; + OnlineControl* onlineControl_; }; #endif // HEADERCONTROL_H \ No newline at end of file diff --git a/UserInterface/LogControl.cxx b/UserInterface/LogControl.cxx index 539493c..8f53742 100644 --- a/UserInterface/LogControl.cxx +++ b/UserInterface/LogControl.cxx @@ -19,11 +19,11 @@ LogControl::~LogControl() { } -void LogControl::AddLog(const wxString& log) +void LogControl::AddLog(const wxString& msg) { std::unique_lock lock(mutex_); auto now = wxDateTime::UNow(); auto strTime = now.Format("%H:%M:%S.%l"); - listBox_->Append(strTime + wxT(" ") + log); + listBox_->Append(strTime + wxT(" ") + msg); listBox_->SetSelection(listBox_->GetCount() - 1); } diff --git a/UserInterface/LogControl.h b/UserInterface/LogControl.h index 8a89bac..1a86bae 100644 --- a/UserInterface/LogControl.h +++ b/UserInterface/LogControl.h @@ -14,7 +14,12 @@ private: void Init(); public: - void AddLog(const wxString& log); + void AddLog(const wxString& msg); + template void AddLog(const wxString& format, Args&&... args) + { + wxString msg = wxString::Format(format, std::forward(args)...); + AddLog(msg); + } public: wxListBox* listBox_; diff --git a/UserInterface/OnLineControl.cxx b/UserInterface/OnLineControl.cxx index e13f7ba..478c933 100644 --- a/UserInterface/OnLineControl.cxx +++ b/UserInterface/OnLineControl.cxx @@ -54,6 +54,22 @@ void OnlineControl::Init() Bind(wxEVT_BUTTON, &OnlineControl::OnFreshClients, this, btnFresh_->GetId()); } +void OnlineControl::SetConnectState(const wxString& state) +{ + elbCurState_->SetLabel(state); +} + +void OnlineControl::SetConnectServer(const wxString& server) +{ + elbCurPoint_->SetLabel(server); +} + +void OnlineControl::ClearClientsShow() +{ + std::unique_lock lock(mutex_); + onLineList_->Clear(); +} + void OnlineControl::InitCall() { clientCore_->ReqOnlineCallback([this](const InfoClientVec& infoClientVec) { OnFreshClientsCall(infoClientVec); }); @@ -61,12 +77,16 @@ void OnlineControl::InitCall() void OnlineControl::OnFreshClients(wxCommandEvent& event) { - InfoClientVec vec; - if (!clientCore_->ReqOnline()) { - logControl_->AddLog(_("Request Get online list failed.")); + if (!clientCore_->IsOk()) { + logControl_->AddLog(_("You have not established a connection with the server.")); return; } - logControl_->AddLog(_("Request Get online list success.")); + InfoClientVec vec; + if (!clientCore_->ReqOnline()) { + logControl_->AddLog(_("The request to obtain the client list was failed.")); + return; + } + logControl_->AddLog(_("The request to obtain the client list was successful.")); } void OnlineControl::OnFreshClientsCall(const InfoClientVec& infoClientVec) diff --git a/UserInterface/OnLineControl.h b/UserInterface/OnLineControl.h index 1ccc8a3..b2905d8 100644 --- a/UserInterface/OnLineControl.h +++ b/UserInterface/OnLineControl.h @@ -17,6 +17,9 @@ public: public: void SetHeaderControl(HeaderControl* headerControl); void SetLogControl(LogControl* logControl); + void SetConnectState(const wxString& state); + void SetConnectServer(const wxString& server); + void ClearClientsShow(); private: void Init();