/*---------------------------------------------------------------------- | PLT_SsdpSender::SendSsdp +---------------------------------------------------------------------*/ NPT_Result PLT_SsdpSender::SendSsdp(NPT_HttpResponse& response, const char* usn, const char* target, NPT_UdpSocket& socket, bool notify, const NPT_SocketAddress* addr /* = NULL */) { NPT_CHECK_SEVERE(FormatPacket(response, usn, target, socket, notify)); // logging NPT_LOG_FINE("Sending SSDP:"); PLT_LOG_HTTP_MESSAGE(NPT_LOG_LEVEL_FINE, &response); // use a memory stream to write all the data NPT_MemoryStream stream; NPT_Result res = response.Emit(stream); if (NPT_FAILED(res)) return res; // copy stream into a data packet and send it NPT_LargeSize size; stream.GetSize(size); if (size != (NPT_Size)size) return NPT_ERROR_OUT_OF_RANGE; NPT_DataBuffer packet(stream.GetData(), (NPT_Size)size); return socket.Send(packet, addr); }
/*---------------------------------------------------------------------- | PLT_SsdpSender::SendSsdp +---------------------------------------------------------------------*/ NPT_Result PLT_SsdpSender::SendSsdp(NPT_HttpRequest& request, const char* usn, const char* target, NPT_UdpSocket& socket, bool notify, const NPT_SocketAddress* addr /* = NULL */) { NPT_CHECK_SEVERE(FormatPacket(request, usn, target, socket, notify)); // logging NPT_String prefix = NPT_String::Format("Sending SSDP %s packet for %s", (const char*)request.GetMethod(), usn); PLT_LOG_HTTP_MESSAGE(NPT_LOG_LEVEL_FINER, prefix, &request); // use a memory stream to write all the data NPT_MemoryStream stream; NPT_Result res = request.Emit(stream); NPT_CHECK(res); // copy stream into a data packet and send it NPT_LargeSize size; stream.GetSize(size); if (size != (NPT_Size)size) NPT_CHECK(NPT_ERROR_OUT_OF_RANGE); NPT_DataBuffer packet(stream.GetData(), (NPT_Size)size); NPT_CHECK_WARNING(socket.Send(packet, addr)); return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | UdpServerLoop +---------------------------------------------------------------------*/ static void UdpServerLoop(int port) { NPT_UdpSocket listener; // info if (Options.verbose) { NPT_Debug("listening on port %d\n", port); } NPT_Result result = listener.Bind(NPT_SocketAddress(NPT_IpAddress::Any, port)); if (NPT_FAILED(result)) { NPT_Debug("ERROR: Bind() failed (%d)\n", result); } // packet loop NPT_DataBuffer packet(32768); NPT_SocketAddress address; do { result = listener.Receive(packet, &address); if (NPT_SUCCEEDED(result)) { if (Options.verbose) { NPT_String ip = address.GetIpAddress().ToString(); NPT_Debug("Received %d bytes from %s:%d\n", packet.GetDataSize(), ip.GetChars(), address.GetPort()); } listener.Send(packet, &address); } } while (NPT_SUCCEEDED(result)); }
/*---------------------------------------------------------------------- | NPT_LogUdpHandler::Log +---------------------------------------------------------------------*/ void NPT_LogUdpHandler::Log(const NPT_LogRecord& record) { // format the record NPT_String msg; NPT_LogTcpHandler::FormatRecord(record, msg); // send it in a datagram NPT_DataBuffer buffer(msg.GetChars(), msg.GetLength()+1, false); m_Socket.Send(buffer, &m_Target); }
void Server::sendReply(std::string host, int port, std::string msg) { NPT_UdpSocket socket; NPT_DataBuffer buffer; NPT_IpAddress ip; NPT_SocketAddress address; ip.Parse(host.c_str()); address.SetIpAddress(ip); address.SetPort(port); buffer.SetData((NPT_Byte*)msg.c_str(), msg.length()); socket.Send(buffer, &address); }