-
Notifications
You must be signed in to change notification settings - Fork 2
Connection: keep-alive #22
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
39b4978
f8d1073
96bd585
c1df9a3
3d72b56
a0e907d
c3ae515
7268fda
b5e9746
22a1c80
ce0fd4e
47992b6
0386340
0051f70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php namespace peer\http; | ||
|
|
||
| use peer\SocketInputStream; | ||
|
|
||
| class Channel implements \io\streams\InputStream { | ||
| private $socket; | ||
| private $reuseable= null; | ||
|
|
||
| /** @param peer.Socket */ | ||
| public function __construct($socket) { | ||
| $this->socket= $socket; | ||
| } | ||
|
|
||
| /** @return peer.Socket */ | ||
| public function socket() { return $this->socket; } | ||
|
|
||
| /** | ||
| * Rebinds to a new socket, closing the existing one if necessary | ||
| * | ||
| * @param peer.Socket | ||
| * @return void | ||
| */ | ||
| public function bind($socket) { | ||
| if ($this->socket->isConnected()) { | ||
| $this->socket->close(); | ||
| } | ||
| $this->socket= $socket; | ||
| } | ||
|
|
||
| /** | ||
| * Connect (if necessary) | ||
| * | ||
| * @param float $connectTimeout | ||
| * @param float $readTimeout | ||
| * @return void | ||
| */ | ||
| public function connect($connectTimeout, $readTimeout) { | ||
| if (false === $this->reuseable) { | ||
| $this->socket->close(); | ||
| } else if ($this->socket->isConnected()) { | ||
|
||
| return; | ||
| } | ||
|
|
||
| $this->socket->setTimeout($readTimeout); | ||
| $this->socket->connect($connectTimeout); | ||
| } | ||
|
|
||
| /** | ||
| * Disconnect (if necessary) | ||
| * | ||
| * @return void | ||
| */ | ||
| public function disconnect() { | ||
| $this->socket->isConnected() && $this->socket->close(); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a request and returns the response | ||
| * | ||
| * @param peer.http.HttpRequest $request | ||
| * @return peer.http.HttpResponse | ||
| */ | ||
| public function send($request) { | ||
| $this->socket->write($request->getRequestString()); | ||
| $this->reuseable= false; | ||
| return new HttpResponse($this, true, function() { $this->reuseable= true; }); | ||
| } | ||
|
|
||
| /** @return int */ | ||
| public function available() { | ||
| return $this->socket->eof() ? 0 : 1; | ||
| } | ||
|
|
||
| /** @return string */ | ||
| public function read($limit= 8192) { | ||
| return $this->socket->readBinary($limit); | ||
| } | ||
|
|
||
| /** @return void */ | ||
| public function close() { | ||
| // NOOP | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class needs some tests!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script used for integration testing timeouts and reconnection behaviors: