RelayFile/ClientCore/ClientCore.cxx

117 lines
2.5 KiB
C++
Raw Permalink 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-06 23:21:10 +08:00
socket_ = std::make_shared<wxSocketClient>();
}
bool ClientCore::Connect(const wxString& host, uint16_t port)
{
wxIPV4address addr;
addr.Hostname(host);
addr.Service(port);
socket_->SetEventHandler(*this, wxID_ANY);
2025-05-10 21:43:25 +08:00
socket_->SetNotify(wxSOCKET_INPUT | wxSOCKET_LOST_FLAG);
2025-05-06 23:21:10 +08:00
socket_->Notify(true);
2025-05-10 21:43:25 +08:00
socket_->SetFlags(wxSOCKET_BLOCK);
2025-05-06 23:21:10 +08:00
if (!socket_->Connect(addr)) {
return false;
}
2025-05-10 21:43:25 +08:00
return true;
2025-05-06 23:21:10 +08:00
}
void ClientCore::Disconnect()
{
}
2025-05-08 21:13:36 +08:00
bool ClientCore::GetOnlineList(InfoClientVec& infoClientVec)
{
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-08 21:13:36 +08:00
return false;
}
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-06 23:21:10 +08:00
switch (event.GetSocketEvent()) {
case wxSOCKET_CONNECTION: {
2025-05-10 21:43:25 +08:00
wxLogMessage(_("Client connected."));
2025-05-06 23:21:10 +08:00
break;
}
case wxSOCKET_INPUT: {
2025-05-10 21:43:25 +08:00
sock->Read(buf_.data(), GBUFFER_SIZE);
auto size = sock->LastCount();
if (size > 0) {
buffer_.Push(buf_.data(), size);
while (thRun_) {
auto* frame = Communicate::ParseBuffer(buffer_);
if (!frame) {
break;
}
UseFrame(frame);
delete frame;
}
} else {
wxLogError(_("Read error: %s"), sock->LastError());
}
2025-05-06 23:21:10 +08:00
break;
}
case wxSOCKET_LOST: {
break;
}
default:
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;
break;
}
default:
break;
}
}
void ClientCore::HeartBeat()
{
}
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;
}