friendev EtherDune TCP/IP library
Public Member Functions | Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Socket Class Reference

Base class for TCP and UDP sockets. More...

#include <Socket.h>

Inheritance diagram for Socket:
NetworkService ListItem ENC28J60 TCPSocket UDPSocket HTTPClient HTTPServer TCPListener< SOCKET, MAX_CLIENTS > DHCP DNSClient

Public Member Functions

uint16_t write (uint16_t len, const byte *data)
 In the case of TCP, writes the given data buffer to the socket. More...
 
uint16_t write (const String &s)
 In the case of TCP, writes the given String to the socket, not including the null-terminating character. More...
 
uint16_t write (const __FlashStringHelper *pattern,...)
 Writes out the specified PROGMEM string that may contain the % escape character. More...
 
template<class T >
uint16_t write (const T &message)
 Writes out the binary representation of the parameter to the socket. More...
 

Public Attributes

nint16_t remotePort
 remote TCP or UDP port More...
 
nint16_t localPort
 local TCP or UDP port More...
 
IPAddress remoteIP
 remote IP address to connect to (TCP) or send the next packet to (UDP) More...
 

Protected Member Functions

void prepareIPPacket ()
 
- Protected Member Functions inherited from NetworkService
 NetworkService ()
 
 ~NetworkService ()
 
virtual bool onPacketReceived ()=0
 Called every time a packet is received. More...
 
virtual void tick ()
 This is a timer function that is called every NETWORK_TIMER_RESOLUTION milliseconds on every service. More...
 
virtual void onDNSResolve (uint8_t status, uint16_t identification, const IPAddress &ip)
 Called on each network service every time a DNS response is received. More...
 

Static Protected Member Functions

static uint16_t calcPseudoHeaderChecksum (uint8_t protocol, uint16_t length)
 Calculates the TCP or UDP pseudo header checksum. More...
 
static uint16_t calcTCPChecksum (bool options, uint16_t dataLength, uint16_t dataChecksum)
 Calculates the TCP checksum. More...
 
static uint16_t calcUDPChecksum (uint16_t dataLength, uint16_t dataChecksum)
 Calculates the UDP checksum. More...
 
static bool verifyUDPTCPChecksum ()
 Verifies if the UDP or TCP checksum of the current packet is correct. More...
 
- Static Protected Member Functions inherited from NetworkService
static bool sendIPPacket (uint16_t length)
 Puts the current in-memory packet in the network More...
 
static void prepareIPPacket (const IPAddress &remoteIP)
 Sets up common IP header values for all outgoing IP packets and calculates the IP header checksum More...
 
static bool sameLAN (IPAddress &dst)
 Determines whether the given IP is in the same subnet as localIP More...
 
static bool begin (uint8_t cspin)
 Initializes EtherDune and the underlying hardware More...
 
static void loop ()
 Gives processing time to EtherDune so that it can check for incoming packets or send queued packets. More...
 
static DNSClientDNS ()
 Obtains access to the DNS service singleton instance. More...
 
- Static Protected Member Functions inherited from ENC28J60
static void writeByte (byte b)
 
static void writeByte (uint16_t dst, byte b)
 
static void writeBuf (uint16_t dst, uint16_t len, const byte *data)
 
static void writeBuf (uint16_t len, const byte *data)
 
static uint16_t hardwareChecksum (uint16_t src, uint16_t len)
 
static uint16_t hardwareChecksumRxOffset (uint16_t offset, uint16_t len)
 
static void moveMem (uint16_t dest, uint16_t src, uint16_t len)
 
static void readBuf (uint16_t src, uint16_t len, byte *data)
 
static void readBuf (uint16_t len, byte *data)
 
static byte readByte (uint16_t src)
 
static void packetSend (uint16_t len)
 
static void packetSend (uint16_t len, const byte *data)
 
static void loadSample ()
 
static void loadAll ()
 
static void release ()
 
static uint8_t begin (uint8_t cspin)
 
static void loop ()
 
static void enableBroadcast ()
 
static bool isLinkUp ()
 Determines whether the network link is ready More...
 

Protected Attributes

SharedBuffer buffer
 output buffer for this socket More...
 

Static Protected Attributes

static uint8_t srcPort_L_count = 0
 self-incrementing counter for local ports. More...
 
- Static Protected Attributes inherited from NetworkService
static EthBuffer packet
 in-memory packet buffer currently being processed. More...
 
static ARPService ARP
 ARP singleton instance. More...
 
static MACAddress localMAC
 Ethernet MAC address. More...
 
static IPAddress localIP = { 0, 0, 0, 0 }
 IP address of this application. More...
 
static IPAddress gatewayIP = { 0, 0, 0, 0 }
 IP address of the gateway in this network. More...
 
static IPAddress netmask = { 0, 0, 0, 0 }
 Subnet mask. More...
 
static IPAddress dnsIP = { 0, 0, 0, 0 }
 IP address of the DNS server to use. More...
 

Detailed Description

Base class for TCP and UDP sockets.

Definition at line 37 of file Socket.h.

Member Function Documentation

uint16_t Socket::calcPseudoHeaderChecksum ( uint8_t  protocol,
uint16_t  length 
)
staticprotected

Calculates the TCP or UDP pseudo header checksum.

Parameters
protocolprotocol code, either IP_PROTO_TCP or IP_PROTO_UDP
lengthTotal length of the frame: UDP/TCP header and data
Returns
The calculated checksum

See more about the TCP/UDP pseudo header checksum here: http://en.wikipedia.org/wiki/Transmission_Control_Protocol#Checksum_computation

Definition at line 158 of file Socket.cpp.

uint16_t Socket::calcTCPChecksum ( bool  options,
uint16_t  dataLength,
uint16_t  dataChecksum 
)
staticprotected

Calculates the TCP checksum.

Parameters
optionsif set to true, considers extra size of a 2 byte length option, used in EtherDune to establish the MSS
dataLengthLength of the payload
dataChecksumChecksum of the payload
Returns
The calculated checksum of the TCP segment

Definition at line 177 of file Socket.cpp.

uint16_t Socket::calcUDPChecksum ( uint16_t  dataLength,
uint16_t  dataChecksum 
)
staticprotected

Calculates the UDP checksum.

Parameters
dataLengthLength of the payload.
dataChecksumChecksum of the payload
Returns
The calculated checksum of the UDP datagram

Definition at line 192 of file Socket.cpp.

void Socket::prepareIPPacket ( )
protected

Definition at line 27 of file Socket.cpp.

bool Socket::verifyUDPTCPChecksum ( )
staticprotected

Verifies if the UDP or TCP checksum of the current packet is correct.

Returns
true if the checksum is correct, false otherwise

Definition at line 205 of file Socket.cpp.

uint16_t Socket::write ( uint16_t  len,
const byte *  data 
)

In the case of TCP, writes the given data buffer to the socket.

For UDP sockets, appends the given data buffer to the current packet being built

Parameters
lenlength of the data buffer
datapointer to the data to send
Returns
Number of bytes written

Definition at line 54 of file Socket.cpp.

uint16_t Socket::write ( const String &  s)

In the case of TCP, writes the given String to the socket, not including the null-terminating character.

For UDP sockets, appends the given String to the current packet being built, not including the null-terminating character.

Parameters
sString to write out
Returns
Number of bytes written

Definition at line 70 of file Socket.cpp.

uint16_t Socket::write ( const __FlashStringHelper *  pattern,
  ... 
)

Writes out the specified PROGMEM string that may contain the % escape character.

For each % found, the next argument in the variable arguments list is output instead. Variable arguments must be of type String*. To actually write a %, put two %%.

Parameters
patternPattern PROGMEM string. You can produce this string by using the F() macro
...Optional substitution String* pointers, one for each % in the pattern string
Returns
Number of bytes written
String status(200);
String statusMessage(F("OK"));
String contentType(F("text/html"));
String message(F("Hello World!"));
socket.write(F("HTTP % %\r\nContent-Type:%\r\n\r\n<html><body><h1>%</h1></body></html>"),
&status, &statusMessage, &contentType, &message);

Output:

HTTP 200 OK
Content-Type:text/html
<html><body><h1>Hello World!</h1></body></html>

Definition at line 103 of file Socket.cpp.

template<class T >
uint16_t Socket::write ( const T &  message)
inline

Writes out the binary representation of the parameter to the socket.

If used with a null-terminated string constant, it will output the string including the null-terminating character \0.

If used with datatypes such as int, it will output the actual binary representation of an integer, not the ASCII visual representation of it.

Useful to serialize structs or complex types for sending.

See UDPClientDemo_NTPClient.ino for an example usage

Parameters
messageThe message.
Returns
Number of bytes written
struct
{
int status;
byte code;
} message;
message.status = 200;
message.code = 10;
socket.write(message);

Definition at line 87 of file Socket.h.

Member Data Documentation

SharedBuffer Socket::buffer
protected

output buffer for this socket

Definition at line 42 of file Socket.h.

nint16_t Socket::localPort

local TCP or UDP port

Definition at line 54 of file Socket.h.

IPAddress Socket::remoteIP

remote IP address to connect to (TCP) or send the next packet to (UDP)

Definition at line 55 of file Socket.h.

nint16_t Socket::remotePort

remote TCP or UDP port

Definition at line 53 of file Socket.h.

uint8_t Socket::srcPort_L_count = 0
staticprotected

self-incrementing counter for local ports.

Definition at line 43 of file Socket.h.


The documentation for this class was generated from the following files: