Example #1
0
void LocalClient::ListenTaskThread::handle_current_packet() {

	PTCLHEADERDATA header;
	protocol_get_header_data(buffer, &header);

	if (header.protocol_id != PROTOCOL_ID) {
		PRINT( "dropping packet. Reason: protocol_id mismatch (%d)\n", header.protocol_id);
		return;
	}

	if (header.sender_id != ID_SERVER) {
		//PRINT( "unexpected sender id. expected ID_SERVER (%x), got %x instead.\n", ID_SERVER, sender_id);
		return;
	}

	const unsigned char &cmd = header.cmd_arg_mask.ch[0];

	switch (cmd) {

		case S_POSITION_UPDATE:
		// don't apply older pupd-package data. we also have an interpolation mechanism :P
			if (header.seq_number > latest_posupd_seq_number) {
				update_positions();
				latest_posupd_seq_number = header.seq_number;
			}
			break;

		case S_PONG: {
			latency_ms = LocalClient::Ping.time_since_last_ping_ms();
			unsigned embedded_seq_number;
			copy_from_buffer(&embedded_seq_number, sizeof(embedded_seq_number), PTCL_HEADER_LENGTH);
			if (embedded_seq_number != Ping._latest_ping_seq_number) {
				PRINT("Received S_PONG from server, but the embedded seq_number (%u) doesn't match with the expected one (%d).\n", embedded_seq_number, Ping._latest_ping_seq_number);
			}
			Ping._latest_ping_seq_number = 0; 
			break;
		}
		case S_CLIENT_CHAT_MESSAGE: {
			// the sender id is embedded into the datafield (bytes 12-14) of the packet
			unsigned short sender_id = 0;
			copy_from_buffer(&sender_id, sizeof(sender_id), PTCL_HEADER_LENGTH);
			auto it = peers.find(sender_id);
			if (it != peers.end()) {
			std::string chatmsg(buffer + PTCL_HEADER_LENGTH + sizeof(sender_id));
				if (!Server::is_running()) {
				PRINT("[%s] <%s>: %s\n", get_timestamp().c_str(), it->second.info.name.c_str(), chatmsg.c_str());
				}
			}
				else {
				// perhaps request a new peer list from the server. :P
				PRINT("warning: server broadcast S_CLIENT_CHAT_MESSAGE with unknown sender id %u!\n", sender_id);
			}
			break;
		}

		case S_SHUTDOWN:
			PRINT( "Client: Received S_SHUTDOWN from server (server going down).\n");
			LocalClient::request_shutdown(); // this will break from the recvfrom loop gracefully
			// this will cause LocalClient::stop() to be called from the main (rendering) thread
			break;

		case C_TERMINATE:
			PRINT("Client:Received C_TERMINATE from self. Stopping.\n");
			LocalClient::request_shutdown();
			break;
	
		case S_PEER_LIST:
			construct_peer_list();
		break;

		case S_CLIENT_DISCONNECT: {
			unsigned short id;
			copy_from_buffer(&id, sizeof(id), PTCL_HEADER_LENGTH);
			auto it = peers.find(id);
			if (it == peers.end()) {
				//PRINT( "Warning: received S_CLIENT_DISCONNECT with unknown id %u.\n", id);
			}
			else {
				PRINT( "Client %s (id %u) disconnected.\n", it->second.info.name.c_str(), id);
				peers.erase(id);
			}
			break;
		}
	
		default:
			PRINT("Warning: received unknown command char %u from server.\n", cmd);
			break;
	}

}
Example #2
0
void ChatDlg::incomingMsg(std::string str)
{
    QString chatmsg(str.c_str());
    ui->textEdit_chat->appendPlainText(chatmsg);
}