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_->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<void(const wxString&)>& 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_);

View File

@ -27,7 +27,7 @@ public:
public:
wxString GetErr() const;
bool IsOk();
void SetLogCallback(const std::function<void(const wxString&)>& callback);
bool ReqOnline();
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_);
online_->SetLogControl(log_);
header_->SetOnlineControl(online_);
clientCore->SetLogCallback([this](const wxString& msg) { log_->AddLog(msg); });
}

View File

@ -1,6 +1,7 @@
#include "HeaderControl.h"
#include "InterfaceDefine.hpp"
#include "LogControl.h"
#include "OnLineControl.h"
#include <ClientCore.h>
HeaderControl::HeaderControl(wxWindow* parent, std::shared_ptr<ClientCore>& 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();
}

View File

@ -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

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_);
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);
}

View File

@ -14,7 +14,12 @@ private:
void Init();
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:
wxListBox* listBox_;

View File

@ -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<std::mutex> 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)

View File

@ -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();