-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from timirey/release/2-0-0-rc2
Release/2 0 0 rc2
- Loading branch information
Showing
23 changed files
with
324 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Timirey\XApi\Connections; | ||
|
||
use Exception; | ||
use Timirey\XApi\Exceptions\SocketException; | ||
|
||
/** | ||
* Represents a socket connection for sending and receiving data. | ||
*/ | ||
class Socket | ||
{ | ||
/** | ||
* @var false|resource The socket resource or false if failed to create. | ||
*/ | ||
protected $socket; | ||
|
||
/** | ||
* Constructor to initialize and connect the socket. | ||
* | ||
* @param string $address The address to connect to. | ||
* @param float|null $timeout The connection timeout in seconds. | ||
* @param integer $flags The flags to use for the connection. | ||
* | ||
* @throws SocketException If socket is unable to init. | ||
*/ | ||
public function __construct( | ||
string $address, | ||
?float $timeout = null, | ||
int $flags = STREAM_CLIENT_CONNECT | ||
) { | ||
$this->socket = stream_socket_client( | ||
$address, | ||
$errorCode, | ||
$errorMessage, | ||
$timeout, | ||
$flags | ||
); | ||
|
||
if ($this->socket === false) { | ||
throw new SocketException("$errorCode: $errorMessage"); | ||
} | ||
} | ||
|
||
/** | ||
* Sends data through the socket. | ||
* | ||
* @param string $payload The data to send. | ||
* @param integer|null $length The length of data to send. Defaults to the full length of the payload. | ||
* | ||
* @return false|integer The number of bytes written, or false on failure. | ||
*/ | ||
public function send(string $payload, ?int $length = null): false|int | ||
{ | ||
return fwrite($this->socket, $payload, $length); | ||
} | ||
|
||
/** | ||
* Receives data from the socket. | ||
* | ||
* @param integer $length The maximum number of bytes to read. | ||
* | ||
* @return false|string The read data, or false on failure. | ||
*/ | ||
public function receive(int $length = 4096): false|string | ||
{ | ||
return fread($this->socket, $length); | ||
} | ||
|
||
/** | ||
* Closes the socket connection. | ||
* | ||
* @return boolean True on success, false on failure. | ||
*/ | ||
public function close(): bool | ||
{ | ||
return fclose($this->socket); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Timirey\XApi\Connections; | ||
|
||
use Generator; | ||
use Timirey\XApi\Exceptions\SocketException; | ||
|
||
/** | ||
* Extends the Socket class to provide additional functionality for | ||
* listening to data from a stream socket connection. | ||
*/ | ||
class StreamSocket extends Socket | ||
{ | ||
/** | ||
* Listen to the stream socket and yield data as it is received. | ||
* | ||
* @return Generator Yields data received from the socket. | ||
* @throws SocketException If socket is empty. | ||
*/ | ||
public function listen(): Generator | ||
{ | ||
while (!feof($this->socket)) { | ||
$response = $this->receive(); | ||
|
||
if (!empty($response)) { | ||
yield $response; | ||
} | ||
} | ||
|
||
throw new SocketException('Unable to subscribe. Empty socket response.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Timirey\XApi\Exceptions; | ||
|
||
use Exception; | ||
|
||
/** | ||
* Custom exception for sockets. | ||
*/ | ||
class SocketException extends Exception | ||
{ | ||
} |
Oops, something went wrong.