Arduino DLNA Server
UDPAsyncService.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <AsyncUDP.h>
4 #include <WiFi.h>
5 
6 #include "basic/QueueLockFree.h"
7 #include "dlna/IUDPService.h"
8 #include "assert.h"
9 
10 namespace tiny_dlna {
11 
18 class UDPAsyncService : public IUDPService {
19  public:
20  bool begin(IPAddressAndPort addr) {
21  peer = addr;
22  DlnaLogger.log(DlnaInfo, "beginMulticast: %s", addr.toString());
23 
24  if (udp.listenMulticast(addr.address,
25  addr.port)) { // Start listening for UDP Multicast
26  // on AP interface only
27  udp.onPacket([&](AsyncUDPPacket packet) {
28  RequestData result;
29  result.peer.address = packet.remoteIP();
30  result.peer.port = packet.remotePort();
31  assert(!(result.peer.address == IPAddress()));
32  // save data
33  result.data.copyFrom((const char*)packet.data(), packet.length());
34 
35  //queue.push_back(result);
36  queue.enqueue(result);
37  });
38  }
39  return true;
40  }
41 
42  bool send(uint8_t* data, int len) { return send(peer, data, len); }
43 
44  bool send(IPAddressAndPort addr, uint8_t* data, int len) {
45  DlnaLogger.log(DlnaDebug, "sending %d bytes", len);
46  int sent = udp.writeTo(data, len, addr.address, addr.port);
47  if(sent != len){
48  DlnaLogger.log(DlnaError, "sending %d bytes -> %d", len, sent);
49  }
50  return sent == len;
51  }
52 
54  RequestData result;
55  // if (queue.size()){
56  // result = queue.back();
57  // queue.pop_back();
58  // }
59  queue.dequeue(result);
60  return result;
61  }
62 
63  protected:
64  AsyncUDP udp;
66 // Vector<RequestData> queue{50};
68 };
69 
70 } // 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:94
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:67
IPAddressAndPort peer
Definition: UDPAsyncService.h:65
bool begin(IPAddressAndPort addr)
Definition: UDPAsyncService.h:20
AsyncUDP udp
Definition: UDPAsyncService.h:64
bool send(IPAddressAndPort addr, uint8_t *data, int len)
Definition: UDPAsyncService.h:44
RequestData receive()
Definition: UDPAsyncService.h:53
bool send(uint8_t *data, int len)
Definition: UDPAsyncService.h:42
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
@ DlnaInfo
Definition: Logger.h:16
@ DlnaError
Definition: Logger.h:16
LoggerClass DlnaLogger
Definition: Logger.cpp:5
IP Adress including Port information.
Definition: IPAddressAndPort.h:13
int port
Definition: IPAddressAndPort.h:17
const char * toString()
Definition: IPAddressAndPort.h:19
IPAddress address
Definition: IPAddressAndPort.h:16
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