TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
UDP.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: UDP.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_UDP
30  * @brief Class for connecting to a remote server through UDP.
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 = "udp://" . $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 = @fread($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  * Writes data to the stream.
101  *
102  * @param string $data
103  * @return void
104  */
105  public function send($data)
106  {
107  $this->connect();
108 
109  @stream_socket_sendto($this->stream, $data);
110 
111  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataSend", $data);
112  }
113 }