Files
ppsspp/Common/Net/WebsocketServer.h
T
libretroadmin f1208a7a5a Make threading lock-free
The core and the headless tools no longer use mutexes, condition variables,
spinlocks, std::atomic, std::thread or volatile flags. Threads hand off through
libretro-common's retro_atomic and rthreads plus a few small pieces on top of
them in Common/Thread: Thread, MpscQueue, EventCounter, ParkingLot waits and
ReaderGate. Every wait blocks until it's woken; nothing polls on a timer.

State that only one thread touches just loses its lock. The PSP filesystem,
sceIo and the savedata dialog now belong to the emulation thread: async reads
and writes run when they're queued and complete later in emulated time, and
the IO and savedata threads are gone. The friend finder only talks to the
adhoc server and hands what it gets to the emulation thread. GPU init always
runs on the thread that starts the game.

The Vulkan render and compile queues, pipeline and render pass caches, delete
list and SPIR-V cache, the GL render queue, the software renderer's jit caches,
the file loader caches, the web debugger, UPnP, input, savestates, logging and
the GE debugger all hand off through queues and atomics. The IR jit's spinning
profiler thread goes.

A CPU thread with nothing to run now sleeps until work is queued instead of
spinning. Headless with --debugger waits for a client while stopped, and ends
the run once every client that connected has left. Web server shutdown keeps
servicing the CPU queue until the server has finished.
2026-10-09 04:56:16 +02:00

120 lines
3.7 KiB
C++

#pragma once
#include <functional>
#include <string>
#include "Common/Net/HTTPServer.h"
#include "Common/Net/Sinks.h"
namespace net {
enum class WebSocketClose : uint16_t {
NORMAL = 1000,
GOING_AWAY = 1001,
PROTOCOL_ERROR = 1002,
UNSUPPORTED_DATA = 1003,
INVALID_DATA = 1007,
POLICY_VIOLATION = 1008,
MESSAGE_TOO_LONG = 1009,
MISSING_EXTENSION = 1010,
INTERNAL_ERROR = 1011,
SERVICE_RESTART = 1012,
TRY_AGAIN_LATER = 1013,
BAD_GATEWAY = 1014,
NO_STATUS = 1005,
ABNORMAL = 1006,
};
// RFC 6455
class WebSocketServer {
public:
static WebSocketServer *CreateAsUpgrade(const http::ServerRequest &request, const std::string &protocol = "");
void Send(const std::string &str);
void Send(const std::vector<uint8_t> &payload);
// Call with finish = false to start and continue, then finally with finish = true to complete.
// Note: Fragmented data cannot be interleaved, per protocol.
void AddFragment(bool finish, const std::string &str);
void AddFragment(bool finish, const std::vector<uint8_t> &payload);
void Ping(const std::vector<uint8_t> &payload = {});
void Pong(const std::vector<uint8_t> &payload = {});
void Close(WebSocketClose reason = WebSocketClose::GOING_AWAY);
// Blocks until the socket makes progress or one of wakeFds (any may be -1) becomes readable,
// then returns whether the connection is still open. Call in a loop; the caller drains its
// own wake fds.
bool Process(const intptr_t *wakeFds = nullptr, int wakeCount = 0);
void SetTextHandler(std::function<void(const std::string &)> func) {
text_ = func;
}
void SetBinaryHandler(std::function<void(const std::vector<uint8_t> &)> func) {
binary_ = func;
}
// Doesn't need to send a Pong.
void SetPingHandler(std::function<void(const std::vector<uint8_t> &)> func) {
ping_ = func;
}
void SetPongHandler(std::function<void(const std::vector<uint8_t> &)> func) {
pong_ = func;
}
bool IsOpen() {
return open_;
}
WebSocketClose CloseReason() {
return closeReason_;
}
protected:
WebSocketServer(size_t fd, InputSink *in, OutputSink *out) : fd_(fd), in_(in), out_(out) {
}
void SendHeader(bool fin, int opcode, size_t sz);
void SendBytes(const void *p, size_t sz);
// True once we've queued our own close frame. RFC 6455 5.5.1: no data frames may follow it.
// Just as importantly, anything queued after that point keeps outBuf_/out_ non-empty, which
// starves the "everything is flushed" check in Process() so the connection never finishes
// closing - and since the socket stays writable, select() stops blocking and we spin.
bool SendingIsOver() const { return sentClose_; }
void CloseAbnormally();
void CloseForReadFailure();
void SendFlush();
void CompactOutBuf();
size_t OutBufPending() const { return outBuf_.size() - outBufOffset_; }
bool ReadFrames();
bool ReadFrame();
bool ReadPending();
bool ReadControlFrame(int opcode, size_t sz);
bool open_ = true;
bool sentClose_ = false;
int fragmentOpcode_ = -1;
size_t fd_ = 0;
InputSink *in_ = nullptr;
OutputSink *out_ = nullptr;
WebSocketClose closeReason_ = WebSocketClose::NO_STATUS;
std::vector<uint8_t> outBuf_;
// How much of outBuf_ has already been handed to out_. See CompactOutBuf().
size_t outBufOffset_ = 0;
size_t lastPressure_ = 0;
std::vector<uint8_t> pendingBuf_;
uint8_t pendingMask_[4]{};
// Bytes left to read in the frame (in case of a partial frame read.)
uint64_t pendingLeft_ = 0;
int pendingOpcode_ = -1;
// Waiting for a frame with FIN.
bool pendingFin_ = false;
std::function<void(const std::string &)> text_;
std::function<void(const std::vector<uint8_t> &)> binary_;
std::function<void(const std::vector<uint8_t> &)> ping_;
std::function<void(const std::vector<uint8_t> &)> pong_;
};
};