PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Server/Network/Socket.php

https://github.com/xxOrpheus/EtherRS
PHP | 172 lines | 95 code | 15 blank | 62 comment | 26 complexity | 45c68f963fcc54f6be4f32af7866ed04 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. namespace Server\Network;
  3. class Socket extends \Server\Server {
  4. protected $currentStream, $activeStreams = array();
  5. protected $currentSocket, $activeSockets = array();
  6. public function __construct(Stream $inStream = null, Stream $outStream = null, $conn = null, $name = null) {
  7. if($inStream != null && $outStream != null) {
  8. $this->addStream($inStream, $outStream, $name, true);
  9. }
  10. if(is_resource($conn)) {
  11. $this->addSocket($conn, $name, true);
  12. }
  13. $this->log("Socket server initialized");
  14. }
  15. /**
  16. *
  17. * Read data from a socket
  18. *
  19. * @param string $name The name of the socket to read from
  20. * @param int $bytes How many bytes to read
  21. *
  22. */
  23. public function read($bytes, $name = null) {
  24. if($name === null) {
  25. $this->currentStream[0]->clear();
  26. $data = @socket_read($this->currentSocket, $bytes, PHP_BINARY_READ);
  27. } else {
  28. $this->activeStreams[$name][0]->clear();
  29. $data = @socket_read($this->activeSockets[$name], $bytes, PHP_BINARY_READ);
  30. }
  31. if(!$data) {
  32. $this->log($this->lastError($name === null ? $this->currentSocket : $name));
  33. }
  34. $data = unpack('C*', $data);
  35. return $data;
  36. }
  37. /**
  38. *
  39. * Write data to a socket
  40. *
  41. * @param string $name The name of the socket to write to
  42. * @param mixed $data The data to write
  43. *
  44. */
  45. public function write($data, $name = null) {
  46. if($name === null) {
  47. $this->currentStream[1]->clear();
  48. socket_write($this->currentSocket, $data);
  49. } else {
  50. if(!is_resource($this->activeSockets[$name])) {
  51. throw new Exception(__METHOD__ . ': Not a valid resource');
  52. }
  53. socket_write($this->activeSockets[$name], $data);
  54. }
  55. }
  56. /**
  57. *
  58. * Write data from a stream to a socket
  59. *
  60. * @param string $name The name of the stream to use
  61. *
  62. */
  63. public function writeStream($name = null) {
  64. if($name === null) {
  65. $stream = $this->currentStream[1]->getStream();
  66. $this->write($stream);
  67. } else {
  68. if(!isset($this->activeStreams[$name])) {
  69. throw new Exception(__METHOD__ . ': Not a valid stream');
  70. }
  71. $stream = $this->activeStreams[$name][1]->getStream();
  72. $this->write($stream);
  73. }
  74. }
  75. /**
  76. *
  77. * Add a socket to the current object
  78. *
  79. * @param Socket $socket The socket to add
  80. * @param string $name The name to use
  81. * @param bool $setActive Should we set this socket to the current socket?
  82. *
  83. */
  84. public function addSocket($socket, $name = null, $setActive = false) {
  85. if(!is_resource($socket)) {
  86. throw new Exception(__METHOD__ . ': $socket is not a valid resource');
  87. }
  88. if($name !== null) {
  89. $this->activeSockets[$name] = $socket;
  90. } else {
  91. $this->activeSockets[] = $socket;
  92. }
  93. if($setActive === true) {
  94. $this->currentSocket = $socket;
  95. }
  96. }
  97. /**
  98. *
  99. * Add a stream to the current object
  100. *
  101. * @param Stream $stream The stream to add
  102. * @param string $name The name to use
  103. * @param bool $setActive Should we use this stream as the current one?
  104. *
  105. */
  106. public function addStream(Stream $inStream, Stream $outStream, $name = null, $setActive = false) {
  107. if($name !== null) {
  108. $this->activeStreams[$name] = array($inStream, $outStream);
  109. } else {
  110. $this->activeStreams[] = array($inStream, $outStream);
  111. }
  112. if($setActive === true) {
  113. $this->currentStream = array($inStream, $outStream);
  114. }
  115. }
  116. /**
  117. *
  118. * Select a stream to use
  119. *
  120. * @param string $name The name of the stream
  121. *
  122. */
  123. public function selectStream($name) {
  124. if(!isset($this->activeStreams[$name]))
  125. return false;
  126. $this->currentStream = $this->activeStreams[$name];
  127. }
  128. /**
  129. *
  130. * Select a socket to use
  131. *
  132. * @param string $name The name of the socket
  133. *
  134. */
  135. public function selectSocket($name) {
  136. if(!isset($this->activeSockets[$name]))
  137. return false;
  138. $this->currentSocket = $this->activeSockets[$name];
  139. }
  140. /**
  141. *
  142. * Get the last error of the socket
  143. *
  144. * @param string $name The name of the socket
  145. *
  146. */
  147. public function lastError($name = null) {
  148. if($name === null) {
  149. return socket_last_error($this->currentSocket);
  150. } else {
  151. if(!isset($this->activeSockets[$name]))
  152. return false;
  153. return socket_last_error($this->activeSockets[$name]);
  154. }
  155. }
  156. }
  157. ?>