diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-05-07 19:24:45 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-05-07 19:24:45 +0200 |
commit | 9e635d9b19e72eefef082dd8071d3e4c9d6cfab1 (patch) | |
tree | 4e205947bc1f6b62f743faf3312f3e1e7e77f647 /plugins/fcgi/socket.h | |
parent | 1cc484b25547e349177cf652f62021b802f48655 (diff) |
Separated out TCP socket class
Diffstat (limited to 'plugins/fcgi/socket.h')
-rw-r--r-- | plugins/fcgi/socket.h | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/plugins/fcgi/socket.h b/plugins/fcgi/socket.h new file mode 100644 index 0000000..b4ec54b --- /dev/null +++ b/plugins/fcgi/socket.h @@ -0,0 +1,70 @@ +#pragma once + +#include "fcgiid.h" + +#include <boost/asio.hpp> + +#include <cstdint> +#include <mutex> +#include <set> +#include <string> + +class fcgi_eof_error: public std::runtime_error +{ +public: + fcgi_eof_error(const std::string& what_arg): std::runtime_error(what_arg) {} +}; + +class Socket +{ + std::mutex m_mutex; // guard socket use in different threads + FCGI_ID m_fcgi_id; + +public: + virtual ~Socket() {} + + std::mutex& getMutex(); + + FCGI_ID& fcgi_id(); + + virtual void open() = 0; + virtual void close() = 0; + virtual bool is_open() = 0; + virtual size_t write(const std::vector<char>& data) = 0; + virtual size_t read(std::vector<char>& data) = 0; +}; + +class SocketFactory +{ + boost::asio::io_context m_io_context; + +public: + SocketFactory(); + std::shared_ptr<Socket> create(const std::string& name); +}; + +class TCPSocket: public Socket +{ + boost::asio::io_context& m_io_context; + std::string m_host; + std::string m_port; + boost::asio::ip::tcp::socket m_socket; + +public: + TCPSocket(const std::string& host, const std::string& port, boost::asio::io_context& io_context); + ~TCPSocket() override; + + void open() override; + void close() override; + bool is_open() override; + size_t write(const std::vector<char>& data) override; + size_t read(std::vector<char>& data) override; +}; + +#if 0 +class FileSocket: public Socket +{ + ~FileSocket() override; +}; +#endif + |