PageRenderTime 72ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/application/third_party/steam-condenser-php-1.3.5/lib/Socket.php

https://github.com/Ygnas/store-webpanel
PHP | 183 lines | 72 code | 21 blank | 90 comment | 13 complexity | 8769e2ec135042e27a6994604cd8a551 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This code is free software; you can redistribute it and/or modify it under
  4. * the terms of the new BSD License.
  5. *
  6. * Copyright (c) 2008-2013, Sebastian Staudt
  7. *
  8. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  9. */
  10. require_once STEAM_CONDENSER_PATH . 'exceptions/SocketException.php';
  11. /**
  12. * This class represents an IP socket
  13. *
  14. * It can connect to a remote host, send and receive packets
  15. *
  16. * @author Sebastian Staudt
  17. * @package steam-condenser
  18. */
  19. abstract class Socket {
  20. /**
  21. * The IP address the socket is connected to
  22. *
  23. * @var string
  24. */
  25. protected $ipAddress;
  26. /**
  27. * The port number the socket is connected to
  28. *
  29. * @var int
  30. */
  31. protected $portNumber;
  32. /**
  33. * @var string
  34. */
  35. protected $readBuffer = '';
  36. /**
  37. * The socket itself
  38. * @var resource
  39. */
  40. protected $socket;
  41. /**
  42. * Stores if the sockets extension is loaded
  43. * @var bool
  44. */
  45. protected $socketsEnabled;
  46. /**
  47. * Constructs the Socket object
  48. *
  49. * This will check if PHP's sockets extension is loaded which might be used
  50. * for socket communication.
  51. */
  52. public function __construct() {
  53. $this->socketsEnabled = extension_loaded('sockets');
  54. }
  55. /**
  56. * Destructor of this socket
  57. *
  58. * Automatically calls close()
  59. */
  60. public function __destruct() {
  61. $this->close();
  62. }
  63. /**
  64. * Connects the socket to the host with the given IP address and port
  65. * number
  66. *
  67. * @param string $ipAddress The IP address to connect to
  68. * @param int $portNumber The TCP port to connect to
  69. * @param int $timeout The timeout in milliseconds
  70. */
  71. abstract public function connect($ipAddress, $portNumber, $timeout);
  72. /**
  73. * Closes the socket
  74. */
  75. public function close() {
  76. if(!empty($this->socket)) {
  77. if($this->socketsEnabled) {
  78. @socket_close($this->socket);
  79. } else {
  80. @fclose($this->socket);
  81. }
  82. $this->socket = null;
  83. }
  84. }
  85. /**
  86. * Returns whether this socket has an open connection
  87. *
  88. * @return bool <var>true</var> if this socket is open
  89. */
  90. public function isOpen() {
  91. return !empty($this->socket);
  92. }
  93. /**
  94. * Receives the specified amount of data from the socket
  95. *
  96. * @param int $length The number of bytes to read from the socket
  97. * @return string The data read from the socket
  98. * @throws SocketException if reading from the socket fails
  99. */
  100. public function recv($length = 128) {
  101. if($this->socketsEnabled) {
  102. $data = @socket_read($this->socket, $length, PHP_BINARY_READ);
  103. if ($data === false) {
  104. throw new SocketException(socket_last_error($this->socket));
  105. }
  106. } else {
  107. $data = fread($this->socket, $length);
  108. if ($data === false) {
  109. throw new SocketException('Could not read from socket.');
  110. }
  111. }
  112. return $data;
  113. }
  114. /**
  115. * Waits for data to be read from this socket before the specified timeout
  116. * occurs
  117. *
  118. * @param int $timeout The number of milliseconds to wait for data arriving
  119. * on this socket before timing out
  120. * @return bool whether data arrived on this socket before the timeout
  121. */
  122. public function select($timeout = 0) {
  123. $read = array($this->socket);
  124. $write = null;
  125. $except = null;
  126. $sec = floor($timeout / 1000);
  127. $usec = $timeout % 1000;
  128. if($this->socketsEnabled) {
  129. $select = socket_select($read, $write, $except, $sec, $usec);
  130. } else {
  131. $select = stream_select($read, $write, $except, $sec, $usec);
  132. }
  133. return $select > 0;
  134. }
  135. /**
  136. * Sends the specified data to the peer this socket is connected to
  137. *
  138. * @param string $data The data to send to the connected peer
  139. * @throws SocketException if sending fails
  140. */
  141. public function send($data) {
  142. if($this->socketsEnabled) {
  143. $sendResult = socket_send($this->socket, $data, strlen($data), 0);
  144. if ($sendResult === false) {
  145. throw new SocketException(socket_last_error($this->socket));
  146. }
  147. } else {
  148. $sendResult = fwrite($this->socket, $data, strlen($data));
  149. if ($sendResult === false) {
  150. throw new SocketException('Could not send data.');
  151. }
  152. }
  153. }
  154. /**
  155. * Returns the file descriptor of the underlying socket
  156. *
  157. * @return resource The underlying socket descriptor
  158. */
  159. public function resource() {
  160. return $this->socket;
  161. }
  162. }