123

Committer:
advxolltm
Date:
Mon Jun 06 16:36:52 2022 +0000
Revision:
19:543c2d21e510
Parent:
14:7648334eb41b
123

Who changed what in which revision?

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