| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\SocketsException; |
| 6 |
|
| 7 |
/** |
| 8 |
* After the socket socket has been created |
| 9 |
* using socket_create, bound to a name with |
| 10 |
* socket_bind, and told to listen for connections |
| 11 |
* with socket_listen, this function will accept |
| 12 |
* incoming connections on that socket. Once a successful connection |
| 13 |
* is made, a new socket resource is returned, which may be used |
| 14 |
* for communication. If there are multiple connections queued on |
| 15 |
* the socket, the first will be used. If there are no pending |
| 16 |
* connections, socket_accept will block until |
| 17 |
* a connection becomes present. If socket |
| 18 |
* has been made non-blocking using |
| 19 |
* socket_set_blocking or |
| 20 |
* socket_set_nonblock, FALSE will be returned. |
| 21 |
* |
| 22 |
* The socket resource returned by |
| 23 |
* socket_accept may not be used to accept new |
| 24 |
* connections. The original listening socket |
| 25 |
* socket, however, remains open and may be |
| 26 |
* reused. |
| 27 |
* |
| 28 |
* @param resource $socket A valid socket resource created with socket_create. |
| 29 |
* @return resource Returns a new socket resource on success. The actual |
| 30 |
* error code can be retrieved by calling |
| 31 |
* socket_last_error. This error code may be passed to |
| 32 |
* socket_strerror to get a textual explanation of the |
| 33 |
* error. |
| 34 |
* @throws SocketsException |
| 35 |
* |
| 36 |
*/ |
| 37 |
function socket_accept($socket) |
| 38 |
{ |
| 39 |
error_clear_last(); |
| 40 |
$result = \socket_accept($socket); |
| 41 |
if ($result === false) { |
| 42 |
throw SocketsException::createFromPhpError(); |
| 43 |
} |
| 44 |
return $result; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* Create a Socket resource, and bind it to the provided AddrInfo resource. The return |
| 50 |
* value of this function may be used with socket_listen. |
| 51 |
* |
| 52 |
* @param resource $addr Resource created from socket_addrinfo_lookup. |
| 53 |
* @return resource Returns a Socket resource on success. |
| 54 |
* @throws SocketsException |
| 55 |
* |
| 56 |
*/ |
| 57 |
function socket_addrinfo_bind($addr) |
| 58 |
{ |
| 59 |
error_clear_last(); |
| 60 |
$result = \socket_addrinfo_bind($addr); |
| 61 |
if ($result === null) { |
| 62 |
throw SocketsException::createFromPhpError(); |
| 63 |
} |
| 64 |
return $result; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
/** |
| 69 |
* Create a Socket resource, and connect it to the provided AddrInfo resource. The return |
| 70 |
* value of this function may be used with the rest of the socket functions. |
| 71 |
* |
| 72 |
* @param resource $addr Resource created from socket_addrinfo_lookup |
| 73 |
* @return resource Returns a Socket resource on success. |
| 74 |
* @throws SocketsException |
| 75 |
* |
| 76 |
*/ |
| 77 |
function socket_addrinfo_connect($addr) |
| 78 |
{ |
| 79 |
error_clear_last(); |
| 80 |
$result = \socket_addrinfo_connect($addr); |
| 81 |
if ($result === null) { |
| 82 |
throw SocketsException::createFromPhpError(); |
| 83 |
} |
| 84 |
return $result; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Binds the name given in address to the socket |
| 90 |
* described by socket. This has to be done before |
| 91 |
* a connection is be established using socket_connect |
| 92 |
* or socket_listen. |
| 93 |
* |
| 94 |
* @param resource $socket A valid socket resource created with socket_create. |
| 95 |
* @param string $address If the socket is of the AF_INET family, the |
| 96 |
* address is an IP in dotted-quad notation |
| 97 |
* (e.g. 127.0.0.1). |
| 98 |
* |
| 99 |
* If the socket is of the AF_UNIX family, the |
| 100 |
* address is the path of a |
| 101 |
* Unix-domain socket (e.g. /tmp/my.sock). |
| 102 |
* @param int $port The port parameter is only used when |
| 103 |
* binding an AF_INET socket, and designates |
| 104 |
* the port on which to listen for connections. |
| 105 |
* @throws SocketsException |
| 106 |
* |
| 107 |
*/ |
| 108 |
function socket_bind($socket, string $address, int $port = 0): void |
| 109 |
{ |
| 110 |
error_clear_last(); |
| 111 |
$result = \socket_bind($socket, $address, $port); |
| 112 |
if ($result === false) { |
| 113 |
throw SocketsException::createFromPhpError(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Initiate a connection to address using the socket resource |
| 120 |
* socket, which must be a valid socket |
| 121 |
* resource created with socket_create. |
| 122 |
* |
| 123 |
* @param resource $socket |
| 124 |
* @param string $address The address parameter is either an IPv4 address |
| 125 |
* in dotted-quad notation (e.g. 127.0.0.1) if |
| 126 |
* socket is AF_INET, a valid |
| 127 |
* IPv6 address (e.g. ::1) if IPv6 support is enabled and |
| 128 |
* socket is AF_INET6 |
| 129 |
* or the pathname of a Unix domain socket, if the socket family is |
| 130 |
* AF_UNIX. |
| 131 |
* @param int $port The port parameter is only used and is mandatory |
| 132 |
* when connecting to an AF_INET or an |
| 133 |
* AF_INET6 socket, and designates |
| 134 |
* the port on the remote host to which a connection should be made. |
| 135 |
* @throws SocketsException |
| 136 |
* |
| 137 |
*/ |
| 138 |
function socket_connect($socket, string $address, int $port = 0): void |
| 139 |
{ |
| 140 |
error_clear_last(); |
| 141 |
$result = \socket_connect($socket, $address, $port); |
| 142 |
if ($result === false) { |
| 143 |
throw SocketsException::createFromPhpError(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* socket_create_listen creates a new socket resource of |
| 150 |
* type AF_INET listening on all |
| 151 |
* local interfaces on the given port waiting for new connections. |
| 152 |
* |
| 153 |
* This function is meant to ease the task of creating a new socket which |
| 154 |
* only listens to accept new connections. |
| 155 |
* |
| 156 |
* @param int $port The port on which to listen on all interfaces. |
| 157 |
* @param int $backlog The backlog parameter defines the maximum length |
| 158 |
* the queue of pending connections may grow to. |
| 159 |
* SOMAXCONN may be passed as |
| 160 |
* backlog parameter, see |
| 161 |
* socket_listen for more information. |
| 162 |
* @return resource socket_create_listen returns a new socket resource |
| 163 |
* on success. The error code can be retrieved with |
| 164 |
* socket_last_error. This code may be passed to |
| 165 |
* socket_strerror to get a textual explanation of the |
| 166 |
* error. |
| 167 |
* @throws SocketsException |
| 168 |
* |
| 169 |
*/ |
| 170 |
function socket_create_listen(int $port, int $backlog = 128) |
| 171 |
{ |
| 172 |
error_clear_last(); |
| 173 |
$result = \socket_create_listen($port, $backlog); |
| 174 |
if ($result === false) { |
| 175 |
throw SocketsException::createFromPhpError(); |
| 176 |
} |
| 177 |
return $result; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* socket_create_pair creates two connected and |
| 183 |
* indistinguishable sockets, and stores them in fd. |
| 184 |
* This function is commonly used in IPC (InterProcess Communication). |
| 185 |
* |
| 186 |
* @param int $domain The domain parameter specifies the protocol |
| 187 |
* family to be used by the socket. See socket_create |
| 188 |
* for the full list. |
| 189 |
* @param int $type The type parameter selects the type of communication |
| 190 |
* to be used by the socket. See socket_create for the |
| 191 |
* full list. |
| 192 |
* @param int $protocol The protocol parameter sets the specific |
| 193 |
* protocol within the specified domain to be used |
| 194 |
* when communicating on the returned socket. The proper value can be retrieved by |
| 195 |
* name by using getprotobyname. If |
| 196 |
* the desired protocol is TCP, or UDP the corresponding constants |
| 197 |
* SOL_TCP, and SOL_UDP |
| 198 |
* can also be used. |
| 199 |
* |
| 200 |
* See socket_create for the full list of supported |
| 201 |
* protocols. |
| 202 |
* @param resource[]|null $fd Reference to an array in which the two socket resources will be inserted. |
| 203 |
* @throws SocketsException |
| 204 |
* |
| 205 |
*/ |
| 206 |
function socket_create_pair(int $domain, int $type, int $protocol, ?iterable &$fd): void |
| 207 |
{ |
| 208 |
error_clear_last(); |
| 209 |
$result = \socket_create_pair($domain, $type, $protocol, $fd); |
| 210 |
if ($result === false) { |
| 211 |
throw SocketsException::createFromPhpError(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
/** |
| 217 |
* Creates and returns a socket resource, also referred to as an endpoint |
| 218 |
* of communication. A typical network connection is made up of 2 sockets, one |
| 219 |
* performing the role of the client, and another performing the role of the server. |
| 220 |
* |
| 221 |
* @param int $domain The domain parameter specifies the protocol |
| 222 |
* family to be used by the socket. |
| 223 |
* @param int $type The type parameter selects the type of communication |
| 224 |
* to be used by the socket. |
| 225 |
* @param int $protocol The protocol parameter sets the specific |
| 226 |
* protocol within the specified domain to be used |
| 227 |
* when communicating on the returned socket. The proper value can be |
| 228 |
* retrieved by name by using getprotobyname. If |
| 229 |
* the desired protocol is TCP, or UDP the corresponding constants |
| 230 |
* SOL_TCP, and SOL_UDP |
| 231 |
* can also be used. |
| 232 |
* @return resource socket_create returns a socket resource on success. The actual error code can be retrieved by calling |
| 233 |
* socket_last_error. This error code may be passed to |
| 234 |
* socket_strerror to get a textual explanation of the |
| 235 |
* error. |
| 236 |
* @throws SocketsException |
| 237 |
* |
| 238 |
*/ |
| 239 |
function socket_create(int $domain, int $type, int $protocol) |
| 240 |
{ |
| 241 |
error_clear_last(); |
| 242 |
$result = \socket_create($domain, $type, $protocol); |
| 243 |
if ($result === false) { |
| 244 |
throw SocketsException::createFromPhpError(); |
| 245 |
} |
| 246 |
return $result; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
/** |
| 251 |
* |
| 252 |
* |
| 253 |
* @param resource $socket |
| 254 |
* @return resource Return resource. |
| 255 |
* @throws SocketsException |
| 256 |
* |
| 257 |
*/ |
| 258 |
function socket_export_stream($socket) |
| 259 |
{ |
| 260 |
error_clear_last(); |
| 261 |
$result = \socket_export_stream($socket); |
| 262 |
if ($result === false) { |
| 263 |
throw SocketsException::createFromPhpError(); |
| 264 |
} |
| 265 |
return $result; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
/** |
| 270 |
* The socket_get_option function retrieves the value for |
| 271 |
* the option specified by the optname parameter for the |
| 272 |
* specified socket. |
| 273 |
* |
| 274 |
* @param resource $socket A valid socket resource created with socket_create |
| 275 |
* or socket_accept. |
| 276 |
* @param int $level The level parameter specifies the protocol |
| 277 |
* level at which the option resides. For example, to retrieve options at |
| 278 |
* the socket level, a level parameter of |
| 279 |
* SOL_SOCKET would be used. Other levels, such as |
| 280 |
* TCP, can be used by |
| 281 |
* specifying the protocol number of that level. Protocol numbers can be |
| 282 |
* found by using the getprotobyname function. |
| 283 |
* @param int $optname Reports whether the socket lingers on |
| 284 |
* socket_close if data is present. By default, |
| 285 |
* when the socket is closed, it attempts to send all unsent data. |
| 286 |
* In the case of a connection-oriented socket, |
| 287 |
* socket_close will wait for its peer to |
| 288 |
* acknowledge the data. |
| 289 |
* |
| 290 |
* If l_onoff is non-zero and |
| 291 |
* l_linger is zero, all the |
| 292 |
* unsent data will be discarded and RST (reset) is sent to the |
| 293 |
* peer in the case of a connection-oriented socket. |
| 294 |
* |
| 295 |
* On the other hand, if l_onoff is |
| 296 |
* non-zero and l_linger is non-zero, |
| 297 |
* socket_close will block until all the data |
| 298 |
* is sent or the time specified in l_linger |
| 299 |
* elapses. If the socket is non-blocking, |
| 300 |
* socket_close will fail and return an error. |
| 301 |
* @return mixed Returns the value of the given options. |
| 302 |
* @throws SocketsException |
| 303 |
* |
| 304 |
*/ |
| 305 |
function socket_get_option($socket, int $level, int $optname) |
| 306 |
{ |
| 307 |
error_clear_last(); |
| 308 |
$result = \socket_get_option($socket, $level, $optname); |
| 309 |
if ($result === false) { |
| 310 |
throw SocketsException::createFromPhpError(); |
| 311 |
} |
| 312 |
return $result; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
/** |
| 317 |
* Queries the remote side of the given socket which may either result in |
| 318 |
* host/port or in a Unix filesystem path, dependent on its type. |
| 319 |
* |
| 320 |
* @param resource $socket A valid socket resource created with socket_create |
| 321 |
* or socket_accept. |
| 322 |
* @param string $address If the given socket is of type AF_INET or |
| 323 |
* AF_INET6, socket_getpeername |
| 324 |
* will return the peers (remote) IP address in |
| 325 |
* appropriate notation (e.g. 127.0.0.1 or |
| 326 |
* fe80::1) in the address |
| 327 |
* parameter and, if the optional port parameter is |
| 328 |
* present, also the associated port. |
| 329 |
* |
| 330 |
* If the given socket is of type AF_UNIX, |
| 331 |
* socket_getpeername will return the Unix filesystem |
| 332 |
* path (e.g. /var/run/daemon.sock) in the |
| 333 |
* address parameter. |
| 334 |
* @param int|null $port If given, this will hold the port associated to |
| 335 |
* address. |
| 336 |
* @throws SocketsException |
| 337 |
* |
| 338 |
*/ |
| 339 |
function socket_getpeername($socket, string &$address, ?int &$port = null): void |
| 340 |
{ |
| 341 |
error_clear_last(); |
| 342 |
$result = \socket_getpeername($socket, $address, $port); |
| 343 |
if ($result === false) { |
| 344 |
throw SocketsException::createFromPhpError(); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
/** |
| 350 |
* |
| 351 |
* |
| 352 |
* @param resource $socket A valid socket resource created with socket_create |
| 353 |
* or socket_accept. |
| 354 |
* @param string|null $addr If the given socket is of type AF_INET |
| 355 |
* or AF_INET6, socket_getsockname |
| 356 |
* will return the local IP address in appropriate notation (e.g. |
| 357 |
* 127.0.0.1 or fe80::1) in the |
| 358 |
* address parameter and, if the optional |
| 359 |
* port parameter is present, also the associated port. |
| 360 |
* |
| 361 |
* If the given socket is of type AF_UNIX, |
| 362 |
* socket_getsockname will return the Unix filesystem |
| 363 |
* path (e.g. /var/run/daemon.sock) in the |
| 364 |
* address parameter. |
| 365 |
* @param int|null $port If provided, this will hold the associated port. |
| 366 |
* @throws SocketsException |
| 367 |
* |
| 368 |
*/ |
| 369 |
function socket_getsockname($socket, ?string &$addr, ?int &$port = null): void |
| 370 |
{ |
| 371 |
error_clear_last(); |
| 372 |
$result = \socket_getsockname($socket, $addr, $port); |
| 373 |
if ($result === false) { |
| 374 |
throw SocketsException::createFromPhpError(); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
/** |
| 380 |
* Imports a stream that encapsulates a socket into a socket extension resource. |
| 381 |
* |
| 382 |
* @param resource $stream The stream resource to import. |
| 383 |
* @return resource|false Returns FALSE. |
| 384 |
* @throws SocketsException |
| 385 |
* |
| 386 |
*/ |
| 387 |
function socket_import_stream($stream) |
| 388 |
{ |
| 389 |
error_clear_last(); |
| 390 |
$result = \socket_import_stream($stream); |
| 391 |
if ($result === null) { |
| 392 |
throw SocketsException::createFromPhpError(); |
| 393 |
} |
| 394 |
return $result; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
/** |
| 399 |
* After the socket socket has been created |
| 400 |
* using socket_create and bound to a name with |
| 401 |
* socket_bind, it may be told to listen for incoming |
| 402 |
* connections on socket. |
| 403 |
* |
| 404 |
* socket_listen is applicable only to sockets of |
| 405 |
* type SOCK_STREAM or |
| 406 |
* SOCK_SEQPACKET. |
| 407 |
* |
| 408 |
* @param resource $socket A valid socket resource created with socket_create |
| 409 |
* or socket_addrinfo_bind |
| 410 |
* @param int $backlog A maximum of backlog incoming connections will be |
| 411 |
* queued for processing. If a connection request arrives with the queue |
| 412 |
* full the client may receive an error with an indication of |
| 413 |
* ECONNREFUSED, or, if the underlying protocol supports |
| 414 |
* retransmission, the request may be ignored so that retries may succeed. |
| 415 |
* |
| 416 |
* The maximum number passed to the backlog |
| 417 |
* parameter highly depends on the underlying platform. On Linux, it is |
| 418 |
* silently truncated to SOMAXCONN. On win32, if |
| 419 |
* passed SOMAXCONN, the underlying service provider |
| 420 |
* responsible for the socket will set the backlog to a maximum |
| 421 |
* reasonable value. There is no standard provision to |
| 422 |
* find out the actual backlog value on this platform. |
| 423 |
* @throws SocketsException |
| 424 |
* |
| 425 |
*/ |
| 426 |
function socket_listen($socket, int $backlog = 0): void |
| 427 |
{ |
| 428 |
error_clear_last(); |
| 429 |
$result = \socket_listen($socket, $backlog); |
| 430 |
if ($result === false) { |
| 431 |
throw SocketsException::createFromPhpError(); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
/** |
| 437 |
* The function socket_read reads from the socket |
| 438 |
* resource socket created by the |
| 439 |
* socket_create or |
| 440 |
* socket_accept functions. |
| 441 |
* |
| 442 |
* @param resource $socket A valid socket resource created with socket_create |
| 443 |
* or socket_accept. |
| 444 |
* @param int $length The maximum number of bytes read is specified by the |
| 445 |
* length parameter. Otherwise you can use |
| 446 |
* \r, \n, |
| 447 |
* or \0 to end reading (depending on the type |
| 448 |
* parameter, see below). |
| 449 |
* @param int $type Optional type parameter is a named constant: |
| 450 |
* |
| 451 |
* |
| 452 |
* |
| 453 |
* PHP_BINARY_READ (Default) - use the system |
| 454 |
* recv() function. Safe for reading binary data. |
| 455 |
* |
| 456 |
* |
| 457 |
* |
| 458 |
* |
| 459 |
* PHP_NORMAL_READ - reading stops at |
| 460 |
* \n or \r. |
| 461 |
* |
| 462 |
* |
| 463 |
* |
| 464 |
* @return string socket_read returns the data as a string on success (including if the remote host has closed the |
| 465 |
* connection). The error code can be retrieved with |
| 466 |
* socket_last_error. This code may be passed to |
| 467 |
* socket_strerror to get a textual representation of |
| 468 |
* the error. |
| 469 |
* @throws SocketsException |
| 470 |
* |
| 471 |
*/ |
| 472 |
function socket_read($socket, int $length, int $type = PHP_BINARY_READ): string |
| 473 |
{ |
| 474 |
error_clear_last(); |
| 475 |
$result = \socket_read($socket, $length, $type); |
| 476 |
if ($result === false) { |
| 477 |
throw SocketsException::createFromPhpError(); |
| 478 |
} |
| 479 |
return $result; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
/** |
| 484 |
* The function socket_send sends |
| 485 |
* len bytes to the socket |
| 486 |
* socket from buf. |
| 487 |
* |
| 488 |
* @param resource $socket A valid socket resource created with socket_create |
| 489 |
* or socket_accept. |
| 490 |
* @param string $buf A buffer containing the data that will be sent to the remote host. |
| 491 |
* @param int $len The number of bytes that will be sent to the remote host from |
| 492 |
* buf. |
| 493 |
* @param int $flags The value of flags can be any combination of |
| 494 |
* the following flags, joined with the binary OR (|) |
| 495 |
* operator. |
| 496 |
* |
| 497 |
* Possible values for flags |
| 498 |
* |
| 499 |
* |
| 500 |
* |
| 501 |
* MSG_OOB |
| 502 |
* |
| 503 |
* Send OOB (out-of-band) data. |
| 504 |
* |
| 505 |
* |
| 506 |
* |
| 507 |
* MSG_EOR |
| 508 |
* |
| 509 |
* Indicate a record mark. The sent data completes the record. |
| 510 |
* |
| 511 |
* |
| 512 |
* |
| 513 |
* MSG_EOF |
| 514 |
* |
| 515 |
* Close the sender side of the socket and include an appropriate |
| 516 |
* notification of this at the end of the sent data. The sent data |
| 517 |
* completes the transaction. |
| 518 |
* |
| 519 |
* |
| 520 |
* |
| 521 |
* MSG_DONTROUTE |
| 522 |
* |
| 523 |
* Bypass routing, use direct interface. |
| 524 |
* |
| 525 |
* |
| 526 |
* |
| 527 |
* |
| 528 |
* |
| 529 |
* @return int socket_send returns the number of bytes sent. |
| 530 |
* @throws SocketsException |
| 531 |
* |
| 532 |
*/ |
| 533 |
function socket_send($socket, string $buf, int $len, int $flags): int |
| 534 |
{ |
| 535 |
error_clear_last(); |
| 536 |
$result = \socket_send($socket, $buf, $len, $flags); |
| 537 |
if ($result === false) { |
| 538 |
throw SocketsException::createFromPhpError(); |
| 539 |
} |
| 540 |
return $result; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
/** |
| 545 |
* |
| 546 |
* |
| 547 |
* @param resource $socket |
| 548 |
* @param array $message |
| 549 |
* @param int $flags |
| 550 |
* @return int Returns the number of bytes sent. |
| 551 |
* @throws SocketsException |
| 552 |
* |
| 553 |
*/ |
| 554 |
function socket_sendmsg($socket, array $message, int $flags = 0): int |
| 555 |
{ |
| 556 |
error_clear_last(); |
| 557 |
$result = \socket_sendmsg($socket, $message, $flags); |
| 558 |
if ($result === false) { |
| 559 |
throw SocketsException::createFromPhpError(); |
| 560 |
} |
| 561 |
return $result; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
/** |
| 566 |
* The function socket_sendto sends |
| 567 |
* len bytes from buf |
| 568 |
* through the socket socket to the |
| 569 |
* port at the address addr. |
| 570 |
* |
| 571 |
* @param resource $socket A valid socket resource created using socket_create. |
| 572 |
* @param string $buf The sent data will be taken from buffer buf. |
| 573 |
* @param int $len len bytes from buf will be |
| 574 |
* sent. |
| 575 |
* @param int $flags The value of flags can be any combination of |
| 576 |
* the following flags, joined with the binary OR (|) |
| 577 |
* operator. |
| 578 |
* |
| 579 |
* Possible values for flags |
| 580 |
* |
| 581 |
* |
| 582 |
* |
| 583 |
* MSG_OOB |
| 584 |
* |
| 585 |
* Send OOB (out-of-band) data. |
| 586 |
* |
| 587 |
* |
| 588 |
* |
| 589 |
* MSG_EOR |
| 590 |
* |
| 591 |
* Indicate a record mark. The sent data completes the record. |
| 592 |
* |
| 593 |
* |
| 594 |
* |
| 595 |
* MSG_EOF |
| 596 |
* |
| 597 |
* Close the sender side of the socket and include an appropriate |
| 598 |
* notification of this at the end of the sent data. The sent data |
| 599 |
* completes the transaction. |
| 600 |
* |
| 601 |
* |
| 602 |
* |
| 603 |
* MSG_DONTROUTE |
| 604 |
* |
| 605 |
* Bypass routing, use direct interface. |
| 606 |
* |
| 607 |
* |
| 608 |
* |
| 609 |
* |
| 610 |
* |
| 611 |
* @param string $addr IP address of the remote host. |
| 612 |
* @param int $port port is the remote port number at which the data |
| 613 |
* will be sent. |
| 614 |
* @return int socket_sendto returns the number of bytes sent to the |
| 615 |
* remote host. |
| 616 |
* @throws SocketsException |
| 617 |
* |
| 618 |
*/ |
| 619 |
function socket_sendto($socket, string $buf, int $len, int $flags, string $addr, int $port = 0): int |
| 620 |
{ |
| 621 |
error_clear_last(); |
| 622 |
$result = \socket_sendto($socket, $buf, $len, $flags, $addr, $port); |
| 623 |
if ($result === false) { |
| 624 |
throw SocketsException::createFromPhpError(); |
| 625 |
} |
| 626 |
return $result; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
/** |
| 631 |
* The socket_set_block function removes the |
| 632 |
* O_NONBLOCK flag on the socket specified by the |
| 633 |
* socket parameter. |
| 634 |
* |
| 635 |
* When an operation (e.g. receive, send, connect, accept, ...) is performed on |
| 636 |
* a blocking socket, the script will pause its execution until it receives |
| 637 |
* a signal or it can perform the operation. |
| 638 |
* |
| 639 |
* @param resource $socket A valid socket resource created with socket_create |
| 640 |
* or socket_accept. |
| 641 |
* @throws SocketsException |
| 642 |
* |
| 643 |
*/ |
| 644 |
function socket_set_block($socket): void |
| 645 |
{ |
| 646 |
error_clear_last(); |
| 647 |
$result = \socket_set_block($socket); |
| 648 |
if ($result === false) { |
| 649 |
throw SocketsException::createFromPhpError(); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
/** |
| 655 |
* The socket_set_nonblock function sets the |
| 656 |
* O_NONBLOCK flag on the socket specified by |
| 657 |
* the socket parameter. |
| 658 |
* |
| 659 |
* When an operation (e.g. receive, send, connect, accept, ...) is performed on |
| 660 |
* a non-blocking socket, the script will not pause its execution until it receives a |
| 661 |
* signal or it can perform the operation. Rather, if the operation would result |
| 662 |
* in a block, the called function will fail. |
| 663 |
* |
| 664 |
* @param resource $socket A valid socket resource created with socket_create |
| 665 |
* or socket_accept. |
| 666 |
* @throws SocketsException |
| 667 |
* |
| 668 |
*/ |
| 669 |
function socket_set_nonblock($socket): void |
| 670 |
{ |
| 671 |
error_clear_last(); |
| 672 |
$result = \socket_set_nonblock($socket); |
| 673 |
if ($result === false) { |
| 674 |
throw SocketsException::createFromPhpError(); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
/** |
| 680 |
* The socket_set_option function sets the option |
| 681 |
* specified by the optname parameter, at the |
| 682 |
* specified protocol level, to the value pointed to |
| 683 |
* by the optval parameter for the |
| 684 |
* socket. |
| 685 |
* |
| 686 |
* @param resource $socket A valid socket resource created with socket_create |
| 687 |
* or socket_accept. |
| 688 |
* @param int $level The level parameter specifies the protocol |
| 689 |
* level at which the option resides. For example, to retrieve options at |
| 690 |
* the socket level, a level parameter of |
| 691 |
* SOL_SOCKET would be used. Other levels, such as |
| 692 |
* TCP, can be used by specifying the protocol number of that level. |
| 693 |
* Protocol numbers can be found by using the |
| 694 |
* getprotobyname function. |
| 695 |
* @param int $optname The available socket options are the same as those for the |
| 696 |
* socket_get_option function. |
| 697 |
* @param int|string|array $optval The option value. |
| 698 |
* @throws SocketsException |
| 699 |
* |
| 700 |
*/ |
| 701 |
function socket_set_option($socket, int $level, int $optname, $optval): void |
| 702 |
{ |
| 703 |
error_clear_last(); |
| 704 |
$result = \socket_set_option($socket, $level, $optname, $optval); |
| 705 |
if ($result === false) { |
| 706 |
throw SocketsException::createFromPhpError(); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
/** |
| 712 |
* The socket_shutdown function allows you to stop |
| 713 |
* incoming, outgoing or all data (the default) from being sent through the |
| 714 |
* socket |
| 715 |
* |
| 716 |
* @param resource $socket A valid socket resource created with socket_create. |
| 717 |
* @param int $how The value of how can be one of the following: |
| 718 |
* |
| 719 |
* possible values for how |
| 720 |
* |
| 721 |
* |
| 722 |
* |
| 723 |
* 0 |
| 724 |
* |
| 725 |
* Shutdown socket reading |
| 726 |
* |
| 727 |
* |
| 728 |
* |
| 729 |
* 1 |
| 730 |
* |
| 731 |
* Shutdown socket writing |
| 732 |
* |
| 733 |
* |
| 734 |
* |
| 735 |
* 2 |
| 736 |
* |
| 737 |
* Shutdown socket reading and writing |
| 738 |
* |
| 739 |
* |
| 740 |
* |
| 741 |
* |
| 742 |
* |
| 743 |
* @throws SocketsException |
| 744 |
* |
| 745 |
*/ |
| 746 |
function socket_shutdown($socket, int $how = 2): void |
| 747 |
{ |
| 748 |
error_clear_last(); |
| 749 |
$result = \socket_shutdown($socket, $how); |
| 750 |
if ($result === false) { |
| 751 |
throw SocketsException::createFromPhpError(); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
/** |
| 757 |
* Exports the WSAPROTOCOL_INFO structure into shared memory and returns |
| 758 |
* an identifier to be used with socket_wsaprotocol_info_import. The |
| 759 |
* exported ID is only valid for the given target_pid. |
| 760 |
* |
| 761 |
* @param resource $socket A valid socket resource. |
| 762 |
* @param int $target_pid The ID of the process which will import the socket. |
| 763 |
* @return string Returns an identifier to be used for the import |
| 764 |
* @throws SocketsException |
| 765 |
* |
| 766 |
*/ |
| 767 |
function socket_wsaprotocol_info_export($socket, int $target_pid): string |
| 768 |
{ |
| 769 |
error_clear_last(); |
| 770 |
$result = \socket_wsaprotocol_info_export($socket, $target_pid); |
| 771 |
if ($result === false) { |
| 772 |
throw SocketsException::createFromPhpError(); |
| 773 |
} |
| 774 |
return $result; |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
/** |
| 779 |
* Imports a socket which has formerly been exported from another process. |
| 780 |
* |
| 781 |
* @param string $info_id The ID which has been returned by a former call to |
| 782 |
* socket_wsaprotocol_info_export. |
| 783 |
* @return resource Returns the socket resource |
| 784 |
* @throws SocketsException |
| 785 |
* |
| 786 |
*/ |
| 787 |
function socket_wsaprotocol_info_import(string $info_id) |
| 788 |
{ |
| 789 |
error_clear_last(); |
| 790 |
$result = \socket_wsaprotocol_info_import($info_id); |
| 791 |
if ($result === false) { |
| 792 |
throw SocketsException::createFromPhpError(); |
| 793 |
} |
| 794 |
return $result; |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
/** |
| 799 |
* Releases the shared memory corresponding to the given info_id. |
| 800 |
* |
| 801 |
* @param string $info_id The ID which has been returned by a former call to |
| 802 |
* socket_wsaprotocol_info_export. |
| 803 |
* @throws SocketsException |
| 804 |
* |
| 805 |
*/ |
| 806 |
function socket_wsaprotocol_info_release(string $info_id): void |
| 807 |
{ |
| 808 |
error_clear_last(); |
| 809 |
$result = \socket_wsaprotocol_info_release($info_id); |
| 810 |
if ($result === false) { |
| 811 |
throw SocketsException::createFromPhpError(); |
| 812 |
} |
| 813 |
} |
| 814 |
|