From 3500e719c6f6b2e80c77b6301e51d6e6d8e94d05 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 17 Dec 2018 19:32:33 +0000 Subject: [PATCH] tcpclient : Add timeout support git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4771 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- tcpclient.php | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tcpclient.php b/tcpclient.php index 7d0bee5..ea95d68 100644 --- a/tcpclient.php +++ b/tcpclient.php @@ -1,4 +1,5 @@ ipv6); shuffle ($this->ipv4); } + // }}} /** Set/get the preferIPv4 property * @param boolean|null $preferIPv4 The preferIPv4 property to set (or to get * if null) */ public function preferIPv4 ($preferIPv4 = null) + // {{{ { if ($preferIPv4 === null) return $this->preferIPv4; @@ -108,11 +120,13 @@ class tcpclient $this->preferIPv4 = !!$preferIPv4; return $this; } + // }}} /** Set/get the read mode : text or binary * @param string|null $readMode The mode to set (or get if null) */ public function readMode ($readMode = null) + // {{{ { if ($readMode === null) return $this->readMode; @@ -122,11 +136,26 @@ class tcpclient $this->readMode = $readMode; return $this; } + // }}} + + /** Set/get the timeout + * @param integer|null $timeout The timeout in seconds + */ + public function timeout ($timeout = null) + // {{{ + { + if ($timeout === null) + return $this->timeout; + $this->timeout = intval ($timeout); + return $this; + } + // }}} /** Initialize the connection to the server * Return the socket */ public function connect () + // {{{ { if ($this->preferIPv4) $ips = array_merge ($this->ipv4, $this->ipv6); @@ -136,7 +165,8 @@ class tcpclient { if (filter_var ($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) $ip = "[$ip]"; - $socket = stream_socket_client ("tcp://$ip:$this->port", $errno, $errstr); + $socket = @stream_socket_client ("tcp://$ip:$this->port", $errno, $errstr, + $this->timeout); if ($socket === false) continue; $this->socket = $socket; @@ -145,6 +175,7 @@ class tcpclient throw new \Exception ("Can not connect to server $this->ipOrName : ". $errstr, 500); } + // }}} /** Activate the SSL connection. * Put the socket in blocking mode, as it is mandatory to have SSL connection @@ -155,6 +186,7 @@ class tcpclient * server */ public function cryptoEnable ($val, $cryptoMethod = null, $options = array ()) + // {{{ { if ($this->socket === null) throw new \Exception ("Can not send to server $this->ipOrName : ". @@ -182,22 +214,26 @@ class tcpclient substr (strrchr ($php_errormsg, ":"), 1), 500); return $rc; } + // }}} /** Send a data to the server. * The connection must be established * @param mixed $data The data to send */ public function send ($data) + // {{{ { if ($this->socket === null) throw new \Exception ("Can not send to server $this->ipOrName : ". "The server is not connected", 500); + stream_set_timeout ($this->socket, $this->timeout); $length = strlen ($data); $sentLen = @fwrite ($this->socket, $data); if ($sentLen < $length) throw new \Exception ("Can not send to server $this->ipOrName", 500); return $sentLen; } + // }}} /** Read the data from the server. * The connection must be established @@ -208,10 +244,12 @@ class tcpclient * @return The content */ public function read ($maxLength = 1024) + // {{{ { if ($this->socket === null) throw new \Exception ("Can not read from server $this->ipOrName : ". "The server is not connected", 500); + stream_set_timeout ($this->socket, $this->timeout); if ($this->readMode === "text") { $read = stream_get_line ($this->socket, $maxLength, "\r\n"); @@ -226,10 +264,12 @@ class tcpclient } return $read; } + // }}} /** Disconnect the socket */ public function disconnect () + // {{{ { if ($this->socket === null) throw new \Exception ("Can not disconnect server $this->ipOrName : ". @@ -237,11 +277,13 @@ class tcpclient @stream_socket_shutdown ($this->socket, STREAM_SHUT_RDWR); $this->socket = null; } + // }}} /** Get the connection peer address, peer port and localaddress and localport * @return array */ public function getInfo () + // {{{ { if ($this->socket === null) throw new \Exception ("Can not getInfo for server $this->ipOrName : ". @@ -256,12 +298,15 @@ class tcpclient $address = substr ($name, 0, $pos); return array ($address, $port, $localAddress, $localPort); } + // }}} /** Get the socket to direct access * @return resource The socket with the client */ public function getSock () + // {{{ { return $this->socket; } + // }}} }