123
Diff: UdpSocket.h
- Revision:
- 14:7648334eb41b
- Parent:
- 11:647d53d146f1
diff -r 95c00132cd98 -r 7648334eb41b UdpSocket.h --- a/UdpSocket.h Sat Aug 31 20:34:52 2019 +0000 +++ b/UdpSocket.h Tue Sep 03 09:16:55 2019 +0000 @@ -22,6 +22,7 @@ #define UDPSOCKET_h #include "mbed.h" +#include "SocketAddress.h" #include "utility/Udp.h" #include "IpAddress.h" #include "utility/MemPool.h" @@ -42,16 +43,23 @@ bool send; } uip_udp_userdata_t; +class UipEthernet; + class UdpSocket : public Udp { private: + uip_udp_userdata_t appdata; struct uip_udp_conn* _uip_udp_conn; - uip_udp_userdata_t appdata; + SocketAddress _remote_addr; + int _timeout_ms; public: - UdpSocket(); // Constructor - virtual ~UdpSocket() { } // Virtual destructor - uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use - void stop(); // Finish with the UDP socket + UdpSocket(); // Constructor + UdpSocket(int timeout_ms); // Constructor + UdpSocket(UipEthernet* ethernet, int timeout_ms = 1000); + virtual ~UdpSocket() { } // Virtual destructor + uint8_t begin(uint16_t port); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use + void stop(); // Finish with the UDP socket + void close(); // Close the UDP socket // Sending UDP packets // Start building up a packet to send to the remote host specific in ip and port // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port @@ -77,18 +85,18 @@ int parsePacket(); // Number of bytes remaining in the current packet - int available(); + size_t available(); // Read a single byte from the current packet int read(); // Read up to len bytes from the current packet and place them into buffer // Returns the number of bytes read, or 0 if none are available - int read(unsigned char* buffer, size_t len); + size_t read(unsigned char* buffer, size_t len); // Read up to len characters from the current packet and place them into buffer // Returns the number of characters read, or 0 if none are available - int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); } + size_t read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); } // Return the next byte from the current packet without moving on to the next byte int peek(); @@ -99,6 +107,14 @@ // Return the port of the host who sent the current incoming packet uint16_t remotePort(); + + // Send data to the specified host and port. + nsapi_size_or_error_t sendto (const char *host, uint16_t port, const void *data, size_t size); + // Send data to the specified address. + nsapi_size_or_error_t sendto (const SocketAddress &address, const void *data, size_t size); + // Receive a datagram and store the source address in address if it's not NULL. + nsapi_size_or_error_t recvfrom (SocketAddress *address, void *data, size_t size); + private: friend void uipudp_appcall();