apr:逻辑健壮性补充。

This commit is contained in:
taynpg 2025-05-11 23:20:22 +08:00
parent abca5ffa4f
commit 418d57a295
9 changed files with 72 additions and 10 deletions

View File

@ -26,17 +26,25 @@ bool ClientCore::Connect(const wxString& host, uint16_t port)
socket_->SetFlags(wxSOCKET_BLOCK); socket_->SetFlags(wxSOCKET_BLOCK);
socket_->Notify(true); socket_->Notify(true);
if (socket_->IsConnected()) {
return true;
}
if (!socket_->Connect(addr)) { if (!socket_->Connect(addr)) {
return false; return false;
} }
Bind(wxEVT_SOCKET, &ClientCore::OnSocketEvent, this); Bind(wxEVT_SOCKET, &ClientCore::OnSocketEvent, this);
if (recvThread_.joinable()) {
recvThread_.join();
}
recvThread_ = std::thread(&ClientCore::Recv, this); recvThread_ = std::thread(&ClientCore::Recv, this);
return true; return true;
} }
void ClientCore::Disconnect() void ClientCore::Disconnect()
{ {
socket_->Destroy(); socket_->Close();
} }
wxString ClientCore::GetErr() const wxString ClientCore::GetErr() const
@ -44,6 +52,11 @@ wxString ClientCore::GetErr() const
return err_; return err_;
} }
bool ClientCore::IsOk()
{
return socket_->IsConnected();
}
void ClientCore::SetLogCallback(const std::function<void(const wxString&)>& callback) void ClientCore::SetLogCallback(const std::function<void(const wxString&)>& callback)
{ {
logCall_ = callback; logCall_ = callback;
@ -122,6 +135,9 @@ void ClientCore::Recv()
while (thRun_) { while (thRun_) {
socket_->Read(buf_.data(), GBUFFER_SIZE); socket_->Read(buf_.data(), GBUFFER_SIZE);
auto len = socket_->LastCount(); auto len = socket_->LastCount();
if (len == 0) {
break;
}
buffer_.Push(buf_.data(), len); buffer_.Push(buf_.data(), len);
while (true) { while (true) {
auto* frame = Communicate::ParseBuffer(buffer_); auto* frame = Communicate::ParseBuffer(buffer_);

View File

@ -27,7 +27,7 @@ public:
public: public:
wxString GetErr() const; wxString GetErr() const;
bool IsOk();
void SetLogCallback(const std::function<void(const wxString&)>& callback); void SetLogCallback(const std::function<void(const wxString&)>& callback);
bool ReqOnline(); bool ReqOnline();
void ReqOnlineCallback(const std::function<void(const InfoClientVec&)>& callback); void ReqOnlineCallback(const std::function<void(const InfoClientVec&)>& callback);

View File

@ -16,6 +16,7 @@ void ControlManager::Init(std::shared_ptr<ClientCore>& clientCore)
header_->SetLogControl(log_); header_->SetLogControl(log_);
online_->SetLogControl(log_); online_->SetLogControl(log_);
header_->SetOnlineControl(online_);
clientCore->SetLogCallback([this](const wxString& msg) { log_->AddLog(msg); }); clientCore->SetLogCallback([this](const wxString& msg) { log_->AddLog(msg); });
} }

View File

@ -1,6 +1,7 @@
#include "HeaderControl.h" #include "HeaderControl.h"
#include "InterfaceDefine.hpp" #include "InterfaceDefine.hpp"
#include "LogControl.h" #include "LogControl.h"
#include "OnLineControl.h"
#include <ClientCore.h> #include <ClientCore.h>
HeaderControl::HeaderControl(wxWindow* parent, std::shared_ptr<ClientCore>& clientCore) : wxPanel(parent), clientCore_(clientCore) HeaderControl::HeaderControl(wxWindow* parent, std::shared_ptr<ClientCore>& clientCore) : wxPanel(parent), clientCore_(clientCore)
@ -17,6 +18,11 @@ void HeaderControl::SetLogControl(LogControl* logControl)
logControl_ = logControl; logControl_ = logControl;
} }
void HeaderControl::SetOnlineControl(OnlineControl* onlineControl)
{
onlineControl_ = onlineControl;
}
void HeaderControl::Init() void HeaderControl::Init()
{ {
auto* topSizer = new wxBoxSizer(wxHORIZONTAL); auto* topSizer = new wxBoxSizer(wxHORIZONTAL);
@ -35,6 +41,7 @@ void HeaderControl::Init()
Layout(); Layout();
Bind(wxEVT_BUTTON, &HeaderControl::OnConnect, this, btnConnect_->GetId()); Bind(wxEVT_BUTTON, &HeaderControl::OnConnect, this, btnConnect_->GetId());
Bind(wxEVT_BUTTON, &HeaderControl::OnDisconnect, this, btnDisconnect_->GetId());
textIP_->SetValue(wxT("127.0.0.1")); textIP_->SetValue(wxT("127.0.0.1"));
textPort_->SetValue(wxT("8080")); textPort_->SetValue(wxT("8080"));
@ -53,9 +60,16 @@ void HeaderControl::OnConnect(wxCommandEvent& event)
logControl_->AddLog(wxString::Format(_("Connect to %s:%d failed."), ip, uPort)); logControl_->AddLog(wxString::Format(_("Connect to %s:%d failed."), ip, uPort));
return; 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) void HeaderControl::OnDisconnect(wxCommandEvent& event)
{ {
clientCore_->Disconnect();
onlineControl_->SetConnectState(_("Disconnected"));
onlineControl_->SetConnectServer(_("None"));
onlineControl_->ClearClientsShow();
} }

View File

@ -5,6 +5,7 @@
class LogControl; class LogControl;
class ClientCore; class ClientCore;
class OnlineControl;
class HeaderControl : public wxPanel class HeaderControl : public wxPanel
{ {
public: public:
@ -13,6 +14,7 @@ public:
public: public:
void SetLogControl(LogControl* logControl); void SetLogControl(LogControl* logControl);
void SetOnlineControl(OnlineControl* onlineControl);
private: private:
void Init(); void Init();
@ -30,6 +32,7 @@ public:
private: private:
LogControl* logControl_; LogControl* logControl_;
OnlineControl* onlineControl_;
}; };
#endif // HEADERCONTROL_H #endif // HEADERCONTROL_H

View File

@ -19,11 +19,11 @@ LogControl::~LogControl()
{ {
} }
void LogControl::AddLog(const wxString& log) void LogControl::AddLog(const wxString& msg)
{ {
std::unique_lock<std::mutex> lock(mutex_); std::unique_lock<std::mutex> lock(mutex_);
auto now = wxDateTime::UNow(); auto now = wxDateTime::UNow();
auto strTime = now.Format("%H:%M:%S.%l"); auto strTime = now.Format("%H:%M:%S.%l");
listBox_->Append(strTime + wxT(" ") + log); listBox_->Append(strTime + wxT(" ") + msg);
listBox_->SetSelection(listBox_->GetCount() - 1); listBox_->SetSelection(listBox_->GetCount() - 1);
} }

View File

@ -14,7 +14,12 @@ private:
void Init(); void Init();
public: public:
void AddLog(const wxString& log); void AddLog(const wxString& msg);
template <typename... Args> void AddLog(const wxString& format, Args&&... args)
{
wxString msg = wxString::Format(format, std::forward<Args>(args)...);
AddLog(msg);
}
public: public:
wxListBox* listBox_; wxListBox* listBox_;

View File

@ -54,6 +54,22 @@ void OnlineControl::Init()
Bind(wxEVT_BUTTON, &OnlineControl::OnFreshClients, this, btnFresh_->GetId()); 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<std::mutex> lock(mutex_);
onLineList_->Clear();
}
void OnlineControl::InitCall() void OnlineControl::InitCall()
{ {
clientCore_->ReqOnlineCallback([this](const InfoClientVec& infoClientVec) { OnFreshClientsCall(infoClientVec); }); clientCore_->ReqOnlineCallback([this](const InfoClientVec& infoClientVec) { OnFreshClientsCall(infoClientVec); });
@ -61,12 +77,16 @@ void OnlineControl::InitCall()
void OnlineControl::OnFreshClients(wxCommandEvent& event) void OnlineControl::OnFreshClients(wxCommandEvent& event)
{ {
InfoClientVec vec; if (!clientCore_->IsOk()) {
if (!clientCore_->ReqOnline()) { logControl_->AddLog(_("You have not established a connection with the server."));
logControl_->AddLog(_("Request Get online list failed."));
return; 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) void OnlineControl::OnFreshClientsCall(const InfoClientVec& infoClientVec)

View File

@ -17,6 +17,9 @@ public:
public: public:
void SetHeaderControl(HeaderControl* headerControl); void SetHeaderControl(HeaderControl* headerControl);
void SetLogControl(LogControl* logControl); void SetLogControl(LogControl* logControl);
void SetConnectState(const wxString& state);
void SetConnectServer(const wxString& server);
void ClearClientsShow();
private: private:
void Init(); void Init();