TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
TCP.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: TCP.php 8/31/2012 11:06:09 scp@orilla $
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @package TeamSpeak3
23  * @version 1.1.16
24  * @author Sven 'ScP' Paulsen
25  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
26  */
27 
28 /**
29  * @class TeamSpeak3_Transport_TCP
30  * @brief Class for connecting to a remote server through TCP.
31  */
33 {
34  /**
35  * Connects to a remote server.
36  *
37  * @throws TeamSpeak3_Transport_Exception
38  * @return void
39  */
40  public function connect()
41  {
42  if($this->stream !== null) return;
43 
44  $host = strval($this->config["host"]);
45  $port = strval($this->config["port"]);
46 
47  $address = "tcp://" . $host . ":" . $port;
48  $timeout = intval($this->config["timeout"]);
49 
50  $this->stream = @stream_socket_client($address, $errno, $errstr, $timeout);
51 
52  if($this->stream === FALSE)
53  {
54  throw new TeamSpeak3_Transport_Exception(utf8_encode($errstr), $errno);
55  }
56 
57  @stream_set_timeout($this->stream, $timeout);
58  @stream_set_blocking($this->stream, $this->config["blocking"] ? 1 : 0);
59  }
60 
61  /**
62  * Disconnects from a remote server.
63  *
64  * @return void
65  */
66  public function disconnect()
67  {
68  if($this->stream === null) return;
69 
70  $this->stream = null;
71 
72  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "Disconnected");
73  }
74 
75  /**
76  * Reads data from the stream.
77  *
78  * @param integer $length
79  * @throws TeamSpeak3_Transport_Exception
80  * @return TeamSpeak3_Helper_String
81  */
82  public function read($length = 4096)
83  {
84  $this->connect();
85  $this->waitForReadyRead();
86 
87  $data = @stream_get_contents($this->stream, $length);
88 
89  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);
90 
91  if($data === FALSE)
92  {
93  throw new TeamSpeak3_Transport_Exception("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
94  }
95 
96  return new TeamSpeak3_Helper_String($data);
97  }
98 
99  /**
100  * Reads a single line of data from the stream.
101  *
102  * @param string $token
103  * @throws TeamSpeak3_Transport_Exception
104  * @return TeamSpeak3_Helper_String
105  */
106  public function readLine($token = "\n")
107  {
108  $this->connect();
109 
111 
112  while(!$line->endsWith($token))
113  {
114  $this->waitForReadyRead();
115 
116  $data = @fgets($this->stream, 4096);
117 
118  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);
119 
120  if($data === FALSE)
121  {
122  if($line->count())
123  {
124  $line->append($token);
125  }
126  else
127  {
128  throw new TeamSpeak3_Transport_Exception("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
129  }
130  }
131  else
132  {
133  $line->append($data);
134  }
135  }
136 
137  return $line->trim();
138  }
139 
140  /**
141  * Writes data to the stream.
142  *
143  * @param string $data
144  * @return void
145  */
146  public function send($data)
147  {
148  $this->connect();
149 
150  @stream_socket_sendto($this->stream, $data);
151 
152  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataSend", $data);
153  }
154 
155  /**
156  * Writes a line of data to the stream.
157  *
158  * @param string $data
159  * @param string $separator
160  * @return void
161  */
162  public function sendLine($data, $separator = "\n")
163  {
164  $size = strlen($data);
165  $pack = 4096;
166 
167  for($seek = 0 ;$seek < $size;)
168  {
169  $rest = $size-$seek;
170  $pack = $rest < $pack ? $rest : $pack;
171  $buff = substr($data, $seek, $pack);
172  $seek = $seek+$pack;
173 
174  if($seek >= $size) $buff .= $separator;
175 
176  $this->send($buff);
177  }
178  }
179 }