-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
28 changed files
with
1,533 additions
and
657 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,19 @@ | ||
<?php // basic server | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Amp\Loop; | ||
use Amp\Socket\ServerSocket; | ||
|
||
Loop::run(function () { | ||
$server = Amp\Socket\listen("tcp://127.0.0.1:0", function (ServerSocket $socket) { | ||
list($ip, $port) = explode(":", $socket->getRemoteAddress()); | ||
|
||
$body = "Hey, your IP is {$ip} and your local port used is {$port}."; | ||
$bodyLength = \strlen($body); | ||
|
||
yield $socket->end("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: {$bodyLength}\r\n\r\n{$body}"); | ||
}); | ||
|
||
echo "Listening for new connections on " . $server->getAddress() . " ...\n"; | ||
}); |
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,64 @@ | ||
<?php | ||
|
||
namespace Amp\Socket; | ||
|
||
use function Amp\Socket\Internal\normalizeBindToOption; | ||
|
||
final class ClientConnectContext { | ||
private $bindTo = null; | ||
private $connectTimeout = 10000; | ||
private $maxAttempts = 2; | ||
|
||
public function withBindTo(string $bindTo = null): self { | ||
$bindTo = normalizeBindToOption($bindTo); | ||
|
||
$clone = clone $this; | ||
$clone->bindTo = $bindTo; | ||
|
||
return $clone; | ||
} | ||
|
||
public function getBindTo() { | ||
return $this->bindTo; | ||
} | ||
|
||
public function withConnectTimeout(int $timeout): self { | ||
if ($timeout <= 0) { | ||
throw new \Error("Invalid connect timeout ({$timeout}), must be greater than 0"); | ||
} | ||
|
||
$clone = clone $this; | ||
$clone->connectTimeout = $timeout; | ||
|
||
return $clone; | ||
} | ||
|
||
public function getConnectTimeout(): int { | ||
return $this->connectTimeout; | ||
} | ||
|
||
public function withMaxAttempts(int $maxAttempts): self { | ||
if ($maxAttempts <= 0) { | ||
throw new \Error("Invalid max attempts ({$maxAttempts}), must be greater than 0"); | ||
} | ||
|
||
$clone = clone $this; | ||
$clone->maxAttempts = $maxAttempts; | ||
|
||
return $clone; | ||
} | ||
|
||
public function getMaxAttempts(): int { | ||
return $this->maxAttempts; | ||
} | ||
|
||
public function toStreamContextArray(): array { | ||
$options = []; | ||
|
||
if ($this->bindTo !== null) { | ||
$options["bindto"] = $this->bindTo; | ||
} | ||
|
||
return ["socket" => $options]; | ||
} | ||
} |
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
Oops, something went wrong.