gtransm/server-core/server-core.h

49 lines
1.2 KiB
C++

#ifndef SERVER_CORE_H
#define SERVER_CORE_H
#include <array>
#include <chrono>
#include <gutil.h>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <thread>
#include <unordered_map>
#include <wx/event.h>
#include <wx/evtloop.h>
#include <wx/socket.h>
#include <wx/wx.h>
using high_clock = std::chrono::time_point<std::chrono::high_resolution_clock>;
struct ClientCache {
std::shared_ptr<wxSocketBase> socket{};
CMutBuffer buffer{};
std::array<char, MAX_BUFFER_SIZE> recv_buffer{};
wxString online_time;
high_clock last_recv_time{};
};
class CServerCore : public wxEvtHandler
{
public:
CServerCore();
~CServerCore();
public:
bool Init(const wxString& ip, unsigned short port);
int Run();
private:
void OnServerEvent(wxSocketEvent& event);
void th_client_thread(const std::shared_ptr<wxSocketBase>& socket, const wxString& id);
private:
std::unique_ptr<wxSocketServer> server_{};
wxWindowID server_id_{};
std::unordered_map<wxString, std::shared_ptr<ClientCache>> client_cache_{};
std::unordered_map<wxString, std::thread> client_thread_{};
std::shared_mutex cli_mut_;
wxString server_ip_;
};
#endif