2015-09-13 20:25:26 +00:00
|
|
|
///
|
2016-07-25 12:00:49 +00:00
|
|
|
/// Copyright 2016 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/>
|
|
|
|
///
|
|
|
|
#ifndef _LAMINAR_SERVER_H_
|
|
|
|
#define _LAMINAR_SERVER_H_
|
|
|
|
|
|
|
|
#include <kj/async-io.h>
|
|
|
|
#include <capnp/message.h>
|
|
|
|
#include <capnp/capability.h>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
struct LaminarInterface;
|
|
|
|
|
2016-07-25 11:59:45 +00:00
|
|
|
// This class abstracts the HTTP/Websockets and Cap'n Proto RPC interfaces
|
|
|
|
// and manages the program's asynchronous event loop
|
2015-09-13 20:25:26 +00:00
|
|
|
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();
|
|
|
|
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
|
|
|
|
void addDescriptor(int fd, std::function<void(char*,size_t)> cb);
|
2015-09-13 20:25:26 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void acceptHttpClient(kj::Own<kj::ConnectionReceiver>&& listener);
|
|
|
|
void acceptRpcClient(kj::Own<kj::ConnectionReceiver>&& listener);
|
2016-07-25 11:59:45 +00:00
|
|
|
kj::Promise<void> handleFdRead(kj::AsyncInputStream* stream, std::function<void(char*,size_t)> cb);
|
2015-09-13 20:25:26 +00:00
|
|
|
|
|
|
|
void taskFailed(kj::Exception&& exception) override {
|
|
|
|
kj::throwFatalException(kj::mv(exception));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct WebsocketConnection;
|
|
|
|
struct HttpImpl;
|
2016-07-25 11:59:45 +00:00
|
|
|
|
|
|
|
int efd_quit;
|
|
|
|
capnp::Capability::Client rpcInterface;
|
2015-09-13 20:25:26 +00:00
|
|
|
HttpImpl* httpInterface;
|
|
|
|
kj::AsyncIoContext ioContext;
|
|
|
|
kj::TaskSet tasks;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _LAMINAR_SERVER_H_
|