Completed
Pull Request — master (#251)
by Vasily
10:22 queued 06:24
created

EventLoop::bufferEventSsl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
namespace PHPDaemon\Core;
3
4
use PHPDaemon\Structures\StackCallbacks;
5
6
class EventLoop
7
{
8
    public static $instance;
9
    protected $base;
10
    protected $dnsBase;
11
    protected $callbacks;
12
    protected $stopped = true;
13
14
15
    public static function init()
16
    {
17
        if (self::$instance !== null) {
18
            self::$instance->reinit();
19
        } else {
20
            self::$instance = new static;
21
        }
22
    }
23
24
    public function __construct()
25
    {
26
        $this->base = new \EventBase;
27
        $this->callbacks = new StackCallbacks;
28
        $this->dnsBase = new \EventDnsBase($this->base, false); // @TODO: test with true
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
29
    }
30
31
    public function getBase()
32
    {
33
        return $this->base;
34
    }
35
36
    public function getDnsBase()
37
    {
38
        return $this->dnsBase;
39
    }
40
41
    public function reinit()
42
    {
43
        $this->base->reinit();
44
    }
45
46
    public function signal(...$args)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
47
    {
48
        return \Event::signal($this->base, ...$args);
49
    }
50
51
    public function timer(...$args)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
52
    {
53
        return \Event::timer($this->base, ...$args);
54
    }
55
56
    public function listener(...$args)
57
    {
58
        return new \EventListener($this->base, ...$args);
59
    }
60
61
    public function bufferEvent(...$args)
62
    {
63
        return new \EventBufferEvent($this->base, ...$args);
64
    }
65
66
    public function bufferEventSsl(...$args)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
67
    {
68
        return \EventBufferEvent::sslSocket($this->base, ...$args);
69
    }
70
71
    public function interrupt($cb = null)
72
    {
73
        if ($cb !== null) {
74
            $this->callbacks->push($cb);
75
        }
76
        $this->base->exit();
77
    }
78
79
    public function stop()
80
    {
81
        $this->stopped = true;
82
        $this->interrupt();
83
    }
84
85
    public function event(...$args)
86
    {
87
        return new \Event($this->base, ...$args);
88
    }
89
90
    public function run()
91
    {
92
        $this->stopped = false;
93
        while (!$this->stopped) {
94
            $this->callbacks->executeAll($this);
95
            if (!$this->base->dispatch()) {
96
                break;
97
            }
98
        }
99
    }
100
}