a8d946e9beaf7ca7ffd7b7143279aabadcd0d9a0
[PHP-P10.git] / Uplink / Client.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)                       *
4  *                                                                      *
5  * This program is free software: you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation, either version 3 of the License, or    *
8  * (at your option) any later version.                                  *
9  *                                                                      * 
10  * This program is distributed in the hope that it will be useful,      *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
13  * GNU General Public License for more details.                         *
14  *                                                                      *
15  * You should have received a copy of the GNU General Public License    *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  *                                                                      *
18  ************************************************************************
19  * 
20  *  Uplink/Client.class.php
21  *
22  * This file contains the basic Client Socket.
23  *
24  */
25
26 class Client {
27         const CLIENT_RECV_MAX_LINES = 20; //maximum Lines to receive within one recv() call
28         
29         private $socket;
30         private $traffic = array("in" => 0, "out" => 0);
31         private $timeout;
32         
33         public function connect($host, $port, $bind = null, $ssl = false, $blocking = 0) {
34                 if($bind)
35                         $options = array('socket' => array('bindto' => $bind.":0"));
36                 else
37                         $options = array();
38                 $context = stream_context_create($options);
39                 $sock = stream_socket_client(($ssl ? 'ssl://' : '').$host.':'.$port, $errno, $errstr, 3, STREAM_CLIENT_CONNECT, $context);
40                 if($sock) {
41                         $this->timeout = $blocking * 1000;
42                         stream_set_blocking($sock, false);
43                         $this->socket = $sock;
44                         return true;
45                 } else
46                         return false;
47         }
48         
49         public function disconnect() {
50                 if($this->socket == null) return;
51                 fclose($this->socket);
52                 $this->socket = null;
53         }
54         
55         public function connected() {
56                 if($this->socket == null) return false;
57                 $read = array($this->socket);
58                 $write= null;
59                 $except=null;
60                 $n=@stream_select($read, $write, $except, 0);
61                 if($n === FALSE || feof($this->socket)) {
62                         $this->socket = false;
63                         return false;
64                 }
65                 return true;
66         }
67         
68         public function recv() {
69                 $read = array($this->socket);
70                 $write= null;
71                 $except=null;
72                 $n=@stream_select($read, $write, $except, 0, $this->timeout);
73                 if($n === FALSE || feof($this->socket)) {
74                         $this->socket = false;
75                         return null;
76                 } elseif($n > 0) {
77                         $lines = array();
78                         while(($line = @fgets($this->socket)) != null) {
79                                 $line=trim($line);
80                                 if(!empty($line)) {
81                                         echo"[recv] ".utf8_decode($line)."\n";
82                                         $this->traffic['in'] += strlen($line);
83                                         $lines[] = $line;
84                                         if(count($lines) >= self::CLIENT_RECV_MAX_LINES) break;
85                                 }
86                         }
87                         return $lines;
88                 }
89                 return null;
90         }
91         
92         public function send($line, $newline = "\r\n") {
93                 if($this->socket == null) return;
94                 echo"[send] ".utf8_decode($line)."\n";
95                 $this->traffic['out'] += strlen($line);
96                 fwrite($this->socket,$line.$newline);
97         }
98         
99         public function getTraffic() {
100                 return $this->traffic;
101         }
102 }
103
104 ?>