Arduino DLNA Server
Loading...
Searching...
No Matches
UDPAsyncService.h
Go to the documentation of this file.
1#pragma once
2
3#include <AsyncUDP.h>
4#include <WiFi.h>
5
6#include "assert.h"
7#include "basic/Queue.h"
10
11namespace tiny_dlna {
12
13// forward-declare RequestData so we can use Queue<RequestData> in this header
14struct RequestData;
15
23 public:
24 bool begin(int port) {
26 addr.port = port;
27 return begin(addr);
28 }
29
31 peer = addr;
32 DlnaLogger.log(DlnaLogLevel::Info, "beginMulticast: %s", addr.toString());
33
34 if (udp.listen(addr.port)) {
35 udp.onPacket([&](AsyncUDPPacket packet) {
36 RequestData result;
37 result.peer.address = packet.remoteIP();
38 result.peer.port = packet.remotePort();
39 assert(!(result.peer.address == IPAddress()));
40 // save data
41 result.data.copyFrom((const char*)packet.data(), packet.length());
42
43 // queue.push_back(result);
44 bool ok = queue.enqueue(result);
45 if (!ok) {
46 DlnaLogger.log(DlnaLogLevel::Warning,
47 "UDPAsyncService::enqueue failed: queue full, packet "
48 "%d bytes, queueSize=%d",
49 packet.length(), (int)queue.size());
50 }
51 });
52 }
53
54 if (udp.listenMulticast(addr.address,
55 addr.port)) { // Start listening for UDP Multicast
56 // on AP interface only
57 udp.onPacket([&](AsyncUDPPacket packet) {
58 RequestData result;
59 result.peer.address = packet.remoteIP();
60 result.peer.port = packet.remotePort();
61 assert(!(result.peer.address == IPAddress()));
62 // save data
63 result.data.copyFrom((const char*)packet.data(), packet.length());
64
65 // queue.push_back(result);
66 bool ok = queue.enqueue(result);
67 if (!ok) {
68 DlnaLogger.log(DlnaLogLevel::Warning,
69 "UDPAsyncService::enqueue failed: multicast queue "
70 "full, packet %d bytes, queueSize=%d",
71 packet.length(), (int)queue.size());
72 }
73 DlnaLogger.log(DlnaLogLevel::Info,
74 "UDPAsyncService::receive: received %d bytes",
75 packet.length());
76 });
77 }
78 return true;
79 }
80
81 bool send(uint8_t* data, int len) { return send(peer, data, len); }
82
83 bool send(IPAddressAndPort addr, uint8_t* data, int len) {
84 DlnaLogger.log(DlnaLogLevel::Debug, "sending %d bytes", len);
85 int sent = udp.writeTo(data, len, addr.address, addr.port);
86 if (sent != len) {
87 DlnaLogger.log(DlnaLogLevel::Error, "sending %d bytes -> %d", len, sent);
88 }
89 return sent == len;
90 }
91
93 RequestData result;
94 queue.dequeue(result);
95 return result;
96 }
97
98 protected:
99 AsyncUDP udp;
101 // keep the lock-free variant available but commented out for now
102 // QueueLockFree<RequestData> queue{50};
103 // use simple FIFO queue (backed by List)
105};
106
107} // namespace tiny_dlna
Abstract Interface for UDP API.
Definition: IUDPService.h:33
FIFO Queue which is based on a List.
Definition: Queue.h:14
void copyFrom(const char *source, int len)
Copy from raw buffer with length.
Definition: Str.h:60
Access to UDP functionality: sending and receiving of data using the Async API of the ESP32.
Definition: UDPAsyncService.h:22
IPAddressAndPort peer
Definition: UDPAsyncService.h:100
bool begin(IPAddressAndPort addr)
Initialize UDP service on specified address and port.
Definition: UDPAsyncService.h:30
AsyncUDP udp
Definition: UDPAsyncService.h:99
Queue< RequestData > queue
Definition: UDPAsyncService.h:104
bool send(IPAddressAndPort addr, uint8_t *data, int len)
Send data to specified address and port.
Definition: UDPAsyncService.h:83
RequestData receive()
Receive incoming UDP data and peer information.
Definition: UDPAsyncService.h:92
bool begin(int port)
Initialize UDP service on specified port.
Definition: UDPAsyncService.h:24
bool send(uint8_t *data, int len)
Send data to the default destination.
Definition: UDPAsyncService.h:81
Definition: Allocator.h:13
IP Adress including Port information.
Definition: IPAddressAndPort.h:20
const char * toString()
Definition: IPAddressAndPort.h:26
int port
Definition: IPAddressAndPort.h:24
IPAddress address
Definition: IPAddressAndPort.h:23
Provides information of the received UDP which consists of the (xml) data and the peer address and po...
Definition: IUDPService.h:22
IPAddressAndPort peer
Definition: IUDPService.h:24
Str data
Definition: IUDPService.h:23