TCP: Transmission Control Protocol

https://en.m.wikipedia.org/wiki/Transmission_Control_Protocol

Connection API

enum TcpConnectionEvent

Values:

enumerator eTCE_Connected

Occurs after connection establishment.

enumerator eTCE_Received

Occurs on data receive.

enumerator eTCE_Sent
enumerator eTCE_Poll
using TcpConnectionDestroyedDelegate = Delegate<void(TcpConnection&)>
NETWORK_DEBUG
NETWORK_SEND_BUFFER_SIZE
class TcpConnection : public IpConnection
#include <TcpConnection.h>

Subclassed by FtpDataStream, FtpServerConnection, TcpClient, TcpServer

Public Functions

inline int writeString(const char *data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)

Writes string data directly to the TCP buffer.

Parameters
  • data – null terminated string

  • apiflags – TCP_WRITE_FLAG_COPY, TCP_WRITE_FLAG_MORE

Returns

int – negative on error, 0 when retry is needed or possitive on success

inline int writeString(const String &data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)

Writes string data directly to the TCP buffer.

Parameters
  • data

  • apiflags – TCP_WRITE_FLAG_COPY, TCP_WRITE_FLAG_MORE

Returns

int – negative on error, 0 when retry is needed or possitive on success

virtual int write(const char *data, int len, uint8_t apiflags = TCP_WRITE_FLAG_COPY)

Base write operation.

Parameters
  • data

  • len

  • apiflags – TCP_WRITE_FLAG_COPY, TCP_WRITE_FLAG_MORE

Returns

int – negative on error, 0 when retry is needed or possitive on success

int write(IDataSourceStream *stream)

Writes stream data directly to the TCP buffer.

Parameters
  • stream

  • apiflags – TCP_WRITE_FLAG_COPY, TCP_WRITE_FLAG_MORE

Returns

int – negative on error, 0 when retry is needed or possitive on success

inline void setDestroyedDelegate(TcpConnectionDestroyedDelegate destroyedDelegate)

Sets a callback to be called when the object instance is destroyed.

Parameters

destroyedDelegate

inline void setSslInitHandler(Ssl::Session::InitDelegate handler)

Set the SSL session initialisation callback.

Parameters

handler

inline Ssl::Session *getSsl()

Get a pointer to the current SSL session object.

Note that this is typically used so we can query properties of an established session. If you need to change session parameters this must be done via setSslInitHandler.

Client API

enum TcpClientState

Values:

enumerator eTCS_Ready
enumerator eTCS_Connecting
enumerator eTCS_Connected
enumerator eTCS_Successful
enumerator eTCS_Failed
enum TcpClientCloseAfterSentState

Values:

enumerator eTCCASS_None
enumerator eTCCASS_AfterSent
enumerator eTCCASS_AfterSent_Ignore_Received
using TcpClientEventDelegate = Delegate<void(TcpClient &client, TcpConnectionEvent sourceEvent)>
using TcpClientCompleteDelegate = Delegate<void(TcpClient &client, bool successful)>
using TcpClientDataDelegate = Delegate<bool(TcpClient &client, char *data, int size)>
TCP_CLIENT_TIMEOUT
class TcpClient : public TcpConnection
#include <TcpClient.h>

Subclassed by HttpConnection, MqttClient, SmtpClient

Public Functions

inline void setReceiveDelegate(TcpClientDataDelegate receiveCb = nullptr)

Set or clear the callback for received data.

Parameters

receiveCb – callback delegate or nullptr

inline void setCompleteDelegate(TcpClientCompleteDelegate completeCb = nullptr)

Set or clear the callback for connection close.

Parameters

completeCb – callback delegate or nullptr

bool send(IDataSourceStream *source, bool forceCloseAfterSent = false)

Sends data stream.

Note

This function takes ownership of the stream pointer!

Parameters
  • source – stream pointer

  • forceCloseAfterSent

Returns

bool – true when the stream can be used. When false the stream will be deleted.

inline void setCloseAfterSent(bool ignoreIncomingData = false)

Schedules the connection to get closed after the data is sent

Parameters

ignoreIncomingData – when that flag is set the connection will start ignoring incoming data.

inline void commit()

Tries to send the pending data immediately.

Note

Call this method to decrease latency. Use it carefully.

Server API

using TcpClientConnectDelegate = Delegate<void(TcpClient *client)>
TCP_SERVER_TIMEOUT
class TcpServer : public TcpConnection
#include <TcpServer.h>

Subclassed by CustomFtpServer, HttpServer, TelnetServer