Completed
Push — class-eventloop ( 1db9ec...0a637a )
by Vasily
04:08
created

EventLoop::bufferEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace PHPDaemon\Core;
3
use PHPDaemon\Structures\StackCallbacks;
4
5
class EventLoop
6
{
7
    protected $base;
8
    protected $dnsBase;
9
    protected $callbacks;
10
    protected $stopped = true;
11
12
    public function __construct() {
13
        $this->base = new \EventBase;
14
        $this->callbacks = new StackCallbacks;
15
        $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...
16
    }
17
18
    public function getBase() {
19
        return $this->base;
20
    }
21
22
    public function getDnsBase() {
23
        return $this->dnsBase;
24
    }
25
26
    public function reinit() {
27
        $this->base->reinit();
28
        return $this;
29
    }
30
31
    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...
32
        return \Event::signal($this->base, ...$args);
33
    }
34
35
    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...
36
        return \Event::timer($this->base, ...$args);
37
    }
38
39
    public function listener(...$args) {
40
        return new \EventListener($this->base, ...$args);
41
    }
42
43
    public function bufferEvent(...$args) {
44
        return new \EventBufferEvent($this->base, ...$args);
45
    }
46
47
    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...
48
        return \EventBufferEvent::sslSocket($this->base, ...$args);
49
    }
50
51
    public function interrupt($cb = null) {
52
        if ($cb !== null) {
53
            $this->callbacks->push($cb);
54
        }
55
        $this->base->exit();
56
    }
57
58
    public function stop() {
59
        $this->stopped = true;
60
        $this->interrupt();
61
    }
62
63
    public function event(...$args) {
64
        return new \Event($this->base, ...$args);
65
    }
66
67
    public function run() {
68
        $this->stopped = false;
69
        while (!$this->stopped) {
70
            $this->callbacks->executeAll($this);
71
            if (!$this->base->dispatch()) {
72
                break;
73
            }
74
        }
75
    }
76
}