RelayFile/ClientCore/ClientCore.cxx

157 lines
3.4 KiB
C++
Raw Normal View History

2025-05-05 23:22:43 +08:00
#include "ClientCore.h"
2025-05-10 21:43:25 +08:00
#include <InfoEnhance.hpp>
2025-05-05 23:22:43 +08:00
ClientCore::ClientCore()
{
2025-05-11 21:27:59 +08:00
socket_ = new wxSocketClient();
}
ClientCore::~ClientCore()
{
Disconnect();
thRun_ = false;
if (recvThread_.joinable()) {
recvThread_.join();
}
2025-05-06 23:21:10 +08:00
}
bool ClientCore::Connect(const wxString& host, uint16_t port)
{
wxIPV4address addr;
addr.Hostname(host);
addr.Service(port);
socket_->SetEventHandler(*this, wxID_ANY);
2025-05-11 21:27:59 +08:00
socket_->SetNotify(wxSOCKET_LOST_FLAG);
2025-05-10 21:43:25 +08:00
socket_->SetFlags(wxSOCKET_BLOCK);
2025-05-11 21:27:59 +08:00
socket_->Notify(true);
2025-05-06 23:21:10 +08:00
if (!socket_->Connect(addr)) {
return false;
}
2025-05-11 21:27:59 +08:00
Bind(wxEVT_SOCKET, &ClientCore::OnSocketEvent, this);
recvThread_ = std::thread(&ClientCore::Recv, this);
2025-05-10 21:43:25 +08:00
return true;
2025-05-06 23:21:10 +08:00
}
void ClientCore::Disconnect()
{
2025-05-11 21:27:59 +08:00
socket_->Destroy();
}
wxString ClientCore::GetErr() const
{
return err_;
2025-05-06 23:21:10 +08:00
}
2025-05-11 21:27:59 +08:00
void ClientCore::SetLogCallback(const std::function<void(const wxString&)>& callback)
{
logCall_ = callback;
}
bool ClientCore::ReqOnline()
2025-05-08 21:13:36 +08:00
{
2025-05-11 00:00:10 +08:00
InfoCommunicate infoCommunicate;
if (!Send<InfoCommunicate>(infoCommunicate, FBT_SER_MSG_ASKCLIENTS)) {
2025-05-11 00:00:10 +08:00
return false;
}
2025-05-11 21:27:59 +08:00
return true;
}
void ClientCore::ReqOnlineCallback(const std::function<void(const InfoClientVec&)>& callback)
{
onlineCallback_ = callback;
2025-05-08 21:13:36 +08:00
}
bool ClientCore::AskDirectory(const wxString& id, const wxString& path, DirFileInfoVec& dirInfoVec)
{
return false;
}
2025-05-06 23:21:10 +08:00
void ClientCore::OnSocketEvent(wxSocketEvent& event)
{
2025-05-10 21:43:25 +08:00
auto* sock = event.GetSocket();
2025-05-11 21:27:59 +08:00
auto type = event.GetSocketEvent();
switch (type) {
2025-05-06 23:21:10 +08:00
case wxSOCKET_LOST: {
2025-05-11 21:27:59 +08:00
logCall_(wxString::Format(_("Lost connection to server.")));
2025-05-06 23:21:10 +08:00
break;
}
default:
2025-05-11 21:27:59 +08:00
logCall_(wxString::Format(_T("No Handled Type: %d."), static_cast<int>(type)));
2025-05-06 23:21:10 +08:00
break;
}
}
2025-05-10 21:43:25 +08:00
void ClientCore::UseFrame(FrameBuffer* buf)
{
std::stringstream ss;
switch (buf->dataType) {
case FBT_SER_MSG_ASKCLIENTS: {
2025-05-10 21:43:25 +08:00
InfoClientVec vec;
ZeroCopyInput input(buf->dataMut, buf->len);
input.archive() >> vec;
2025-05-11 21:27:59 +08:00
if (onlineCallback_) {
onlineCallback_(vec);
}
break;
}
case FBT_SER_MSG_YOURID: {
InfoCommunicate info;
ZeroCopyInput input(buf->dataMut, buf->len);
input.archive() >> info;
id_ = wxString::FromUTF8(info.data);
logCall_(wxString::Format(_("Your id is %s."), id_));
break;
}
case FBT_SER_MSG_RESPONSE: {
InfoCommunicate info;
2025-05-10 21:43:25 +08:00
break;
}
default:
break;
}
}
void ClientCore::HeartBeat()
{
}
2025-05-11 21:27:59 +08:00
void ClientCore::Recv()
{
while (thRun_) {
socket_->Read(buf_.data(), GBUFFER_SIZE);
auto len = socket_->LastCount();
buffer_.Push(buf_.data(), len);
while (true) {
auto* frame = Communicate::ParseBuffer(buffer_);
if (frame == nullptr) {
break;
}
UseFrame(frame);
delete frame;
}
}
}
2025-05-10 21:43:25 +08:00
bool ClientCore::Send(FrameBuffer* buf)
{
if (buf == nullptr) {
return false;
}
char* od = nullptr;
int odLen = 0;
if (!Communicate::PackBuffer(buf, &od, odLen)) {
return false;
}
socket_->Write(od, odLen);
if (socket_->Error()) {
delete[] od;
wxLogError(wxT("Send error: %s"), socket_->LastError());
return false;
}
delete[] od;
return true;
}