fix a lot of issues on psogc; add proxy module
- $ann implemented - concurrency removed; server is now single-threaded, event-driven and much more stable - rare seed is no longer the game id; ids are sequential from server startup so they weren't random at all before - supports dropping privileges; now you can run it as root so it can open a sockets on low ports, then it will switch to the given user before serving any traffic - newserv now behaves like a proxy if you run it with the --proxy-destination=<IP_OR_HOSTNAME> argument; there's also an (invisible) shell in this mode where you can inject commands to the server or client. e.g. it can always be christmas in the lobby if you do `sc DA 01 00 00` - increased the mtu on PSODolphinConfig's tap0 configuration; this seems to make the connection more stable - fixed some uninitialized memory bugs - the shell is now event-driven and now uses libevent too; unfortunately this means readline doesn't work anymore (no history and vim-like shortcuts) - made network command display consistent for input vs. output (the header appears in both cases now) - fixed bugs in some subcommand handling (the BB logic was being applied to non-BB clients erroneously, causing most item drops not to work at all) - fixed player tags in the short lobby data struct. unclear if this was actually a problem but it was inconsistent with other servers - fixed "unused" field in game join command (actually it appears to be disable_udp and should be 1, not 0) - cleaned up Server abstraction a bit - rewrote some text functions; asan was complaining about the built-in ones for some reason - added an optional welcome message
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
#include "Client.hh"
|
||||
#include "ServerState.hh"
|
||||
@@ -14,62 +13,12 @@
|
||||
|
||||
|
||||
class Server {
|
||||
private:
|
||||
struct WorkerThread {
|
||||
Server* server;
|
||||
int worker_num;
|
||||
std::unique_ptr<struct event_base, void(*)(struct event_base*)> base;
|
||||
std::unordered_set<std::unique_ptr<struct evconnlistener, void(*)(struct evconnlistener*)>> listeners;
|
||||
std::unordered_map<struct bufferevent*, std::shared_ptr<Client>> bev_to_client;
|
||||
std::thread t;
|
||||
std::string thread_name;
|
||||
|
||||
WorkerThread(Server* server, int worker_num);
|
||||
|
||||
void disconnect_client(struct bufferevent* bev);
|
||||
|
||||
static void dispatch_on_listen_accept(struct evconnlistener* listener,
|
||||
evutil_socket_t fd, struct sockaddr *address, int socklen, void* ctx);
|
||||
static void dispatch_on_listen_error(struct evconnlistener* listener, void* ctx);
|
||||
static void dispatch_on_client_input(struct bufferevent* bev, void* ctx);
|
||||
static void dispatch_on_client_error(struct bufferevent* bev, short events,
|
||||
void* ctx);
|
||||
static void dispatch_on_disconnecting_client_output(struct bufferevent* bev,
|
||||
void* ctx);
|
||||
static void dispatch_on_disconnecting_client_error(struct bufferevent* bev,
|
||||
short events, void* ctx);
|
||||
static void dispatch_check_for_thread_exit(evutil_socket_t fd, short what, void* ctx);
|
||||
};
|
||||
|
||||
std::atomic<bool> should_exit;
|
||||
std::vector<WorkerThread> threads;
|
||||
|
||||
std::atomic<size_t> client_count;
|
||||
std::unordered_map<int, std::pair<GameVersion, ServerBehavior>> listen_fd_to_version_and_state;
|
||||
std::shared_ptr<ServerState> state;
|
||||
|
||||
void on_listen_accept(WorkerThread& wt, struct evconnlistener *listener,
|
||||
evutil_socket_t fd, struct sockaddr *address, int socklen);
|
||||
void on_listen_error(WorkerThread& wt, struct evconnlistener *listener);
|
||||
void on_client_input(WorkerThread& wt, struct bufferevent *bev);
|
||||
void on_client_error(WorkerThread& wt, struct bufferevent *bev, short events);
|
||||
void on_disconnecting_client_output(WorkerThread& wt, struct bufferevent *bev);
|
||||
void on_disconnecting_client_error(WorkerThread& wt, struct bufferevent *bev, short events);
|
||||
void check_for_thread_exit(WorkerThread& wt, evutil_socket_t fd, short what);
|
||||
|
||||
void receive_and_process_commands(std::shared_ptr<Client> c, struct bufferevent* buf);
|
||||
|
||||
void process_client_connect(std::shared_ptr<Client> c);
|
||||
void process_client_disconnect(std::shared_ptr<Client> c);
|
||||
void process_client_command(std::shared_ptr<Client> c, const std::string& command);
|
||||
|
||||
void run_thread(int thread_id);
|
||||
|
||||
public:
|
||||
Server() = delete;
|
||||
Server(const Server&) = delete;
|
||||
Server(Server&&) = delete;
|
||||
Server(std::shared_ptr<ServerState> state);
|
||||
Server(std::shared_ptr<struct event_base> base,
|
||||
std::shared_ptr<ServerState> state);
|
||||
virtual ~Server() = default;
|
||||
|
||||
void listen(const std::string& socket_path, GameVersion version, ServerBehavior initial_state);
|
||||
@@ -77,7 +26,44 @@ public:
|
||||
void listen(int port, GameVersion version, ServerBehavior initial_state);
|
||||
void add_socket(int fd, GameVersion version, ServerBehavior initial_state);
|
||||
|
||||
virtual void start();
|
||||
virtual void schedule_stop();
|
||||
virtual void wait_for_stop();
|
||||
private:
|
||||
std::shared_ptr<struct event_base> base;
|
||||
|
||||
struct ListeningSocket {
|
||||
int fd;
|
||||
GameVersion version;
|
||||
ServerBehavior behavior;
|
||||
std::unique_ptr<struct evconnlistener, void(*)(struct evconnlistener*)> listener;
|
||||
|
||||
ListeningSocket(Server* s, int fd, GameVersion version,
|
||||
ServerBehavior behavior);
|
||||
};
|
||||
std::unordered_map<int, ListeningSocket> listening_sockets;
|
||||
std::unordered_map<struct bufferevent*, std::shared_ptr<Client>> bev_to_client;
|
||||
|
||||
std::shared_ptr<ServerState> state;
|
||||
|
||||
static void dispatch_on_listen_accept(struct evconnlistener* listener,
|
||||
evutil_socket_t fd, struct sockaddr *address, int socklen, void* ctx);
|
||||
static void dispatch_on_listen_error(struct evconnlistener* listener, void* ctx);
|
||||
static void dispatch_on_client_input(struct bufferevent* bev, void* ctx);
|
||||
static void dispatch_on_client_error(struct bufferevent* bev, short events,
|
||||
void* ctx);
|
||||
static void dispatch_on_disconnecting_client_output(struct bufferevent* bev,
|
||||
void* ctx);
|
||||
static void dispatch_on_disconnecting_client_error(struct bufferevent* bev,
|
||||
short events, void* ctx);
|
||||
|
||||
void disconnect_client(struct bufferevent* bev);
|
||||
void disconnect_client(std::shared_ptr<Client> c);
|
||||
|
||||
void on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd,
|
||||
struct sockaddr *address, int socklen);
|
||||
void on_listen_error(struct evconnlistener* listener);
|
||||
void on_client_input(struct bufferevent* bev);
|
||||
void on_client_error(struct bufferevent* bev, short events);
|
||||
void on_disconnecting_client_output(struct bufferevent* bev);
|
||||
void on_disconnecting_client_error(struct bufferevent* bev, short events);
|
||||
|
||||
void receive_and_process_commands(std::shared_ptr<Client> c);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user