/libraries/TeamSpeak3/Transport/Abstract.php

https://gitlab.com/skokkk/CleverSpeak · PHP · 268 lines · 101 code · 32 blank · 135 comment · 14 complexity · 146ae35adc29cb9fbc766196dd0769df MD5 · raw file

  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * $Id: Abstract.php 10/11/2013 11:35:22 scp@orilla $
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @package TeamSpeak3
  22. * @version 1.1.23
  23. * @author Sven 'ScP' Paulsen
  24. * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
  25. */
  26. /**
  27. * @class TeamSpeak3_Transport_Abstract
  28. * @brief Abstract class for connecting to a TeamSpeak 3 Server through different ways of transport.
  29. */
  30. abstract class TeamSpeak3_Transport_Abstract
  31. {
  32. /**
  33. * Stores user-provided configuration settings.
  34. *
  35. * @var array
  36. */
  37. protected $config = null;
  38. /**
  39. * Stores the stream resource of the connection.
  40. *
  41. * @var resource
  42. */
  43. protected $stream = null;
  44. /**
  45. * Stores the TeamSpeak3_Adapter_Abstract object using this transport.
  46. *
  47. * @var TeamSpeak3_Adapter_Abstract
  48. */
  49. protected $adapter = null;
  50. /**
  51. * The TeamSpeak3_Transport_Abstract constructor.
  52. *
  53. * @param array $config
  54. * @throws TeamSpeak3_Transport_Exception
  55. * @return TeamSpeak3_Transport_Abstract
  56. */
  57. public function __construct(array $config)
  58. {
  59. if(!array_key_exists("host", $config))
  60. {
  61. throw new TeamSpeak3_Transport_Exception("config must have a key for 'host' which specifies the server host name");
  62. }
  63. if(!array_key_exists("port", $config))
  64. {
  65. throw new TeamSpeak3_Transport_Exception("config must have a key for 'port' which specifies the server port number");
  66. }
  67. if(!array_key_exists("timeout", $config))
  68. {
  69. $config["timeout"] = 10;
  70. }
  71. if(!array_key_exists("blocking", $config))
  72. {
  73. $config["blocking"] = 1;
  74. }
  75. $this->config = $config;
  76. }
  77. /**
  78. * Commit pending data.
  79. *
  80. * @return array
  81. */
  82. public function __sleep()
  83. {
  84. return array("config");
  85. }
  86. /**
  87. * Reconnects to the remote server.
  88. *
  89. * @return void
  90. */
  91. public function __wakeup()
  92. {
  93. $this->connect();
  94. }
  95. /**
  96. * The TeamSpeak3_Transport_Abstract destructor.
  97. *
  98. * @return void
  99. */
  100. public function __destruct()
  101. {
  102. if($this->adapter instanceof TeamSpeak3_Adapter_Abstract)
  103. {
  104. $this->adapter->__destruct();
  105. }
  106. $this->disconnect();
  107. }
  108. /**
  109. * Connects to a remote server.
  110. *
  111. * @throws TeamSpeak3_Transport_Exception
  112. * @return void
  113. */
  114. abstract public function connect();
  115. /**
  116. * Disconnects from a remote server.
  117. *
  118. * @return void
  119. */
  120. abstract public function disconnect();
  121. /**
  122. * Reads data from the stream.
  123. *
  124. * @param integer $length
  125. * @throws TeamSpeak3_Transport_Exception
  126. * @return TeamSpeak3_Helper_String
  127. */
  128. abstract public function read($length = 4096);
  129. /**
  130. * Writes data to the stream.
  131. *
  132. * @param string $data
  133. * @return void
  134. */
  135. abstract public function send($data);
  136. /**
  137. * Returns the underlying stream resource.
  138. *
  139. * @return resource
  140. */
  141. public function getStream()
  142. {
  143. return $this->stream;
  144. }
  145. /**
  146. * Returns the configuration variables in this adapter.
  147. *
  148. * @param string $key
  149. * @param mixed $default
  150. * @return array
  151. */
  152. public function getConfig($key = null, $default = null)
  153. {
  154. if($key !== null)
  155. {
  156. return array_key_exists($key, $this->config) ? $this->config[$key] : $default;
  157. }
  158. return $this->config;
  159. }
  160. /**
  161. * Sets the TeamSpeak3_Adapter_Abstract object using this transport.
  162. *
  163. * @param TeamSpeak3_Adapter_Abstract $adapter
  164. * @return void
  165. */
  166. public function setAdapter(TeamSpeak3_Adapter_Abstract $adapter)
  167. {
  168. $this->adapter = $adapter;
  169. }
  170. /**
  171. * Returns the TeamSpeak3_Adapter_Abstract object using this transport.
  172. *
  173. * @return TeamSpeak3_Adapter_Abstract
  174. */
  175. public function getAdapter()
  176. {
  177. return $this->adapter;
  178. }
  179. /**
  180. * Returns the adapter type.
  181. *
  182. * @return string
  183. */
  184. public function getAdapterType()
  185. {
  186. if($this->adapter instanceof TeamSpeak3_Adapter_Abstract)
  187. {
  188. $string = TeamSpeak3_Helper_String::factory(get_class($this->adapter));
  189. return $string->substr($string->findLast("_"))->replace(array("_", " "), "")->toString();
  190. }
  191. return "Unknown";
  192. }
  193. /**
  194. * Returns header/meta data from stream pointer.
  195. *
  196. * @throws TeamSpeak3_Transport_Exception
  197. * @return array
  198. */
  199. public function getMetaData()
  200. {
  201. if($this->stream === null)
  202. {
  203. throw new TeamSpeak3_Transport_Exception("unable to retrieve header/meta data from stream pointer");
  204. }
  205. return stream_get_meta_data($this->stream);
  206. }
  207. /**
  208. * Returns TRUE if the transport is connected.
  209. *
  210. * @return boolean
  211. */
  212. public function isConnected()
  213. {
  214. return (is_resource($this->stream)) ? TRUE : FALSE;
  215. }
  216. /**
  217. * Blocks a stream until data is available for reading if the stream is connected
  218. * in non-blocking mode.
  219. *
  220. * @param integer $time
  221. * @return void
  222. */
  223. protected function waitForReadyRead($time = 0)
  224. {
  225. if(!$this->isConnected() || $this->config["blocking"]) return;
  226. do {
  227. $read = array($this->stream);
  228. $null = null;
  229. if($time)
  230. {
  231. TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "WaitTimeout", $time, $this->getAdapter());
  232. }
  233. $time = $time+$this->config["timeout"];
  234. } while(@stream_select($read, $null, $null, $this->config["timeout"]) == 0);
  235. }
  236. }