EtherSia
A minimal IPv6 Ethernet library for Arduino.
Public Member Functions | List of all members
Socket Class Referenceabstract

Abstract base class for a IP socket. More...

#include <Socket.h>

Public Member Functions

 Socket (EtherSia &ether)
 Construct a socket The local port number will be set to a random port number. More...
 
 Socket (EtherSia &ether, uint16_t localPort)
 Construct a socket, with a listening port defined. More...
 
boolean setRemoteAddress (const char *remoteAddress, uint16_t remotePort)
 Set the remote address (as a string) and port to send packets to. More...
 
boolean setRemoteAddress (const __FlashStringHelper *remoteAddress, uint16_t remotePort)
 Set the remote address (as a string in flash memory) and port to send packets to. More...
 
boolean setRemoteAddress (IPv6Address &remoteAddress, uint16_t remotePort)
 Set the remote address and port to send packets to. More...
 
IPv6AddressremoteAddress ()
 Get the remote address that packets are being sent to. More...
 
uint16_t remotePort ()
 Get the remote port number that packets are being sent to. More...
 
uint16_t localPort ()
 Get the local port number that packets are being sent to. More...
 
IPv6AddresspacketSource ()
 Get the IPv6 source address of the last packet received. More...
 
IPv6AddresspacketDestination ()
 Get the IPv6 destination address of the last packet received. More...
 
void send (boolean isReply=false)
 Send out a packet. More...
 
void send (uint16_t length, boolean isReply=false)
 Send the contents of the packet payload buffer. More...
 
void send (const char *data, boolean isReply=false)
 Send a packet containing a string from socket. More...
 
void send (const void *data, uint16_t length, boolean isReply=false)
 Send a packet containing an array of bytes from socket. More...
 
void sendReply ()
 Send a reply to an incoming packet. More...
 
void sendReply (uint16_t length)
 Send a reply to the last packet received. More...
 
void sendReply (const char *data)
 Send a reply to the last packet received. More...
 
void sendReply (const void *data, uint16_t length)
 Send a reply to the last packet received. More...
 
virtual uint8_t * payload ()=0
 Get a pointer to the payload of the current packet in the buffer. More...
 
virtual uint16_t payloadLength ()=0
 Get the length (in bytes) of the payload of the packet. More...
 
boolean payloadEquals (const char *str)
 Check if the current payload equals a string. More...
 
virtual uint8_t * transmitPayload ()
 Get a pointer to the next packet payload to be sent. More...
 
virtual size_t write (uint8_t chr)
 Write a single character into the packet buffer. More...
 

Detailed Description

Abstract base class for a IP socket.

Constructor & Destructor Documentation

◆ Socket() [1/2]

Socket::Socket ( EtherSia ether)

Construct a socket The local port number will be set to a random port number.

Parameters
etherThe Ethernet interface to attach the socket to

◆ Socket() [2/2]

Socket::Socket ( EtherSia ether,
uint16_t  localPort 
)

Construct a socket, with a listening port defined.

Parameters
etherThe Ethernet interface to attach the socket to
localPortThe local port number to listen on

References localPort(), and IPv6Address::setZero().

Member Function Documentation

◆ localPort()

uint16_t Socket::localPort ( )

Get the local port number that packets are being sent to.

Returns
the port number

◆ packetDestination()

IPv6Address & Socket::packetDestination ( )

Get the IPv6 destination address of the last packet received.

Note
Please call havePacket() first, before calling this method.
Returns
The destination IPv6 address

References IPv6Packet::destination(), and EtherSia::packet().

◆ packetSource()

IPv6Address & Socket::packetSource ( )

Get the IPv6 source address of the last packet received.

Note
Please call havePacket() first, before calling this method.
Returns
The source IPv6 address
Examples:
NanodeUDPServer.ino, and SNTPClient.ino.

References EtherSia::packet(), and IPv6Packet::source().

◆ payload()

virtual uint8_t* Socket::payload ( )
pure virtual

Get a pointer to the payload of the current packet in the buffer.

Note
This method must be implemented by sub-classes
Returns
A pointer to the payload

Implemented in TCPServer, UDPSocket, and PingClient.

◆ payloadEquals()

boolean Socket::payloadEquals ( const char *  str)

Check if the current payload equals a string.

Note
Please call havePacket() first, before calling this method.
Parameters
strThe null-terminated string to compare to
Returns
True if the UDP payload is the same as the str parameter
Examples:
NanodeUDPServer.ino.

References payload(), and payloadLength().

◆ payloadLength()

virtual uint16_t Socket::payloadLength ( )
pure virtual

Get the length (in bytes) of the payload of the packet.

Note
This method must be implemented by sub-classes
Returns
A pointer to the payload

Implemented in TCPServer, UDPSocket, and PingClient.

◆ remoteAddress()

IPv6Address & Socket::remoteAddress ( )

Get the remote address that packets are being sent to.

Returns
the IPv6 remote address
Examples:
NanodeUDPClient.ino, PingClient.ino, and SNTPClient.ino.

◆ remotePort()

uint16_t Socket::remotePort ( )

Get the remote port number that packets are being sent to.

Returns
the port number

◆ send() [1/4]

void Socket::send ( boolean  isReply = false)

Send out a packet.

Before calling this, the payload should have been written to the packet buffer using the print() and println() methods.

Parameters
isReplytrue if the sent packet is a reply to the packet current in the buffer
Examples:
NanodeUDPClient.ino, and SNTPClient.ino.

◆ send() [2/4]

void Socket::send ( uint16_t  length,
boolean  isReply = false 
)

Send the contents of the packet payload buffer.

Place the data in the payload() buffer before calling this method.

Parameters
lengthThe length of the payload
isReplytrue if the sent packet is a reply to the packet current in the buffer

References EtherSia::packet(), EtherSia::prepareReply(), EtherSia::prepareSend(), IPv6Packet::setDestination(), and IPv6Packet::setEtherDestination().

◆ send() [3/4]

void Socket::send ( const char *  data,
boolean  isReply = false 
)

Send a packet containing a string from socket.

Parameters
dataThe null-terminated string to send
isReplytrue if the sent packet is a reply to the packet current in the buffer

References send().

◆ send() [4/4]

void Socket::send ( const void *  data,
uint16_t  length,
boolean  isReply = false 
)

Send a packet containing an array of bytes from socket.

Parameters
dataThe data to send as the payload
lengthThe length (in bytes) of the data to send
isReplytrue if the sent packet is a reply to the packet current in the buffer

References payload(), send(), and transmitPayload().

◆ sendReply() [1/4]

void Socket::sendReply ( )

Send a reply to an incoming packet.

Before calling this, the payload should have been written to the packet buffer using the print() and println() methods.

Examples:
MiniHTTPServer.ino, NanodeUDPServer.ino, and WebToggler.ino.

References send().

◆ sendReply() [2/4]

void Socket::sendReply ( uint16_t  length)

Send a reply to the last packet received.

Place the data in the payload() buffer before calling this method.

Parameters
lengthThe length (in bytes) of the data to send

References send().

◆ sendReply() [3/4]

void Socket::sendReply ( const char *  data)

Send a reply to the last packet received.

Parameters
dataThe null-terminated string to send

References send().

◆ sendReply() [4/4]

void Socket::sendReply ( const void *  data,
uint16_t  length 
)

Send a reply to the last packet received.

Parameters
dataA pointer to the data to send
lengthThe length (in bytes) of the data to send

References send().

◆ setRemoteAddress() [1/3]

boolean Socket::setRemoteAddress ( const char *  remoteAddress,
uint16_t  remotePort 
)

Set the remote address (as a string) and port to send packets to.

If the remote address looks like a hostname, it will be looked up using DNS.

Parameters
remoteAddressThe remote address or hostname
remotePortThe port number to send packets to
Returns
true if the remote address was set successfully
Examples:
NanodeUDPClient.ino, and SNTPClient.ino.

References containsColon(), IPv6Address::fromString(), EtherSia::lookupHostname(), remoteAddress(), and remotePort().

◆ setRemoteAddress() [2/3]

boolean Socket::setRemoteAddress ( const __FlashStringHelper *  remoteAddress,
uint16_t  remotePort 
)

Set the remote address (as a string in flash memory) and port to send packets to.

If the remote address looks like a hostname, it will be looked up using DNS.

Parameters
remoteAddressThe remote address or hostname (as a flash string - use the F() macro)
remotePortThe port number to send packets to
Returns
true if the remote address was set successfully

References remoteAddress(), remotePort(), and setRemoteAddress().

◆ setRemoteAddress() [3/3]

boolean Socket::setRemoteAddress ( IPv6Address remoteAddress,
uint16_t  remotePort 
)

Set the remote address and port to send packets to.

Parameters
remoteAddressThe remote address as a 16-byte array
remotePortThe remote port number to send packets to
Returns
true if the remote address was set successfully

References EtherSia::discoverNeighbour(), EtherSia::inOurSubnet(), remoteAddress(), remotePort(), and EtherSia::routerMac().

◆ transmitPayload()

uint8_t * Socket::transmitPayload ( )
virtual

Get a pointer to the next packet payload to be sent.

Returns
A pointer to the transmit payload buffer

Reimplemented in TCPServer.

References payload().

◆ write()

size_t Socket::write ( uint8_t  chr)
virtual

Write a single character into the packet buffer.

Parameters
chrThe character to write
Returns
The number of bytes written to the buffer

References payload(), and transmitPayload().