8a38610dba6870d5fa961f8c9acb6073b5df6da0
[PHP-P10.git] / Uplink / Client.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Uplink/Client.class.php
24  *
25  * This file contains the basic Client Socket.
26  *
27  ************************************************************************
28  * accessable methods:
29  *
30  * bool connect(String $host, int $port, String $bind = null, bool $ssl = false, int $blocking = 0)
31  *     connects the socket to $host:$port with the provided options
32  *
33  * void disconnect()
34  *     disconnects the socket
35  *
36  * bool connected()
37  *     returns the state of the socket (true if the socket is connected with the server)
38  *
39  * String[] recv()
40  *     tries to receive datas from the socket. Returns null or a array containing the lines received.
41  *     this method BLOCKS THE SCRIPT EXECUTION for the defined time if $blocking is set on connect().
42  *
43  * void send(String $line, String $newline = "\r\n")
44  *     tries to send datas through the socket.
45  *
46  * Array[] getTraffic()
47  *     returns an array containing the traffic the socket has received and sent.
48  *     index in = incoming (received) traffic count
49        index out = outgoing (sent) traffic count
50  */
51
52 class Client {
53         const CLIENT_RECV_MAX_LINES = 20; //maximum Lines to receive within one recv() call
54         
55         private $socket;
56         private $traffic = array("in" => 0, "out" => 0);
57         
58         public function connect($host, $port, $bind = null, $ssl = false, $blocking = 0) {
59                 if($bind)
60                         $options = array('socket' => array('bindto' => $bind.":0"));
61                 else
62                         $options = array();
63                 $context = stream_context_create($options);
64                 $sock = stream_socket_client(($ssl ? 'ssl://' : '').$host.':'.$port, $errno, $errstr, 3, STREAM_CLIENT_CONNECT, $context);
65                 if($sock) {
66                         if($blocking) {
67                                 stream_set_blocking($sock, true);
68                                 stream_set_timeout($sock, 0, ($blocking / 1000));
69                         } else {
70                                 stream_set_blocking($sock, false);
71                         }
72                         $this->socket = $sock;
73                         return true;
74                 } else
75                         return false;
76         }
77         
78         public function disconnect() {
79                 if($this->socket == null) return;
80                 fclose($this->socket);
81                 $this->socket = null;
82         }
83         
84         public function connected() {
85                 if($this->socket == null) return false;
86                 $read = array($this->socket);
87                 $write= null;
88                 $except=null;
89                 $n=@stream_select($read, $write, $except, 0);
90                 if($n === FALSE || feof($this->socket)) {
91                         $this->socket = false;
92                         return false;
93                 }
94                 return true;
95         }
96         
97         public function recv() {
98                 $read = array($this->socket);
99                 $write= null;
100                 $except=null;
101                 $n=@stream_select($read, $write, $except, 0);
102                 if($n === FALSE || feof($this->socket)) {
103                         $this->socket = false;
104                         return null;
105                 } elseif($n > 0) {
106                         $lines = array();
107                         while(($line = @fgets($this->socket)) != null) {
108                                 $line=trim($line);
109                                 if(!empty($line)) {
110                                         echo"[recv] ".$line."\n";
111                                         $this->traffic['in'] += strlen($line);
112                                         $lines[] = $line;
113                                         if(count($lines) >= self::CLIENT_RECV_MAX_LINES) break;
114                                 }
115                         }
116                         return $lines;
117                 }
118                 return null;
119         }
120         
121         public function send($line, $newline = "\r\n") {
122                 if($this->socket == null) return;
123                 echo"[send] ".$line."\n";
124                 $this->traffic['out'] += strlen($line);
125                 fwrite($this->socket,$line.$newline);
126         }
127         
128         public function getTraffic() {
129                 return $this->traffic;
130         }
131 }
132
133 ?>