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 
17 #pragma once
18 
19 #include <IpAddress.h>
20 
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 
73 protected:
74  virtual void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort);
75 
76 protected:
77  bool initialize(udp_pcb* pcb = nullptr);
78  static void staticOnReceive(void* arg, struct udp_pcb* pcb, struct pbuf* p, LWIP_IP_ADDR_T* addr, u16_t port);
79 
80 protected:
81  udp_pcb* udp = nullptr;
83 };
84 
#define LWIP_IP_ADDR_T
Definition: IpAddress.h:30
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:37
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:600
UdpConnection(UdpConnectionDataDelegate dataHandler)
Definition: UdpConnection.h:34
The String class.
Definition: WString.h:136
bool sendString(const char *data)
Definition: UdpConnection.h:51
Delegate< void(UdpConnection &connection, char *data, int size, IpAddress remoteIP, uint16_t remotePort)> UdpConnectionDataDelegate
Definition: UdpConnection.h:21
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:228
UdpConnectionDataDelegate onDataCallback
Definition: UdpConnection.h:82
bool initialize(udp_pcb *pcb=nullptr)
unsigned short u16_t
Definition: params_test.h:80
virtual ~UdpConnection()
Definition: UdpConnection.h:39
udp_pcb * udp
Definition: UdpConnection.h:81
virtual bool connect(IpAddress ip, uint16_t port)
virtual bool send(const char *data, int length)
virtual void close()
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)