Remove all the {{{ and }}} folding
This commit is contained in:
@@ -24,7 +24,6 @@ class Tcpserver
|
||||
////////////////////
|
||||
// PROPERTIES //
|
||||
////////////////////
|
||||
// {{{
|
||||
/** Allow to debug with messages on screen
|
||||
*/
|
||||
private $debug = false;
|
||||
@@ -66,7 +65,6 @@ class Tcpserver
|
||||
private $processName = "tcpserver";
|
||||
|
||||
private $ipOrName;
|
||||
// }}}
|
||||
|
||||
////////////////////////
|
||||
// PUBLIC METHODS //
|
||||
@@ -75,27 +73,22 @@ class Tcpserver
|
||||
* stream_select () when the process is killed
|
||||
*/
|
||||
public function __construct ()
|
||||
// {{{
|
||||
{
|
||||
$this->logDebug ("NEW OBJECT CONSTRUCTED");
|
||||
set_error_handler([$this, 'errorHandler']);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** The destructor is a log for debug
|
||||
*/
|
||||
public function __destruct ()
|
||||
// {{{
|
||||
{
|
||||
$this->logDebug ("Object destructed");
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Set/get the max children, the maximum of concurrent connections
|
||||
* @param integer|null $val The number of child to get/set
|
||||
*/
|
||||
final public function maxChild ($val = null)
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($val === null)
|
||||
@@ -103,13 +96,11 @@ class Tcpserver
|
||||
$this->maxChild = intval ($val);
|
||||
return $this;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Set/get the read mode : text or binary
|
||||
* @param string|null $val The mode to set (or get if null)
|
||||
*/
|
||||
final public function readMode ($val = null)
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($val === null)
|
||||
@@ -120,13 +111,11 @@ class Tcpserver
|
||||
$this->readMode = $val;
|
||||
return $this;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Set the process name displayed in system
|
||||
* @param string|null $val The name of the process to set (or get if null)
|
||||
*/
|
||||
final public function processName ($val = null)
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($val === null)
|
||||
@@ -134,13 +123,11 @@ class Tcpserver
|
||||
$this->processName = $val;
|
||||
return $this;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Set the timeout for open communication without any data transmited.
|
||||
* @param string|null $val The number of seconds to set (or get if null)
|
||||
*/
|
||||
final public function timeout ($val = null)
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($val === null)
|
||||
@@ -154,7 +141,6 @@ class Tcpserver
|
||||
$this->timeout = intval ($val);
|
||||
return $this;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** 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
|
||||
@@ -164,7 +150,6 @@ class Tcpserver
|
||||
* connected to the address:port
|
||||
*/
|
||||
final public function init ($address, $port, $handler)
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
$this->nbChild = 0;
|
||||
@@ -173,12 +158,10 @@ class Tcpserver
|
||||
$this->ports[] = $port;
|
||||
return $this;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Start the main loop after the init and keep in it until loopStop
|
||||
*/
|
||||
final public function loop ()
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if (empty ($this->addresses))
|
||||
@@ -335,25 +318,21 @@ class Tcpserver
|
||||
}
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Request the loop to stop. Will not allow new connections, but wait the
|
||||
* end of the existing processus
|
||||
* Block until all is closed
|
||||
*/
|
||||
final public function loopStop ()
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
$this->loopStop = true;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Start the main loop in background and do not wait its end
|
||||
* @return integer the PID of the child
|
||||
*/
|
||||
final public function loopInBackgroundStart ()
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
$this->logDebug ("Start loopInBackground");
|
||||
@@ -384,12 +363,10 @@ class Tcpserver
|
||||
$this->loop ();
|
||||
exit;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Stop the main loop in background and wait until its end
|
||||
*/
|
||||
final public function loopInBackgroundStop ()
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
$this->logDebug ("Request loopInBackgroundStop");
|
||||
@@ -397,13 +374,11 @@ class Tcpserver
|
||||
pcntl_waitpid ($this->pidLoopInBackground, $status);
|
||||
$this->logDebug ("Request loopInBackgroundStop : END");
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** In child, get the socket to direct access
|
||||
* @return resource The socket with the client
|
||||
*/
|
||||
final public function getSock ()
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($this->parent === true)
|
||||
@@ -412,7 +387,6 @@ class Tcpserver
|
||||
throw new \Exception ("Can not send to client : not connected", 500);
|
||||
return $this->socket;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Get an array with the peer address, peer port, local address and local
|
||||
* port
|
||||
@@ -420,7 +394,6 @@ class Tcpserver
|
||||
* port)
|
||||
*/
|
||||
final public function getInfo ()
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($this->parent === true)
|
||||
@@ -441,7 +414,6 @@ class Tcpserver
|
||||
$localAddress = substr ($localAddress, 8, -1);
|
||||
return array ($address, $port, $localAddress, $localPort);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Activate the SSL connection
|
||||
* Put the socket in blocking mode, as it is mandatory to have SSL connection
|
||||
@@ -450,7 +422,6 @@ class Tcpserver
|
||||
*/
|
||||
final public function cryptoEnable ($val,
|
||||
$cryptoMethod = STREAM_CRYPTO_METHOD_TLS_SERVER)
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($this->socket === null)
|
||||
@@ -465,13 +436,11 @@ class Tcpserver
|
||||
stream_context_set_option ($this->socket, $options);
|
||||
return @stream_socket_enable_crypto ($this->socket, !!$val, $cryptoMethod);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Set context SSL option.
|
||||
* @param array $options The ssl array to set
|
||||
*/
|
||||
final public function setSSLOptions ($options)
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($this->socket === null)
|
||||
@@ -479,14 +448,12 @@ class Tcpserver
|
||||
"The server is not connected", 500);
|
||||
return stream_context_set_option ($this->socket, array ("ssl" => $options));
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Send data to the client
|
||||
* @param mixed $data The data to send
|
||||
* @return the length of data sent
|
||||
*/
|
||||
final public function send ($data)
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($this->parent === true)
|
||||
@@ -500,7 +467,6 @@ class Tcpserver
|
||||
$this->logSend ($data);
|
||||
return $sentLen;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Read the data from the client.
|
||||
* The connection must be established
|
||||
@@ -510,7 +476,6 @@ class Tcpserver
|
||||
* @return string The content
|
||||
*/
|
||||
final public function read ($maxLength = 1024)
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($this->parent === true)
|
||||
@@ -534,12 +499,10 @@ class Tcpserver
|
||||
$this->logReceive ($read);
|
||||
return $read;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Disconnect the socket
|
||||
*/
|
||||
final public function disconnect ()
|
||||
// {{{
|
||||
{
|
||||
$this->logMethods (__METHOD__, func_get_args ());
|
||||
if ($this->parent === true)
|
||||
@@ -549,14 +512,12 @@ class Tcpserver
|
||||
@stream_socket_shutdown ($this->socket, STREAM_SHUT_RDWR);
|
||||
$this->socket = null;
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** 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
|
||||
*/
|
||||
public function logSend ($data)
|
||||
// {{{
|
||||
{
|
||||
if (! $this->debug)
|
||||
return;
|
||||
@@ -564,14 +525,12 @@ class Tcpserver
|
||||
file_put_contents ("/tmp/debug", date ("H:i:s")." [".posix_getpid ().
|
||||
"] S> $data", FILE_APPEND);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** 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
|
||||
*/
|
||||
public function logReceive ($data)
|
||||
// {{{
|
||||
{
|
||||
if (! $this->debug)
|
||||
return;
|
||||
@@ -579,7 +538,6 @@ class Tcpserver
|
||||
file_put_contents ("/tmp/debug", date ("H:i:s")." [".posix_getpid ().
|
||||
"] C> $data", FILE_APPEND);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Log the methods called. By default, do nothing, but can be overrided by
|
||||
* the user
|
||||
@@ -587,7 +545,6 @@ class Tcpserver
|
||||
* @param mixed|null $args The data to store in log
|
||||
*/
|
||||
public function logMethods ($method, $args)
|
||||
// {{{
|
||||
{
|
||||
if (! $this->debug)
|
||||
return;
|
||||
@@ -617,33 +574,28 @@ class Tcpserver
|
||||
file_put_contents ("/tmp/debug", date ("H:i:s")." [".posix_getpid ().
|
||||
"] METHOD $data", FILE_APPEND);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Log the debug, By defaul do nothing, but can be overrided by the user
|
||||
* @param mixed|null $params The data to store in log
|
||||
*/
|
||||
public function logDebug ($params)
|
||||
// {{{
|
||||
{
|
||||
if (! $this->debug)
|
||||
return;
|
||||
file_put_contents ("/tmp/debug", date ("H:i:s")." [".posix_getpid ().
|
||||
"] DEBUG $params\n", FILE_APPEND);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Log the errors, By defaul do nothing, but can be overrided by the user
|
||||
* @param mixed|null $params The data to store in log
|
||||
*/
|
||||
public function logError ($params)
|
||||
// {{{
|
||||
{
|
||||
if (! $this->debug)
|
||||
return;
|
||||
file_put_contents ("/tmp/debug", date ("H:i:s")." [".posix_getpid ().
|
||||
"] ERROR $params\n", FILE_APPEND);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Error catcher.
|
||||
* By default, do nothing, but can be overrided by the user
|
||||
@@ -653,38 +605,32 @@ class Tcpserver
|
||||
* @param integer $line The line where the error raised
|
||||
*/
|
||||
public function errorHandler ($errNo, $errMsg, $file, $line)
|
||||
// {{{
|
||||
{
|
||||
// @-operator : error suppressed
|
||||
if (0 === error_reporting())
|
||||
return false;
|
||||
$this->logError ("line $line : $errMsg");
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Exception catcher
|
||||
* By default do nothing, but can be overrided by the user
|
||||
* @param object $exception The exception to catch
|
||||
*/
|
||||
public function exceptionHandler ($exception)
|
||||
// {{{
|
||||
{
|
||||
$this->logError ("Exception ".$exception->getMessage () . " (".
|
||||
$exception->getFile ().":".$exception->getLine().")");
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Manage the timeout handler
|
||||
* By default, disconnect and generate an exception
|
||||
*/
|
||||
public function timeoutHandler ()
|
||||
// {{{
|
||||
{
|
||||
$this->disconnect ();
|
||||
throw new \Exception (dgettext ("domframework",
|
||||
"Disconnected for inactivity"), 500);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/////////////////////////
|
||||
// PRIVATE METHODS //
|
||||
@@ -692,7 +638,6 @@ class Tcpserver
|
||||
/** Manage the child stop signal
|
||||
*/
|
||||
private function sigCHLD ()
|
||||
// {{{
|
||||
{
|
||||
$this->nbChild --;
|
||||
$this->logDebug ("One child finished : $this->nbChild childs remain ".
|
||||
@@ -701,17 +646,14 @@ class Tcpserver
|
||||
$rc = pcntl_wait ($status);
|
||||
$this->logDebug ( "One child finished : $rc");
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Manage the term / int signals
|
||||
* Will catch the stop signal, but the real end will be done when the last
|
||||
* child will be closed
|
||||
*/
|
||||
private function sigTERMINT ()
|
||||
// {{{
|
||||
{
|
||||
$this->logDebug ("Request TERM/INT : Wait for last childs");
|
||||
$this->loopStop ();
|
||||
}
|
||||
// }}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user