friendev EtherDune TCP/IP library
/home/jander/temp/etherdune/Socket.h
Go to the documentation of this file.
1 // EtherDune Socket base class
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Base class for TCP and UDP sockets
4 //
5 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
6 //
7 // This source is subject to the GPLv2 license.
8 // Please see the License.txt file for more information.
9 // All other rights reserved.
10 //
11 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
12 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 // PARTICULAR PURPOSE.
15 
19 
26 #ifndef _SOCKET_h
27 #define _SOCKET_h
28 
29 #include <ACross.h>
30 
31 #include "SharedBuffer.h"
32 #include "NetworkService.h"
33 
34 class Socket;
35 
36 
37 class Socket : protected NetworkService
38 {
39 
40  protected:
41 
43  static uint8_t srcPort_L_count;
44 
45  void prepareIPPacket();
46  static uint16_t calcPseudoHeaderChecksum(uint8_t protocol, uint16_t length);
47  static uint16_t calcTCPChecksum(bool options, uint16_t dataLength, uint16_t dataChecksum);
48  static uint16_t calcUDPChecksum(uint16_t dataLength, uint16_t dataChecksum);
49  static bool verifyUDPTCPChecksum();
50 
51  public:
52 
56 
57  uint16_t write(uint16_t len, const byte* data);
58  uint16_t write(const String& s);
59  uint16_t write(const __FlashStringHelper* pattern, ...);
60 
86  template <class T>
87  inline uint16_t write(const T& message)
88  {
89  return write(sizeof(T), (byte*)&message);
90  }
91 
92 };
93 
94 
95 
96 
97 #endif
98 
nint16_t localPort
local TCP or UDP port
Definition: Socket.h:54
uint16_t write(uint16_t len, const byte *data)
In the case of TCP, writes the given data buffer to the socket.
Definition: Socket.cpp:54
void prepareIPPacket()
Definition: Socket.cpp:27
nint16_t remotePort
remote TCP or UDP port
Definition: Socket.h:53
static bool verifyUDPTCPChecksum()
Verifies if the UDP or TCP checksum of the current packet is correct.
Definition: Socket.cpp:205
represents an IP address in memory
Definition: inet.h:181
SharedBuffer buffer
output buffer for this socket
Definition: Socket.h:42
Represents a network byte order 16 bit integer.
Definition: inet.h:59
static uint16_t calcUDPChecksum(uint16_t dataLength, uint16_t dataChecksum)
Calculates the UDP checksum.
Definition: Socket.cpp:192
Implements a "shared" circular buffer using spare ENC28J60 memory.
Definition: SharedBuffer.h:158
Base class for TCP and UDP sockets.
Definition: Socket.h:37
static uint16_t calcTCPChecksum(bool options, uint16_t dataLength, uint16_t dataChecksum)
Calculates the TCP checksum.
Definition: Socket.cpp:177
static uint8_t srcPort_L_count
self-incrementing counter for local ports.
Definition: Socket.h:43
static uint16_t calcPseudoHeaderChecksum(uint8_t protocol, uint16_t length)
Calculates the TCP or UDP pseudo header checksum.
Definition: Socket.cpp:158
IPAddress remoteIP
remote IP address to connect to (TCP) or send the next packet to (UDP)
Definition: Socket.h:55
uint16_t write(const T &message)
Writes out the binary representation of the parameter to the socket.
Definition: Socket.h:87
Abstract base class for any network service running in EtherDune.