Pavel S / UIPEthernet

Fork of UIPEthernet by Zoltan Hudak

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 : public Stream {
00041 class   UDP
00042 {
00043 public:
00044     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
00045     virtual void        stop(void) = 0;         // Finish with the UDP socket
00046 
00047     // Sending UDP packets
00048     // Start building up a packet to send to the remote host specific in ip and port
00049     // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
00050     virtual int         beginPacket(IPAddress ip, uint16_t port) = 0;
00051 
00052     // Start building up a packet to send to the remote host specific in host and port
00053     // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
00054     virtual int         beginPacket(const char* host, uint16_t port) = 0;
00055 
00056     // Finish off this packet and send it
00057     // Returns 1 if the packet was sent successfully, 0 if there was an error
00058     virtual int         endPacket(void) = 0;
00059 
00060     // Write a single byte into the packet
00061     virtual size_t      write(uint8_t) = 0;
00062 
00063     // Write size bytes from buffer into the packet
00064     virtual size_t      write(const uint8_t* buffer, size_t size) = 0;
00065 
00066     // Start processing the next available incoming packet
00067     // Returns the size of the packet in bytes, or 0 if no packets are available
00068     virtual int         parsePacket(void) = 0;
00069 
00070     // Number of bytes remaining in the current packet
00071     virtual int         available(void) = 0;
00072 
00073     // Read a single byte from the current packet
00074     virtual int         read(void) = 0;
00075 
00076     // Read up to len bytes from the current packet and place them into buffer
00077     // Returns the number of bytes read, or 0 if none are available
00078     virtual int         read(unsigned char* buffer, size_t len) = 0;
00079 
00080     // Read up to len characters from the current packet and place them into buffer
00081     // Returns the number of characters read, or 0 if none are available
00082     virtual int         read(char* buffer, size_t len) = 0;
00083 
00084     // Return the next byte from the current packet without moving on to the next byte
00085     virtual int         peek(void) = 0;
00086     virtual void        flush(void) = 0;        // Finish reading the current packet
00087 
00088     // Return the IP address of the host who sent the current incoming packet
00089     virtual IPAddress   remoteIP(void) = 0;
00090 
00091     // Return the port of the host who sent the current incoming packet
00092     virtual uint16_t    remotePort(void) = 0;
00093 protected:
00094     uint8_t*    rawIPAddress(IPAddress& addr)   { return addr.raw_address(); };
00095 };
00096 #endif