1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
#include "socket.h"
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
using namespace std::string_literals;
std::mutex& Socket::getMutex()
{
return m_mutex;
}
FCGI_ID& Socket::fcgi_id()
{
return m_fcgi_id;
}
SocketFactory::SocketFactory()
{
}
std::shared_ptr<Socket> SocketFactory::create(const std::string& app_addr)
{
size_t pos { app_addr.find(':') };
if (pos != app_addr.npos) { // tcp socket: host:port
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
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;
} else {
std::cerr << "FCGI Error: Invalid app_addr type." << std::endl;
}
return {};
}
TCPSocket::TCPSocket(const std::string& host, const std::string& port, boost::asio::io_context& io_context)
: m_io_context(io_context)
, m_host(host)
, m_port(port)
, m_socket(io_context)
{
}
TCPSocket::~TCPSocket()
{
}
void TCPSocket::open()
{
boost::asio::ip::tcp::resolver resolver(m_io_context);
auto endpoints{resolver.resolve(m_host, m_port)};
try {
boost::asio::connect(m_socket, endpoints);
} catch(const std::exception& ex) {
std::cerr << "FCGI Error: Error on connecting to " << m_host << ":" << m_port << ": " << ex.what() << std::endl;
return;
}
boost::asio::socket_base::keep_alive keepAlive(true);
m_socket.set_option(keepAlive);
struct timeval tv;
tv.tv_sec = 0; // infinite
tv.tv_usec = 0;
if (setsockopt(m_socket.native_handle(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
std::cerr << "FCGI Error: SO_RCVTIMEO" << std::endl;
if (setsockopt(m_socket.native_handle(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)))
std::cerr << "FCGI Error: SO_SNDTIMEO" << std::endl;
int val{1};
if (setsockopt(m_socket.native_handle(), SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)))
std::cerr << "FCGI Error: SO_KEEPALIVE" << std::endl;
}
bool TCPSocket::is_open()
{
if (m_socket.is_open()) {
std::vector<char> inbuf;
int error;
socklen_t len = sizeof (error);
int retval {getsockopt(m_socket.native_handle(), SOL_SOCKET, SO_ERROR, &error, &len)};
if (retval) {
std::cerr << "FCGI Error: getsockopt() error in is_open(): " << strerror(errno) << std::endl;
try {
// for graceful shutdown, according to
// https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/basic_stream_socket/close/overload1.html
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
m_socket.close(); // correct state
} catch (...) {
std::cerr << "Error on shutdown/close" << std::endl;
}
return false;
}
if (error != 0) {
try {
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
m_socket.close(); // correct state
} catch (...) {
std::cerr << "Error on shutdown/close" << std::endl;
}
return false;
}
return true;
} else
return false;
}
void TCPSocket::close()
{
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
m_socket.close();
}
size_t TCPSocket::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 if (ex.code() == boost::asio::error::broken_pipe) {
throw fcgi_broken_pipe_error("Application server connection broken");
} 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 TCPSocket::read(std::vector<char>& data)
{
try {
size_t result{0};
do {
std::vector<char> inbuf_part(1024);
size_t got { m_socket.read_some(boost::asio::buffer(inbuf_part))};
data.insert(data.end(), inbuf_part.begin(), inbuf_part.begin() + got);
result += got;
} while (m_socket.available());
return result;
} 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());
}
}
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 if (ex.code() == boost::asio::error::broken_pipe) {
throw fcgi_broken_pipe_error("Application server connection broken");
} 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 {
size_t result{0};
do {
std::vector<char> inbuf_part(1024);
size_t got { m_socket.read_some(boost::asio::buffer(inbuf_part))};
data.insert(data.end(), inbuf_part.begin(), inbuf_part.begin() + got);
result += got;
} while (m_socket.available());
return result;
} 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());
}
}
|