mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Committer:
hudakz
Date:
Sun Mar 08 20:26:56 2015 +0000
Revision:
4:d774541a34da
Parent:
utility/Udp.h@3:5b17e4656dd0
Child:
8:4acb22344932
Version 1.09 (fixed leaking client-data caused by race-condition on remote close)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 3:5b17e4656dd0 1 /*
hudakz 3:5b17e4656dd0 2 * Udp.cpp: Library to send/receive UDP packets.
hudakz 3:5b17e4656dd0 3 *
hudakz 3:5b17e4656dd0 4 * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
hudakz 3:5b17e4656dd0 5 * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
hudakz 3:5b17e4656dd0 6 * might not happen often in practice, but in larger network topologies, a UDP
hudakz 3:5b17e4656dd0 7 * packet can be received out of sequence.
hudakz 3:5b17e4656dd0 8 * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
hudakz 3:5b17e4656dd0 9 * aware of it. Again, this may not be a concern in practice on small local networks.
hudakz 3:5b17e4656dd0 10 * For more information, see http://www.cafeaulait.org/course/week12/35.html
hudakz 3:5b17e4656dd0 11 *
hudakz 3:5b17e4656dd0 12 * MIT License:
hudakz 3:5b17e4656dd0 13 * Copyright (c) 2008 Bjoern Hartmann
hudakz 3:5b17e4656dd0 14 * Permission is hereby granted, free of charge, to any person obtaining a copy
hudakz 3:5b17e4656dd0 15 * of this software and associated documentation files (the "Software"), to deal
hudakz 3:5b17e4656dd0 16 * in the Software without restriction, including without limitation the rights
hudakz 3:5b17e4656dd0 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hudakz 3:5b17e4656dd0 18 * copies of the Software, and to permit persons to whom the Software is
hudakz 3:5b17e4656dd0 19 * furnished to do so, subject to the following conditions:
hudakz 3:5b17e4656dd0 20 *
hudakz 3:5b17e4656dd0 21 * The above copyright notice and this permission notice shall be included in
hudakz 3:5b17e4656dd0 22 * all copies or substantial portions of the Software.
hudakz 3:5b17e4656dd0 23 *
hudakz 3:5b17e4656dd0 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hudakz 3:5b17e4656dd0 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hudakz 3:5b17e4656dd0 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hudakz 3:5b17e4656dd0 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hudakz 3:5b17e4656dd0 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hudakz 3:5b17e4656dd0 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hudakz 3:5b17e4656dd0 30 * THE SOFTWARE.
hudakz 3:5b17e4656dd0 31 *
hudakz 3:5b17e4656dd0 32 * bjoern@cs.stanford.edu 12/30/2008
hudakz 3:5b17e4656dd0 33 */
hudakz 3:5b17e4656dd0 34 #ifndef udp_h
hudakz 3:5b17e4656dd0 35 #define udp_h
hudakz 3:5b17e4656dd0 36
hudakz 3:5b17e4656dd0 37 #include <mbed.h>
hudakz 3:5b17e4656dd0 38 #include <IPAddress.h>
hudakz 3:5b17e4656dd0 39
hudakz 3:5b17e4656dd0 40 //class UDP : public Stream {
hudakz 3:5b17e4656dd0 41 class UDP
hudakz 3:5b17e4656dd0 42 {
hudakz 3:5b17e4656dd0 43 public:
hudakz 3:5b17e4656dd0 44 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
hudakz 3:5b17e4656dd0 45 virtual void stop(void) = 0; // Finish with the UDP socket
hudakz 3:5b17e4656dd0 46
hudakz 3:5b17e4656dd0 47 // Sending UDP packets
hudakz 3:5b17e4656dd0 48 // Start building up a packet to send to the remote host specific in ip and port
hudakz 3:5b17e4656dd0 49 // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
hudakz 3:5b17e4656dd0 50 virtual int beginPacket(IPAddress ip, uint16_t port) = 0;
hudakz 3:5b17e4656dd0 51
hudakz 3:5b17e4656dd0 52 // Start building up a packet to send to the remote host specific in host and port
hudakz 3:5b17e4656dd0 53 // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
hudakz 3:5b17e4656dd0 54 virtual int beginPacket(const char* host, uint16_t port) = 0;
hudakz 3:5b17e4656dd0 55
hudakz 3:5b17e4656dd0 56 // Finish off this packet and send it
hudakz 3:5b17e4656dd0 57 // Returns 1 if the packet was sent successfully, 0 if there was an error
hudakz 3:5b17e4656dd0 58 virtual int endPacket(void) = 0;
hudakz 3:5b17e4656dd0 59
hudakz 3:5b17e4656dd0 60 // Write a single byte into the packet
hudakz 3:5b17e4656dd0 61 virtual size_t write(uint8_t) = 0;
hudakz 3:5b17e4656dd0 62
hudakz 3:5b17e4656dd0 63 // Write size bytes from buffer into the packet
hudakz 3:5b17e4656dd0 64 virtual size_t write(const uint8_t* buffer, size_t size) = 0;
hudakz 3:5b17e4656dd0 65
hudakz 3:5b17e4656dd0 66 // Start processing the next available incoming packet
hudakz 3:5b17e4656dd0 67 // Returns the size of the packet in bytes, or 0 if no packets are available
hudakz 3:5b17e4656dd0 68 virtual int parsePacket(void) = 0;
hudakz 3:5b17e4656dd0 69
hudakz 3:5b17e4656dd0 70 // Number of bytes remaining in the current packet
hudakz 3:5b17e4656dd0 71 virtual int available(void) = 0;
hudakz 3:5b17e4656dd0 72
hudakz 3:5b17e4656dd0 73 // Read a single byte from the current packet
hudakz 3:5b17e4656dd0 74 virtual int read(void) = 0;
hudakz 3:5b17e4656dd0 75
hudakz 3:5b17e4656dd0 76 // Read up to len bytes from the current packet and place them into buffer
hudakz 3:5b17e4656dd0 77 // Returns the number of bytes read, or 0 if none are available
hudakz 3:5b17e4656dd0 78 virtual int read(unsigned char* buffer, size_t len) = 0;
hudakz 3:5b17e4656dd0 79
hudakz 3:5b17e4656dd0 80 // Read up to len characters from the current packet and place them into buffer
hudakz 3:5b17e4656dd0 81 // Returns the number of characters read, or 0 if none are available
hudakz 3:5b17e4656dd0 82 virtual int read(char* buffer, size_t len) = 0;
hudakz 3:5b17e4656dd0 83
hudakz 3:5b17e4656dd0 84 // Return the next byte from the current packet without moving on to the next byte
hudakz 3:5b17e4656dd0 85 virtual int peek(void) = 0;
hudakz 3:5b17e4656dd0 86 virtual void flush(void) = 0; // Finish reading the current packet
hudakz 3:5b17e4656dd0 87
hudakz 3:5b17e4656dd0 88 // Return the IP address of the host who sent the current incoming packet
hudakz 3:5b17e4656dd0 89 virtual IPAddress remoteIP(void) = 0;
hudakz 3:5b17e4656dd0 90
hudakz 3:5b17e4656dd0 91 // Return the port of the host who sent the current incoming packet
hudakz 3:5b17e4656dd0 92 virtual uint16_t remotePort(void) = 0;
hudakz 3:5b17e4656dd0 93 protected:
hudakz 3:5b17e4656dd0 94 uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
hudakz 3:5b17e4656dd0 95 };
hudakz 3:5b17e4656dd0 96 #endif