Create a TCP server, wait for client connections. Allow to have a SSL encryption if needed. The server is waiting on a port. When a client is connected to the port, run the provided handler.
Each connection is fork in an other process to allow the server to get a new request immediately.
When the handler is finished, the child process is killed.
The default number of childs (active connections) is 500.
public function handler ($tcpserver)
{
// This handler will manage the connection if a client is connected on
// port 5000
print_r ($tcpserver->getInfo ());
$read = $tcpserver->read ();
$tcpserver->send ("Hi\r\n");
$tcpserver->disconnect ();
}
$tcpserver = new Domframework\Tcpserver ();
$tcpserver->init ("::", 5000, array ($this, "handler"));
$tcpserver->loop ();
$options = array (
"cafile" => "$caCertDir/CA.crt",
"local_cert" => "$cacheKeysDir/$host.crt",
"local_pk" => "$cacheKeysDir/$host.key",
"verify_peer" => false,
);
$tcpserver->setSSLOptions ($options);
$tcpserver->cryptoEnable (true);
By default, the server is waiting for text data. It read one line and finished on \r or \n or \r\n. It is possible to put the server in binary mode. Then, the read method will read the maxNumber of bytes.
$tcpserver->readMode ("text");
$read = $tcpserver->read ();
$tcpserver->readMode ("binary");
$read = $tcpserver->read (10);
Namespace Domframework
/** This class allow to start a TCP server and call a function each time a client is connected on it. Each client is separated in a child, so the server allow to have multiple simultaneous connections. The handler method or function will get one parameter : the client from socket_accept. It will allow to use socket_getpeername to get the peer address and port, socket_read to get the data from the client, socket_write to write on the client, socket_shutdown ($client) and socket_close ($client) to finish the connection The server has a child limit set to 500 connections by default
No property available
/** The constructor add the error handler needed to catch the error on stream_select () when the process is killed
/** The destructor is a log for debug
/** Activate the SSL connection Put the socket in blocking mode, as it is mandatory to have SSL connection@param boolean $val
True to activate, false to disable SSL@param integer $cryptoMethod
The cryptoMethod allowed
/** Disconnect the socket
/** Error catcher. By default, do nothing, but can be overrided by the user@param integer $errNo
The error number@param string $errMsg
The error message@param string $file
The file name@param integer $line
The line where the error raised
/**
Exception catcher
By default do nothing, but can be overrided by the user
@param object $exception
The exception to catch
/**
Get an array with the peer address, peer port, local address and local
port
@return
array array ("peer address", peer port, "local address", local
port)
/**
In child, get the socket to direct access
@return
resource The socket with the client
/** Set the address, port and handler that will be enabled by loop@param string $address
The server address (can be 0.0.0.0 for all IPv4 interfaces or :: for all the IPv4 and IPv6 interfaces)@param integer $port
The port to listen@param callable $handler
The handler that will be called when a client is connected to the address:port
/**
Log the debug, By defaul do nothing, but can be overrided by the user
@param mixed|null $params
The data to store in log
/**
Log the errors, By defaul do nothing, but can be overrided by the user
@param mixed|null $params
The data to store in log
/** Log the methods called. By default, do nothing, but can be overrided by the user@param string $method
The data to store in log@param mixed|null $args
The data to store in log
/**
Log the data received from the client. By default, do nothing, but can be
overrided by the user
@param string $data
The data to store in log
/**
Log the data send to the client. By default, do nothing, but can be
overrided by the user
@param string $data
The data to store in log
/** Start the main loop after the init and keep in it until loopStop
/**
Start the main loop in background and do not wait its end
@return
integer the PID of the child
/** Stop the main loop in background and wait until its end
/** Request the loop to stop. Will not allow new connections, but wait the end of the existing processus Block until all is closed
/**
Set/get the max children, the maximum of concurrent connections
@param integer|null $val
The number of child to get/set
/**
Set the process name displayed in system
@param string|null $val
The name of the process to set (or get if null)
/** Read the data from the client. The connection must be established Use the readMode in text or binary (text by default) In text mode, the read return when found the first \r or the first \n.@param integer $maxLength
Limit the length of the data from the server@return
string The content
/**
Set/get the read mode : text or binary
@param string|null $val
The mode to set (or get if null)
/** Send data to the client@param mixed $data
The data to send@return
the length of data sent
/**
Set context SSL option.
@param array $options
The ssl array to set
/**
Set the timeout for open communication without any data transmited.
@param string|null $val
The number of seconds to set (or get if null)
/** Manage the timeout handler By default, disconnect and generate an exception