Arduino DLNA Server
UDPService.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <WiFi.h>
4 #include <WiFiUdp.h>
5 #include "dlna/IUDPService.h"
6 #include "assert.h"
7 
8 namespace tiny_dlna {
9 
16 class UDPService : public IUDPService {
17  public:
18 
19  bool begin(IPAddressAndPort addr) {
20  peer = addr;
21  DlnaLogger.log(DlnaInfo, "beginMulticast: %s", addr.toString());
22  return udp.beginMulticast(addr.address, addr.port);
23  }
24 
25  bool send(uint8_t *data, int len) { return send(peer, data, len); }
26 
27  bool send(IPAddressAndPort addr, uint8_t *data, int len) {
28  DlnaLogger.log(DlnaDebug, "sending %d bytes", len);
29  udp.beginPacket(addr.address, addr.port);
30  int sent = udp.write(data, len);
31  assert(sent == len);
32  bool result = udp.endPacket();
33  if (!result) {
34  DlnaLogger.log(DlnaDebug, "Sending failed");
35  }
36  return result;
37  }
38 
40  RequestData result;
41  // udp.flush();
42  int packetSize = udp.parsePacket();
43  if (packetSize > 0) {
44  result.peer.address = udp.remoteIP();
45  result.peer.port = udp.remotePort();
46  char tmp[packetSize + 1] = {0};
47  int len = udp.readBytes(tmp, len);
48  result.data = tmp;
49  DlnaLogger.log(DlnaDebug, "(%s [%d])->: %s", result.peer.toString(),
50  packetSize, tmp);
51  }
52  return result;
53  }
54 
55  protected:
56  WiFiUDP udp;
58 };
59 
60 } // 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
Access to UDP functionality: sending and receiving of data It seems that the UDP receive is not worki...
Definition: UDPService.h:16
RequestData receive()
Definition: UDPService.h:39
IPAddressAndPort peer
Definition: UDPService.h:57
bool send(uint8_t *data, int len)
Definition: UDPService.h:25
bool send(IPAddressAndPort addr, uint8_t *data, int len)
Definition: UDPService.h:27
bool begin(IPAddressAndPort addr)
Definition: UDPService.h:19
WiFiUDP udp
Definition: UDPService.h:56
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
@ DlnaInfo
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