diff options
Diffstat (limited to 'backends')
-rw-r--r-- | backends/module.mk | 1 | ||||
-rw-r--r-- | backends/networking/sdl_net/client.cpp | 99 | ||||
-rw-r--r-- | backends/networking/sdl_net/client.h | 63 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.cpp | 73 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.h | 5 |
5 files changed, 206 insertions, 35 deletions
diff --git a/backends/module.mk b/backends/module.mk index 6fb8758650..74412a792a 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -60,6 +60,7 @@ endif ifdef USE_SDL_NET MODULE_OBJS += \ + networking/sdl_net/client.o \ networking/sdl_net/localwebserver.o endif diff --git a/backends/networking/sdl_net/client.cpp b/backends/networking/sdl_net/client.cpp new file mode 100644 index 0000000000..2af940c232 --- /dev/null +++ b/backends/networking/sdl_net/client.cpp @@ -0,0 +1,99 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#define FORBIDDEN_SYMBOL_ALLOW_ALL + +#include "backends/networking/sdl_net/client.h" +#include "common/textconsole.h" +#include <SDL/SDL_net.h> + +namespace Networking { + +Client::Client(): _state(INVALID), _set(nullptr), _socket(nullptr) {} + +Client::Client(SDLNet_SocketSet set, TCPsocket socket): _state(INVALID), _set(nullptr), _socket(nullptr) { + open(set, socket); +} + +Client::~Client() { + close(); +} + +void Client::open(SDLNet_SocketSet set, TCPsocket socket) { + if (_state != INVALID) close(); + _state = READING_HEADERS; + _socket = socket; + _set = set; + if (set) { + int numused = SDLNet_TCP_AddSocket(set, socket); + if (numused == -1) { + error("SDLNet_AddSocket: %s\n", SDLNet_GetError()); + } + } +} + +void Client::readHeaders() { + if (!_socket) return; + if (!SDLNet_SocketReady(_socket)) return; + + const uint32 BUFFER_SIZE = 16 * 1024; + char buffer[BUFFER_SIZE]; + int bytes = SDLNet_TCP_Recv(_socket, buffer, BUFFER_SIZE); + if (bytes <= 0) { + warning("Client::readHeaders recv fail"); + _state = INVALID; + return; + } + _headers += Common::String(buffer, bytes); + checkIfHeadersEnded(); +} + +void Client::checkIfHeadersEnded() { + const char *cstr = _headers.c_str(); + const char *position = strstr(cstr, "\r\n\r\n"); + if (position) _state = READ_HEADERS; +} + +void Client::close() { + if (_set) { + if (_socket) { + int numused = SDLNet_TCP_DelSocket(_set, _socket); + if (numused == -1) + error("SDLNet_DelSocket: %s\n", SDLNet_GetError()); + } + _set = nullptr; + } + + if (_socket) { + SDLNet_TCP_Close(_socket); + _socket = nullptr; + } + + _state = INVALID; +} + + +ClientState Client::state() { return _state; } + +Common::String Client::headers() { return _headers; } + +} // End of namespace Networking diff --git a/backends/networking/sdl_net/client.h b/backends/networking/sdl_net/client.h new file mode 100644 index 0000000000..74c700e447 --- /dev/null +++ b/backends/networking/sdl_net/client.h @@ -0,0 +1,63 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef BACKENDS_NETWORKING_SDL_NET_CLIENT_H +#define BACKENDS_NETWORKING_SDL_NET_CLIENT_H + +#include "common/scummsys.h" +#include "common/str.h" + +typedef struct _SDLNet_SocketSet *SDLNet_SocketSet; +typedef struct _TCPsocket *TCPsocket; + +namespace Networking { + +enum ClientState { + INVALID, + READING_HEADERS, + READ_HEADERS +}; + +class Client { + ClientState _state; + SDLNet_SocketSet _set; + TCPsocket _socket; + Common::String _headers; + + void checkIfHeadersEnded(); + +public: + Client(); + Client(SDLNet_SocketSet set, TCPsocket socket); + virtual ~Client(); + + void open(SDLNet_SocketSet set, TCPsocket socket); + void readHeaders(); + void close(); + + ClientState state(); + Common::String headers(); +}; + +} // End of namespace Networking + +#endif diff --git a/backends/networking/sdl_net/localwebserver.cpp b/backends/networking/sdl_net/localwebserver.cpp index 681485701b..783814de14 100644 --- a/backends/networking/sdl_net/localwebserver.cpp +++ b/backends/networking/sdl_net/localwebserver.cpp @@ -37,10 +37,7 @@ DECLARE_SINGLETON(Networking::LocalWebserver); namespace Networking { -LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), _clients(0) { - for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) - _clientSocket[i] = nullptr; -} +LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), _clients(0) {} LocalWebserver::~LocalWebserver() { stop(); @@ -94,51 +91,59 @@ void LocalWebserver::start() { void LocalWebserver::stop() { if (_timerStarted) stopTimer(); - if (_set) { - SDLNet_FreeSocketSet(_set); - _set = nullptr; - } - if (_serverSocket) { SDLNet_TCP_Close(_serverSocket); _serverSocket = nullptr; } for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) - if (_clientSocket[i]) { - SDLNet_TCP_Close(_clientSocket[i]); - _clientSocket[i] = nullptr; - } + _client[i].close(); _clients = 0; + + if (_set) { + SDLNet_FreeSocketSet(_set); + _set = nullptr; + } } void LocalWebserver::handle() { int numready = SDLNet_CheckSockets(_set, 0); if (numready == -1) { error("SDLNet_CheckSockets: %s\n", SDLNet_GetError()); - } else if (numready) { - if (SDLNet_SocketReady(_serverSocket)) { - TCPsocket client = SDLNet_TCP_Accept(_serverSocket); - if (client) { - if (_clients == MAX_CONNECTIONS) { //drop the connection - SDLNet_TCP_Close(client); - } else { - int numused = SDLNet_TCP_AddSocket(_set, _serverSocket); - if (numused == -1) { - error("SDLNet_AddSocket: %s\n", SDLNet_GetError()); - } - _clientSocket[_clients++] = client; - } - } - } - - for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) { - if (!_clientSocket[i]) continue; - if (!SDLNet_SocketReady(_clientSocket[i])) continue; - //TODO: handle client - } + } else if (numready) acceptClient(); + + for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) + handleClient(i); +} + +void LocalWebserver::handleClient(uint32 i) { + switch (_client[i].state()) { + case INVALID: return; + case READING_HEADERS: _client[i].readHeaders(); break; + case READ_HEADERS: //decide what to do next with that client + //if GET, check whether we know a handler for such URL + //if PUT, check whether we know a handler for that URL + //if no handler, answer with default BAD REQUEST + warning("headers %s", _client[i].headers().c_str()); + _client[i].close(); + break; } } + +void LocalWebserver::acceptClient() { + if (!SDLNet_SocketReady(_serverSocket)) return; + + TCPsocket client = SDLNet_TCP_Accept(_serverSocket); + if (!client) return; + + if (_clients == MAX_CONNECTIONS) { //drop the connection + SDLNet_TCP_Close(client); + return; + } + + _client[_clients++].open(_set, client); +} + } // End of namespace Networking diff --git a/backends/networking/sdl_net/localwebserver.h b/backends/networking/sdl_net/localwebserver.h index a11be0a938..8db05f72e1 100644 --- a/backends/networking/sdl_net/localwebserver.h +++ b/backends/networking/sdl_net/localwebserver.h @@ -23,6 +23,7 @@ #ifndef BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H #define BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H +#include "backends/networking/sdl_net/client.h" #include "common/singleton.h" #include "common/scummsys.h" @@ -41,13 +42,15 @@ class LocalWebserver : public Common::Singleton<LocalWebserver> { SDLNet_SocketSet _set; TCPsocket _serverSocket; - TCPsocket _clientSocket[MAX_CONNECTIONS]; + Client _client[MAX_CONNECTIONS]; int _clients; bool _timerStarted; void startTimer(int interval = TIMER_INTERVAL); void stopTimer(); void handle(); + void handleClient(uint32 i); + void acceptClient(); public: LocalWebserver(); |