com:尝试基本的通信。
This commit is contained in:
parent
113fbee659
commit
9e4230f696
@ -20,9 +20,9 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
|
||||
include_directories(3rd)
|
||||
include_directories(Information)
|
||||
|
||||
add_subdirectory(ClientCore)
|
||||
add_subdirectory(Util)
|
||||
add_subdirectory(Protocol)
|
||||
add_subdirectory(UserInterface)
|
||||
add_subdirectory(RelayServer)
|
||||
add_subdirectory(ClientCore)
|
||||
add_subdirectory(Test)
|
@ -12,4 +12,5 @@ ClientCore.cxx
|
||||
)
|
||||
|
||||
add_library(ClientCore STATIC ${MSOURCES})
|
||||
target_link_libraries(ClientCore PRIVATE wx::base wx::core Protocol Util)
|
||||
target_link_libraries(ClientCore PRIVATE wx::base wx::core Protocol Util)
|
||||
target_include_directories(ClientCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
@ -29,6 +29,11 @@ void ClientCore::Disconnect()
|
||||
|
||||
bool ClientCore::GetOnlineList(InfoClientVec& infoClientVec)
|
||||
{
|
||||
InfoCommunicate infoCommunicate;
|
||||
infoCommunicate.type = MSG_TYPE_ASK_CLIENTS;
|
||||
if (!Send<InfoCommunicate>(infoCommunicate)) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include <InfoClient.hpp>
|
||||
#include <InfoCommunicate.hpp>
|
||||
#include <InfoDirFile.hpp>
|
||||
#include <Communicate.h>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
@ -12,6 +11,7 @@
|
||||
#include <wx/socket.h>
|
||||
|
||||
#include <Util.h>
|
||||
#include <Communicate.h>
|
||||
|
||||
class ClientCore : public wxEvtHandler
|
||||
{
|
||||
@ -44,7 +44,7 @@ private:
|
||||
buf->dataConst = ss.view().data();
|
||||
buf->len = ss.str().size();
|
||||
|
||||
return Send(wxSock, buf.get());
|
||||
return Send(buf.get());
|
||||
}
|
||||
bool Send(FrameBuffer* buf);
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
#include "RelayServer.h"
|
||||
#include <InfoClient.hpp>
|
||||
|
||||
RemoteServer::RemoteServer()
|
||||
RelayServer::RelayServer()
|
||||
{
|
||||
}
|
||||
|
||||
bool RemoteServer::Init(const wxString& ip, unsigned short port)
|
||||
bool RelayServer::Init(const wxString& ip, unsigned short port)
|
||||
{
|
||||
thRun_ = true;
|
||||
wxIPV4address addr;
|
||||
@ -29,23 +29,23 @@ bool RemoteServer::Init(const wxString& ip, unsigned short port)
|
||||
// wxLogInfo(wxT("Server socket created on %s:%d"), addr.IPAddress(), addr.Service());
|
||||
|
||||
serverId_ = wxNewId();
|
||||
//server_->SetFlags(wxSOCKET_NOWAIT);
|
||||
// server_->SetFlags(wxSOCKET_NOWAIT);
|
||||
server_->SetEventHandler(*this, serverId_);
|
||||
|
||||
server_->SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_LOST_FLAG);
|
||||
server_->Notify(true);
|
||||
Bind(wxEVT_SOCKET, &RemoteServer::OnServerEvent, this, serverId_);
|
||||
Bind(wxEVT_SOCKET, &RelayServer::OnServerEvent, this, serverId_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int RemoteServer::Run()
|
||||
int RelayServer::Run()
|
||||
{
|
||||
wxEventLoop loop;
|
||||
return loop.Run();
|
||||
}
|
||||
|
||||
void RemoteServer::OnServerEvent(wxSocketEvent& event)
|
||||
void RelayServer::OnServerEvent(wxSocketEvent& event)
|
||||
{
|
||||
auto* sock = event.GetSocket();
|
||||
switch (event.GetSocketEvent()) {
|
||||
@ -67,7 +67,7 @@ void RemoteServer::OnServerEvent(wxSocketEvent& event)
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
client->lastRecvTime = std::chrono::high_resolution_clock::now();
|
||||
clients_[id] = client;
|
||||
threads_[id] = std::thread(&RemoteServer::thClientThread, this, newer, id);
|
||||
threads_[id] = std::thread(&RelayServer::thClientThread, this, newer, id);
|
||||
break;
|
||||
}
|
||||
case wxSOCKET_LOST: {
|
||||
@ -90,7 +90,7 @@ void RemoteServer::OnServerEvent(wxSocketEvent& event)
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, const wxString& id)
|
||||
void RelayServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, const wxString& id)
|
||||
{
|
||||
wxLogMessage(wxT("Client thread started: %s"), id);
|
||||
std::shared_ptr<TranClient> client = nullptr;
|
||||
@ -133,7 +133,7 @@ void RemoteServer::thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, c
|
||||
}
|
||||
}
|
||||
|
||||
bool RemoteServer::Forword(const sockPtr& wxSock, FrameBuffer* buf)
|
||||
bool RelayServer::Forword(const sockPtr& wxSock, FrameBuffer* buf)
|
||||
{
|
||||
std::shared_ptr<TranClient> fcl = nullptr;
|
||||
std::shared_ptr<TranClient> tcl = nullptr;
|
||||
@ -161,7 +161,7 @@ bool RemoteServer::Forword(const sockPtr& wxSock, FrameBuffer* buf)
|
||||
return farward;
|
||||
}
|
||||
|
||||
bool RemoteServer::Reply(const sockPtr& wxSock, InfoCommunicate& info)
|
||||
bool RelayServer::Reply(const sockPtr& wxSock, InfoCommunicate& info)
|
||||
{
|
||||
switch (info.type) {
|
||||
case MSG_TYPE_ASK_CLIENTS: {
|
||||
@ -177,7 +177,7 @@ bool RemoteServer::Reply(const sockPtr& wxSock, InfoCommunicate& info)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RemoteServer::RpyOnline(const sockPtr& wxSock)
|
||||
bool RelayServer::RpyOnline(const sockPtr& wxSock)
|
||||
{
|
||||
InfoClientVec infoClients;
|
||||
{
|
||||
@ -195,14 +195,14 @@ bool RemoteServer::RpyOnline(const sockPtr& wxSock)
|
||||
return Send<InfoClientVec>(wxSock, infoClients);
|
||||
}
|
||||
|
||||
bool RemoteServer::RpyForwordFailed(const sockPtr& wxSock)
|
||||
bool RelayServer::RpyForwordFailed(const sockPtr& wxSock)
|
||||
{
|
||||
InfoCommunicate info;
|
||||
info.type = MSG_TYPE_FORWORD_FAILED;
|
||||
return Send<InfoCommunicate>(wxSock, info);
|
||||
}
|
||||
|
||||
bool RemoteServer::Send(const sockPtr& wxSock, FrameBuffer* buf)
|
||||
bool RelayServer::Send(const sockPtr& wxSock, FrameBuffer* buf)
|
||||
{
|
||||
if (buf == nullptr) {
|
||||
return false;
|
||||
|
@ -28,10 +28,10 @@ struct TranClient {
|
||||
std::array<char, GBUFFER_SIZE> buf;
|
||||
};
|
||||
|
||||
class RemoteServer : public wxEvtHandler
|
||||
class RelayServer : public wxEvtHandler
|
||||
{
|
||||
public:
|
||||
RemoteServer();
|
||||
RelayServer();
|
||||
|
||||
public:
|
||||
bool Init(const wxString& ip, unsigned short port);
|
||||
|
@ -1,13 +1,18 @@
|
||||
#include "RelayServer.h"
|
||||
#include <Util.h>
|
||||
#include <wx/init.h>
|
||||
#include <wx/log.h>
|
||||
|
||||
int main()
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
wxInitializer initializer;
|
||||
wxInitializer initializer(argc, argv);
|
||||
if (!initializer.IsOk()) {
|
||||
fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
|
||||
return -1;
|
||||
}
|
||||
auto server_ = std::make_unique<RemoteServer>();
|
||||
auto* dl = wxLog::GetActiveTarget();
|
||||
dl->SetFormatter(new MLogFormatter());
|
||||
auto server_ = std::make_unique<RelayServer>();
|
||||
if (server_->Init("127.0.0.1", 8080)) {
|
||||
server_->Run();
|
||||
}
|
||||
|
@ -29,5 +29,5 @@ ControlManager.cxx
|
||||
)
|
||||
|
||||
add_executable(RelayFile ${MSOURCES})
|
||||
target_link_libraries(RelayFile PRIVATE wx::base wx::core wx::aui Util)
|
||||
target_link_libraries(RelayFile PRIVATE wx::base wx::core wx::aui wx::net Util ClientCore Protocol)
|
||||
set_target_properties(RelayFile PROPERTIES WIN32_EXECUTABLE TRUE)
|
@ -7,10 +7,12 @@ ControlManager::ControlManager(wxWindow* parent) : parent_(parent), header_(null
|
||||
|
||||
void ControlManager::Init()
|
||||
{
|
||||
log_ = new LogControl(parent_);
|
||||
header_ = new HeaderControl(parent_);
|
||||
local_ = new LocalControl(parent_);
|
||||
remote_ = new RemoteControl(parent_);
|
||||
task_ = new TaskControl(parent_);
|
||||
log_ = new LogControl(parent_);
|
||||
online_ = new OnlineControl(parent_);
|
||||
|
||||
header_->SetLogControl(log_);
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
#include "HeaderControl.h"
|
||||
#include "InterfaceDefine.hpp"
|
||||
#include "LogControl.h"
|
||||
|
||||
HeaderControl::HeaderControl(wxWindow* parent) : wxPanel(parent)
|
||||
{
|
||||
@ -10,6 +11,11 @@ HeaderControl::~HeaderControl()
|
||||
{
|
||||
}
|
||||
|
||||
void HeaderControl::SetLogControl(LogControl* logControl)
|
||||
{
|
||||
logControl_ = logControl;
|
||||
}
|
||||
|
||||
void HeaderControl::Init()
|
||||
{
|
||||
auto* topSizer = new wxBoxSizer(wxHORIZONTAL);
|
||||
@ -26,4 +32,25 @@ void HeaderControl::Init()
|
||||
|
||||
SetSizer(topSizer);
|
||||
Layout();
|
||||
}
|
||||
|
||||
Bind(wxEVT_BUTTON, &HeaderControl::OnConnect, this, btnConnect_->GetId());
|
||||
}
|
||||
|
||||
void HeaderControl::OnConnect(wxCommandEvent& event)
|
||||
{
|
||||
wxString ip = textIP_->GetValue();
|
||||
unsigned int port{};
|
||||
if (!textPort_->GetValue().ToUInt(&port)) {
|
||||
return;
|
||||
}
|
||||
auto uPort = static_cast<unsigned short>(port);
|
||||
if (!clientCore_.Connect(ip, uPort)) {
|
||||
logControl_->AddLog(wxString::Format(_("Connect to %s:%d failed."), ip, uPort));
|
||||
return;
|
||||
}
|
||||
logControl_->AddLog(wxString::Format(_("Connect to %s:%d Success."), ip, uPort));
|
||||
}
|
||||
|
||||
void HeaderControl::OnDisconnect(wxCommandEvent& event)
|
||||
{
|
||||
}
|
||||
|
@ -1,22 +1,35 @@
|
||||
#ifndef HEADERCONTROL_H
|
||||
#define HEADERCONTROL_H
|
||||
|
||||
#include <ClientCore.h>
|
||||
#include <wx/wx.h>
|
||||
|
||||
class LogControl;
|
||||
class HeaderControl : public wxPanel
|
||||
{
|
||||
public:
|
||||
HeaderControl(wxWindow* parent);
|
||||
~HeaderControl() override;
|
||||
|
||||
public:
|
||||
void SetLogControl(LogControl* logControl);
|
||||
|
||||
private:
|
||||
void Init();
|
||||
|
||||
private:
|
||||
void OnConnect(wxCommandEvent& event);
|
||||
void OnDisconnect(wxCommandEvent& event);
|
||||
|
||||
public:
|
||||
wxTextCtrl* textIP_;
|
||||
wxTextCtrl* textPort_;
|
||||
wxButton* btnConnect_;
|
||||
wxButton* btnDisconnect_;
|
||||
ClientCore clientCore_;
|
||||
|
||||
private:
|
||||
LogControl* logControl_;
|
||||
};
|
||||
|
||||
#endif // HEADERCONTROL_H
|
42
Util/Util.h
42
Util/Util.h
@ -13,6 +13,48 @@ public:
|
||||
TranUtil();
|
||||
};
|
||||
|
||||
class MLogFormatter : public wxLogFormatter
|
||||
{
|
||||
public:
|
||||
wxString Format(wxLogLevel level, const wxString& msg, const wxLogRecordInfo& info) const override
|
||||
{
|
||||
wxDateTime time(info.timestampMS / 1000);
|
||||
time.SetMillisecond(info.timestampMS % 1000);
|
||||
auto timeStr = time.Format(wxT("%H:%M:%S.%l"));
|
||||
wxString levelStr;
|
||||
switch (level) {
|
||||
case wxLOG_FatalError:
|
||||
levelStr = "FATAL";
|
||||
break;
|
||||
case wxLOG_Error:
|
||||
levelStr = "ERROR";
|
||||
break;
|
||||
case wxLOG_Warning:
|
||||
levelStr = "WARN";
|
||||
break;
|
||||
case wxLOG_Message:
|
||||
levelStr = "INFO";
|
||||
break;
|
||||
case wxLOG_Status:
|
||||
levelStr = "STATUS";
|
||||
break;
|
||||
case wxLOG_Info:
|
||||
levelStr = "INFO";
|
||||
break;
|
||||
case wxLOG_Debug:
|
||||
levelStr = "DEBUG";
|
||||
break;
|
||||
case wxLOG_Trace:
|
||||
levelStr = "TRACE";
|
||||
break;
|
||||
default:
|
||||
levelStr = "OTHER";
|
||||
break;
|
||||
}
|
||||
return wxString::Format("[%s][%s] %s", timeStr, levelStr, msg);
|
||||
}
|
||||
};
|
||||
|
||||
class MutBuffer
|
||||
{
|
||||
public:
|
||||
|
Loading…
x
Reference in New Issue
Block a user