start writing PHP P10 pack v2 (hopefully without bugs :D)
[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  * bool connected()
34  *     returns the state of the socket (true if the socket is connected with the server)
35  *
36  * String[] recv()
37  *     tries to receive datas from the socket. Returns null or a array containing the lines received.
38  *     this method BLOCKS THE SCRIPT EXECUTION for the defined time if $blocking is set on connect().
39  *
40  * void send(String $line, String $newline = "\r\n")
41  *     tries to send datas through the socket.
42  *
43  * Array[] getTraffic()
44  *     returns an array containing the traffic the socket has received and sent.
45  *     index in = incoming (received) traffic count
46        index out = outgoing (sent) traffic count
47  */
48
49 class Client {
50         const CLIENT_RECV_MAX_LINES = 20; //maximum Lines to receive within one recv() call
51         
52         private $socket;
53         private $traffic = array("in" => 0, "out" => 0);
54         
55         public function connect($host, $port, $bind = null, $ssl = false, $blocking = 0) {
56                 if($bind)
57                         $options = array('socket' => array('bindto' => $bind.":0"));
58                 else
59                         $options = array();
60                 $context = stream_context_create($options);
61                 $sock = stream_socket_client(($ssl ? 'ssl://' : '').$host.':'.$port, $errno, $errstr, 3, STREAM_CLIENT_CONNECT, $context);
62                 if($sock) {
63                         if($blocking) {
64                                 stream_set_blocking($sock, true);
65                                 stream_set_timeout($sock, 0, ($blocking / 1000));
66                         } else {
67                                 stream_set_blocking($sock, false);
68                         }
69                         $this->socket = $sock;
70                         return true;
71                 } else
72                         return false;
73         }
74         
75         public function connected() {
76                 if($this->socket == null) return false;
77                 $read = array($this->socket);
78                 $write= null;
79                 $except=null;
80                 $n=@stream_select($read, $write, $except, 0);
81                 if($n === FALSE || feof($this->socket)) {
82                         $this->socket = false;
83                         return false;
84                 }
85                 return true;
86         }
87         
88         public function recv() {
89                 $read = array($this->socket);
90                 $write= null;
91                 $except=null;
92                 $n=@stream_select($read, $write, $except, 0);
93                 if($n === FALSE || feof($this->socket)) {
94                         $this->socket = false;
95                         return null;
96                 } elseif($n > 0) {
97                         $lines = array();
98                         while(($line = @fgets($this->socket)) != null) {
99                                 $line=trim($line);
100                                 if(!empty($line)) {
101                                         if(DEBUG_RAW) echo"[recv] ".$line."\n";
102                                         $this->traffic['in'] += strlen($line);
103                                         $lines[] = $line;
104                                         if(count($lines) >= self::CLIENT_RECV_MAX_LINES) break;
105                                 }
106                         }
107                         return $lines;
108                 }
109                 return null;
110         }
111         
112         public function send($line, $newline = "\r\n") {
113                 if($this->socket == null) return;
114                 if(DEBUG_RAW) echo"[send] ".$line."\n";
115                 $this->traffic['out'] += strlen($line);
116                 fwrite($this->socket,$line.$newline);
117         }
118         
119         public function getTraffic() {
120                 return $this->traffic;
121         }
122 }
123
124 ?>