Have Server take list of listen fds and add createLocalConnection
This commit is contained in:
129
src/main.cpp
129
src/main.cpp
@@ -6,7 +6,14 @@
|
||||
#include "server.hpp"
|
||||
#include <atomic>
|
||||
#include <csignal>
|
||||
#include <fcntl.h>
|
||||
#include <iostream>
|
||||
#include <netdb.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
PERFETTO_TRACK_EVENT_STATIC_STORAGE();
|
||||
|
||||
@@ -21,6 +28,117 @@ void signal_handler(int sig) {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> create_listen_sockets(const weaseldb::Config &config) {
|
||||
std::vector<int> listen_fds;
|
||||
|
||||
// Check if unix socket path is specified
|
||||
if (!config.server.unix_socket_path.empty()) {
|
||||
// Create unix socket
|
||||
int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sfd == -1) {
|
||||
perror("socket");
|
||||
throw std::runtime_error("Failed to create unix socket");
|
||||
}
|
||||
|
||||
// Remove existing socket file if it exists
|
||||
unlink(config.server.unix_socket_path.c_str());
|
||||
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
|
||||
if (config.server.unix_socket_path.length() >= sizeof(addr.sun_path)) {
|
||||
close(sfd);
|
||||
throw std::runtime_error("Unix socket path too long");
|
||||
}
|
||||
|
||||
strncpy(addr.sun_path, config.server.unix_socket_path.c_str(),
|
||||
sizeof(addr.sun_path) - 1);
|
||||
|
||||
if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
|
||||
perror("bind");
|
||||
close(sfd);
|
||||
throw std::runtime_error("Failed to bind unix socket");
|
||||
}
|
||||
|
||||
if (listen(sfd, SOMAXCONN) == -1) {
|
||||
perror("listen");
|
||||
close(sfd);
|
||||
throw std::runtime_error("Failed to listen on unix socket");
|
||||
}
|
||||
|
||||
listen_fds.push_back(sfd);
|
||||
return listen_fds;
|
||||
}
|
||||
|
||||
// TCP socket creation
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result, *rp;
|
||||
int s;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
||||
hints.ai_socktype = SOCK_STREAM; /* stream socket */
|
||||
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
|
||||
hints.ai_protocol = 0; /* Any protocol */
|
||||
hints.ai_canonname = nullptr;
|
||||
hints.ai_addr = nullptr;
|
||||
hints.ai_next = nullptr;
|
||||
|
||||
s = getaddrinfo(config.server.bind_address.c_str(),
|
||||
std::to_string(config.server.port).c_str(), &hints, &result);
|
||||
if (s != 0) {
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
||||
throw std::runtime_error("Failed to resolve bind address");
|
||||
}
|
||||
|
||||
int sfd = -1;
|
||||
for (rp = result; rp != nullptr; rp = rp->ai_next) {
|
||||
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
if (sfd == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int val = 1;
|
||||
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) == -1) {
|
||||
perror("setsockopt SO_REUSEADDR");
|
||||
close(sfd);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Enable TCP_NODELAY for low latency (only for TCP sockets)
|
||||
if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) {
|
||||
if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1) {
|
||||
perror("setsockopt TCP_NODELAY");
|
||||
close(sfd);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0) {
|
||||
break; /* Success */
|
||||
}
|
||||
|
||||
close(sfd);
|
||||
sfd = -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(result);
|
||||
|
||||
if (rp == nullptr || sfd == -1) {
|
||||
throw std::runtime_error("Could not bind to any address");
|
||||
}
|
||||
|
||||
if (listen(sfd, SOMAXCONN) == -1) {
|
||||
perror("listen");
|
||||
close(sfd);
|
||||
throw std::runtime_error("Failed to listen on socket");
|
||||
}
|
||||
|
||||
listen_fds.push_back(sfd);
|
||||
return listen_fds;
|
||||
}
|
||||
|
||||
void print_help(const char *program_name) {
|
||||
std::cout << "WeaselDB - High-performance write-side database component\n\n";
|
||||
std::cout << "Usage: " << program_name << " [OPTIONS]\n\n";
|
||||
@@ -117,9 +235,18 @@ int main(int argc, char *argv[]) {
|
||||
<< config->subscription.keepalive_interval.count() << " seconds"
|
||||
<< std::endl;
|
||||
|
||||
// Create listen sockets
|
||||
std::vector<int> listen_fds;
|
||||
try {
|
||||
listen_fds = create_listen_sockets(*config);
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Failed to create listen sockets: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create handler and server
|
||||
HttpHandler http_handler;
|
||||
auto server = Server::create(*config, http_handler);
|
||||
auto server = Server::create(*config, http_handler, listen_fds);
|
||||
g_server = server.get();
|
||||
|
||||
// Setup signal handling
|
||||
|
||||
Reference in New Issue
Block a user