UdpConnection.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * UdpConnection.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include <Network/IpConnection.h>
14 #include <lwip/udp.h>
15 
22 class UdpConnection;
23 
25  Delegate<void(UdpConnection& connection, char* data, int size, IpAddress remoteIP, uint16_t remotePort)>;
26 
28 {
29 public:
31  {
32  initialize();
33  }
34 
36  {
37  initialize();
38  }
39 
40  virtual ~UdpConnection()
41  {
42  close();
43  }
44 
45  virtual bool listen(int port);
46  virtual bool connect(IpAddress ip, uint16_t port);
47  virtual void close();
48 
49  // After connect(..)
50  virtual bool send(const char* data, int length);
51 
52  bool sendString(const char* data)
53  {
54  return send(data, strlen(data));
55  }
56 
57  bool sendString(const String& data)
58  {
59  return send(data.c_str(), data.length());
60  }
61 
62  virtual bool sendTo(IpAddress remoteIP, uint16_t remotePort, const char* data, int length);
63 
64  bool sendStringTo(IpAddress remoteIP, uint16_t remotePort, const char* data)
65  {
66  return sendTo(remoteIP, remotePort, data, strlen(data));
67  }
68 
69  bool sendStringTo(IpAddress remoteIP, uint16_t remotePort, const String& data)
70  {
71  return sendTo(remoteIP, remotePort, data.c_str(), data.length());
72  }
73 
83 
93  bool setMulticastTtl(size_t ttl);
94 
95 protected:
96  virtual void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort);
97 
98 protected:
99  bool initialize(udp_pcb* pcb = nullptr);
100  static void staticOnReceive(void* arg, struct udp_pcb* pcb, struct pbuf* p, LWIP_IP_ADDR_T* addr, u16_t port);
101 
102 protected:
103  udp_pcb* udp = nullptr;
105 };
106 
#define LWIP_IP_ADDR_T
Definition: IpAddress.h:37
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:45
Definition: IpConnection.h:22
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
Definition: UdpConnection.h:28
UdpConnectionDataDelegate onDataCallback
Definition: UdpConnection.h:104
virtual bool sendTo(IpAddress remoteIP, uint16_t remotePort, const char *data, int length)
virtual bool listen(int port)
bool setMulticast(IpAddress ip)
Sets the UDP multicast IP.
bool sendStringTo(IpAddress remoteIP, uint16_t remotePort, const char *data)
Definition: UdpConnection.h:64
UdpConnection()
Definition: UdpConnection.h:30
bool setMulticastTtl(size_t ttl)
Sets the UDP multicast Time-To-Live(TTL).
virtual void close()
virtual ~UdpConnection()
Definition: UdpConnection.h:40
virtual bool send(const char *data, int length)
udp_pcb * udp
Definition: UdpConnection.h:103
virtual bool connect(IpAddress ip, uint16_t port)
bool sendStringTo(IpAddress remoteIP, uint16_t remotePort, const String &data)
Definition: UdpConnection.h:69
bool initialize(udp_pcb *pcb=nullptr)
static void staticOnReceive(void *arg, struct udp_pcb *pcb, struct pbuf *p, LWIP_IP_ADDR_T *addr, u16_t port)
virtual void onReceive(pbuf *buf, IpAddress remoteIP, uint16_t remotePort)
bool sendString(const char *data)
Definition: UdpConnection.h:52
bool sendString(const String &data)
Definition: UdpConnection.h:57
UdpConnection(UdpConnectionDataDelegate dataHandler)
Definition: UdpConnection.h:35
unsigned short u16_t
Definition: params_test.h:80