ITcpEndpoint

An end point means either a TcpServer that listens on a port or a TcpClient that tries to establish a TCP connection


struct ITcpEndpoint
{
    virtual void SetAcceptor(ISocketConnectionAcceptor*) = 0;
    virtual Iocp* GetIocp() = 0;
    virtual void Connect() = 0;
    virtual void Close() = 0;
};


void SetAcceptor(ISocketConnectionAcceptor*)

Set a callback for receiving notifications of connections from the end point


Iocp* GetIocp()

Retrieves the associated Iocp.


void Connect()

Causes the ITcpEndpoint to queue an asynchronous accept (for a TcpServer) or connect (for a TcpClient). Multiple pending calls to Connect() are supported. However the connections will be made sequentially.

Every call to Connect() is paired with one call to either OnConnect() or OnConnectFailure() on the associated ISocketConnectionAcceptor.

It is safe to call Connect() if either the following are true:

It is safe to make concurrent calls to Connect().


void Close()

Every successful call to CreateTcpServer or CreateTcpClient (i.e. that doesn't return nullptr) must be paired with a call to Close().

Close() has an abortive nature and therefore may cause calls to OnConnectFailure() on the ISocketConnectionAcceptor for each queued Connect().

For a TcpServer this closes the socket listening on the port before returning.

Close() is synchronous. It waits until all calls to the ISocketConnectionAcceptor have completed before returning. Therefore it is appropriate to delete the ISocketConnectionAcceptor just after calling Close().

The TcpServer/TcpClient must be closed in order that the Iocp can be closed without blocking indefinitely

Threading rules

x -> y         : means the implementation of x invokes y
x |-> y        : means a call to x causes an asynchronous call to y

||(Connect)

Connect |-> (OnConnect,OnConnectFailure)

not ((OnConnect,OnConnectFailure) -> Connect) => (Connect < Close)