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 
21 class UdpConnection;
22 
25 
27 {
28 public:
30  {
31  initialize();
32  }
33 
35  {
36  initialize();
37  }
38 
39  virtual ~UdpConnection()
40  {
41  close();
42  }
43 
44  virtual bool listen(int port);
45  virtual bool connect(IpAddress ip, uint16_t port);
46  virtual void close();
47 
48  // After connect(..)
49  virtual bool send(const char* data, int length);
50 
51  bool sendString(const char* data)
52  {
53  return send(data, strlen(data));
54  }
55 
56  bool sendString(const String& data)
57  {
58  return send(data.c_str(), data.length());
59  }
60 
61  virtual bool sendTo(IpAddress remoteIP, uint16_t remotePort, const char* data, int length);
62 
63  bool sendStringTo(IpAddress remoteIP, uint16_t remotePort, const char* data)
64  {
65  return sendTo(remoteIP, remotePort, data, strlen(data));
66  }
67 
68  bool sendStringTo(IpAddress remoteIP, uint16_t remotePort, const String& data)
69  {
70  return sendTo(remoteIP, remotePort, data.c_str(), data.length());
71  }
72 
81  bool setMulticast(IpAddress ip);
82 
92  bool setMulticastTtl(size_t ttl);
93 
94 protected:
95  virtual void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort);
96 
97 protected:
98  bool initialize(udp_pcb* pcb = nullptr);
99  static void staticOnReceive(void* arg, struct udp_pcb* pcb, struct pbuf* p, LWIP_IP_ADDR_T* addr, u16_t port);
100 
101 protected:
102  udp_pcb* udp = nullptr;
104 };
105 
#define LWIP_IP_ADDR_T
Definition: IpAddress.h:36
bool sendString(const String &data)
Definition: UdpConnection.h:56
virtual bool sendTo(IpAddress remoteIP, uint16_t remotePort, const char *data, int length)
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:43
Definition: UdpConnection.h:26
bool sendStringTo(IpAddress remoteIP, uint16_t remotePort, const char *data)
Definition: UdpConnection.h:63
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:641
UdpConnection(UdpConnectionDataDelegate dataHandler)
Definition: UdpConnection.h:34
The String class.
Definition: WString.h:136
bool sendString(const char *data)
Definition: UdpConnection.h:51
static void staticOnReceive(void *arg, struct udp_pcb *pcb, struct pbuf *p, LWIP_IP_ADDR_T *addr, u16_t port)
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:237
UdpConnectionDataDelegate onDataCallback
Definition: UdpConnection.h:103
bool initialize(udp_pcb *pcb=nullptr)
unsigned short u16_t
Definition: params_test.h:80
virtual ~UdpConnection()
Definition: UdpConnection.h:39
bool setMulticastTtl(size_t ttl)
Sets the UDP multicast Time-To-Live(TTL).
udp_pcb * udp
Definition: UdpConnection.h:102
virtual bool connect(IpAddress ip, uint16_t port)
bool setMulticast(IpAddress ip)
Sets the UDP multicast IP.
virtual bool send(const char *data, int length)
virtual void close()
Definition: IpConnection.h:21
bool sendStringTo(IpAddress remoteIP, uint16_t remotePort, const String &data)
Definition: UdpConnection.h:68
virtual void onReceive(pbuf *buf, IpAddress remoteIP, uint16_t remotePort)
UdpConnection()
Definition: UdpConnection.h:29
virtual bool listen(int port)