void IRCConnection::worker_main() { try { auto connection = uicore::TCPConnection::connect(server); IRCRawString read_line, write_line; IRCRawString::size_type write_pos = 0; std::unique_lock<std::mutex> lock(mutex); while (true) { if (stop_flag) break; if (!read_connection_data(*connection, read_line)) break; write_connection_data(write_line, write_pos, *connection); uicore::NetworkEvent *events[] = { connection.get() }; change_event.wait(lock, 1, events); } queues.set_disconnected(std::string()); uicore::RunLoop::main_thread_async(uicore::bind_member(this, &IRCConnection::process)); } catch (uicore::Exception &e) { queues.set_disconnected(e.message); uicore::RunLoop::main_thread_async(uicore::bind_member(this, &IRCConnection::process)); } }
void IRCConnection::worker_main() { try { //clan::ConsoleWindow console("Debug"); clan::TCPConnection connection(server); connection.set_nodelay(true); connection.set_keep_alive(true, 60*1000, 60*1000); clan::Event read_event = connection.get_read_event(); clan::Event write_event = connection.get_write_event(); IRCRawString read_line, write_line; IRCRawString::size_type write_pos = 0; std::vector<clan::Event> wait_events; bool continue_loop = true; while (continue_loop) { wait_events.clear(); wait_events.push_back(read_event); if (write_line.length() != write_pos) wait_events.push_back(write_event); else wait_events.push_back(queues.send_event); wait_events.push_back(stop_event); int wakeup_reason = clan::Event::wait(wait_events, -1); if (wakeup_reason == 0) // read_event flagged { continue_loop = read_connection_data(connection, read_line); } else if (wakeup_reason == 1) // write_event / queues.send_event flagged { continue_loop = write_connection_data(write_line, write_pos, connection); } else // stop_event flagged { continue_loop = false; } } queues.set_disconnected(std::string()); set_wakeup_event(); } catch (clan::Exception &e) { queues.set_disconnected(e.message); set_wakeup_event(); } }
void NetGameConnectionImpl::connection_main() { try { if (!is_connected) connection = TCPConnection::connect(socket_name); is_connected = true; site->add_network_event(NetGameNetworkEvent(base, NetGameNetworkEvent::client_connected)); int bytes_received = 0; int bytes_sent = 0; const int max_event_packet_size = 32000 + 2; auto receive_buffer = DataBuffer::create(max_event_packet_size); auto send_buffer = DataBuffer::create(0); bool send_graceful_close = false; while (true) { if (read_connection_data(*receive_buffer, bytes_received)) break; if (write_connection_data(*send_buffer, bytes_sent, send_graceful_close)) break; std::unique_lock<std::mutex> lock(mutex); if (stop_flag) break; NetworkEvent *events[] = { connection.get() }; worker_event.wait(lock, 1, events); } site->add_network_event(NetGameNetworkEvent(base, NetGameNetworkEvent::client_disconnected)); } catch (const Exception& e) { site->add_network_event(NetGameNetworkEvent(base, NetGameNetworkEvent::client_disconnected, NetGameEvent(e.message))); } }
void DCCChatConnection::worker_main() { { std::unique_lock<std::mutex> lock(mutex); started_flag = true; started_event.notify_all(); lock.unlock(); } try { std::unique_lock<std::mutex> lock(mutex); clan::TCPConnection connection; if (is_server) { queue_system(clan::string_format("Started listening on port %1", socket_name.get_port())); clan::TCPListen listen(socket_name); while (true) { if (stop_flag) { queue_disconnected(IRCRawString()); return; } clan::SocketName end_point; connection = listen.accept(end_point); if (!connection.is_null()) break; clan::NetworkEvent *events[] = { &listen }; change_event.wait(lock, 1, events); } clan::SocketName remote_name = connection.get_remote_name(); lock.unlock(); try { queue_system(clan::string_format("Accepted connection from %1 port %2", remote_name.lookup_hostname(), remote_name.get_port())); } catch (clan::Exception &) { queue_system(clan::string_format("Accepted connection from %1 port %2", remote_name.get_address(), remote_name.get_port())); } lock.lock(); } else { lock.unlock(); try { queue_system(clan::string_format("Connecting to %1 port %2", socket_name.lookup_hostname(), socket_name.get_port())); } catch (clan::Exception &) { queue_system(clan::string_format("Connecting to %1 port %2", socket_name.get_address(), socket_name.get_port())); } connection = clan::TCPConnection(socket_name); lock.lock(); } IRCRawString read_line, write_line; IRCRawString::size_type write_pos = 0; while (!stop_flag) { if (!read_connection_data(connection, read_line)) break; if (!write_connection_data(write_line, write_pos, connection)) break; clan::NetworkEvent *events[] = { &connection }; change_event.wait(lock, 1, events); } queue_disconnected(IRCRawString()); } catch (clan::Exception &e) { std::unique_lock<std::mutex> lock(mutex); queue_disconnected(e.message); } }