75 randomSeed((uint16_t)millis() + analogRead(A1) + analogRead(A5));
122 sequenceNumber = listener.sequenceNumber;
128 void TCPSocket::prepareTCPPacket(
bool options, uint16_t dataLength)
155 void TCPSocket::sendSYN(
bool ack)
157 ACTRACE(
"sendSYN ACK=%d",ack);
159 prepareTCPPacket(
true, 0);
213 void TCPSocket::tick()
217 ACDEBUG(
"tick/state=%S",getStateString());
221 ACTRACE(
"%S state timeout", getStateString());
259 goto sck_state_established;
273 sck_state_established:
274 processOutgoingBuffer();
282 bool TCPSocket::onPacketReceived()
297 #if ENABLE_UDPTCP_RX_CHECKSUM
301 ACWARN(
"TCP checksum error");
309 uint32_t incomingAckNum =
packet.tcp.acknowledgementNumber;
310 uint32_t incomingSeqNum =
packet.tcp.sequenceNumber;
312 int32_t bytesAck = (int32_t)(incomingAckNum - sequenceNumber);
313 int32_t bytesReceived;
315 ACTRACE("incomingAck=%lu localSeqNum=%lu bytesAck=%ld incomingSeqNum=%lu localAckNum=%lu",
316 incomingAckNum, sequenceNumber, bytesAck, incomingSeqNum, ackNumber);
327 sequenceNumber += bytesAck;
332 ackNumber = incomingSeqNum + 1;
355 releaseWindow(bytesAck);
357 if (ackNumber != incomingSeqNum)
359 ACDEBUG(
"dropped packet out of sequence.");
366 ackNumber += bytesReceived;
367 ACTRACE(
"bytesReceived=%ld", bytesReceived);
371 if (bytesReceived > 0)
463 void TCPSocket::processOutgoingBuffer()
465 ACTRACE(
"processOutgoingBuffer");
466 uint16_t dataLength = 0;
467 uint16_t dataChecksum = 0;
475 if (nextFlags.
raw !=0)
477 prepareTCPPacket(
false, dataLength);
482 ACTRACE(
"dataLength=%d dataChecksum=%d", dataLength, dataChecksum);
500 nextFlags.
PSH =
true;
501 processOutgoingBuffer();
504 void TCPSocket::releaseWindow(int32_t& bytesAck)
510 ACASSERT(bytesAck >= 0,
"released too much. bytesAck=%d", bytesAck);
514 __FlashStringHelper* TCPSocket::getStateString()
537 return (__FlashStringHelper*)s;
uint16_t release()
Releases one fragment of data
static const uint8_t TCP_SRC_PORT_H
Indicates the most significant byte of the source port number that EtherDune will use by default when...
static const uint8_t SCK_STATE_FIN_WAIT_1
(both server and client) represents waiting for a connection termination request from the remote TCP...
Union of all the different protocol headers and layers to help EtherDune interpret or build packet...
nint16_t localPort
local TCP or UDP port
virtual void onConnectRequest()
Called when a listening socket receives a connection request.
uint8_t RST
Reset the connection.
static const uint8_t SCK_STATE_LAST_ACK
(both server and client) represents waiting for an acknowledgment of the connection termination reque...
static const uint16_t ETHTYPE_IP
Ethernet header protocol type for IP.
nint16_t option1_value
option value
uint32_t u
access the IP as a uint32_t for convenience
ACROSS_MODULE("TCPSocket")
virtual void onConnect()
Fires when the socket connection is established.
static EthBuffer packet
in-memory packet buffer currently being processed.
void push()
Sets the PSH TCP flag and also sends data in the outgoing buffer immediately.
static const uint8_t SCK_STATE_FIN_WAIT_2
(both server and client) represents waiting for a connection termination request from the remote TCP...
void setState(uint8_t newState, uint8_t timeout)
Changes to a new state
nint16_t remotePort
remote TCP or UDP port
uint16_t rawValue
provides low level access to the memory containing the network-order integer
void listen()
Starts listening on the local port indicated by the localPort property.
static bool verifyUDPTCPChecksum()
Verifies if the UDP or TCP checksum of the current packet is correct.
static const uint8_t SCK_STATE_SYN_RECEIVED
(server) represents waiting for a confirming connection request acknowledgment after having both rece...
static const uint8_t SCK_STATE_CLOSED
(both server and client) represents no connection state at all.
uint8_t stateTimer
state timer, in ticks
uint8_t raw
byte-wise access to all flags
SharedBuffer buffer
output buffer for this socket
static const uint8_t SCK_STATE_SYN_SENT
(client) represents waiting for a matching connection request after having sent a connection request...
static const uint8_t SCK_STATE_RESOLVING
IP Address is being resolved.
void flush()
Releases all data in this buffer, freeing up space.
static const uint8_t SCK_STATE_TIME_WAIT
(either server or client) represents waiting for enough time to pass to be sure the remote TCP receiv...
void connect()
Initiates a TCP connection to remoteIP and remotePort.
static const uint16_t TCP_MAXIMUM_SEGMENT_SIZE
Maximum size of payload accepted by the library.
uint8_t option1_length
option length
Implements the TCP protocol.
void clear()
Sets all flags to zero.
uint8_t PSH
Push function.
bool isEmpty()
Determines whether this buffer is empty
static bool sendIPPacket(uint16_t length)
Puts the current in-memory packet in the network
static uint16_t calcTCPChecksum(bool options, uint16_t dataLength, uint16_t dataChecksum)
Calculates the TCP checksum.
Structure to encode one 2-byte long TCP option.
static const uint8_t IP_PROTO_TCP
IP header protocol type for TCP.
static const uint8_t SCK_STATE_LISTEN
(server) represents waiting for a connection request from any remote TCP and port.
uint8_t FIN
No more data from sender.
static const uint8_t SCK_STATE_ESTABLISHED
(both server and client) represents an open connection, data received can be delivered to the user...
static const uint8_t SCK_STATE_CLOSING
(both server and client) represents waiting for a connection termination request acknowledgment from ...
static uint8_t srcPort_L_count
self-incrementing counter for local ports.
uint8_t l
least significant byte
void terminate()
Immediately shuts down the socket and makes it available for a new task.
virtual void onTerminate()
Called when the socket is ready to be reused.
IPAddress remoteIP
remote IP address to connect to (TCP) or send the next packet to (UDP)
uint8_t h
most significant byte
uint8_t option1
Option kind.
void close()
Attempts to gracefully close a connection.
void accept()
Accepts a connection request that has been received on the port this instance was listening on ...
virtual void onReceive(uint16_t len, const byte *data)
Called for each data packet received
void zero()
sets the variable to 0.
uint16_t fillTxBuffer(uint16_t dstOffset, uint16_t &checksum, uint16_t count=0xFFFF)
Copies up to count fragments to the transmit buffer via DMA, concatenating them starting at given off...
virtual void onClose()
Fires when the other party closed the incoming end of the connection because it has no more data to s...
uint8_t ACK
Indicates that the Acknowledgment field is significant.
uint8_t SYN
Synchronize sequence numbers.
static const uint8_t SCK_STATE_CLOSE_WAIT
(both server and client) represents waiting for a connection termination request from the local user...