Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Socket often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Socket, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Socket |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var resource Will store a reference to the php socket object. |
||
| 16 | */ |
||
| 17 | protected $resource = null; |
||
| 18 | /** |
||
| 19 | * @var int Should be set to one of the php predefined constants for Sockets - AF_UNIX, AF_INET, or AF_INET6 |
||
| 20 | */ |
||
| 21 | protected $domain = null; |
||
| 22 | /** |
||
| 23 | * @var int Should be set to one of the php predefined constants for Sockets - SOCK_STREAM, SOCK_DGRAM, |
||
| 24 | * SOCK_SEQPACKET, SOCK_RAW, SOCK_RDM |
||
| 25 | */ |
||
| 26 | protected $type = null; |
||
| 27 | /** |
||
| 28 | * @var int Should be set to the protocol number to be used. Can use getprotobyname to get the value. |
||
| 29 | * Alternatively, there are two predefined constants for Sockets that could be used - SOL_TCP, SOL_UDP |
||
| 30 | */ |
||
| 31 | protected $protocol = null; |
||
| 32 | /** |
||
| 33 | * @var array An internal storage of php socket resources and their associated Socket object. |
||
| 34 | */ |
||
| 35 | protected static $map = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Sets up the Socket Resource and stores it in the local map. |
||
| 39 | * |
||
| 40 | * <p>This class uses the <a href="https://en.wikipedia.org/wiki/Factory_(object-oriented_programming)"> |
||
| 41 | * Factory pattern</a> to create instances. Please use the <code>create</code> method to create new instances |
||
| 42 | * of this class. |
||
| 43 | * |
||
| 44 | * @see Socket::create() |
||
| 45 | * |
||
| 46 | * @param resource $resource The php socket resource. This is just a reference to the socket object created using |
||
| 47 | * the <code>socket_create</code> method. |
||
| 48 | */ |
||
| 49 | 4 | protected function __construct($resource) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Cleans up the Socket and dereferences the internal resource. |
||
| 57 | */ |
||
| 58 | public function __destruct() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Return the php socket resource name. |
||
| 66 | * |
||
| 67 | * <p>Resources are always converted to strings with the structure "Resource id#1", where 1 is the resource number |
||
| 68 | * assigned to the resource by PHP at runtime. While the exact structure of this string should not be relied on and |
||
| 69 | * is subject to change, it will always be unique for a given resource within the lifetime of the script execution |
||
| 70 | * and won't be reused.</p> |
||
| 71 | * |
||
| 72 | * <p>If the resource object has been dereferrenced (set to <code>null</code>), this will return an empty |
||
| 73 | * string.</p> |
||
| 74 | * |
||
| 75 | * @return string The string representation of the resource or an empty string if the resource was null. |
||
| 76 | */ |
||
| 77 | public function __toString() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Accept a connection. |
||
| 84 | * |
||
| 85 | * <p>After the socket socket has been created using <code>create()</code>, bound to a name with |
||
| 86 | * <code>bind()</code>, and told to listen for connections with <code>listen()</code>, this function will accept |
||
| 87 | * incoming connections on that socket. Once a successful connection is made, a new Socket resource is returned, |
||
| 88 | * which may be used for communication. If there are multiple connections queued on the socket, the first will be |
||
| 89 | * used. If there are no pending connections, this will block until a connection becomes present. If socket has |
||
| 90 | * been made non-blocking using <code>setBlocking()</code>, a <code>SocketException</code> will be thrown.</p> |
||
| 91 | * |
||
| 92 | * <p>The Socket returned by this method may not be used to accept new connections. The original listening Socket, |
||
| 93 | * however, remains open and may be reused.</p> |
||
| 94 | * |
||
| 95 | * @throws Exception\SocketException If the Socket is set as non-blocking and there are no pending connections. |
||
| 96 | * |
||
| 97 | * @see Socket::create() |
||
| 98 | * @see Socket::bind() |
||
| 99 | * @see Socket::listen() |
||
| 100 | * @see Socket::setBlocking() |
||
| 101 | * |
||
| 102 | * @return Socket A new Socket representation of the accepted socket. |
||
| 103 | */ |
||
| 104 | public function accept() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Binds a name to a socket. |
||
| 117 | * |
||
| 118 | * <p>Binds the name given in address to the php socket resource currently in use. This has to be done before a |
||
| 119 | * connection is established using <code>connect()</code> or <code>listen()</code>.</p> |
||
| 120 | * |
||
| 121 | * @param string $address <p>If the socket is of the AF_INET family, the address is an IP in dotted-quad |
||
| 122 | * notation (e.g. <code>127.0.0.1</code>).</p> <p>If the socket is of the AF_UNIX family, the address is the path |
||
| 123 | * of the Unix-domain socket (e.g. <code>/tmp/my.sock</code>).</p> |
||
| 124 | * @param int $port <p>(Optional) The port parameter is only used when binding an AF_INET socket, and designates the port |
||
| 125 | * on which to listen for connections.</p> |
||
| 126 | * |
||
| 127 | * @throws Exception\SocketException If the bind was unsuccessful. |
||
| 128 | * |
||
| 129 | * @return bool <p>Returns <code>true</code> if the bind was successful.</p> |
||
| 130 | */ |
||
| 131 | 1 | public function bind($address, $port = 0) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Close the socket. |
||
| 144 | * |
||
| 145 | * <p>Closes the php socket resource currently in use and removes the reference to it in the internal map.</p> |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | public function close() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Connect to a socket. |
||
| 157 | * |
||
| 158 | * <p>Initiate a connection to the address given using the current php socket resource, which must be a valid |
||
| 159 | * socket resource created with <code>create()</code>. |
||
| 160 | * |
||
| 161 | * @param string $address <p>The address parameter is either an IPv4 address in dotted-quad notation (e.g. |
||
| 162 | * <code>127.0.0.1</code>) if the socket is AF_INET, a valid IPv6 address (e.g. <code>::1</code>) if IPv6 support |
||
| 163 | * is enabled and the socket is AF_INET6, or the pathname of a Unix domain socket, if the socket family is AF_UNIX. |
||
| 164 | * </p> |
||
| 165 | * @param int $port <p>(Optional) The port parameter is only used and is mandatory when connecting to an AF_INET or |
||
| 166 | * an AF_INET6 socket, and designates the port on the remote host to which a connection should be made.</p> |
||
| 167 | * |
||
| 168 | * @throws Exception\SocketException If the connect was unsuccessful or if the socket is non-blocking. |
||
| 169 | * |
||
| 170 | * @see Socket::bind() |
||
| 171 | * @see Socket::listen() |
||
| 172 | * @see Socket::create() |
||
| 173 | * |
||
| 174 | * @return bool <p>Returns <code>true</code> if the connect was successful. |
||
| 175 | */ |
||
| 176 | public function connect($address, $port = 0) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Build Socket objects based on an array of php socket resources. |
||
| 189 | * |
||
| 190 | * @param array $resources The resources parameter is a list of php socket resource objects. |
||
| 191 | * |
||
| 192 | * @return Socket[] <p>Returns an array of Socket objects built from the given php socket resources.</p> |
||
| 193 | */ |
||
| 194 | 1 | protected static function constructFromResources(array $resources) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Create a socket. |
||
| 203 | * |
||
| 204 | * <p>Creates and returns a Socket. A typical network connection is made up of two sockets, one performing the role |
||
| 205 | * of the client, and another performing the role of the server.</p> |
||
| 206 | * |
||
| 207 | * @param int $domain <p>The domain parameter specifies the protocol family to be used by the socket.</p><p> |
||
| 208 | * <code>AF_INET</code> - IPv4 Internet based protocols. TCP and UDP are common protocols of this protocol family. |
||
| 209 | * </p><p><code>AF_INET6</code> - IPv6 Internet based protocols. TCP and UDP are common protocols of this protocol |
||
| 210 | * family.</p><p><code>AF_UNIX</code> - Local communication protocol family. High efficiency and low overhead make |
||
| 211 | * it a great form of IPC (Interprocess Communication).</p> |
||
| 212 | * @param int $type <p>The type parameter selects the type of communication to be used by the socket.</p><p> |
||
| 213 | * <code>SOCK_STREAM</code> - Provides sequenced, reliable, full-duplex, connection-based byte streams. An |
||
| 214 | * out-of-band data transmission mechanism may be supported. The TCP protocol is based on this socket type.</p><p> |
||
| 215 | * <code>SOCK_DGRAM</code> - Supports datagrams (connectionless, unreliable messages of a fixed maximum length). |
||
| 216 | * The UDP protocol is based on this socket type.</p><p><code>SOCK_SEQPACKET</code> - Provides a sequenced, |
||
| 217 | * reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is |
||
| 218 | * required to read an entire packet with each read call.</p><p><code>SOCK_RAW</code> - Provides raw network |
||
| 219 | * protocol access. This special type of socket can be used to manually construct any type of protocol. A common |
||
| 220 | * use for this socket type is to perform ICMP requests (like ping).</p><p><code>SOCK_RDM</code> - Provides a |
||
| 221 | * reliable datagram layer that does not guarantee ordering. This is most likely not implemented on your operating |
||
| 222 | * system.</p> |
||
| 223 | * @param int $protocol <p>The protocol parameter sets the specific protocol within the specified domain to be used |
||
| 224 | * when communicating on the returned socket. The proper value can be retrieved by name by using |
||
| 225 | * <code>getprotobyname()</code>. If the desired protocol is TCP, or UDP the corresponding constants |
||
| 226 | * <code>SOL_TCP</code>, and <code>SOL_UDP</code> can also be used.<p><p>Some of the common protocol types</p><p> |
||
| 227 | * icmp - The Internet Control Message Protocol is used primarily by gateways and hosts to report errors in |
||
| 228 | * datagram communication. The "ping" command (present in most modern operating systems) is an example application |
||
| 229 | * of ICMP.</p><p>udp - The User Datagram Protocol is a connectionless, unreliable, protocol with fixed record |
||
| 230 | * lengths. Due to these aspects, UDP requires a minimum amount of protocol overhead.</p><p>tcp - The Transmission |
||
| 231 | * Control Protocol is a reliable, connection based, stream oriented, full duplex protocol. TCP guarantees that all |
||
| 232 | * data packets will be received in the order in which they were sent. If any packet is somehow lost during |
||
| 233 | * communication, TCP will automatically retransmit the packet until the destination host acknowledges that packet. |
||
| 234 | * For reliability and performance reasons, the TCP implementation itself decides the appropriate octet boundaries |
||
| 235 | * of the underlying datagram communication layer. Therefore, TCP applications must allow for the possibility of |
||
| 236 | * partial record transmission.</p> |
||
| 237 | * |
||
| 238 | * @throws Exception\SocketException If there is an error creating the php socket. |
||
| 239 | * |
||
| 240 | * @return Socket Returns a Socket object based on the successful creation of the php socket. |
||
| 241 | */ |
||
| 242 | 4 | public static function create($domain, $type, $protocol) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Opens a socket on port to accept connections. |
||
| 260 | * |
||
| 261 | * <p>Creates a new socket resource of type <code>AF_INET</code> listening on all local interfaces on the given |
||
| 262 | * port waiting for new connections.</p> |
||
| 263 | * |
||
| 264 | * @param int $port The port on which to listen on all interfaces. |
||
| 265 | * @param int $backlog <p>The backlog parameter defines the maximum length the queue of pending connections may |
||
| 266 | * grow to. <code>SOMAXCONN</code> may be passed as the backlog parameter.</p> |
||
| 267 | * |
||
| 268 | * @throws Exception\SocketException If the socket is not successfully created. |
||
| 269 | * |
||
| 270 | * @see Socket::create() |
||
| 271 | * @see Socket::bind() |
||
| 272 | * @see Socket::listen() |
||
| 273 | * |
||
| 274 | * @return Socket Returns a Socket object based on the successful creation of the php socket. |
||
| 275 | */ |
||
| 276 | public static function createListen($port, $backlog = 128) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Creates a pair of indistinguishable sockets and stores them in an array. |
||
| 292 | * |
||
| 293 | * <p>Creates two connected and indistinguishable sockets. This function is commonly used in IPC (InterProcess |
||
| 294 | * Communication).</p> |
||
| 295 | * |
||
| 296 | * @param int $domain <p>The domain parameter specifies the protocol family to be used by the socket. See |
||
| 297 | * <code>create()</code> for the full list.</p> |
||
| 298 | * @param int $type <p>The type parameter selects the type of communication to be used by the socket. See |
||
| 299 | * <code>create()</code> for the full list.</p> |
||
| 300 | * @param int $protocol <p>The protocol parameter sets the specific protocol within the specified domain to be used |
||
| 301 | * when communicating on the returned socket. The proper value can be retrieved by name by using |
||
| 302 | * <code>getprotobyname()</code>. If the desired protocol is TCP, or UDP the corresponding constants |
||
| 303 | * <code>SOL_TCP</code>, and <code>SOL_UDP</code> can also be used. See <code>create()</code> for the full list of |
||
| 304 | * supported protocols. |
||
| 305 | * |
||
| 306 | * @throws Exception\SocketException If the creation of the php sockets is not successful. |
||
| 307 | * |
||
| 308 | * @see Socket::create() |
||
| 309 | * |
||
| 310 | * @return Socket[] An array of Socket objects containing identical sockets. |
||
| 311 | */ |
||
| 312 | public static function createPair($domain, $type, $protocol) |
||
| 313 | { |
||
| 314 | $array = []; |
||
| 315 | $return = @socket_create_pair($domain, $type, $protocol, $array); |
||
| 316 | |||
| 317 | if ($return === false) { |
||
| 318 | throw new SocketException(); |
||
| 319 | } |
||
| 320 | |||
| 321 | $sockets = self::constructFromResources($array); |
||
| 322 | |||
| 323 | foreach ($sockets as $socket) { |
||
| 324 | $socket->domain = $domain; |
||
| 325 | $socket->type = $type; |
||
| 326 | $socket->protocol = $protocol; |
||
| 327 | } |
||
| 328 | |||
| 329 | return $sockets; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Gets socket options. |
||
| 334 | * |
||
| 335 | * <p>Retrieves the value for the option specified by the optname parameter for the current socket.</p> |
||
| 336 | * |
||
| 337 | * @param int $level <p>The level parameter specifies the protocol level at which the option resides. For example, |
||
| 338 | * to retrieve options at the socket level, a level parameter of <code>SOL_SOCKET</code> would be used. Other |
||
| 339 | * levels, such as <code>TCP</code>, can be used by specifying the protocol number of that level. Protocol numbers |
||
| 340 | * can be found by using the <code>getprotobyname()</code> function. |
||
| 341 | * @param int $optname <p><b>Available Socket Options</b></p><p><code>SO_DEBUG</code> - Reports whether debugging |
||
| 342 | * information is being recorded. Returns int.</p><p><code>SO_BROADCAST</code> - Reports whether transmission of |
||
| 343 | * broadcast messages is supported. Returns int.</p><p><code>SO_REUSERADDR</code> - Reports whether local addresses |
||
| 344 | * can be reused. Returns int.</p><p><code>SO_KEEPALIVE</code> - Reports whether connections are kept active with |
||
| 345 | * periodic transmission of messages. If the connected socket fails to respond to these messages, the connection is |
||
| 346 | * broken and processes writing to that socket are notified with a SIGPIPE signal. Returns int.</p><p> |
||
| 347 | * <code>SO_LINGER</code> - Reports whether the socket lingers on <code>close()</code> if data is present. By |
||
| 348 | * default, when the socket is closed, it attempts to send all unsent data. In the case of a connection-oriented |
||
| 349 | * socket, <code>close()</code> will wait for its peer to acknowledge the data. If <code>l_onoff</code> is non-zero |
||
| 350 | * and <code>l_linger</code> is zero, all the unsent data will be discarded and RST (reset) is sent to the peer in |
||
| 351 | * the case of a connection-oriented socket. On the other hand, if <code>l_onoff</code> is non-zero and |
||
| 352 | * <code>l_linger</code> is non-zero, <code>close()</code> will block until all the data is sent or the time |
||
| 353 | * specified in <code>l_linger</code> elapses. If the socket is non-blocking, <code>close()</code> will fail and |
||
| 354 | * return an error. Returns an array with two keps: <code>l_onoff</code> and <code>l_linger</code>.</p><p> |
||
| 355 | * <code>SO_OOBINLINE</code> - Reports whether the socket leaves out-of-band data inline. Returns int.</p><p> |
||
| 356 | * <code>SO_SNDBUF</code> - Reports the size of the send buffer. Returns int.</p><p><code>SO_RCVBUF</code> - |
||
| 357 | * Reports the size of the receive buffer. Returns int.</p><p><code>SO_ERROR</code> - Reports information about |
||
| 358 | * error status and clears it. Returns int.</p><p><code>SO_TYPE</code> - Reports the socket type (e.g. |
||
| 359 | * <code>SOCK_STREAM</code>). Returns int.</p><p><code>SO_DONTROUTE</code> - Reports whether outgoing messages |
||
| 360 | * bypass the standard routing facilities. Returns int.</p><p><code>SO_RCVLOWAT</code> - Reports the minimum number |
||
| 361 | * of bytes to process for socket input operations. Returns int.</p><p><code>SO_RCVTIMEO</code> - Reports the |
||
| 362 | * timeout value for input operations. Returns an array with two keys: <code>sec</code> which is the seconds part |
||
| 363 | * on the timeout value and <code>usec</code> which is the microsecond part of the timeout value.</p><p> |
||
| 364 | * <code>SO_SNDTIMEO</code> - Reports the timeout value specifying the amount of time that an output function |
||
| 365 | * blocks because flow control prevents data from being sent. Returns an array with two keys: <code>sec</code> |
||
| 366 | * which is the seconds part on the timeout value and <code>usec</code> which is the microsecond part of the |
||
| 367 | * timeout value.</p><p><code>SO_SNDLOWAT</code> - Reports the minimum number of bytes to process for socket output |
||
| 368 | * operations. Returns int.</p><p><code>TCP_NODELAY</code> - Reports whether the Nagle TCP algorithm is disabled. |
||
| 369 | * Returns int.</p><p><code>IP_MULTICAST_IF</code> - The outgoing interface for IPv4 multicast packets. Returns the |
||
| 370 | * index of the interface (int).</p><p><code>IPV6_MULTICAST_IF</code> - The outgoing interface for IPv6 multicast |
||
| 371 | * packets. Returns the same thing as <code>IP_MULTICAST_IF</code>.</p><p><code>IP_MULTICAST_LOOP</code> - The |
||
| 372 | * multicast loopback policy for IPv4 packets, which determines whether multicast packets sent by this socket also |
||
| 373 | * reach receivers in the same host that have joined the same multicast group on the outgoing interface used by |
||
| 374 | * this socket. This is the case by default. Returns int.</p><p><code>IPV6_MULTICAST_LOOP</code> - Analogous to |
||
| 375 | * <code>IP_MULTICAST_LOOP</code>, but for IPv6. Returns int.</p><p><code>IP_MULTICAST_TTL</code> - The |
||
| 376 | * time-to-live of outgoing IPv4 multicast packets. This should be a value between 0 (don't leave the interface) |
||
| 377 | * and 255. The default value is 1 (only the local network is reached). Returns int.</p><p> |
||
| 378 | * <code>IPV6_MULTICAST_HOPS</code> - Analogous to <code>IP_MULTICAST_TTL</code>, but for IPv6 packets. The value |
||
| 379 | * -1 is also accepted, meaning the route default should be used. Returns int.</p> |
||
| 380 | * |
||
| 381 | * @throws Exception\SocketException If there was an error retrieving the option. |
||
| 382 | * |
||
| 383 | * @return mixed See the descriptions based on the option being requested above. |
||
| 384 | */ |
||
| 385 | public function getOption($level, $optname) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem |
||
| 398 | * path, dependent on its type. |
||
| 399 | * |
||
| 400 | * @param string $address <p>If the given socket is of type <code>AF_INET</code> or <code>AF_INET6</code>, |
||
| 401 | * <code>getPeerName()</code> will return the peers (remote) IP address in appropriate notation (e.g. |
||
| 402 | * <code>127.0.0.1</code> or <code>fe80::1</code>) in the address parameter and, if the optional port parameter is |
||
| 403 | * present, also the associated port.</p><p>If the given socket is of type <code>AF_UNIX</code>, |
||
| 404 | * <code>getPeerName()</code> will return the Unix filesystem path (e.g. <code>/var/run/daemon.sock</cod>) in the |
||
| 405 | * address parameter.</p> |
||
| 406 | * @param int $port (Optional) If given, this will hold the port associated to the address. |
||
| 407 | * |
||
| 408 | * @throws Exception\SocketException <p>If the retrieval of the peer name fails or if the socket type is not |
||
| 409 | * <code>AF_INET</code>, <code>AF_INET6</code>, or <code>AF_UNIX</code>.</p> |
||
| 410 | * |
||
| 411 | * @return bool <p>Returns <code>true</code> if the retrieval of the peer name was successful.</p> |
||
| 412 | */ |
||
| 413 | public function getPeerName(&$address, &$port) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, |
||
| 426 | * dependent on its type. |
||
| 427 | * |
||
| 428 | * <p><b>Note:</b> <code>getSockName()</code> should not be used with <code>AF_UNIX</code> sockets created with |
||
| 429 | * <code>connect()</code>. Only sockets created with <code>accept()</code> or a primary server socket following a |
||
| 430 | * call to <code>bind()</code> will return meaningful values.</p> |
||
| 431 | * |
||
| 432 | * @param string $address <p>If the given socket is of type <code>AF_INET</code> or <code>AF_INET6</code>, |
||
| 433 | * <code>getSockName()</code> will return the local IP address in appropriate notation (e.g. |
||
| 434 | * <code>127.0.0.1</code> or <code>fe80::1</code>) in the address parameter and, if the optional port parameter is |
||
| 435 | * present, also the associated port.</p><p>If the given socket is of type <code>AF_UNIX</code>, |
||
| 436 | * <code>getSockName()</code> will return the Unix filesystem path (e.g. <code>/var/run/daemon.sock</cod>) in the |
||
| 437 | * address parameter.</p> |
||
| 438 | * @param int $port If provided, this will hold the associated port. |
||
| 439 | * |
||
| 440 | * @throws Exception\SocketException <p>If the retrieval of the socket name fails or if the socket type is not |
||
| 441 | * <code>AF_INET</code>, <code>AF_INET6</code>, or <code>AF_UNIX</code>.</p> |
||
| 442 | * |
||
| 443 | * @return bool <p>Returns <code>true</code> if the retrieval of the socket name was successful.</p> |
||
| 444 | */ |
||
| 445 | 1 | public function getSockName(&$address, &$port) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Imports a stream. |
||
| 462 | * |
||
| 463 | * <p>Imports a stream that encapsulates a socket into a socket extension resource.</p> |
||
| 464 | * |
||
| 465 | * @param $stream The stream resource to import. |
||
| 466 | * |
||
| 467 | * @throws Exception\SocketException If the import of the stream is not successful. |
||
| 468 | * |
||
| 469 | * @return Socket Returns a Socket object based on the stream. |
||
| 470 | */ |
||
| 471 | public static function importStream($stream) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Listens for a connection on a socket. |
||
| 484 | * |
||
| 485 | * <p>After the socket has been created using <code>create()</code> and bound to a name with <code>bind()</code>, |
||
| 486 | * it may be told to listen for incoming connections on socket.</p> |
||
| 487 | * |
||
| 488 | * @param int $backlog <p>A maximum of backlog incoming connections will be queued for processing. If a connection |
||
| 489 | * request arrives with the queue full the client may receive an error with an indication of ECONNREFUSED, or, if |
||
| 490 | * the underlying protocol supports retransmission, the request may be ignored so that retries may succeed.</p><p> |
||
| 491 | * <b>Note:</b> The maximum number passed to the backlog parameter highly depends on the underlying platform. On |
||
| 492 | * Linux, it is silently truncated to <code>SOMAXCONN</code>. On win32, if passed <code>SOMAXCONN</code>, the |
||
| 493 | * underlying service provider responsible for the socket will set the backlog to a maximum reasonable value. There |
||
| 494 | * is no standard provision to find out the actual backlog value on this platform.</p> |
||
| 495 | * |
||
| 496 | * @throws Exception\SocketException If the listen fails. |
||
| 497 | * |
||
| 498 | * @return bool <p>Returns <code>true</code> on success. |
||
| 499 | */ |
||
| 500 | 1 | public function listen($backlog = 0) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * reads a maximum of length bytes from a socket. |
||
| 513 | * |
||
| 514 | * <p>Reads from the socket created by the <code>create()</code> or <code>accept()</code> functions.</p> |
||
| 515 | * |
||
| 516 | * @param int $length <p>The maximum number of bytes read is specified by the length parameter. Otherwise you can |
||
| 517 | * use <code>\r</code>, <code>\n</code>, or <code>\0</code> to end reading (depending on the type parameter, see |
||
| 518 | * below).</p> |
||
| 519 | * @param int $type <p>(Optional) type parameter is a named constant:<ul><li><code>PHP_BINARY_READ</code> (Default) |
||
| 520 | * - use the system <code>recv()</code> function. Safe for reading binary data.</li><li> |
||
| 521 | * <code>PHP_NORMAL_READ</code> - reading stops at <code>\n</code> or <code>\r</code>.</li></ul></p> |
||
| 522 | * |
||
| 523 | * @throws Exception\SocketException If there was an error reading or if the host closed the connection. |
||
| 524 | * |
||
| 525 | * @see Socket::create() |
||
| 526 | * @see Socket::accept() |
||
| 527 | * |
||
| 528 | * @return string Returns the data as a string. Returns a zero length string ("") when there is no more data to |
||
| 529 | * read. |
||
| 530 | */ |
||
| 531 | public function read($length, $type = PHP_BINARY_READ) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Receives data from a connected socket. |
||
| 544 | * |
||
| 545 | * <p>Receives length bytes of data in buffer from the socket. <code>receive()</code> can be used to gather data |
||
| 546 | * from connected sockets. Additionally, one or more flags can be specified to modify the behaviour of the |
||
| 547 | * function.</p><p>buffer is passed by reference, so it must be specified as a variable in the argument list. Data |
||
| 548 | * read from socket by <code>receive()</code> will be returned in buffer.</p> |
||
| 549 | * |
||
| 550 | * @param string $buffer <p>The data received will be fetched to the variable specified with buffer. If an error |
||
| 551 | * occurs, if the connection is reset, or if no data is available, buffer will be set to <code>NULL</code>.</p> |
||
| 552 | * @param int $length Up to length bytes will be fetched from remote host. |
||
| 553 | * @param int $flags <p>The value of flags can be any combination of the following flags, joined with the binary OR |
||
| 554 | * (<code>|</code>) operator.<ul><li><code>MSG_OOB</code> - Process out-of-band data.</li><li><code>MSG_PEEK</code> |
||
| 555 | * - Receive data from the beginning of the receive queue without removing it from the queue.</li><li> |
||
| 556 | * <code>MSG_WAITALL</code> - Block until at least length are received. However, if a signal is caught or the |
||
| 557 | * remote host disconnects, the function may return less data.</li><li><code>MSG_DONTWAIT</code> - With this flag |
||
| 558 | * set, the function returns even if it would normally have blocked.</li></ul></p> |
||
| 559 | * |
||
| 560 | * @throws Exception\SocketException If there was an error receiving data. |
||
| 561 | * |
||
| 562 | * @return int Returns the number of bytes received. |
||
| 563 | */ |
||
| 564 | public function receive(&$buffer, $length, $flags) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Runs the select() system call on the given arrays of sockets with a specified timeout. |
||
| 577 | * |
||
| 578 | * <p>accepts arrays of sockets and waits for them to change status. Those coming with BSD sockets background will |
||
| 579 | * recognize that those socket resource arrays are in fact the so-called file descriptor sets. Three independent |
||
| 580 | * arrays of socket resources are watched.</p><p><b>WARNING:</b> On exit, the arrays are modified to indicate which |
||
| 581 | * socket resource actually changed status.</p><p>ou do not need to pass every array to <code>select()</code>. You |
||
| 582 | * can leave it out and use an empty array or <code>NULL</code> instead. Also do not forget that those arrays are |
||
| 583 | * passed by reference and will be modified after <code>select()</code> returns. |
||
| 584 | * |
||
| 585 | * @param Socket[] &$read <p>The sockets listed in the read array will be watched to see if characters become |
||
| 586 | * available for reading (more precisely, to see if a read will not block - in particular, a socket resource is also |
||
| 587 | * ready on end-of-file, in which case a <code>read()</code> will return a zero length string).</p> |
||
| 588 | * @param Socket[] &$write The sockets listed in the write array will be watched to see if a write will not block. |
||
| 589 | * @param Socket[] &$except he sockets listed in the except array will be watched for exceptions. |
||
| 590 | * @param int $timeoutSeconds The seconds portion of the timeout parameters (in conjunction with |
||
| 591 | * timeoutMilliseconds). The timeout is an upper bound on the amount of time elapsed before <code>select()</code> |
||
| 592 | * returns. timeoutSeconds may be zero, causing the <code>select()</code> to return immediately. This is useful for |
||
| 593 | * polling. If timeoutSeconds is <code>NULL</code> (no timeout), the <code>select()</code> can block |
||
| 594 | * indefinitely.</p> |
||
| 595 | * @param int $timeoutMilliseconds See the description for timeoutSeconds. |
||
| 596 | * |
||
| 597 | * @throws SocketException If there was an error. |
||
| 598 | * |
||
| 599 | * @return int Returns the number of socket resources contained in the modified arrays, which may be zero if the |
||
| 600 | * timeout expires before anything interesting happens. |
||
| 601 | */ |
||
| 602 | public static function select( |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Maps an array of Sockets to an array of socket resources. |
||
| 654 | * |
||
| 655 | * @param Socket[] $sockets An array of sockets to map. |
||
| 656 | * |
||
| 657 | * @return resource[] Returns the corresponding array of resources. |
||
| 658 | */ |
||
| 659 | protected static function mapClassToRawSocket($sockets) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Maps an array of socket resources to an array of Sockets. |
||
| 668 | * |
||
| 669 | * @param resource[] $sockets An array of socket resources to map. |
||
| 670 | * |
||
| 671 | * @return Socket[] Returns the corresponding array of Socket objects. |
||
| 672 | */ |
||
| 673 | protected static function mapRawSocketToClass($sockets) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Write to a socket. |
||
| 682 | * |
||
| 683 | * <p>The function <code>write()</code> writes to the socket from the given buffer.</p> |
||
| 684 | * |
||
| 685 | * @param string $buffer The buffer to be written. |
||
| 686 | * @param int $length The optional parameter length can specify an alternate length of bytes written to the socket. |
||
| 687 | * If this length is greater than the buffer length, it is silently truncated to the length of the buffer. |
||
| 688 | * |
||
| 689 | * @throws Exception\SocketException If there was a failure. |
||
| 690 | * |
||
| 691 | * @return int Returns the number of bytes successfully written to the socket. |
||
| 692 | */ |
||
| 693 | 1 | View Code Duplication | public function write($buffer, $length = null) |
| 717 | |||
| 718 | /** |
||
| 719 | * Sends data to a connected socket. |
||
| 720 | * |
||
| 721 | * <p>Sends length bytes to the socket from buffer.</p> |
||
| 722 | * |
||
| 723 | * @param string $buffer A buffer containing the data that will be sent to the remote host. |
||
| 724 | * @param int $flags <p>The value of flags can be any combination of the following flags, joined with the binary OR |
||
| 725 | * (<code>|</code>) operator.<ul><li><code>MSG_OOB</code> - Send OOB (out-of-band) data.</li><li> |
||
| 726 | * <code>MSG_EOR</code> - Indicate a record mark. The sent data completes the record.</li><li><code>MSG_EOF</code> - |
||
| 727 | * Close the sender side of the socket and include an appropriate notification of this at the end of the sent data. |
||
| 728 | * The sent data completes the transaction.</li><li><code>MSG_DONTROUTE</code> - Bypass routing, use direct |
||
| 729 | * interface.</li></ul></p> |
||
| 730 | * @param int $length The number of bytes that will be sent to the remote host from buffer. |
||
| 731 | * |
||
| 732 | * @throws Exception\SocketException If there was a failure. |
||
| 733 | * |
||
| 734 | * @return int Returns the number of bytes sent. |
||
| 735 | */ |
||
| 736 | View Code Duplication | public function send($buffer, $flags = 0, $length = null) |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Set the socket to blocking / non blocking. |
||
| 763 | * |
||
| 764 | * <p>Removes (blocking) or set (non blocking) the <code>O_NONBLOCK</code> flag on the socket.</p><p>When an |
||
| 765 | * operation is performed on a blocking socket, the script will pause its execution until it receives a signal or it |
||
| 766 | * can perform the operation.</p><p>When an operation is performed on a non-blocking socket, the script will not |
||
| 767 | * pause its execution until it receives a signal or it can perform the operation. Rather, if the operation would |
||
| 768 | * result in a block, the called function will fail.</p> |
||
| 769 | * |
||
| 770 | * @param bool $bool Flag to indicate if the Socket should block (<code>true</code>) or not block |
||
| 771 | * (<code>false</code>). |
||
| 772 | * |
||
| 773 | * @return void |
||
| 774 | */ |
||
| 775 | public function setBlocking($bool) |
||
| 783 | } |
||
| 784 |