Internet Direct (Indy) Version 9.0.1 Beta
TIdTCPConnection.ReadFromStack
Reads data from the protocol stack using the socket binding.

function ReadFromStack(const ARaiseExceptionIfDisconnected: Boolean; ATimeout: Integer; const ARaiseExceptionOnTimeout: Boolean): Integer; virtual;
Parameters
const ARaiseExceptionIfDisconnected: Boolean = True
Raise an Exception when disconnected. Default value is True.

ATimeout: Integer = IdTimeoutDefault
Timeout value to wait for a readble socket handle. Default value is IdTimeoutDefault.

const ARaiseExceptionOnTimeout: Boolean = True
Raise an exception when a timeout occurs. Default value is True.

Returns
Integer - Number of bytes read from the protocol stack.

Description
ReadFromStack is an Integer function used to read data from the IP protocol stack for the peer connection into the Indy buffer maintained for the connection.

ATimeout indicates the number of milliseconds to wait for the IOHandler to become readable prior to reading data. When ATimeout contains the value IdTimeoutDefault, ReadTimeout will be used as the timeout value when it is non-zero. Otherwise, IdTimeoutInfinite is used as the timeout value. If a timeout occurs, an EIdReadTimeout exception will be raised when ARaiseExceptionOnTimeout is True. Otherwise ReadFromStack performs no additional processing, and uses -1 as the return value for the method.

ReadFromStack calls CheckForDisconnect using the value specifed in ARaiseExceptionIfDisconnected to determine if an exception is raised when the connection is closed. If the socket is not Connected, ReadFromStack will exit from the method.

ReadFromStack uses IOHandler to determine when the connection is readable, and to receive data from the input source. ReadFromStack updates ClosedGracefully indicating when all data has been read from the stack prior to closing the connection, and calls TIdStack.CheckForSocketError to determine if the connection closed abnormally.

When ClosedGracefully is False, the protocol stack is used to check for an Id_WSAESHUTDOWN error and allows the socket to disconnect when the error is detected. If the Indy read buffer contains no unread data, the Id_WSAESHUTDOWN exception is re-raised using the protocol stack handler.

ReadFromStack provides support for TIdConnectionIntercept by calling the Receive handler for the Intercept when Enabled. ReadFromStack also performs 8-bit to 7-bit character translations when ASCIIFilter is True.

Finally, ReadFromStack moves data read from the input source to the internal Indy buffer, and sets the return value to the number of bytes handled by the operation.

ReadFromStack can be a time intensive operation due to network contention and the nature of blocking sockets. ReadFromStack should be the only location where data is read from the protocol stack.