mirror of
https://github.com/ohwgiles/laminar.git
synced 2026-09-24 21:06:21 +00:00
replace websockets with sse and refactor
Large refactor that more closely aligns the codebase to the kj async style, more clearly exposes an interface for functional testing and removes cruft. There is a slight increase in coupling between the Laminar and Http/Rpc classes, but this was always an issue, just until now more obscured by the arbitrary pure virtual LaminarInterface class (which has been removed in this change) and the previous lumping together of all the async stuff in the Server class (which is now more spread around the code according to function). This change replaces the use of Websockets with Server Side Events (SSE). They are simpler and more suitable for the publish-style messages used by Laminar, and typically require less configuration of the reverse proxy HTTP server. Use of gmock is also removed, which eases testing in certain envs. Resolves #90.
This commit is contained in:
37
src/server.h
37
src/server.h
@@ -25,19 +25,14 @@
|
||||
#include <capnp/capability.h>
|
||||
#include <functional>
|
||||
|
||||
struct LaminarInterface;
|
||||
struct Laminar;
|
||||
struct Http;
|
||||
struct Rpc;
|
||||
|
||||
// This class abstracts the HTTP/Websockets and Cap'n Proto RPC interfaces
|
||||
// and manages the program's asynchronous event loop
|
||||
// This class manages the program's asynchronous event loop
|
||||
class Server final : public kj::TaskSet::ErrorHandler {
|
||||
public:
|
||||
// Initializes the server with a LaminarInterface to handle requests from
|
||||
// HTTP/Websocket or RPC clients and bind addresses for each of those
|
||||
// interfaces. See the documentation for kj::AsyncIoProvider::getNetwork
|
||||
// for a description of the address format
|
||||
Server(LaminarInterface& li, kj::StringPtr rpcBindAddress, kj::StringPtr httpBindAddress);
|
||||
Server(kj::AsyncIoContext& ioContext);
|
||||
~Server();
|
||||
void start();
|
||||
void stop();
|
||||
@@ -52,32 +47,28 @@ public:
|
||||
|
||||
// get a promise which resolves when a child process exits
|
||||
kj::Promise<int> onChildExit(kj::Maybe<pid_t>& pid);
|
||||
// add a path to be watched for changes
|
||||
void addWatchPath(const char* dpath);
|
||||
|
||||
struct PathWatcher {
|
||||
virtual PathWatcher& addPath(const char* path) = 0;
|
||||
};
|
||||
|
||||
PathWatcher& watchPaths(std::function<void()>);
|
||||
|
||||
void listenRpc(Rpc& rpc, kj::StringPtr rpcBindAddress);
|
||||
void listenHttp(Http& http, kj::StringPtr httpBindAddress);
|
||||
|
||||
private:
|
||||
kj::Promise<void> acceptRpcClient(kj::Own<kj::ConnectionReceiver>&& listener);
|
||||
kj::Promise<void> acceptRpcClient(Rpc& rpc, kj::Own<kj::ConnectionReceiver>&& listener);
|
||||
kj::Promise<void> handleFdRead(kj::AsyncInputStream* stream, char* buffer, std::function<void(const char*,size_t)> cb);
|
||||
|
||||
void taskFailed(kj::Exception&& exception) override;
|
||||
|
||||
private:
|
||||
int efd_quit;
|
||||
LaminarInterface& laminarInterface;
|
||||
kj::AsyncIoContext ioContext;
|
||||
kj::AsyncIoContext& ioContext;
|
||||
kj::Own<kj::TaskSet> listeners;
|
||||
kj::TaskSet childTasks;
|
||||
kj::Maybe<kj::Promise<void>> reapWatch;
|
||||
int inotify_fd;
|
||||
kj::Maybe<kj::Promise<void>> pathWatch;
|
||||
|
||||
// TODO: restructure so this isn't necessary
|
||||
friend class ServerTest;
|
||||
kj::PromiseFulfillerPair<void> httpReady;
|
||||
|
||||
// TODO: WIP
|
||||
kj::Own<Http> http;
|
||||
kj::Own<Rpc> rpc;
|
||||
};
|
||||
|
||||
#endif // LAMINAR_SERVER_H_
|
||||
|
||||
Reference in New Issue
Block a user