Server::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 7
rs 10
1
<?php
2
/**
3
 * RPC server instance
4
 * User: moyo
5
 * Date: 2018/5/25
6
 * Time: 2:31 PM
7
 */
8
9
namespace Carno\HRPC;
10
11
use Carno\HTTP\Server as HServer;
12
use Carno\Net\Address;
13
use Carno\Net\Contracts\Conn;
14
use Carno\Net\Events\HTTP;
15
use Carno\Net\Events\Worker;
16
use Carno\RPC\Server as RServer;
17
use Carno\Serving\Chips\Boots;
18
use Carno\Serving\Chips\Events;
19
use Carno\Serving\Chips\Plugins;
20
use Carno\Serving\Chips\Wants;
21
22
class Server
23
{
24
    use Events;
25
    use Boots;
26
    use Wants;
27
    use Plugins;
28
29
    /**
30
     * @var string
31
     */
32
    private $name = null;
33
34
    /**
35
     * @var Address
36
     */
37
    private $listen = null;
38
39
    /**
40
     * HRPC constructor.
41
     * @param string $name
42
     * @param Address $listen
43
     */
44
    public function __construct(string $name, Address $listen)
45
    {
46
        $this->name = $name;
47
        $this->listen = $listen;
48
49
        $this->events()->attach(Worker::STARTED, static function (Conn $ctx) {
50
            $ctx->events()->attach(HTTP::REQUESTING, RServer::layers()->handler());
51
        });
52
    }
53
54
    /**
55
     * @param int $workers
56
     */
57
    public function run(int $workers) : void
58
    {
59
        HServer::listen($this->listen, $this->events(), $workers, $this->name)->serve();
60
    }
61
}
62