Node.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Websocket;
  36. use Hoa\Socket as HoaSocket;
  37. /**
  38. * Class \Hoa\Websocket\Node.
  39. *
  40. * Describe a WebSocket node.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. class Node extends HoaSocket\Node
  46. {
  47. /**
  48. * Protocol implementation.
  49. *
  50. * @var \Hoa\Websocket\Protocol\Generic
  51. */
  52. protected $_protocol = null;
  53. /**
  54. * Whether the handshake succeed.
  55. *
  56. * @var bool
  57. */
  58. protected $_handshake = false;
  59. /**
  60. * Fragments of message.
  61. *
  62. * @var string
  63. */
  64. protected $_messageFragments = null;
  65. /**
  66. * Number of fragments.
  67. *
  68. * @var int
  69. */
  70. protected $_numberOfFragments = 0;
  71. /**
  72. * Whether the message is complete or not.
  73. *
  74. * @var bool
  75. */
  76. protected $_complete = true;
  77. /**
  78. * Whether the message is binary or not.
  79. *
  80. * @var bool
  81. */
  82. protected $_isBinary = false;
  83. /**
  84. * Set protocol implementation.
  85. *
  86. * @param \Hoa\Websocket\Protocol\Generic $protocol Protocol.
  87. * @return \Hoa\Websocket\Protocol\Generic
  88. */
  89. public function setProtocolImplementation(Protocol\Generic $protocol)
  90. {
  91. $old = $this->_protocol;
  92. $this->_protocol = $protocol;
  93. return $old;
  94. }
  95. /**
  96. * Get protocol implementation.
  97. *
  98. * @return \Hoa\Websocket\Protocol\Generic
  99. */
  100. public function getProtocolImplementation()
  101. {
  102. return $this->_protocol;
  103. }
  104. /**
  105. * Set handshake success.
  106. *
  107. * @param bool $handshake Handshake.
  108. * @return bool
  109. */
  110. public function setHandshake($handshake)
  111. {
  112. $old = $this->_handshake;
  113. $this->_handshake = $handshake;
  114. return $old;
  115. }
  116. /**
  117. * Whether the handshake succeed.
  118. *
  119. * @return bool
  120. */
  121. public function getHandshake()
  122. {
  123. return $this->_handshake;
  124. }
  125. /**
  126. * Append a fragment to a message (if we have fragmentation).
  127. *
  128. * @param string $fragment Fragment.
  129. * @return string
  130. */
  131. public function appendMessageFragment($fragment)
  132. {
  133. ++$this->_numberOfFragments;
  134. return $this->_messageFragments .= $fragment;
  135. }
  136. /**
  137. * Get the fragmented message.
  138. *
  139. * @return string
  140. */
  141. public function getFragmentedMessage()
  142. {
  143. return $this->_messageFragments;
  144. }
  145. /**
  146. * Get number of fragments.
  147. *
  148. * @return int
  149. */
  150. public function getNumberOfFragments()
  151. {
  152. return $this->_numberOfFragments;
  153. }
  154. /**
  155. * Set whether the message is complete or not.
  156. *
  157. * @param bool $complete Is it complete?
  158. * @return bool
  159. */
  160. public function setComplete($complete)
  161. {
  162. $old = $this->_complete;
  163. $this->_complete = $complete;
  164. return $old;
  165. }
  166. /**
  167. * Check if the message is complete or not.
  168. *
  169. * @return bool
  170. */
  171. public function isMessageComplete()
  172. {
  173. return $this->_complete;
  174. }
  175. /**
  176. * Whether the message is binary or not.
  177. *
  178. * @param bool $binary Binary.
  179. * @return bool
  180. */
  181. public function setBinary($binary)
  182. {
  183. $old = $this->_isBinary;
  184. $this->_isBinary = $binary;
  185. return $old;
  186. }
  187. /**
  188. * Check if the message is binary or not.
  189. *
  190. * @return bool
  191. */
  192. public function isBinary()
  193. {
  194. return $this->_isBinary;
  195. }
  196. /**
  197. * Clear the fragmentation.
  198. *
  199. * @return void
  200. */
  201. public function clearFragmentation()
  202. {
  203. $this->_messageFragments = null;
  204. $this->_numberOfFragments = 0;
  205. $this->_isBinary = false;
  206. $this->_complete = true;
  207. return;
  208. }
  209. }