-
Notifications
You must be signed in to change notification settings - Fork 8
/
Curl.php
69 lines (54 loc) · 2.45 KB
/
Curl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
namespace libasynCurl;
use Closure;
use InvalidArgumentException;
use libasynCurl\thread\CurlDeleteTask;
use libasynCurl\thread\CurlGetTask;
use libasynCurl\thread\CurlPostTask;
use libasynCurl\thread\CurlPutTask;
use libasynCurl\thread\CurlThreadPool;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\ClosureTask;
class Curl
{
/** @var bool */
private static bool $registered = false;
/** @var CurlThreadPool */
private static CurlThreadPool $threadPool;
public static function register(PluginBase $plugin): void
{
if (self::isRegistered()) {
throw new InvalidArgumentException("{$plugin->getName()} attempted to register " . self::class . " twice.");
}
$server = $plugin->getServer();
self::$threadPool = new CurlThreadPool(CurlThreadPool::POOL_SIZE, CurlThreadPool::MEMORY_LIMIT, $server->getLoader(), $server->getLogger(), $server->getTickSleeper());
$plugin->getScheduler()->scheduleRepeatingTask(new ClosureTask(function (): void {
self::$threadPool->collectTasks();
}), CurlThreadPool::COLLECT_INTERVAL);
$plugin->getScheduler()->scheduleRepeatingTask(new ClosureTask(function (): void {
self::$threadPool->triggerGarbageCollector();
}), CurlThreadPool::GARBAGE_COLLECT_INTERVAL);
self::$registered = true;
}
public static function isRegistered(): bool
{
return self::$registered;
}
public static function postRequest(string $page, array|string $args, int $timeout = 10, array $headers = [], Closure $closure = null): void
{
self::$threadPool->submitTask(new CurlPostTask($page, $args, $timeout, $headers, $closure));
}
public static function putRequest(string $page, array|string $args, int $timeout = 10, array $headers = [], Closure $closure = null): void
{
self::$threadPool->submitTask(new CurlPutTask($page, $args, $timeout, $headers, $closure));
}
public static function deleteRequest(string $page, array|string $args, int $timeout = 10, array $headers = [], Closure $closure = null): void
{
self::$threadPool->submitTask(new CurlDeleteTask($page, $args, $timeout, $headers, $closure));
}
public static function getRequest(string $page, int $timeout = 10, array $headers = [], Closure $closure = null): void
{
self::$threadPool->submitTask(new CurlGetTask($page, $timeout, $headers, $closure));
}
}