PHP WebSocket2 Library
PHP5 Library for browsing web
NetworkSocket Class Reference

Base class for network socket implementations. More...

Inheritance diagram for NetworkSocket:
HttpSocket FileSocket LowLevelSocket

Public Member Functions

 setOnReceiveBodyCallback (callable $on_receive_body=null)
 Sets callback function that will be called when a part of response body is read from socket. More...
 
 getOnReceiveBodyCallback ()
 Returns callback function that will be called when a part of response body is read from socket. More...
 
 setOnReceiveHeadersCallback (callable $on_receive_headers=null)
 Sets callback function that will be called when response headers are read from socket. More...
 
 getOnReceiveHeadersCallback ()
 Returns callback function that will be called when response headers are read from socket. More...
 
 sendRequest (WebRequest $request)
 
 setTimeout ($timeout)
 Sets timeout in seconds for socket operations. More...
 
 getTimeout ()
 Returns timeout in seconds for socket operations. More...
 
 getAvailableTime ()
 Returns timeout in seconds for socket operations. More...
 
 setTimeoutMode ($timeout_mode)
 Sets timeout calculation mode for socket operations. More...
 
 getTimeoutMode ()
 Returns timeout calculation mode for socket operations. More...
 
 writeRaw ($data)
 
- Public Member Functions inherited from HttpSocket
 __destruct ()
 Destructor.
 
 sendRequest (WebRequest $request)
 Sends request to socket and returns response. More...
 
 writeRaw ($request)
 Writes raw data to socket. More...
 

Data Fields

const TIMEOUT_MODE_EVERY_OPERATION = 0
 Timeout calculation mode: check for timeout for each network operation independently. More...
 
const TIMEOUT_MODE_TOTAL = 1
 Timeout calculation mode: check for timeout for all network operations in total from open() call. More...
 
const TIMEOUT_MODE_SUM_OF_OPERATIONS = 2
 Timeout calculation mode: check for timeout for all network operations in total. More...
 

Protected Member Functions

 readLength ($length, $headers, WebRequest $request, &$aborted)
 
 readAll ($headers, WebRequest $request)
 
 setSecure ($secure)
 Sets socket to secure mode or turns off secure mode. More...
 
 isSecure ()
 Returns true if socket is in secure mode. More...
 
 writeRawPart ($data)
 Writes part of raw data to socket. More...
 
 startTimeMeasurement ($socket_open=false)
 Starts time measurement for socket operation (read, write, connect, etc). More...
 
 checkPoint ()
 Stops time measurement for socket operation (read, write, connect, etc) and checks for timeout. More...
 
- Protected Member Functions inherited from HttpSocket
 open ($address, $port)
 Opens socket. More...
 
 read ($size)
 Reads raw data from socket. More...
 
 close ()
 Closes socket.
 
 isOpen ()
 Returns true if socket is open. More...
 
 readChunked ($headers, WebRequest $request)
 Reads chunked content from socket and puts chunks to single string. More...
 
 readLength ($length, $headers, WebRequest $request, &$aborted)
 Reads data with specified length from socket. If socket closes connection or no more data is available to reach length, throws WebRequestException. More...
 
 readAll ($headers, WebRequest $request)
 Reads all available data from socket until it closes connection. More...
 
 readUntil ($text)
 Reads all available data from socket until specified substring is found inside contents. More...
 
 readHeaders ()
 Reads response headers. More...
 
 write (WebRequest $request)
 Writes request to socket. More...
 

Protected Attributes

 $timeout = 30
 
 $timeout_mode = self::TIMEOUT_MODE_SUM_OF_OPERATIONS
 
 $start_time = 0
 
 $socket_open_time = 0
 
 $time_taken = 0
 

Detailed Description

Base class for network socket implementations.

Member Function Documentation

checkPoint ( )
protected

Stops time measurement for socket operation (read, write, connect, etc) and checks for timeout.

Exceptions
WebRequestExceptionif timeout was detected
getAvailableTime ( )

Returns timeout in seconds for socket operations.

Differs from getTimeout when TIMEOUT_MODE_TOTAL or TIMEOUT_MODE_SUM_OF_OPERATIONS mode is selected (see setTimeoutMode). In this case, returns number of seconds that is available for socket operation. For example, if socket open operation took 5 seconds and total timeout was set to 30 seconds, then further calls will have only 25 seconds to complete.

Return values
floatTimeout in seconds for socket operations
getOnReceiveBodyCallback ( )

Returns callback function that will be called when a part of response body is read from socket.

See also setOnReceiveBodyCallback.

Return values
callableCallback function or null
getOnReceiveHeadersCallback ( )

Returns callback function that will be called when response headers are read from socket.

See also setOnReceiveHeadersCallback.

Return values
callableCallback function or null
getTimeout ( )

Returns timeout in seconds for socket operations.

Default value is 30 seconds.

Return values
intTimeout in seconds for socket operations
getTimeoutMode ( )

Returns timeout calculation mode for socket operations.

See also TIMEOUT_MODE_TOTAL, TIMEOUT_MODE_EVERY_OPERATION, TIMEOUT_MODE_SUM_OF_OPERATIONS. Default value is TIMEOUT_MODE_SUM_OF_OPERATIONS.

Return values
intTimeout calculation mode for socket operations
isSecure ( )
abstractprotected

Returns true if socket is in secure mode.

Return values
boolTrue if socket is in secure mode
setOnReceiveBodyCallback ( callable  $on_receive_body = null)

Sets callback function that will be called when a part of response body is read from socket.

Callback function prototype:

bool func(string $headers, string $data_part, HttpSocket $this, WebRequest $request)

Where: headers - raw response headers data;
data_part - part of response body data;
this - this HttpSocket instance;
request - WebRequest that is being sent.
This function must return true to continue reading from socket or false to abort reading. When this callback is specified, NetworkSocket::sendRequest function will always return just headers string. This will allow to still use HttpRequestManager for automatic redirection, cookies and authentication processing.

Parameters
callable$on_receive_bodyCallback function or null
setOnReceiveHeadersCallback ( callable  $on_receive_headers = null)

Sets callback function that will be called when response headers are read from socket.

Callback function prototype:

bool func(string $headers, HttpSocket $this, WebRequest $request)

Where: headers - raw response headers data;
this - this HttpSocket instance;
request - WebRequest that is being sent.
This function must return true to continue reading from socket or false to abort reading. When false is returned from callback, NetworkSocket::sendRequest function will return null.

Parameters
callable$on_receive_headersCallback function or null
Examples:
response_interception.php.
setSecure (   $secure)
abstractprotected

Sets socket to secure mode or turns off secure mode.

Parameters
bool$secureTrue to set socket to secure mode, false to turn secure mode off
Exceptions
WebRequestExceptionin case of errors
setTimeout (   $timeout)

Sets timeout in seconds for socket operations.

Default value is 30 seconds.

Parameters
int$timeoutTimeout in seconds for socket operations
Examples:
timeouts.php.
setTimeoutMode (   $timeout_mode)

Sets timeout calculation mode for socket operations.

See also TIMEOUT_MODE_TOTAL, TIMEOUT_MODE_EVERY_OPERATION, TIMEOUT_MODE_SUM_OF_OPERATIONS. Default value is TIMEOUT_MODE_SUM_OF_OPERATIONS.

Parameters
int$timeout_modeTimeout calculation mode for socket operations
startTimeMeasurement (   $socket_open = false)
protected

Starts time measurement for socket operation (read, write, connect, etc).

Parameters
bool$socket_openSet to true, if startTimeMeasurement is made from open function
writeRawPart (   $data)
abstractprotected

Writes part of raw data to socket.

Return values
intNumber of bytes written

Field Documentation

const TIMEOUT_MODE_EVERY_OPERATION = 0

Timeout calculation mode: check for timeout for each network operation independently.

This mode is used to calculate timeout for each socket operation independently. If you set timeout value to 30 seconds, then each call to open, read, write, etc will have 30 seconds to complete.

Examples:
timeouts.php.
const TIMEOUT_MODE_SUM_OF_OPERATIONS = 2

Timeout calculation mode: check for timeout for all network operations in total.

This mode is used to calculate total time taken by all calls to open, read, write, etc. This may be not very accurate.

Examples:
timeouts.php.
const TIMEOUT_MODE_TOTAL = 1

Timeout calculation mode: check for timeout for all network operations in total from open() call.

This mode is used to calculate elapsed time from socket open operation till any other socket operation. For example, if timeout value is 30 seconds and socket was opened at 11:11:00, then you have 30 seconds to do all operations like reading and writing. If you put sleep(10) between read and write calls, this time will count, too. This timeout is flushed when socket is closed.

Examples:
timeouts.php.

The documentation for this class was generated from the following file: