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"
8#include "dlna/IUDPService.h"
9
10namespace tiny_dlna {
11
19 public:
20 bool begin(int port) {
22 addr.port = port;
23 return begin(addr);
24 }
25
27 peer = addr;
28 DlnaLogger.log(DlnaLogLevel::Info, "beginMulticast: %s", addr.toString());
29
30 if (udp.listen(addr.port)) {
31 udp.onPacket([&](AsyncUDPPacket packet) {
32 RequestData result;
33 result.peer.address = packet.remoteIP();
34 result.peer.port = packet.remotePort();
35 assert(!(result.peer.address == IPAddress()));
36 // save data
37 result.data.copyFrom((const char*)packet.data(), packet.length());
38
39 // queue.push_back(result);
40 queue.enqueue(result);
41 });
42 }
43
44 if (udp.listenMulticast(addr.address,
45 addr.port)) { // Start listening for UDP Multicast
46 // on AP interface only
47 udp.onPacket([&](AsyncUDPPacket packet) {
48 RequestData result;
49 result.peer.address = packet.remoteIP();
50 result.peer.port = packet.remotePort();
51 assert(!(result.peer.address == IPAddress()));
52 // save data
53 result.data.copyFrom((const char*)packet.data(), packet.length());
54
55 // queue.push_back(result);
56 queue.enqueue(result);
57 });
58 }
59 return true;
60 }
61
62 bool send(uint8_t* data, int len) { return send(peer, data, len); }
63
64 bool send(IPAddressAndPort addr, uint8_t* data, int len) {
65 DlnaLogger.log(DlnaLogLevel::Debug, "sending %d bytes", len);
66 int sent = udp.writeTo(data, len, addr.address, addr.port);
67 if (sent != len) {
68 DlnaLogger.log(DlnaLogLevel::Error, "sending %d bytes -> %d", len, sent);
69 }
70 return sent == len;
71 }
72
74 RequestData result;
75 // if (queue.size()){
76 // result = queue.back();
77 // queue.pop_back();
78 // }
79 queue.dequeue(result);
80 return result;
81 }
82
83 protected:
84 AsyncUDP udp;
86 // Vector<RequestData> queue{50};
88};
89
90} // namespace tiny_dlna
Abstract Interface for UDP API.
Definition: IUDPService.h:34
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
A simple single producer, single consumer lock free queue.
Definition: QueueLockFree.h:14
void copyFrom(const char *source, int len, int maxlen=0)
assigns a memory buffer
Definition: Str.h:93
Access to UDP functionality: sending and receiving of data using the Async API of the ESP32.
Definition: UDPAsyncService.h:18
QueueLockFree< RequestData > queue
Definition: UDPAsyncService.h:87
IPAddressAndPort peer
Definition: UDPAsyncService.h:85
bool begin(IPAddressAndPort addr)
Definition: UDPAsyncService.h:26
AsyncUDP udp
Definition: UDPAsyncService.h:84
bool send(IPAddressAndPort addr, uint8_t *data, int len)
Definition: UDPAsyncService.h:64
RequestData receive()
Definition: UDPAsyncService.h:73
bool begin(int port)
Definition: UDPAsyncService.h:20
bool send(uint8_t *data, int len)
Definition: UDPAsyncService.h:62
Definition: Allocator.h:6
LoggerClass DlnaLogger
Definition: Logger.cpp:5
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:23
IPAddressAndPort peer
Definition: IUDPService.h:25
Str data
Definition: IUDPService.h:24