void NetworkClient::send_datagram(DatagramHandle dg) { std::lock_guard<std::recursive_mutex> lock(m_lock); //TODO: make this asynch if necessary dgsize_t len = swap_le(dg->size()); try { m_socket->non_blocking(true); m_socket->native_non_blocking(true); std::list<boost::asio::const_buffer> gather; gather.push_back(boost::asio::buffer((uint8_t*)&len, sizeof(dgsize_t))); gather.push_back(boost::asio::buffer(dg->get_data(), dg->size())); socket_write(gather); } catch(const boost::system::system_error &err) { // We assume that the message just got dropped if the remote end died // before we could send it. send_disconnect(err.code()); } }
void EventLogger::process_packet(DatagramHandle dg, const uvw::Addr& sender) { DatagramIterator dgi(dg); std::stringstream stream; try { msgpack_decode(stream, dgi); } catch(DatagramIteratorEOF&) { m_log.error() << "Received truncated packet from " << sender.ip << ":" << sender.port << std::endl; return; } if(dgi.tell() != dg->size()) { m_log.error() << "Received packet with extraneous data from " << sender.ip << ":" << sender.port << std::endl; return; } std::string data = stream.str(); m_log.trace() << "Received: " << data << std::endl; // This is a little bit of a kludge, but we should make sure we got a // MessagePack map as the event log element, and not some other type. The // easiest way to do this is to make sure that the JSON representation // begins with { if(data[0] != '{') { m_log.error() << "Received non-map event log from " << sender.ip << ":" << sender.port << ": " << data << std::endl; return; } // Now let's insert our timestamp: time_t rawtime; time(&rawtime); char timestamp[64]; strftime(timestamp, 64, "{\"_time\": \"%Y-%m-%d %H:%M:%S%z\", ", localtime(&rawtime)); *m_file.get() << timestamp << data.substr(1) << std::endl; }