Starts the server and begins accepting clients.
89{
91 LILA_WARN(Emitter::SERVER,
"Server already running");
92 return false;
93 }
94
95 try {
96#ifdef MAYAFLUX_PLATFORM_WINDOWS
97 WSADATA wsaData;
98 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
99 throw std::system_error(WSAGetLastError(), std::system_category(), "WSAStartup failed");
100 }
101#endif
102
105 throw std::system_error(
socket_errno(), std::system_category(),
"socket creation failed");
106 }
107
108 int opt = 1;
109 if (setsockopt(
m_server_fd, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*
>(&opt),
sizeof(opt)) < 0) {
110 throw std::system_error(
socket_errno(), std::system_category(),
"setsockopt failed");
111 }
112
114 throw std::system_error(
socket_errno(), std::system_category(),
"set non-blocking failed");
115 }
116
117 sockaddr_in address {};
118 address.sin_family = AF_INET;
119 address.sin_addr.s_addr = INADDR_ANY;
120 address.sin_port = htons(
m_port);
121
122 if (bind(
m_server_fd,
reinterpret_cast<sockaddr*
>(&address),
sizeof(address)) < 0) {
123 throw std::system_error(
socket_errno(), std::system_category(),
"bind failed");
124 }
125
127 throw std::system_error(
socket_errno(), std::system_category(),
"listen failed");
128 }
129
134 }
135
137
138 LILA_INFO(Emitter::SERVER,
"Server started on port " + std::to_string(
m_port));
139
141
142 return true;
143
144 } catch (const std::system_error& e) {
145 LILA_ERROR(Emitter::SERVER,
"Failed to start server: " + std::string(e.what()) +
" (code: " + std::to_string(e.code().value()) +
")");
149 }
150#ifdef MAYAFLUX_PLATFORM_WINDOWS
151 WSACleanup();
152#endif
153 return false;
154 }
155}
static MayaFlux::Nodes::ProcessingToken token
void publish(const StreamEvent &event)
Publish an event to all subscribers of its type.
ServerThread m_server_thread
Server thread.
std::atomic< bool > m_running
Server running state.
int m_server_fd
Server socket file descriptor.
int m_port
TCP port to listen on.
StartHandler m_start_handler
Handler for server start.
EventBus m_event_bus
Event bus for publishing server events.
void server_loop(const ServerThread::StopToken &stop_token)
Main server loop; accepts and manages clients.
static int set_nonblocking(int fd)
static void socket_close(int fd)
static int socket_errno()