-
Notifications
You must be signed in to change notification settings - Fork 550
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
27 changed files
with
376 additions
and
563 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 |
---|---|---|
|
@@ -2,29 +2,37 @@ | |
|
||
/** | ||
* This file is part of web3.php package. | ||
* | ||
* | ||
* (c) Kuan-Cheng,Lai <[email protected]> | ||
* | ||
* | ||
* @author Peter Lai <[email protected]> | ||
* @license MIT | ||
*/ | ||
|
||
namespace Web3\RequestManagers; | ||
namespace Web3\Providers; | ||
|
||
use InvalidArgumentException; | ||
use Psr\Http\Message\StreamInterface; | ||
use RuntimeException as RPCException; | ||
use Psr\Http\Message\ResponseInterface; | ||
use GuzzleHttp\Exception\RequestException; | ||
use React; | ||
use React\Async; | ||
use React\EventLoop\Loop; | ||
use React\Http\Browser; | ||
use React\Socket\Connector; | ||
use Web3\RequestManagers\RequestManager; | ||
use Web3\RequestManagers\IRequestManager; | ||
use Web3\Providers\Provider; | ||
use Web3\Providers\IProvider; | ||
|
||
class HttpAsyncRequestManager extends RequestManager implements IRequestManager | ||
class HttpAsyncProvider extends Provider implements IProvider | ||
{ | ||
/** | ||
* methods | ||
* | ||
* @var array | ||
*/ | ||
protected $methods = []; | ||
|
||
/** | ||
* client | ||
* | ||
|
@@ -48,6 +56,92 @@ public function __construct($host, $timeout = 1) | |
$this->client = (new Browser($connector, Loop::get()))->withRejectErrorResponse(false); | ||
} | ||
|
||
/** | ||
* close | ||
* | ||
* @return void | ||
*/ | ||
public function close() {} | ||
|
||
/** | ||
* send | ||
* | ||
* @param \Web3\Methods\Method $method | ||
* @param callable $callback | ||
* @return void | ||
*/ | ||
public function send($method, $callback) | ||
{ | ||
$payload = $method->toPayloadString(); | ||
|
||
if (!$this->isBatch) { | ||
$proxy = function ($err, $res) use ($method, $callback) { | ||
if ($err !== null) { | ||
return call_user_func($callback, $err, null); | ||
} | ||
if (!is_array($res)) { | ||
$res = $method->transform([$res], $method->outputFormatters); | ||
return call_user_func($callback, null, $res[0]); | ||
} | ||
$res = $method->transform($res, $method->outputFormatters); | ||
|
||
return call_user_func($callback, null, $res); | ||
}; | ||
return $this->sendPayload($payload, $proxy); | ||
} else { | ||
$this->methods[] = $method; | ||
$this->batch[] = $payload; | ||
} | ||
} | ||
|
||
/** | ||
* batch | ||
* | ||
* @param bool $status | ||
* @return void | ||
*/ | ||
public function batch($status) | ||
{ | ||
$status = is_bool($status); | ||
|
||
$this->isBatch = $status; | ||
} | ||
|
||
/** | ||
* execute | ||
* | ||
* @param callable $callback | ||
* @return void | ||
*/ | ||
public function execute($callback) | ||
{ | ||
if (!$this->isBatch) { | ||
throw new \RuntimeException('Please batch json rpc first.'); | ||
} | ||
$methods = $this->methods; | ||
$proxy = function ($err, $res) use ($methods, $callback) { | ||
if ($err !== null) { | ||
return call_user_func($callback, $err, null); | ||
} | ||
foreach ($methods as $key => $method) { | ||
if (isset($res[$key])) { | ||
if (!is_array($res[$key])) { | ||
$transformed = $method->transform([$res[$key]], $method->outputFormatters); | ||
$res[$key] = $transformed[0]; | ||
} else { | ||
$transformed = $method->transform($res[$key], $method->outputFormatters); | ||
$res[$key] = $transformed; | ||
} | ||
} | ||
} | ||
return call_user_func($callback, null, $res); | ||
}; | ||
$r = $this->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); | ||
$this->methods = []; | ||
$this->batch = []; | ||
return $r; | ||
} | ||
|
||
/** | ||
* sendPayload | ||
* | ||
|
@@ -122,4 +216,4 @@ public function sendPayload($payload, $callback) | |
|
||
return Async\coroutine($request); | ||
} | ||
} | ||
} |
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.