-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
143 additions
and
73 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,113 @@ | ||
<?php | ||
|
||
namespace Amp\Socket; | ||
|
||
final class SocketAddress | ||
{ | ||
/** @var string */ | ||
private $host; | ||
|
||
/** @var int|null */ | ||
private $port; | ||
|
||
/** | ||
* @param resource $resource | ||
* | ||
* @return self | ||
*/ | ||
public static function fromPeerResource($resource): self | ||
{ | ||
$name = @\stream_socket_get_name($resource, true); | ||
|
||
if ($name === false || $name === "\0") { | ||
return self::fromLocalResource($resource); | ||
} | ||
|
||
return self::fromSocketName($name); | ||
} | ||
|
||
/** | ||
* @param resource $resource | ||
* | ||
* @return self | ||
*/ | ||
public static function fromLocalResource($resource): self | ||
{ | ||
$wantPeer = false; | ||
|
||
do { | ||
$name = @\stream_socket_get_name($resource, $wantPeer); | ||
|
||
if ($name !== false && $name !== "\0") { | ||
return self::fromSocketName($name); | ||
} | ||
} while ($wantPeer = !$wantPeer); | ||
|
||
return new self(''); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return SocketAddress|null | ||
*/ | ||
public static function fromSocketName(string $name): self | ||
{ | ||
if ($portStartPos = \strrpos($name, ':')) { | ||
$host = \substr($name, 0, $portStartPos); | ||
$port = (int) \substr($name, $portStartPos + 1); | ||
return new self($host, $port); | ||
} | ||
|
||
return new self($name); | ||
} | ||
|
||
/** | ||
* @param string $host | ||
* @param int|null $port | ||
*/ | ||
public function __construct(string $host, ?int $port = null) | ||
{ | ||
if ($port !== null && ($port < 1 || $port > 65535)) { | ||
throw new \Error('Port number must be null or an integer between 1 and 65535'); | ||
} | ||
|
||
if (\strrpos($host, ':')) { | ||
$host = \trim($host, '[]'); | ||
} | ||
|
||
$this->host = $host; | ||
$this->port = $port; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getHost(): string | ||
{ | ||
return $this->host; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getPort(): ?int | ||
{ | ||
return $this->port; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
$host = $this->host; | ||
|
||
if (\strrpos($host, ':')) { | ||
$host = '[' . $host . ']'; | ||
} | ||
|
||
if ($this->port === null) { | ||
return $host; | ||
} | ||
|
||
return $host . ':' . $this->port; | ||
} | ||
} |
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