Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / Ethernet / EthernetUdp.h @ 58d82c77

History | View | Annotate | Download (4.65 KB)

1
/*
2
 *  Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
3
 *  This version only offers minimal wrapping of socket.c/socket.h
4
 *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
5
 *
6
 * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
7
 * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
8
 * might not happen often in practice, but in larger network topologies, a UDP
9
 * packet can be received out of sequence. 
10
 * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
11
 * aware of it. Again, this may not be a concern in practice on small local networks.
12
 * For more information, see http://www.cafeaulait.org/course/week12/35.html
13
 *
14
 * MIT License:
15
 * Copyright (c) 2008 Bjoern Hartmann
16
 * Permission is hereby granted, free of charge, to any person obtaining a copy
17
 * of this software and associated documentation files (the "Software"), to deal
18
 * in the Software without restriction, including without limitation the rights
19
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
 * copies of the Software, and to permit persons to whom the Software is
21
 * furnished to do so, subject to the following conditions:
22
 * 
23
 * The above copyright notice and this permission notice shall be included in
24
 * all copies or substantial portions of the Software.
25
 * 
26
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32
 * THE SOFTWARE.
33
 *
34
 * bjoern@cs.stanford.edu 12/30/2008
35
 */
36

    
37
#ifndef ethernetudp_h
38
#define ethernetudp_h
39

    
40
#include <Udp.h>
41

    
42
#define UDP_TX_PACKET_MAX_SIZE 24
43

    
44
class EthernetUDP : public UDP {
45
private:
46
  uint8_t _sock;  // socket ID for Wiz5100
47
  uint16_t _port; // local port to listen on
48
  IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
49
  uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
50
  uint16_t _offset; // offset into the packet being sent
51

    
52
public:
53
  EthernetUDP();  // Constructor
54
  virtual uint8_t begin(uint16_t);        // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
55
  virtual void stop();  // Finish with the UDP socket
56

    
57
  // Sending UDP packets
58
  
59
  // Start building up a packet to send to the remote host specific in ip and port
60
  // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
61
  virtual int beginPacket(IPAddress ip, uint16_t port);
62
  // Start building up a packet to send to the remote host specific in host and port
63
  // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
64
  virtual int beginPacket(const char *host, uint16_t port);
65
  // Finish off this packet and send it
66
  // Returns 1 if the packet was sent successfully, 0 if there was an error
67
  virtual int endPacket();
68
  // Write a single byte into the packet
69
  virtual size_t write(uint8_t);
70
  // Write size bytes from buffer into the packet
71
  virtual size_t write(const uint8_t *buffer, size_t size);
72
  
73
  using Print::write;
74

    
75
  // Start processing the next available incoming packet
76
  // Returns the size of the packet in bytes, or 0 if no packets are available
77
  virtual int parsePacket();
78
  // Number of bytes remaining in the current packet
79
  virtual int available();
80
  // Read a single byte from the current packet
81
  virtual int read();
82
  // Read up to len bytes from the current packet and place them into buffer
83
  // Returns the number of bytes read, or 0 if none are available
84
  virtual int read(unsigned char* buffer, size_t len);
85
  // Read up to len characters from the current packet and place them into buffer
86
  // Returns the number of characters read, or 0 if none are available
87
  virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
88
  // Return the next byte from the current packet without moving on to the next byte
89
  virtual int peek();
90
  virtual void flush();        // Finish reading the current packet
91

    
92
  // Return the IP address of the host who sent the current incoming packet
93
  virtual IPAddress remoteIP() { return _remoteIP; };
94
  // Return the port of the host who sent the current incoming packet
95
  virtual uint16_t remotePort() { return _remotePort; };
96
};
97

    
98
#endif