Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update socket connection exceptions #57

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 51 additions & 47 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## [6.0.1](https://github.com/timirey/xapi-php/compare/6.0.0..6.0.1) - 2024-07-30

- Fixed `SocketConnection` exception messages to be more informative.

## [6.0.0](https://github.com/timirey/xapi-php/compare/5.0.0..6.0.0) - 2024-07-30

### Added
Expand Down Expand Up @@ -31,122 +35,122 @@

## [5.0.0](https://github.com/timirey/xapi-php/compare/4.0.1..5.0.0) - 2024-07-17

* Changed the way client inits, `$userId` and `$password` are directly sent in the payload.
-Changed the way client inits, `$userId` and `$password` are directly sent in the payload.

## [4.0.1](https://github.com/timirey/xapi-php/compare/4.0.0..4.0.1) - 2024-07-16

* Fix stream listener by adding `feof()` check.
-Fix stream listener by adding `feof()` check.

## [4.0.0](https://github.com/timirey/xapi-php/compare/3.0.0..4.0.0) - 2024-07-16

* Dropped minimum support to 8.1 PHP.
* Added buffer to make sure it reads all the way down to `"\n\n"` guaranteed response separator.
-Dropped minimum support to 8.1 PHP.
-Added buffer to make sure it reads all the way down to `"\n\n"` guaranteed response separator.

## [3.0.0](https://github.com/timirey/xapi-php/compare/2.0.1...3.0.0) - 2024-07-16

* Add minimum support of 8.3 PHP.
* Use `json_validate()` when parsing response.
-Add minimum support of 8.3 PHP.
-Use `json_validate()` when parsing response.

## [2.0.1](https://github.com/timirey/xapi-php/compare/2.0.0...2.0.1) - 2024-07-16

* See issue [#32](https://github.com/timirey/xapi-php/issues/32).
-See issue [#32](https://github.com/timirey/xapi-php/issues/32).

## [2.0.0](https://github.com/timirey/xapi-php/compare/1.2.1...2.0.0) - 2024-07-15

* Added stream commands.
* Removed WebSocket dependency (now it uses native php sockets).
-Added stream commands.
-Removed WebSocket dependency (now it uses native php sockets).

## [2.0.0-rc2](https://github.com/timirey/xapi-php/compare/2.0.0-rc...2.0.0-rc2) - 2024-07-15

* Removed WebSocket package dependency.
* Instead of WebSocket it uses [php sockets](https://www.php.net/manual/en/book.sockets.php).
* Updated tests.
-Removed WebSocket package dependency.
-Instead of WebSocket it uses [php sockets](https://www.php.net/manual/en/book.sockets.php).
-Updated tests.

## [2.0.0-rc](https://github.com/timirey/xapi-php/compare/2.0.0-alpha...2.0.0-rc) - 2024-07-09

* Added documentation for streaming commands.
* Updated code style and php doc blocks.
* Updated Pest tests to include more instance check-ups.
* Property names have changed to adapt standards.
-Added documentation for streaming commands.
-Updated code style and php doc blocks.
-Updated Pest tests to include more instance check-ups.
-Property names have changed to adapt standards.

## [2.0.0-alpha](https://github.com/timirey/xapi-php/compare/1.2.1...2.0.0-alpha) - 2024-07-08

* Added support for streaming communication.
* Added [streaming commands](http://developers.xstore.pro/documentation/#available-streaming-commands).
* Changed back from Pint to PHP CS.
* Backwards incompatible changes.
-Added support for streaming communication.
-Added [streaming commands](http://developers.xstore.pro/documentation/#available-streaming-commands).
-Changed back from Pint to PHP CS.
-Backwards incompatible changes.

## [1.2.1](https://github.com/timirey/xapi-php/compare/1.2.0...1.2.1) - 2024-07-04

* Fix DateTime objects in Pest tests.
-Fix DateTime objects in Pest tests.

## [1.2.0](https://github.com/timirey/xapi-php/compare/1.1.0...1.2.0) - 2024-07-04

* Add Pint with PSR12.
* Refactor Pest tests.
-Add Pint with PSR12.
-Refactor Pest tests.

## [1.1.0](https://github.com/timirey/xapi-php/compare/1.0.1...1.1.0) - 2024-07-02

* Add new exceptions and improved error handling.
-Add new exceptions and improved error handling.

## [1.0.1](https://github.com/timirey/xapi-php/compare/1.0.0...1.0.1) - 2024-07-02

* Some small changes to README.md and GitHub workflows.
-Some small changes to README.md and GitHub workflows.

## [1.0.0](https://github.com/timirey/xapi-php/compare/0.7.2.2...1.0.0) - 2024-07-02

* Add documentation.
* Add dev tools.
* Add non-streaming commands and tests.
* Add PHPDoc blocks on every property, method and class.
* Update folder structure.
* Add enums for host, payloads and responses.
-Add documentation.
-Add dev tools.
-Add non-streaming commands and tests.
-Add PHPDoc blocks on every property, method and class.
-Update folder structure.
-Add enums for host, payloads and responses.

## [1.0.0-rc.3](https://github.com/timirey/xapi-php/compare/1.0.0-rc.2...1.0.0-rc.3) - 2024-07-02

* Update CHANGELOG.md, composer.json and README.md.
-Update CHANGELOG.md, composer.json and README.md.

## [1.0.0-rc.2](https://github.com/timirey/xapi-php/compare/1.0.0-rc...1.0.0-rc.2) - 2024-07-02

* Adjusted README.md with documentation.
-Adjusted README.md with documentation.

## [1.0.0-rc](https://github.com/timirey/xapi-php/compare/1.0.0-beta...1.0.0-rc) - 2024-07-01

* Prepare for release.
-Prepare for release.

## [1.0.0-beta](https://github.com/timirey/xapi-php/compare/1.0.0-alpha.2...1.0.0-beta) - 2024-07-01

* Refactor adding DateTime instead of int.
* Updated tests.
-Refactor adding DateTime instead of int.
-Updated tests.

## [1.0.0-alpha.2](https://github.com/timirey/xapi-php/compare/1.0.0-alpha...1.0.0-alpha.2) - 2024-07-01

* Refactored types for enum usage.
-Refactored types for enum usage.

## [1.0.0-alpha](https://github.com/timirey/xapi-php/compare/0.7.2.2...1.0.0-alpha) - 2024-06-30

* Add main commands for non-stream web socket.
* Add test coverage (featuring mocks) for each command.
-Add main commands for non-stream web socket.
-Add test coverage (featuring mocks) for each command.

## [0.7.2.2](https://github.com/timirey/xapi-php/compare/0.7.2.1...0.7.2.2) - 2024-06-28

* Fix linter issues.
-Fix linter issues.

## [0.7.2.1](https://github.com/timirey/xapi-php/compare/0.7.2...0.7.2.1) - 2024-06-28

* Rename workflow.
-Rename workflow.

## [0.7.2](https://github.com/timirey/xapi-php/compare/0.7.1...0.7.2) - 2024-06-28

* Update .gitignore.
-Update .gitignore.

## [0.7.1](https://github.com/timirey/xapi-php/compare/0.7.0...0.7.1) - 2024-06-28

* Update .gitignore.
-Update .gitignore.

## [0.7.0](https://github.com/timirey/xapi-php/commits/0.7.0) - 2024-06-28

* Add default client.
* Add commands: login, logout, pint, tradeTransaction, tradeTransactionStatus, getAllSymbols, getSymbol.
* Add pest tests.
* Add php linter.
-Add default client.
-Add commands: login, logout, pint, tradeTransaction, tradeTransactionStatus, getAllSymbols, getSymbol.
-Add pest tests.
-Add php linter.
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ protected function subscribe(AbstractStreamPayload $payload, string $responseCla
*/
protected function ensureRequestConnection(): void
{
if (!isset($this->request) || !$this->request->isConnected()) {
if (!isset($this->request)) {
$this->connect();
}
}
Expand Down
28 changes: 19 additions & 9 deletions src/Connections/SocketConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public function __construct(protected string $address)
$this->open();
}

/**
* Destructor to close the socket.
*
* @throws SocketException If socket is not initialized.
*/
public function __destruct()
{
$this->close();
}

/**
* Opens the socket connection.
*
Expand All @@ -42,7 +52,7 @@ public function open(): bool
{
$this->socket = stream_socket_client($this->address, $errorCode, $errorMessage);

if ($this->socket === false) {
if (!$this->isConnected()) {
throw new SocketException("$errorCode: $errorMessage");
}

Expand All @@ -59,8 +69,8 @@ public function open(): bool
*/
public function send(string $payload): false|int
{
if ($this->socket === false) {
throw new SocketException('The socket is not initialized.');
if (!$this->isConnected()) {
throw new SocketException('The socket is not accepting messages.');
}

return fwrite($this->socket, $payload);
Expand All @@ -74,8 +84,8 @@ public function send(string $payload): false|int
*/
public function receive(): string
{
if ($this->socket === false) {
throw new SocketException('The socket is not initialized.');
if (!$this->isConnected()) {
throw new SocketException('The socket is not sending messages.');
}

$buffer = '';
Expand All @@ -101,8 +111,8 @@ public function receive(): string
*/
public function listen(): Generator
{
if ($this->socket === false) {
throw new SocketException('The socket is not initialized.');
if (!$this->isConnected()) {
throw new SocketException('The socket is not subscribable.');
}

while (!feof($this->socket)) {
Expand All @@ -124,8 +134,8 @@ public function listen(): Generator
*/
public function close(): bool
{
if ($this->socket === false) {
throw new SocketException('The socket is not initialized.');
if (!$this->isConnected()) {
throw new SocketException('The socket is already closed.');
}

return fclose($this->socket);
Expand Down