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