[Change] Remove hybrid local/remote network model

Client architecture has been refactored to be fully
server-authoritative, remove the previous "hybrid" model that supported
both local and remote command execution, that was a stupid idea.

- Client now connects to server on startup.
- The local command processor and VFS have been removed from the
  Terminal class.
- All command processing is now handled by the server.
- The client is now just a thin client essentially

I'll in the future enable single player mode by running the server on
the local machine in a separate thread.
This commit is contained in:
Ritchie Cunningham 2025-09-23 20:54:39 +01:00
parent a502fa64cf
commit 388c6429cf
12 changed files with 137 additions and 142 deletions

View File

@ -0,0 +1,4 @@
-- /bin/ssh - Connects to a remote host.
local target = arg[1]
if not target then return "ssh: missing operand" end
return { action = "ssh", target = target }

View File

@ -1,9 +1,9 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <SDL3_net/SDL_net.h> #include <vector>
#include "SDL3_net/SDL_net.h"
#include "client_network.h" #include "client_network.h"
#include <SDL3/SDL_error.h>
ClientNetwork::ClientNetwork(void) { ClientNetwork::ClientNetwork(void) {
if(!NET_Init()) { if(!NET_Init()) {
@ -55,9 +55,9 @@ bool ClientNetwork::connect(const char* host, int port) {
return false; return false;
} }
void ClientNetwork::send_command(const char* command) { void ClientNetwork::send_command(const std::string& command) {
if(!_socket) return; if(!_socket) return;
NET_WriteToStreamSocket(_socket, command, strlen(command)+1); NET_WriteToStreamSocket(_socket, command.c_str(), command.length()+1);
} }
void ClientNetwork::disconnect(void) { void ClientNetwork::disconnect(void) {
@ -80,6 +80,7 @@ std::string ClientNetwork::receive_response(void) {
return std::string(buffer); return std::string(buffer);
} }
} }
return ""; /* Return empty on timeout or error. */ return ""; /* Return empty on timeout or error. */
} }

View File

@ -9,7 +9,7 @@ public:
~ClientNetwork(void); ~ClientNetwork(void);
bool connect(const char* host, int port); bool connect(const char* host, int port);
void send_command(const char* command); void send_command(const std::string& data);
void disconnect(void); void disconnect(void);
std::string receive_response(void); std::string receive_response(void);

View File

@ -4,26 +4,26 @@
#include <SDL3/SDL_events.h> #include <SDL3/SDL_events.h>
#include "client_network.h" #include "client_network.h"
#include "command_processor.h"
#include "gfx/txt_renderer.h" #include "gfx/txt_renderer.h"
#include "vfs_manager.h"
#include "vfs.h"
#include "terminal.h" #include "terminal.h"
Terminal::Terminal(void) { Terminal::Terminal(void) {
/* Placeholder welcome message to history. */ /* Placeholder welcome message to history. */
_history.push_back("Welcome to Bettola (local mode)"); _history.push_back("Welcome to Bettola (local mode)");
_history.push_back("Connecting to server...");
_network = new ClientNetwork(); _network = new ClientNetwork();
_is_remote = false;
_should_close = false; _should_close = false;
/* Create local file system for the client. */
VFSManager vfs_manager;
_local_vfs = vfs_manager.create_vfs("local");
_local_cmd_processor = new CommandProcessor(_local_vfs);
_current_path = get_full_path(_local_cmd_processor->get_current_dir());
_scroll_offset = 0; _scroll_offset = 0;
/* TODO: Move network to main.cpp, or better yet, a dedicatated game state file */
if(_network->connect("localhost", 8080)) {
_history.push_back("Connection successful.");
_prompt = _network->receive_response(); /* Get initial prompt from server. */
} else {
_history.push_back("Connection failed. Please restart.");
_prompt = "error>";
}
} }
bool Terminal::close(void) { bool Terminal::close(void) {
@ -41,52 +41,27 @@ void Terminal::_on_ret_press(void) {
_input_buffer.clear(); /* Clear the input buffer immediately. */ _input_buffer.clear(); /* Clear the input buffer immediately. */
/* Add the command to history. */ /* Add the command to history. */
_history.push_back(_current_path + "> " + command); _history.push_back(_prompt + "> " + command);
if(_is_remote) { if(command == "exit") {
/* REMOTE STATE. */ _should_close = true;
_network->send_command(command.c_str()); /* TODO: Window will close, and the OS will close the socket.
* server will detect the disconnection.
*/
return;
} else if(command == "clear"){
_history.clear();
return;
}
if(command == "exit") { _network->send_command(command);
/* Don't wait for a response, the server booted us. */ std::string response = _network->receive_response();
_is_remote = false; /* Server sends back "output\nprompt". Split them. */
_network->disconnect(); size_t last_newline = response.find_last_of('\n');
_current_path = get_full_path(_local_cmd_processor->get_current_dir()); if(last_newline != std::string::npos) {
_history.push_back("Connection closed."); _prompt = response.substr(last_newline+1);
} else { std::string output = response.substr(0, last_newline);
/* For all other commands, wait for a response. */ if(!output.empty()) { _history.push_back(output); }
std::string response = _network->receive_response();
if(!response.empty()) { _history.push_back(response); }
}
} else {
/* LOCAL STATE! */
if(command.rfind("ssh ", 0) == 0) {
if(_network->connect("localhost", 8080)) {
_network->send_command(command.c_str());
std::string response = _network->receive_response();
if(response.rfind("CONNECTED ", 0) == 0) {
_is_remote = true;
std::string ip = response.substr(10);
_current_path = "root@" + ip;
} else {
_history.push_back(response);
_network->disconnect(); /* Disconnect on fialed ssh login. */
}
} else {
_history.push_back("Connection failed.");
}
} else if(command == "exit") {
_should_close = true;
} else if(command == "clear") {
_history.clear();
} else {
std::string response = _local_cmd_processor->process_command(command);
if(response.rfind("/", 0) == 0) {
_current_path = response;
} else if(!response.empty()) {
_history.push_back(response);
}
}
} }
/* TODO: Ugly hack. Refactor to pass window height /* TODO: Ugly hack. Refactor to pass window height
@ -151,7 +126,7 @@ void Terminal::render(TextRenderer* renderer, int x, int y, int width, int heigh
} }
/* Draw current input line. */ /* Draw current input line. */
std::string line_to_render = _current_path + "> " + _input_buffer; std::string line_to_render = _prompt + "> " + _input_buffer;
if(show_cursor) { if(show_cursor) {
line_to_render += "_"; line_to_render += "_";
} }

View File

@ -5,8 +5,6 @@
#include "gfx/txt_renderer.h" #include "gfx/txt_renderer.h"
#include "client_network.h" #include "client_network.h"
#include "vfs_manager.h"
#include "command_processor.h"
class Terminal { class Terminal {
public: public:
@ -25,9 +23,6 @@ private:
bool _should_close; bool _should_close;
std::vector<std::string> _history; std::vector<std::string> _history;
int _scroll_offset; int _scroll_offset;
std::string _current_path; std::string _prompt;
bool _is_remote;
vfs_node* _local_vfs;
CommandProcessor* _local_cmd_processor;
ClientNetwork* _network; ClientNetwork* _network;
}; };

View File

@ -1,5 +1,7 @@
#include "lua_processor.h" #include "lua_processor.h"
#include <sol/forward.hpp> #include <sol/forward.hpp>
#include <sol/object.hpp>
#include <sol/protected_function_result.hpp>
#include "vfs.h" #include "vfs.h"
LuaProcessor::LuaProcessor(void) { LuaProcessor::LuaProcessor(void) {
@ -27,7 +29,13 @@ sol::object LuaProcessor::execute(const std::string& script, vfs_node* current_d
} }
_lua["arg"] = arg_table; _lua["arg"] = arg_table;
return _lua.script(script); sol::protected_function_result result = _lua.safe_script(script);
if(result.valid()) {
return result;
} else {
sol::error err = result;
return sol::make_object(_lua, err.what());
}
} catch(const sol::error& e) { } catch(const sol::error& e) {
/* Return the error message as a string in a sol::object. */ /* Return the error message as a string in a sol::object. */

View File

@ -75,7 +75,7 @@ vfs_node* VFSManager::create_vfs(const std::string& system_type) {
user->children["readme.txt"] = readme; user->children["readme.txt"] = readme;
/* Link to the shared directories from the template. */ /* Link to the shared directories from the template. */
root->children["bin"] = _vfs_root->children["bin"]; root->children["bin"] = copy_vfs_node(_vfs_root->children["bin"], root);
if(system_type == "npc") { if(system_type == "npc") {
vfs_node* npc_file = new_node("npc_system.txt", FILE_NODE, root); vfs_node* npc_file = new_node("npc_system.txt", FILE_NODE, root);

View File

@ -1,24 +1,11 @@
#include <cstdio> #include <cstdio>
#include <map>
#include <string>
#include "vfs.h"
#include "vfs_manager.h"
#include "network_manager.h" #include "network_manager.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
printf("=== Server starting ===\n"); printf("=== Server starting ===\n");
VFSManager vfs_manager; NetworkManager* net_manager = new NetworkManager();
/* Create the world map of networks. */
std::map<std::string, vfs_node*> world_vfs;
world_vfs["8.8.8.8"] = vfs_manager.create_vfs("npc"); /* Basic npc system. */
world_vfs["10.0.2.15"] = vfs_manager.create_vfs("npc"); /* Another one. */
printf("Created world with %zu networks.\n", world_vfs.size());
/* Network manager now managed the whole world. */
NetworkManager* net_manager = new NetworkManager(world_vfs);
net_manager->start(); /* Our loop. */ net_manager->start(); /* Our loop. */
delete net_manager; /* Shouldn't get here. */ delete net_manager; /* Shouldn't get here. */

View File

@ -1,15 +1,26 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <algorithm>
#include <SDL3_net/SDL_net.h>
#include "SDL3_net/SDL_net.h"
#include "vfs.h" #include "vfs.h"
#include "vfs_manager.h"
#include "command_processor.h" #include "command_processor.h"
#include "network_manager.h" #include "network_manager.h"
NetworkManager::NetworkManager(std::map<std::string, vfs_node*> world_vfs) { /* Send a length-prefixed string. */
_world_vfs = world_vfs; void send_response_string(NET_StreamSocket* socket, const std::string& data) {
if(!socket) return;
NET_WriteToStreamSocket(socket, data.c_str(), data.length()+1);
}
NetworkManager::NetworkManager(void) {
/* The VFSManager for the world's NPC's.
* Player VSManagers will be created on a per-player basis.
*/
_world_vfs["8.8.8.8"] = _npc_vfs_manager.create_vfs("npc");
_world_vfs["10.0.2.15"] = _npc_vfs_manager.create_vfs("npc");
printf("Created world with %zu networks.\n", _world_vfs.size());
if(!NET_Init()) { if(!NET_Init()) {
printf("Error: SDLNet_Init: %s\n", SDL_GetError()); printf("Error: SDLNet_Init: %s\n", SDL_GetError());
@ -24,6 +35,9 @@ NetworkManager::NetworkManager(std::map<std::string, vfs_node*> world_vfs) {
} }
NetworkManager::~NetworkManager(void) { NetworkManager::~NetworkManager(void) {
for(auto player : _players) {
delete player;
}
NET_DestroyServer(_server_socket); NET_DestroyServer(_server_socket);
NET_Quit(); NET_Quit();
} }
@ -47,83 +61,80 @@ void NetworkManager::_handle_new_connections(void) {
if(NET_AcceptClient(_server_socket, &client_socket)) { if(NET_AcceptClient(_server_socket, &client_socket)) {
if(client_socket) { if(client_socket) {
Player* new_player = new Player(client_socket); Player* new_player = new Player(client_socket);
new_player->cmd_processor = nullptr; /* Not connected to remote host yet. */
_players.push_back(new_player); _players.push_back(new_player);
printf("Client connected. Total players: %zu\n", _players.size()); printf("Client connected. Total players: %zu\n", _players.size());
/* Send the initial prompt to the new client. */
std::string prompt = get_full_path(new_player->cmd_processor->get_current_dir());
send_response_string(client_socket, prompt);
} }
} }
} }
void NetworkManager::_handle_client_activity(void) { void NetworkManager::_handle_client_activity(void) {
char buffer [512];
/* Iterate backwards so we can safely remove disconnected clients. */ /* Iterate backwards so we can safely remove disconnected clients. */
for(int i = _players.size()-1; i >= 0; --i) { for(int i = _players.size()-1; i >= 0; --i) {
Player* player = _players[i]; Player* player = _players[i];
NET_StreamSocket* sock_array[1] = { player->socket };
/* NET_ReadFromStreamSocket returns > 0 if data was received. /* Timeout of 0 for non-blocking check. */
* 0 if no data, and < 0 on error (disconnect). int ready = NET_WaitUntilInputAvailable((void**)sock_array, 1, 0);
*/ if(ready == -1) {
int bytes_received = NET_ReadFromStreamSocket(player->socket, buffer, 512); /* An error occured on the socket. */
if(bytes_received > 0) { fprintf(stderr, "[SERVER] Socket error, disconnecting client.\n");
printf("bytes_received: %d\n", bytes_received); _disconnect_client(player, i);
continue;
} }
if(bytes_received > 0) { if(ready > 0) {
/* _process_command returns true, the client wants to exit, boot it! */ /* Socket has data. If processing fails, disconnect the client. */
if(_process_command(player, buffer)) { fprintf(stderr, "[SERVER] Socket has data, processing command...\n");
if(!_process_command(player)) {
fprintf(stderr, "[SERVER] _process_command failed, disconnecting client.\n");
_disconnect_client(player, i); _disconnect_client(player, i);
} }
} else if(bytes_received < 0) {
/* Disconnect on error or if the client closes the connection. */
_disconnect_client(player, i);
} }
} }
} }
bool NetworkManager::_process_command(Player* player, char* command) { bool NetworkManager::_process_command(Player* player) {
/* Create a clean std::string from the buffer. */ /* Read the length-prefixed command from the client. */
std::string cmd_str = command; char buffer[2048];
memset(buffer, 0, sizeof(buffer));
if(cmd_str == "exit") { int bytes_received = NET_ReadFromStreamSocket(player->socket, buffer, sizeof(buffer)-1);
/* Client is disconnecting, no response needed. */ if(bytes_received <= 0) {
return true; /* Disconnect signal. */ return false; /* Error or disconnect. */
} else if(strncmp(cmd_str.c_str(), "ssh ", 4) == 0) {
std::string target = cmd_str.substr(4);
std::string target_ip = target;
size_t at_pos = target.find('@');
if(at_pos != std::string::npos) {
target_ip = target.substr(at_pos + 1);
}
if(_world_vfs.count(target_ip)) {
/* Create new command processor for the player's session on the target VFS. */
if(player->cmd_processor) { delete player->cmd_processor; }
player->cmd_processor = new CommandProcessor(_world_vfs[target_ip]);
std::string response = "CONNECTED " + target_ip;
NET_WriteToStreamSocket(player->socket, response.c_str(), response.length()+1);
} else {
NET_WriteToStreamSocket(player->socket, "ssh: Could not resolve hostname\n", 32);
}
} else {
/* If not ssh command, and we have a command processor, process it. */
if(player->cmd_processor) {
std::string response = player->cmd_processor->process_command(cmd_str);
printf("OK: cmd_processor exits. Processing command '%s'\n", cmd_str.c_str());
NET_WriteToStreamSocket(player->socket, response.c_str(), response.length()+1);
} else {
printf("ERROR: cmd_processor is null. Cannot process command '%s'\n", cmd_str.c_str());
NET_WriteToStreamSocket(player->socket, "Error: Not in a remote session.\n", 32);
}
} }
return false; /* Don't disconnect. */
std::string cmd_str(buffer);
fprintf(stderr, "[SERVER] Received command: \"%s\"\n", cmd_str.c_str());
/* Process the command. */
std::string response;
if(player->cmd_processor) {
response = player->cmd_processor->process_command(cmd_str);
} else {
response = "Error: No command processor available for this player.";
}
/* Get the new prompt. Append it to response with a known separator. */
std::string new_prompt = get_full_path(player->cmd_processor->get_current_dir());
std::string final_response = response + "\n" + new_prompt;
send_response_string(player->socket, final_response);
return true;
} }
void NetworkManager::_disconnect_client(Player* player, int index) { void NetworkManager::_disconnect_client(Player* player, int index) {
fprintf(stderr, "[SERVER] Disconnecting client.\n");
NET_DestroyStreamSocket(player->socket); NET_DestroyStreamSocket(player->socket);
/* If we have a valid index, remove the player. Otherwise, find them. */ /* If we have a valid index, remove the player. Otherwise, find them. */
if(index != -1) { if(index != -1) {
_players.erase(_players.begin() + index); _players.erase(_players.begin() + index);
} }
if(player->cmd_processor) { delete player->cmd_processor; }
delete player; delete player;
printf("Client disconnected. Total players: %zu\n", _players.size());
} }

View File

@ -6,10 +6,11 @@
#include <SDL3_net/SDL_net.h> #include <SDL3_net/SDL_net.h>
#include "player.h" #include "player.h"
#include "vfs_manager.h"
class NetworkManager { class NetworkManager {
public: public:
NetworkManager(std::map<std::string, vfs_node*> world_vfs); NetworkManager();
~NetworkManager(void); ~NetworkManager(void);
void start(void); void start(void);
@ -17,10 +18,11 @@ public:
private: private:
void _handle_new_connections(void); void _handle_new_connections(void);
void _handle_client_activity(void); void _handle_client_activity(void);
bool _process_command(Player* player, char* command); bool _process_command(Player* player);
void _disconnect_client(Player* player, int index); void _disconnect_client(Player* player, int index);
NET_Server* _server_socket; NET_Server* _server_socket;
std::vector<Player*> _players; std::vector<Player*> _players;
std::map<std::string, vfs_node*> _world_vfs; std::map<std::string, vfs_node*> _world_vfs; /* For NPC's. */
VFSManager _npc_vfs_manager;
}; };

View File

@ -1,7 +1,15 @@
#include "player.h" #include "player.h"
#include <SDL3_net/SDL_net.h> #include "command_processor.h"
Player::Player(NET_StreamSocket* new_socket) { Player::Player(NET_StreamSocket* new_socket) :
socket = new_socket; socket(new_socket),
cmd_processor = nullptr; vfs_root(vfs_manager.create_vfs("player")),
cmd_processor(new CommandProcessor(vfs_root)) {}
Player::~Player(void) {
if(cmd_processor) {
delete cmd_processor;
}
/* TODO: The VFSManager should handle deleting the vfs_root. */
} }

View File

@ -3,13 +3,17 @@
#include <SDL3_net/SDL_net.h> #include <SDL3_net/SDL_net.h>
#include "vfs.h" #include "vfs.h"
#include "vfs_manager.h"
#include "command_processor.h" #include "command_processor.h"
class Player { class Player {
public: public:
Player(NET_StreamSocket* socket); Player(NET_StreamSocket* socket);
~Player(void);
NET_StreamSocket* socket; NET_StreamSocket* socket;
VFSManager vfs_manager;
vfs_node* vfs_root;
CommandProcessor* cmd_processor; /* Manages the VFS state for the remote session. */ CommandProcessor* cmd_processor; /* Manages the VFS state for the remote session. */
private: private:
}; };