1
0
mirror of https://github.com/ohwgiles/laminar.git synced 2024-09-28 22:40:45 +00:00
ohwgiles_laminar/src/server.h

76 lines
2.4 KiB
C
Raw Normal View History

2015-09-13 20:25:26 +00:00
///
2020-09-25 23:33:36 +00:00
/// Copyright 2015-2020 Oliver Giles
2015-09-13 20:25:26 +00:00
///
/// This file is part of Laminar
///
/// Laminar 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 3 of the License, or
/// (at your option) any later version.
///
2015-09-24 19:07:48 +00:00
/// Laminar is distributed in the hope that it will be useful,
2015-09-13 20:25:26 +00:00
/// 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 Laminar. If not, see <http://www.gnu.org/licenses/>
///
2017-12-20 06:24:25 +00:00
#ifndef LAMINAR_SERVER_H_
#define LAMINAR_SERVER_H_
2015-09-13 20:25:26 +00:00
#include <kj/async-io.h>
#include <kj/compat/http.h>
2015-09-13 20:25:26 +00:00
#include <capnp/message.h>
#include <capnp/capability.h>
#include <functional>
#include <sys/types.h>
2015-09-13 20:25:26 +00:00
2020-09-25 23:33:36 +00:00
class Laminar;
class Http;
class Rpc;
2015-09-13 20:25:26 +00:00
// This class manages the program's asynchronous event loop
2015-09-13 20:25:26 +00:00
class Server final : public kj::TaskSet::ErrorHandler {
public:
Server(kj::AsyncIoContext& ioContext);
2015-09-13 20:25:26 +00:00
~Server();
void start();
void stop();
2016-07-25 11:59:45 +00:00
// add a file descriptor to be monitored for output. The callback will be
// invoked with the read data
kj::Promise<void> readDescriptor(int fd, std::function<void(const char*,size_t)> cb);
2015-09-13 20:25:26 +00:00
void addTask(kj::Promise<void> &&task);
// add a one-shot timer callback
kj::Promise<void> addTimeout(int seconds, std::function<void()> cb);
// get a promise which resolves when a child process exits
kj::Promise<int> onChildExit(kj::Maybe<pid_t>& pid);
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);
2015-09-13 20:25:26 +00:00
private:
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);
2015-09-13 20:25:26 +00:00
void taskFailed(kj::Exception&& exception) override;
2015-09-13 20:25:26 +00:00
private:
2016-07-25 11:59:45 +00:00
int efd_quit;
kj::AsyncIoContext& ioContext;
kj::Own<kj::TaskSet> listeners;
kj::TaskSet childTasks;
kj::Maybe<kj::Promise<void>> reapWatch;
2015-09-13 20:25:26 +00:00
};
2017-12-20 06:24:25 +00:00
#endif // LAMINAR_SERVER_H_