-
Notifications
You must be signed in to change notification settings - Fork 24
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 #82 from clue-labs/connection
Connections now resolve with a ConnectionInterface
- Loading branch information
Showing
12 changed files
with
382 additions
and
47 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,102 @@ | ||
<?php | ||
|
||
namespace React\SocketClient; | ||
|
||
use React\Stream\DuplexStreamInterface; | ||
|
||
/** | ||
* Any outgoing connection is represented by this interface, | ||
* such as a normal TCP/IP connection. | ||
* | ||
* An outgoing connection is a duplex stream (both readable and writable) that | ||
* implements React's | ||
* [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface). | ||
* It contains additional properties for the local and remote address | ||
* where this connection has been established to. | ||
* | ||
* Most commonly, instances implementing this `ConnectionInterface` are returned | ||
* by all classes implementing the [`ConnectorInterface`](#connectorinterface). | ||
* | ||
* > Note that this interface is only to be used to represent the client-side end | ||
* of an outgoing connection. | ||
* It MUST NOT be used to represent an incoming connection in a server-side context. | ||
* If you want to accept incoming connections, | ||
* use the [`Socket`](https://github.com/reactphp/socket) component instead. | ||
* | ||
* Because the `ConnectionInterface` implements the underlying | ||
* [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface) | ||
* you can use any of its events and methods as usual: | ||
* | ||
* ```php | ||
* $connection->on('data', function ($chunk) { | ||
* echo $data; | ||
* }); | ||
* | ||
* $conenction->on('close', function () { | ||
* echo 'closed'; | ||
* }); | ||
* | ||
* $connection->write($data); | ||
* $connection->end($data = null); | ||
* $connection->close(); | ||
* // … | ||
* ``` | ||
* | ||
* For more details, see the | ||
* [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface). | ||
* | ||
* @see DuplexStreamInterface | ||
* @see ConnectorInterface | ||
*/ | ||
interface ConnectionInterface extends DuplexStreamInterface | ||
{ | ||
/** | ||
* Returns the remote address (IP and port) where this connection has been established to | ||
* | ||
* ```php | ||
* $address = $connection->getRemoteAddress(); | ||
* echo 'Connected to ' . $address . PHP_EOL; | ||
* ``` | ||
* | ||
* If the remote address can not be determined or is unknown at this time (such as | ||
* after the connection has been closed), it MAY return a `NULL` value instead. | ||
* | ||
* Otherwise, it will return the full remote address as a string value. | ||
* If this is a TCP/IP based connection and you only want the remote IP, you may | ||
* use something like this: | ||
* | ||
* ```php | ||
* $address = $connection->getRemoteAddress(); | ||
* $ip = trim(parse_url('tcp://' . $address, PHP_URL_HOST), '[]'); | ||
* echo 'Connected to ' . $ip . PHP_EOL; | ||
* ``` | ||
* | ||
* @return ?string remote address (IP and port) or null if unknown | ||
*/ | ||
public function getRemoteAddress(); | ||
|
||
/** | ||
* Returns the full local address (IP and port) where this connection has been established from | ||
* | ||
* ```php | ||
* $address = $connection->getLocalAddress(); | ||
* echo 'Connected via ' . $address . PHP_EOL; | ||
* ``` | ||
* | ||
* If the local address can not be determined or is unknown at this time (such as | ||
* after the connection has been closed), it MAY return a `NULL` value instead. | ||
* | ||
* Otherwise, it will return the full local address as a string value. | ||
* | ||
* This method complements the [`getRemoteAddress()`](#getremoteaddress) method, | ||
* so they should not be confused. | ||
* | ||
* If your system has multiple interfaces (e.g. a WAN and a LAN interface), | ||
* you can use this method to find out which interface was actually | ||
* used for this connection. | ||
* | ||
* @return ?string local address (IP and port) or null if unknown | ||
* @see self::getRemoteAddress() | ||
*/ | ||
public function getLocalAddress(); | ||
} |
Oops, something went wrong.