event loop with threadmgr
Some checks failed
Docker Build & Publish / build (push) Has been cancelled
Some checks failed
Docker Build & Publish / build (push) Has been cancelled
This commit is contained in:
parent
f09b261b62
commit
ca05aa1e5a
9 changed files with 69 additions and 122 deletions
|
@ -45,22 +45,33 @@ openssl_socket::openssl_socket(int port, int max_queue)
|
|||
|
||||
openssl_socket::~openssl_socket() = default;
|
||||
|
||||
void openssl_socket::wait_for_conn()
|
||||
bool openssl_socket::wait_for_conn()
|
||||
{
|
||||
client_ip = "";
|
||||
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
|
||||
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
|
||||
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
|
||||
client_ip = std::string(ip_str.data());
|
||||
|
||||
_ssl = SSL_new(_context);
|
||||
SSL_set_fd(_ssl, client_socket);
|
||||
if (SSL_accept(_ssl) <= 0) {
|
||||
log::warn << "Unable to open SSL connection with client" << std::endl;
|
||||
client_ip = "";
|
||||
close(client_socket);
|
||||
client_socket = -1;
|
||||
struct timeval tv = {.tv_sec = 1, .tv_usec = 0};
|
||||
fd_set read_fd;
|
||||
FD_ZERO(&read_fd);
|
||||
FD_SET(server_socket, &read_fd);
|
||||
if (select(server_socket+1, &read_fd, NULL, NULL, &wait_timeout)) {
|
||||
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
|
||||
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
|
||||
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
|
||||
client_ip = std::string(ip_str.data());
|
||||
_ssl = SSL_new(_context);
|
||||
SSL_set_fd(_ssl, client_socket);
|
||||
if (SSL_accept(_ssl) <= 0) {
|
||||
log::warn << "Unable to open SSL connection with client" << std::endl;
|
||||
client_ip = "";
|
||||
close(client_socket);
|
||||
client_socket = -1;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void openssl_socket::close_conn()
|
||||
|
|
|
@ -14,7 +14,7 @@ class openssl_socket : public anthracite_socket {
|
|||
openssl_socket(int port, int max_queue = MAX_QUEUE_LENGTH);
|
||||
~openssl_socket();
|
||||
|
||||
void wait_for_conn() override;
|
||||
bool wait_for_conn() override;
|
||||
void close_conn() override;
|
||||
void send_message(std::string& msg) override;
|
||||
std::string recv_message(int buffer_size) override;
|
||||
|
|
|
@ -29,13 +29,22 @@ anthracite_socket::anthracite_socket(int port, int max_queue)
|
|||
listen(server_socket, max_queue);
|
||||
}
|
||||
|
||||
void anthracite_socket::wait_for_conn()
|
||||
bool anthracite_socket::wait_for_conn()
|
||||
{
|
||||
client_ip = "";
|
||||
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
|
||||
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
|
||||
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
|
||||
client_ip = std::string(ip_str.data());
|
||||
struct timeval tv = {.tv_sec = 1, .tv_usec = 0};
|
||||
fd_set read_fd;
|
||||
FD_ZERO(&read_fd);
|
||||
FD_SET(server_socket, &read_fd);
|
||||
if (select(server_socket+1, &read_fd, NULL, NULL, &wait_timeout)) {
|
||||
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
|
||||
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
|
||||
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
|
||||
client_ip = std::string(ip_str.data());
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const std::string& anthracite_socket::get_client_ip()
|
||||
|
|
|
@ -14,6 +14,7 @@ namespace anthracite::socket {
|
|||
class anthracite_socket {
|
||||
|
||||
protected:
|
||||
struct timeval wait_timeout = { .tv_sec = 1, .tv_usec = 0};
|
||||
int server_socket;
|
||||
int client_socket {};
|
||||
std::string client_ip;
|
||||
|
@ -27,7 +28,7 @@ public:
|
|||
|
||||
virtual const std::string& get_client_ip() final;
|
||||
|
||||
virtual void wait_for_conn();
|
||||
virtual bool wait_for_conn();
|
||||
virtual void close_conn();
|
||||
virtual void send_message(std::string& msg);
|
||||
virtual std::string recv_message(int buffer_size);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue