PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Stream.php

https://github.com/donglinkang/Xmpp
PHP | 239 lines | 97 code | 24 blank | 118 comment | 16 complexity | 2fc2dd16c107ed67e5148bd3a02969bd MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Stream
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to the new BSD license that is bundled
  10. * with this package in the file LICENSE.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://m.me.uk/xmpp/license/
  13. *
  14. * @category Xmpp
  15. * @package Xmpp
  16. * @author Alex Mace <a@m.me.uk>
  17. * @copyright 2010-2011 Alex Mace (http://m.me.uk)
  18. * @license http://m.me.uk/xmpp/license/ New BSD License
  19. * @link http://pear.m.me.uk/package/Xmpp
  20. */
  21. /**
  22. * Stream
  23. *
  24. * Stream is a wrapper for the PHP steam functions
  25. *
  26. * PHP Version 5
  27. *
  28. *
  29. * @package Stream
  30. * @author Alex Mace <alex@hollytree.co.uk>
  31. * @copyright 2010 Alex Mace
  32. * @license The PHP License http://www.php.net/license/
  33. */
  34. /**
  35. * The Stream class wraps up the stream functions, so you can pass around the
  36. * stream as an object and perform operations on it.
  37. *
  38. * @category Xmpp
  39. * @package Xmpp
  40. * @author Alex Mace <a@m.me.uk>
  41. * @license http://m.me.uk/xmpp/license/ New BSD License
  42. * @link http://pear.m.me.uk/package/Xmpp
  43. */
  44. class Stream
  45. {
  46. private $_conn = null;
  47. private $_connected = false;
  48. private $_errorNumber = 0;
  49. private $_errorString = '';
  50. private $_logger = null;
  51. /**
  52. * Creates an instance of the Stream class
  53. *
  54. * @param string $remoteSocket The remote socket to connect to
  55. * @param int $timeOut Give up trying to connect after these
  56. * number of seconds
  57. * @param int $flags Connection flags
  58. * @param resource $context Context of the stream
  59. */
  60. public function __construct(
  61. $remoteSocket, $timeOut = null, $flags = null, $context = null
  62. ) {
  63. // Attempt to set up logging
  64. $this->_logger = $this->_getLogger(Zend_Log::EMERG);
  65. // Attempt to make the connection. stream_socket_client needs to be
  66. // called in the correct way based on what we have been passed.
  67. if (is_null($timeOut) && is_null($flags) && is_null($context)) {
  68. $this->_conn = stream_socket_client(
  69. $remoteSocket, $this->_errorNumber, $this->_errorString
  70. );
  71. } else if (is_null($flags) && is_null($context)) {
  72. $this->_conn = stream_socket_client(
  73. $remoteSocket, $this->_errorNumber, $this->_errorString, $timeOut
  74. );
  75. } else if (is_null($context)) {
  76. $this->_conn = stream_socket_client(
  77. $remoteSocket, $this->_errorNumber, $this->_errorString, $timeOut,
  78. $flags
  79. );
  80. } else {
  81. $this->_conn = stream_socket_client(
  82. $remoteSocket, $this->_errorNumber, $this->_errorString, $timeOut,
  83. $flags, $context
  84. );
  85. }
  86. // If the connection comes back as false, it could not be established.
  87. // Note that a connection may appear to be successful at this stage and
  88. // yet be invalid. e.g. UDP connections are "connectionless" and not
  89. // actually made until they are required.
  90. if ($this->_conn === false) {
  91. throw new Stream_Exception($this->_errorString, $this->_errorNumber);
  92. }
  93. // Set the time out of the stream.
  94. stream_set_timeout($this->_conn, 1);
  95. $this->_connected = true;
  96. }
  97. /**
  98. * Class destructor
  99. *
  100. * @return void
  101. */
  102. public function __destruct()
  103. {
  104. $this->disconnect();
  105. }
  106. /**
  107. * Closes the connection to whatever this class is connected to.
  108. *
  109. * @return void
  110. */
  111. public function disconnect()
  112. {
  113. // If there is a valid connection it will attempt to close it.
  114. if (!is_null($this->_conn) && $this->_conn !== false) {
  115. fclose($this->_conn);
  116. $this->_conn = null;
  117. $this->_connected = false;
  118. }
  119. }
  120. /**
  121. * Set up the instance of Zend_Log for doing logging.
  122. *
  123. * @param int $logLevel The level of logging to be done
  124. *
  125. * @return Zend_Log
  126. */
  127. private function _getLogger($logLevel)
  128. {
  129. $writer = new Zend_Log_Writer_Stream('php://output');
  130. return new Zend_Log($writer);
  131. }
  132. /**
  133. * Returns whether or not this object is currently connected to anything.
  134. *
  135. * @return booleans
  136. */
  137. public function isConnected()
  138. {
  139. return $this->_connected;
  140. }
  141. /**
  142. * Attempts to read some data from the stream
  143. *
  144. * @param int $length The amount of data to be returned
  145. *
  146. * @return string
  147. */
  148. public function read($length)
  149. {
  150. return fread($this->_conn, $length);
  151. }
  152. /**
  153. * Waits for some data to be sent or received on the stream
  154. *
  155. * @return int|boolean
  156. */
  157. public function select()
  158. {
  159. $read = array($this->_conn);
  160. $write = array();
  161. $except = array();
  162. return stream_select($read, $write, $except, 0, 200000);
  163. }
  164. /**
  165. * Will sent the message passed in down the stream
  166. *
  167. * @param string $message Content to be sent down the stream
  168. *
  169. * @return void
  170. */
  171. public function send($message)
  172. {
  173. // Perhaps need to check the stream is still open here?
  174. $this->_logger->debug('Sent: ' . $message);
  175. // Write out the message to the stream
  176. return fwrite($this->_conn, $message);
  177. }
  178. /**
  179. * Turns blocking on or off on the stream
  180. *
  181. * @param boolean $enable Set what to do with blocking, turn it on or off.
  182. *
  183. * @return boolean
  184. */
  185. public function setBlocking($enable)
  186. {
  187. if ($enable) {
  188. $res = stream_set_blocking($this->_conn, 1);
  189. } else {
  190. $res = stream_set_blocking($this->_conn, 0);
  191. }
  192. return $res;
  193. }
  194. /**
  195. * Toggle whether or not TLS is use on the connection.
  196. *
  197. * @param boolean $enable Whether or not to turn on TLS.
  198. *
  199. * @return mixed
  200. */
  201. public function setTLS($enable)
  202. {
  203. if ($enable) {
  204. $res = stream_socket_enable_crypto(
  205. $this->_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT
  206. );
  207. } else {
  208. $res = stream_socket_enable_crypto(
  209. $this->_conn, false, STREAM_CRYPTO_METHOD_TLS_CLIENT
  210. );
  211. }
  212. return $res;
  213. }
  214. }