#ifndef REMOTE_SERVER_H #define REMOTE_SERVER_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include // constexpr int gBufferSize = 1024 * 1024; constexpr int gBufferSize = 256; using highClock_t = std::chrono::time_point; using sockPtr = std::shared_ptr; struct TranClient { sockPtr wxSock; MutBuffer buffer; int64_t onlineTime; std::string name; highClock_t lastRecvTime; std::array buf; }; 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& wxSock, const wxString& id); bool Forword(const sockPtr& wxSock, FrameBuffer* buf); bool Reply(const sockPtr& wxSock, InfoCommunicate& info); private: bool RpyOnline(const sockPtr& wxSock); bool RpyForwordFailed(const sockPtr& wxSock); private: template bool Send(const sockPtr& wxSock, const T& info) { std::stringstream ss; cereal::BinaryOutputArchive archive(ss); archive(info); auto buf = std::make_shared(); buf->dataConst = ss.view().data(); buf->len = ss.str().size(); return Send(wxSock, buf.get()); } bool Send(const sockPtr& wxSock, FrameBuffer* buf); private: bool thRun_{false}; std::string strID_; wxWindowID serverId_; std::shared_mutex clientsMutex_; std::unique_ptr server_; std::unordered_map threads_; std::unordered_map> clients_; }; #endif // REMOTE_SERVER_H