Home Units Hierarchy Routines All identifiers

Class unaSocks

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type unaSocks = class(unaObject)

Description

This class is used to create server and client sockets and manage the connections.

Hierarchy

Overview

Methods

Public constructor create(threadsInPool: unsigned = 4 );
Public destructor Destroy(); override;
Public function activate(id: tConID): bool;
Public function closeThread(id: tConID; timeout: tTimeout = 2700): bool;
Public function createConnection(const host, port: string; protocol: int = IPPROTO_TCP; activate: bool = true; const bindToIP: string = '0.0.0.0'; const bindToPort: string = '0' ): tConID;
Public function createServer(const port: string; protocol: int = IPPROTO_TCP; activate: bool = true; backlog: int = SOMAXCONN; udpConnectionTimeout: tTimeout = c_defUdpConnTimeout; const bindToIP: string = '0.0.0.0' ): tConID;
Public function enter(ro: bool; timeout: tTimeout = tTimeout(INFINITE)): bool;
Public function getConnection(id: tConID; connId: tConID; timeout: tTimeout = 2000; needAcquire: bool = true): unaSocksConnection;
Public function getRemoteHostAddr(id, connId: tConID): pSockAddrIn;
Public function getRemoteHostInfo(addr: pSockAddrIn; out ip, port: string): bool; overload;
Public function getRemoteHostInfo(id, connId: tConID; out ip, port: string): bool; overload;
Public function getRemoteHostInfoEx(id, connId: tConID; out ip, port: string; out proto: int): bool;
Public function getSocketError(id: tConID; needLock: bool = true): int;
Public function getThreadByID(id: tConID; timeout: tTimeout = 1000): unaSocksThread;
Public function getThreadSocket(id: tConID): unaSocket;
Public function isActive(id: tConID): bool;
Public function okToWrite(id, connId: tConID; timeout: tTimeout = 100; noCheckState: bool = false): bool;
Public function removeConnection(id: tConID; connId: tConID): bool;
Public function sendData(id: tConID; data: pointer; size: uint; connId: tConID; out asynch: bool; noCheck: bool = false; timeout: tTimeout = 500): uint;
Public procedure clear(clearServers: bool = false; clearClients: bool = false);
Public procedure leaveRO();
Public procedure leaveWO();
Public procedure setPoolSize(threadsInPool: unsigned);
Protected procedure event(event: unaSocketEvent; id, connId: tConID; data: pointer = nil; size: uint = 0); virtual;

Properties

Public property onEvent: unaSocksOnEventEvent read f_onEvent write f_onEvent;

Description

Methods

Public constructor create(threadsInPool: unsigned = 4 );

Creates socks object.

Parameters
threadsInPool
Number of threads to create in pool (default is 4).
isIOCP
Use IOCP for sockets' operations (default is False).
Public destructor Destroy(); override;

Destroys socks object.

Public function activate(id: tConID): bool;

Activate a thread (start server or connect client).

Parameters
id
Thread id to activate.
Returns

True if thread was started successfully.

Public function closeThread(id: tConID; timeout: tTimeout = 2700): bool;

Closes and removes all connection for specified thread. Also stops the thread and makes it available for new connections.

Parameters
id
Thread id.
timeout
Timeout in ms (default is 2700).
Returns

True if operation was successfull.

Public function createConnection(const host, port: string; protocol: int = IPPROTO_TCP; activate: bool = true; const bindToIP: string = '0.0.0.0'; const bindToPort: string = '0' ): tConID;

Creates new client socket. Returns thread id which will handle events for this client connection or 0 if some error occured.

Parameters
host
Remote host to connect to.
port
Remote port to connect to.
protocol
Proto (UDP or TCP, default is IPPROTO_TCP).
activate
Connect client after creation (default is True).
bindToIP
Bind to this local IP (default is '0.0.0.0').
bindToPort
Bind to this local port (default is '0').
overlapped
Create overlapped socket (default is True).
Returns

Thread id or 0 if some error occured.

Public function createServer(const port: string; protocol: int = IPPROTO_TCP; activate: bool = true; backlog: int = SOMAXCONN; udpConnectionTimeout: tTimeout = c_defUdpConnTimeout; const bindToIP: string = '0.0.0.0' ): tConID;

Creates new server socket. Returns thread id which will handle connections for this socket or 0 if some error occur.

Parameters
port
Local port to listen at.
protocol
Proto (UDP or TCP, default is IPPROTO_TCP).
activate
Start server after creation (default is True).
backlog
Number of pending client connections (default is SOMAXCONN).
udpConnectionTimeout
Timeout for UDP clients (default is c_defUdpConnTimeout (3 minutes)).
bindToIP
Bind to this local IP (default is '0.0.0.0').
overlapped
Create overlapped socket (default is True).
Returns

Thread id or 0 if some error occured.

Public function enter(ro: bool; timeout: tTimeout = tTimeout(INFINITE)): bool;

Locks socks object.

Parameters
timeout
Timeout (default is INFINITE).
Public function getConnection(id: tConID; connId: tConID; timeout: tTimeout = 2000; needAcquire: bool = true): unaSocksConnection;

Returns specified connection.

WARNING! If needAcquire was True (default), you must call release() method of returned connection when it is no longer needed.

Parameters
id
Thread id which handles the connection.
connId
Client connection id (0 for client threads).
timeout
Timeout in ms for acquiring (if needAcquire is True, default is 2000).
needAcquire
Lock connection object before returning (default is True).
Returns

Connection object or nil.

Public function getRemoteHostAddr(id, connId: tConID): pSockAddrIn;

Returns remote address for connection.

Parameters
id
Thread id which handles the connection.
connId
Client connection id (0 for client threads).
Returns

Pointer to address record or nil.

Public function getRemoteHostInfo(addr: pSockAddrIn; out ip, port: string): bool; overload;

Converts address record into IP and port.

Parameters
addr
Address record.
ip
[OUT] Remote IP.
port
[OUT] Remote port.
Returns

True if function was successfull.

Public function getRemoteHostInfo(id, connId: tConID; out ip, port: string): bool; overload;

Returns remote IP and port for connection.

Parameters
id
Thread id which handles the connection.
connId
Client connection id (0 for client threads).
ip
[OUT] Remote IP.
port
[OUT] Remote port.
Returns

True if function was successfull.

Public function getRemoteHostInfoEx(id, connId: tConID; out ip, port: string; out proto: int): bool;

Returns remote IP, port and proto for connection.

Parameters
id
Thread id which handles the connection.
connId
Client connection id (0 for client threads).
ip
[OUT] Remote IP.
port
[OUT] Remote port.
proto
[OUT] Remote proto (IPPROTO_TCP/IPPROTO_UDP).
Returns

True if function was successfull.

Public function getSocketError(id: tConID; needLock: bool = true): int;

Returns socket error for a thread.

Parameters
id
Thread id.
needLock
Lock the threads (defualt is True).
Returns

Error code.

Public function getThreadByID(id: tConID; timeout: tTimeout = 1000): unaSocksThread;

Returns thread object.

Parameters
id
Thread id.
Returns

Thread object or nil.

Public function getThreadSocket(id: tConID): unaSocket;

Returns socket object assigned to a thread.

Parameters
id
Thread id.
Returns

Socket object or nil.

Public function isActive(id: tConID): bool;

Returns thread active state.

Returns

True if thread is active (server is listening, client is connected).

Public function okToWrite(id, connId: tConID; timeout: tTimeout = 100; noCheckState: bool = false): bool;

Returns true if it seems that we can send data to remote side over specified connection.

Parameters
id
Thread id which handles the connection.
connId
Client connection id (0 for client threads).
timeout
Timeout in ms for error checking (valid if noCheck is False, default is 100).
noCheckState
Do not check the state of connection before sending (speeds up sending a little, default is False).
Returns

True if data can be send.

Public function removeConnection(id: tConID; connId: tConID): bool;

Closes and removes specified connection. If thread has no more connections, closes it as well.

Parameters
id
Thread id which handles the connection.
connId
Client connection id (0 for client threads).
Returns

True if connection was closed.

Public function sendData(id: tConID; data: pointer; size: uint; connId: tConID; out asynch: bool; noCheck: bool = false; timeout: tTimeout = 500): uint;

Sends data to remote side over specified connection.

Parameters
id
Thread id which handles the connection.
data
Data buffer to send.
size
Size of data buffer.
connId
Client connection id (0 for client threads).
noCheck
Do not check the state of connection before sending (speeds up sending a little, default is False).
timeout
Timeout in ms for error checking (valid if noCheck is False, default is 1000).
Returns

0 if data was sent successfully, or specific WSA error otherwise.

Public procedure clear(clearServers: bool = false; clearClients: bool = false);

Closes all client or server (or both) threads.

Parameters
clearServers
Close all server threads (default is False).
clearClients
Close all client threads (default is False).
Public procedure leaveRO();

Unlocks socks object.

Public procedure leaveWO();

Unlocks socks object.

Public procedure setPoolSize(threadsInPool: unsigned);

Sets new pool size.

Parameters
threadsInPool
Specifies the number of threads in the pool to be created.
Protected procedure event(event: unaSocketEvent; id, connId: tConID; data: pointer = nil; size: uint = 0); virtual;

This virtual method is called every time new sockets event occur.

Parameters
id
Specifies the thread id for which the even applies.
connId
Specifies the connection id of the thread for which the even applies.
event
Specifies the type of event (see unaSocketEvent for details).
data
Data block available from client or server.
size
Size of data.

Properties

Public property onEvent: unaSocksOnEventEvent read f_onEvent write f_onEvent;

This event is fired every time new sockets event occurs. Refer to .event() method for more information.

(c) 2012 Lake of Soft


Generated by PasDoc 0.12.1 on 2012-10-09