Indy 9
|
TIdSocketHandle = class(TCollectionItem)
TIdSocketHandle is used by TIdTCPConnection descendants in Indy to encapsulate the low-level connection and methods used to access the protocol stack in Binding.
property ClientPortMax: Integer;
property ClientPortMin: Integer;
property Handle: TIdStackSocketHandle;
Handle is assigned a valid value in Accept, and AllocateSocket. The value in Handle is Id_INVALID_SOCKET prior to calling one of these methods. Handle is release in CloseSocket.
HandleAllocated reflects the current state of Handle.
property HandleAllocated: Boolean;
HandleAlocated is True when a valid socket descriptor has been created in AllocateSocket or Accept. HandleAllocated is False when a socket descriptor has not been selected, or the value of Handle is Id_INVALID_SOCKET.
HandleAllocated is used to allow sockets, altered using SetSockOpt, to indicate that the socket handle is no longer available for send and receive operations.
property IP: string;
IP is used in Bind and Connect to initialize or open a socket descriptor for the connection. IP is used in conjunction with Port to determine the local association used to identify the connection endpoint.
property PeerIP: string;
PeerIP is used in conjunction with PeerPort to determine the association used to identify the remote connection endpoint.
PeerIP is updated in SetPeer, and as a result of Accept, SendTo, and RecvFrom operations.
property PeerPort: integer;
Note: Many port numbers are associated, by convention, with a particular service such as ftp or http as described in IdPorts. Some protocols use pre-defined port numbers. Port numbers below 1024 are reserved, but using a number above 1024 does not guarantee there will be no conflict.
PeerPort is updated using SetPeer, or when Accept is used to bind a connection to a remote system.
property Port: integer;
Note: Many port numbers are associated, by convention, with a particular service such as ftp or http as described in IdPorts. Some protocols use pre-defined port numbers. Port numbers below 1024 are reserved, but using a number above 1024 does not guarantee there will be no conflict.
Port is used in conjunction with IP to determine the local association used to identify the connection endpoint from Bind and Connect.
function Accept(ASocket: TIdStackSocketHandle): boolean;
Accept is used by the listening thread of server applications that spawn new threads for each connection request, like TIdListenerThread and TIdUDPListenerThread.
Accept assigns the new socket descriptor to Handle and sets HandleAllocated to True.
Accept calls UpdateBindingLocal to insure that the protocol stack handler uses the correct protocol family, IP, and Port for servers that may listen on more than one local IP address.
Accept calls UpdateBindingPeer to insure that the protocol stack handler uses the correct protocol family, PeerIP, and PeerPort for the client connection.
If no pending connections are present on the socket descriptor, Accept will wait until a connection is present before exiting from the method.
procedure AllocateSocket(const ASocketType: Integer = Id_SOCK_STREAM; const AProtocol: Integer = Id_IPPROTO_IP);
ASocketType determines address family used for the connection, and indicates the valid protocol family options for the socket type. The following socket types are supported:
AProtocol identifies the Internet protocol to be used for the given socket type. Only a single protocol exists to support a particular socket type using a given address format. The following protocol families are supported:
AllocateSocket will call CloseSocket if the socket handle is already allocated.
AllocateSocket will set HandleAllocated to True to reflect the state of the socket handle.
procedure Assign(Source: TPersistent); override;
procedure Bind;
If the application does not care what address is assigned to it, it may use a blank IP (''). This allows the protocol stack to assign the Internet address Id_INADDR_ANY, and an any appropriate network interface will be used.
If the application does not care what port is assigned to it, it may use Port 0. This allows the protocol stack to assign a unique port to the application with a value between 1024 and 5000.
Use SetSockOpt to alter socket-level settings including broadcast, linger, buffering, and Nagle options.
Bind will raise an EIdException if the address in IP and Port is already in use.
procedure CloseSocket(const AResetLocal: boolean = True); virtual;
AResetLocal indicates that rest should be called for the local connection.
CloseSocket is affected by options used in SetSockOpt for Id_SO_LINGER.
function Connect(const AFamily: Integer = Id_PF_INET): Integer; virtual;
Connect uses IP and Port, and the parameter AFamily, to call the Connect facilities of the protocol stack. When Connect has successfully completed, the socket will be ready to send and receive data using Send, SendTo, Recv, and RecvFrom.
Use Readable, any time following a successful Connect, to determine if the socket is in a valid input/output state.
constructor Create(ACollection: TCollection); override;
The ACollection parameter is the TIdSocketHandles collection that owns the collection item instance. ACollection is used to retrieve the value in DefaultPort, and to assign that value to Port in the TIdSocketHandle instance.
destructor Destroy; override;
Note: Do not call Destroy directly. Call Free instead.
procedure GetSockOpt(level: Integer; optname: Integer; optval: PChar; optlen: Integer);
OptNames is one of the constant values declared in IdStackConts.pas.
OptVal is the resulting socket option after a call to WSGetSockOpt using the global stack instance GStack.
Use SetSockOpt to update the options for a socket descriptor.
procedure Listen(const anQueueCount: integer = 5);
anQueueCount identifies the maximum number of pending connection requests to allow for the socket descriptor. Use care when specifying a larger value for anQueueCount; most protocol stacks limit the number of pending connections requests allowed per socket descriptor to a small value. Use Accept to service a pending connection request.
Handle identifies the socket descriptor used to listen for connections, and must not be connected prior to calling Listen.
function Readable(AMSec: Integer = IdTimeoutDefault): boolean;
Readable will raise an EIdConnClosedGraceful exception if HandleAllocated contains False.
Readable detects the use of a TIdAntifreeze instance in an application. When present, Readable yields processing cycles during the lifetime of the method call to the TIdAntifreeze.DoProcess method.
When AMSec is IdTimeoutInfinite, Readable uses the IdleTimeout value in TIdAntifreeze as its time-out value. Readable will continue executing until the socket is ready to read.
When AMsec is larger than the IdleTimeout value in TIdAntifreeze, Readable is called using the difference between the two time-out values. If the socket is ready to read, no further processing is performed. If the socket is not ready to read, the time-out value is set to the IdleTimeout value in TIdAntifreeze and Readable is called again to determine the socket status. Readable yields processing cycles to the TIdAntifreeze.DoProcess method if the socket is still not ready to read.
Readable does not yield processing cycles when an instance of TIdAntifreeze has not been used in the application.
function Recv(var ABuf; ALen: Integer; AFlags: Integer): Integer;
AFlags can be used to alter the behavior of the Recv method beyond the options specified in SetSockOpt. The supported values of AFlags includes:
function RecvFrom(var ABuffer; const ALength: Integer; const AFlags: Integer; var VIP: string; var VPort: Integer): Integer; virtual;
RecvFrom is used on streaming sockets, to read all available data up to the size of the read buffer. RecvFrom can also retrieve Out-of-Band data on sockets configured using Id_SO_OOBINLINE in SetSockOpt. If no data is available on the socket, RecvFrom waits for data to arrive.
AFlags can be used to alter the behavior of the RecvFrom method beyond the options specified in SetSockOpt. The supported values of AFlags includes:
procedure Reset(const AResetLocal: boolean = True);
When aResetLocal is True, the IP and Port for the socket descriptor are reset to the following values:
Reset is called from Create and CloseSocket.
function Select(ASocket: TIdStackSocketHandle; ATimeOut: Integer): boolean;
On successful compeletion of TIdStack.WSSelect, TIdAntiFreezeBase.DoProcess is called to force an idle state in the main thread of execution, and to allow message processing for other threads of execution.
function Send(var Buf; len: Integer; flags: Integer): Integer;
Datagram sockets cannot write datagrams that are larger than the MAXUDPDG constant declared by the protocol stack. Streaming socket connections will attempt to write all data in the buffer up to the size specified.
AFlags can be used to alter the behavior of the Send method beyond the options specified in SetSockOpt. The supported values of Flags includes:
procedure SendTo(const AIP: string; const APort: Integer; var ABuffer; const ABufferSize: Integer);
SendTo will raise an EIdException exception if the number of bytes sent does not match the value in ABufferSize, or if the number of bytes sent is Id_SOCKET_ERROR.
SendTo is used on datagram or stream sockets. For datagram sockets, care must be taken not to exceed the maximum packet size of the protocol stack declared in MAXUDPDG. Streaming sockets will attempt to write all data in the buffer to the socket connection.
To send a broadcast datagram, the IP address parameter must contain Id_INADDR_ANY.
procedure SetPeer(const asIP: string; anPort: integer);
SetPeer is also used by the listening thread of server applications that spawn new threads for each connection request, like TIdListenerThread and TIdUDPListenerThread.
procedure SetSockOpt(level: Integer; optname: Integer; optval: PChar; optlen: Integer);
There are two types of socket options: Boolean and Data. Boolean options enable or disable a specific feature. Use a zero value in OptVal to disable a feature, and a non-zero value in OptVal to enable the feature.
Data options require the address used to store the value or structure in OptVal. OptNames use the contant values declared in IdStackConts.pas, including:
Allow transmission of broadcast messages on the socket.
Record debugging information.
Don't block CloseSocket for unsent data.
Don't route dsata, send it directly to the interface.
Send keepalive packets.
Linger on close if unsent data is present.
Receive out-of-band data in the normal data stream.
Specify buffer size for receives.
Allow the socket to be bound to an address which is already in use.
Specify buffer size for sends.
Receive timeout.
Send timeout.
Set Time-To-Live in IP header fields.
Disables the Nagle algorithm for send coalascing.
procedure UpdateBindingLocal;
procedure UpdateBindingPeer;