From 7e743b73433475df086fcec81be7b10c1d695a42 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Fri, 7 May 2021 02:18:07 +0300 Subject: initial --- src/server/connection.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/server/connection.h (limited to 'src/server/connection.h') diff --git a/src/server/connection.h b/src/server/connection.h new file mode 100644 index 0000000..eb28d51 --- /dev/null +++ b/src/server/connection.h @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: BSD-3-Clause + +#ifndef INVERTER_TOOLS_CONNECTION_H +#define INVERTER_TOOLS_CONNECTION_H + +#include +#include +#include + +#include "server.h" +#include "../formatter/formatter.h" + +namespace server { + +class Server; +struct Response; + +struct ConnectionOptions { + ConnectionOptions() + : version(1), format(formatter::Format::JSON) + {} + + unsigned version; + formatter::Format format; +}; + + +class Connection { +private: + int sock_; + std::thread thread_; + struct sockaddr_in addr_; + Server* server_; + ConnectionOptions options_; + +public: + explicit Connection(int sock, struct sockaddr_in addr, Server* server); + ~Connection(); + void run(); + std::string ipv4() const; + bool sendResponse(Response& resp) const; + int readLoop(char* buf, size_t bufSize) const; + bool writeLoop(const char* buf, size_t bufSize) const; + Response processRequest(char* buf); +}; + + +enum class RequestType { + Version, + Format, + Execute, + Raw, +}; + + +enum class ResponseType { + OK, + Error +}; + + +struct Response { + ResponseType type; + std::ostringstream buf; +}; +std::ostream& operator<<(std::ostream& os, Response& resp); + +} + +#endif //INVERTER_TOOLS_CONNECTION_H -- cgit v1.2.3