summaryrefslogtreecommitdiff
path: root/src/server/connection.h
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-05-07 02:18:07 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-05-07 02:18:07 +0300
commit7e743b73433475df086fcec81be7b10c1d695a42 (patch)
tree1737c5f9bdad2a40f740e9a655e510641331b9e2 /src/server/connection.h
initial
Diffstat (limited to 'src/server/connection.h')
-rw-r--r--src/server/connection.h70
1 files changed, 70 insertions, 0 deletions
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 <thread>
+#include <netinet/in.h>
+#include <sstream>
+
+#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