This repository has been archived on 2025-06-17. You can view files and clone it, but cannot push or open issues or pull requests.
RelayFile/RelayServer/RelayServer.h

50 lines
1.2 KiB
C
Raw Normal View History

2025-05-05 23:22:43 +08:00
#ifndef REMOTE_SERVER_H
#define REMOTE_SERVER_H
#include <Util.h>
#include <array>
#include <chrono>
#include <cstdint>
2025-05-07 12:59:46 +08:00
#include <memory>
2025-05-05 23:22:43 +08:00
#include <shared_mutex>
#include <thread>
#include <unordered_map>
#include <wx/event.h>
#include <wx/evtloop.h>
#include <wx/socket.h>
#include <wx/wx.h>
2025-05-10 01:25:02 +08:00
// constexpr int gBufferSize = 1024 * 1024;
constexpr int gBufferSize = 256;
2025-05-05 23:22:43 +08:00
using highClock_t = std::chrono::time_point<std::chrono::high_resolution_clock>;
struct TranClient {
std::shared_ptr<wxSocketBase> wxSock;
2025-05-10 01:25:02 +08:00
std::array<char, gBufferSize> buf;
2025-05-05 23:22:43 +08:00
MutBuffer buffer;
int64_t onlineTime;
highClock_t lastRecvTime;
};
class RemoteServer : public wxEvtHandler
{
public:
RemoteServer();
public:
bool Init(const wxString& ip, unsigned short port);
int Run();
private:
void OnServerEvent(wxSocketEvent& event);
void thClientThread(const std::shared_ptr<wxSocketBase>& wxSock, const wxString& id);
private:
2025-05-10 01:25:02 +08:00
bool thRun_{false};
2025-05-05 23:22:43 +08:00
wxWindowID serverId_;
std::shared_mutex clientsMutex_;
std::unique_ptr<wxSocketServer> server_;
std::unordered_map<wxString, std::thread> threads_;
std::unordered_map<wxString, std::shared_ptr<TranClient>> clients_;
};
#endif // REMOTE_SERVER_H