Mbed library for ENC28J60 Ethernet modules. Full support for TCP/IP and UDP Server, Client and HTTP server (webserver). DHCP and DNS is included.

Dependents:   mBuino_ENC28_MQTT Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC Serial_over_Ethernet ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Udp.h Source File

Udp.h

00001 /*
00002  *  Udp.cpp: Library to send/receive UDP packets.
00003  *
00004  * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
00005  * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
00006  * might not happen often in practice, but in larger network topologies, a UDP
00007  * packet can be received out of sequence. 
00008  * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
00009  * aware of it. Again, this may not be a concern in practice on small local networks.
00010  * For more information, see http://www.cafeaulait.org/course/week12/35.html
00011  *
00012  * MIT License:
00013  * Copyright (c) 2008 Bjoern Hartmann
00014  * Permission is hereby granted, free of charge, to any person obtaining a copy
00015  * of this software and associated documentation files (the "Software"), to deal
00016  * in the Software without restriction, including without limitation the rights
00017  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00018  * copies of the Software, and to permit persons to whom the Software is
00019  * furnished to do so, subject to the following conditions:
00020  * 
00021  * The above copyright notice and this permission notice shall be included in
00022  * all copies or substantial portions of the Software.
00023  * 
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00025  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00026  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00027  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00028  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00029  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00030  * THE SOFTWARE.
00031  *
00032  * bjoern@cs.stanford.edu 12/30/2008
00033  */
00034 #ifndef udp_h
00035 #define udp_h
00036 
00037 #include "mbed.h"
00038 #include "IpAddress.h"
00039 
00040 class   Udp
00041 {
00042 public:
00043     virtual uint8_t     begin(uint16_t) = 0;    // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
00044     virtual void        stop(void) = 0;         // Finish with the UDP socket
00045 
00046     // Sending UDP packets
00047     // Start building up a packet to send to the remote host specific in ip and port
00048     // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
00049     virtual int         beginPacket(IpAddress ip, uint16_t port) = 0;
00050 
00051     // Start building up a packet to send to the remote host specific in host and port
00052     // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
00053     virtual int         beginPacket(const char* host, uint16_t port) = 0;
00054 
00055     // Finish off this packet and send it
00056     // Returns 1 if the packet was sent successfully, 0 if there was an error
00057     virtual int         endPacket(void) = 0;
00058 
00059     // Write a single byte into the packet
00060     virtual size_t      write(uint8_t) = 0;
00061 
00062     // Write size bytes from buffer into the packet
00063     virtual size_t      write(const uint8_t* buffer, size_t size) = 0;
00064 
00065     // Start processing the next available incoming packet
00066     // Returns the size of the packet in bytes, or 0 if no packets are available
00067     virtual int         parsePacket(void) = 0;
00068 
00069     // Number of bytes remaining in the current packet
00070     virtual size_t      available(void) = 0;
00071 
00072     // Read a single byte from the current packet. Returns -1 if no byte is available.
00073     virtual int         read(void) = 0;
00074 
00075     // Read up to len bytes from the current packet and place them into buffer
00076     // Returns the number of bytes read, or 0 if none are available
00077     virtual size_t      read(unsigned char* buffer, size_t len) = 0;
00078 
00079     // Read up to len characters from the current packet and place them into buffer
00080     // Returns the number of characters read, or 0 if none are available
00081     virtual size_t      read(char* buffer, size_t len) = 0;
00082 
00083     // Return the next byte from the current packet without moving on to the next byte
00084     virtual int         peek(void) = 0;         // returns -1 if fails
00085     virtual void        flush(void) = 0;        // Finish reading the current packet
00086 
00087     // Return the IP address of the host who sent the current incoming packet
00088     virtual IpAddress   remoteIP(void) = 0;
00089 
00090     // Return the port of the host who sent the current incoming packet
00091     virtual uint16_t    remotePort(void) = 0;
00092 protected:
00093     uint8_t*    rawIPAddress(IpAddress& addr)   { return addr.rawAddress(); }
00094 };
00095 #endif