MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ start()

bool Lila::Server::start ( )
noexcept

Starts the server and begins accepting clients.

Returns
True if server started successfully, false otherwise

Definition at line 88 of file Server.cpp.

89{
90 if (m_running) {
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
103 m_server_fd = socket(AF_INET, SOCK_STREAM, 0);
104 if (m_server_fd < 0) {
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
113 if (set_nonblocking(m_server_fd) < 0) {
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
126 if (listen(m_server_fd, 10) < 0) {
127 throw std::system_error(socket_errno(), std::system_category(), "listen failed");
128 }
129
130 m_running = true;
131 m_event_bus.publish(StreamEvent { EventType::ServerStart });
132 if (m_start_handler) {
134 }
135
136 m_server_thread = ServerThread([this](const auto& token) { server_loop(token); });
137
138 LILA_INFO(Emitter::SERVER, "Server started on port " + std::to_string(m_port));
139
140 m_event_bus.publish(StreamEvent { EventType::ServerStart });
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()) + ")");
146 if (m_server_fd >= 0) {
148 m_server_fd = -1;
149 }
150#ifdef MAYAFLUX_PLATFORM_WINDOWS
151 WSACleanup();
152#endif
153 return false;
154 }
155}
#define LILA_WARN(emitter, msg)
#define LILA_ERROR(emitter, msg)
#define LILA_INFO(emitter, msg)
static MayaFlux::Nodes::ProcessingToken token
Definition Timers.cpp:8
void publish(const StreamEvent &event)
Publish an event to all subscribers of its type.
Definition EventBus.cpp:18
ServerThread m_server_thread
Server thread.
Definition Server.hpp:165
std::atomic< bool > m_running
Server running state.
Definition Server.hpp:164
int m_server_fd
Server socket file descriptor.
Definition Server.hpp:163
int m_port
TCP port to listen on.
Definition Server.hpp:162
StartHandler m_start_handler
Handler for server start.
Definition Server.hpp:170
EventBus m_event_bus
Event bus for publishing server events.
Definition Server.hpp:171
void server_loop(const ServerThread::StopToken &stop_token)
Main server loop; accepts and manages clients.
Definition Server.cpp:194
static int set_nonblocking(int fd)
Definition Server.cpp:62
static void socket_close(int fd)
Definition Server.cpp:43
static int socket_errno()
Definition Server.cpp:24

References LILA_ERROR, LILA_INFO, LILA_WARN, m_event_bus, m_port, m_running, m_server_fd, m_server_thread, m_start_handler, Lila::EventBus::publish(), server_loop(), Lila::set_nonblocking(), Lila::socket_close(), Lila::socket_errno(), and token.

+ Here is the call graph for this function: