bool DCCChatConnection::write_connection_data(IRCRawString &write_line, IRCRawString::size_type &write_pos, clan::TCPConnection &connection) { while (true) { if (write_line.length() != write_pos) { int bytes_written = connection.write(write_line.data() + write_pos, write_line.length() - write_pos); if (bytes_written == -1) return true; write_pos += bytes_written; } else { write_line.clear(); write_pos = 0; if (!send_queue.empty()) { if (send_queue.front().type == Message::type_disconnect) { send_queue.erase(send_queue.begin()); return false; } else { write_line = send_queue.front().text + "\r\n"; send_queue.erase(send_queue.begin()); } } } } }
void DCCChatConnection::process() { std::vector<Message> received_messages; std::unique_lock<std::mutex> lock(mutex); received_messages.swap(receive_queue); lock.unlock(); for (size_t i = 0; i < received_messages.size(); i++) { switch (received_messages[i].type) { case Message::type_system: sig_system_text(received_messages[i].text); break; case Message::type_text: { IRCRawString text = received_messages[i].text; if (text.length() >= 2 && text.substr(text.length() - 2, 2) == "\r\n") text = text.substr(0, text.length() - 2); else if (text.length() >= 1 && text[text.length() - 1] == '\n') text = text.substr(0, text.length() - 1); sig_text_line(IRCText::from_raw(text)); } break; case Message::type_disconnect: sig_disconnected(received_messages[i].text); break; } } }
bool IRCConnection::read_connection_data(uicore::TCPConnection &connection, IRCRawString &read_line) { while (true) { char buffer[16 * 1024]; int data_read = connection.read(buffer, 16 * 1024); if (data_read == -1) return true; else if (data_read == 0) // EOF from server return false; int start_pos = 0; for (int i = 0; i < data_read; i++) { if (buffer[i] == '\n') { read_line.append(buffer + start_pos, i - start_pos + 1); start_pos = i + 1; queues.push_received(read_line); uicore::RunLoop::main_thread_async(uicore::bind_member(this, &IRCConnection::process)); read_line.clear(); } } read_line.append(buffer + start_pos, data_read - start_pos); } }
bool DCCChatConnection::read_connection_data(clan::TCPConnection &connection, IRCRawString &read_line) { while (true) { char buffer[16 * 1024]; int data_read = connection.read(buffer, 16 * 1024); if (data_read == -1) return true; if (data_read == 0) // EOF from server return false; int start_pos = 0; for (int i = 0; i<data_read; i++) { if (buffer[i] == '\n') { read_line.append(buffer + start_pos, i - start_pos + 1); start_pos = i + 1; queue_line(read_line); read_line.clear(); } } read_line.append(buffer + start_pos, data_read - start_pos); } }
void IRCConnection::process() { while (true) { std::unique_lock<std::mutex> lock(mutex); IRCRawString line = queues.pop_received(); if (line.empty()) break; lock.unlock(); IRCMessage message = IRCMessage::parse_line(line); // * This is broken - We do not know if the IRC server resets the ping interval when it either recieves or sends any command successfully * //if (message.get_type() == IRCMessage::type_ping) // calculate_ping_interval(); if (cb_message_received) cb_message_received(message); } std::unique_lock<std::mutex> lock(mutex); std::string reason; bool was_disconnected = queues.pop_disconnected(reason); lock.unlock(); if (was_disconnected && cb_disconnected) { ping_timeout_timer->stop(); cb_disconnected(reason); } }
bool IRCConnection::write_connection_data(IRCRawString &write_line, IRCRawString::size_type &write_pos, clan::TCPConnection &connection) { if (write_line.length() != write_pos) { //int old_pos = write_pos; write_pos += connection.write(write_line.data()+write_pos, write_line.length()-write_pos); //clan::Console::write_line(std::string8(write_line.data()+old_pos, write_pos-old_pos, false)); } else { write_line = queues.pop_send(); write_pos = 0; } return true; }
void IRCSession::extract_ctcp_command(const IRCText &ctcp_data, IRCRawString &command, IRCRawString &data) { command = ctcp_data.to_raw(); data = ctcp_data.to_raw(); IRCRawString::size_type pos = command.find(" "); if (pos != IRCRawString::npos) { command = command.substr(0, pos); data = data.substr(pos+1); } else { data.clear(); } }
void IRCConnection::send_join(const IRCChannel &channel, const IRCRawString &password) { std::vector<IRCRawString> params; params.push_back(channel.to_raw()); if (!password.empty()) params.push_back(password); send_command("JOIN", params); }
void IRCConnection::send_pong(const IRCRawString &daemon1, const IRCRawString &daemon2) { std::vector<IRCRawString> params; params.push_back(daemon1); if (!daemon2.empty()) params.push_back(daemon2); send_command("PONG", params); }
void IRCConnection::process() { while (true) { IRCRawString line = queues.pop_received(); if (line.empty()) break; IRCMessage message = IRCMessage::parse_line(line); if (cb_message_received) cb_message_received(message); } std::string reason; bool was_disconnected = queues.pop_disconnected(reason); if (was_disconnected && cb_disconnected) cb_disconnected(reason); }
void IRCConnection::write_connection_data(IRCRawString &write_line, IRCRawString::size_type &write_pos, uicore::TCPConnection &connection) { while (true) { if (write_line.length() != write_pos) { int result = connection.write(write_line.data() + write_pos, (int)(write_line.length() - write_pos)); if (result == -1) break; write_pos += result; } else { write_line = queues.pop_send(); write_pos = 0; if (write_line.empty()) break; } } }
bool IRCConnection::read_connection_data(clan::TCPConnection &connection, IRCRawString &read_line) { char buffer[16*1024]; int data_read = connection.read(buffer, 16*1024, false); if (data_read == 0) // EOF from server { connection.disconnect_graceful(); return false; } int start_pos = 0; for (int i=0; i<data_read; i++) { if (buffer[i] == '\n') { read_line.append(buffer+start_pos, i-start_pos+1); start_pos = i+1; queues.push_received(read_line); set_wakeup_event(); read_line.clear(); } } read_line.append(buffer+start_pos, data_read-start_pos); return true; }
void IRCSession::on_unknown_message(const IRCMessage &message) { IRCRawString s = IRCMessage::create_line(message.get_prefix().to_raw(), message.get_command(), message.get_params()); cb_system_text(IRCText::from_raw(s.substr(0, s.length()-2))); }