diff options
Diffstat (limited to 'plugins/fcgi/socket.cpp')
-rw-r--r-- | plugins/fcgi/socket.cpp | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/plugins/fcgi/socket.cpp b/plugins/fcgi/socket.cpp index 0a2a381..49a8651 100644 --- a/plugins/fcgi/socket.cpp +++ b/plugins/fcgi/socket.cpp @@ -29,8 +29,7 @@ std::shared_ptr<Socket> SocketFactory::create(const std::string& app_addr) return std::make_shared<TCPSocket>(app_addr.substr(0, pos), app_addr.substr(pos + 1), m_io_context); } else if (fs::is_socket(fs::path{app_addr})) { // Unix domain socket - // TODO - std::cerr << "FCGI Error: Unix domain sockets not yet implemented." << std::endl; + return std::make_shared<FileSocket>(app_addr, m_io_context); } else if (fs::is_regular_file(fs::path{app_addr})) { // Executable to start // TODO std::cerr << "FCGI Error: Executable FCGI not yet implemented." << std::endl; @@ -119,3 +118,62 @@ size_t TCPSocket::read(std::vector<char>& data) } } +FileSocket::FileSocket(const std::string& app_addr, boost::asio::io_context& io_context) + : m_app_addr(app_addr) + , m_socket(io_context) +{ +} + +FileSocket::~FileSocket() +{ +} + +void FileSocket::open() +{ + try { + boost::asio::local::stream_protocol::endpoint ep(m_app_addr); + m_socket.connect(ep); + } catch(const std::exception& ex) { + std::cerr << "FCGI Error: Error on connecting to " << m_app_addr << ": " << ex.what() << std::endl; + return; + } +} + +bool FileSocket::is_open() +{ + return m_socket.is_open(); +} + +void FileSocket::close() +{ + m_socket.close(); +} + +size_t FileSocket::write(const std::vector<char>& data) +{ + try { + return m_socket.write_some(boost::asio::buffer(data)); + } catch (const boost::system::system_error& ex) { + if (ex.code() == boost::asio::error::eof) { + throw fcgi_eof_error("EOF on write"); + } else + throw std::runtime_error("FCGI Error: Unknown boost asio exception on write: "s + ex.what()); + } catch (const std::exception& ex) { + throw std::runtime_error("FCGI Error: Unknown exception on write: "s + ex.what()); + } +} + +size_t FileSocket::read(std::vector<char>& data) +{ + try { + return m_socket.read_some(boost::asio::buffer(data)); + } catch (const boost::system::system_error& ex) { + if (ex.code() == boost::asio::error::eof) { + throw fcgi_eof_error("EOF on read"); + } else + throw std::runtime_error("FCGI Error: Unknown boost asio exception on read: "s + ex.what()); + } catch (const std::exception& ex) { + throw std::runtime_error("FCGI Error: Unknown exception on read: "s + ex.what()); + } +} + |