1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2016, Efimov Evgenij <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
namespace AsyncSockets\RequestExecutor\LibEvent; |
11
|
|
|
|
12
|
|
|
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class LeEvent |
16
|
|
|
*/ |
17
|
|
|
class LeEvent |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Libevent event handle |
21
|
|
|
* |
22
|
|
|
* @var resource |
23
|
|
|
*/ |
24
|
|
|
private $handle; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Callback object |
28
|
|
|
* |
29
|
|
|
* @var LeCallbackInterface |
30
|
|
|
*/ |
31
|
|
|
private $callback; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* RequestDescriptor |
35
|
|
|
* |
36
|
|
|
* @var RequestDescriptor |
37
|
|
|
*/ |
38
|
|
|
private $requestDescriptor; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Timeout for event |
42
|
|
|
* |
43
|
|
|
* @var int|null |
44
|
|
|
*/ |
45
|
|
|
private $timeout; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* LeEvent constructor. |
49
|
|
|
* |
50
|
|
|
* @param LeCallbackInterface $callback Callback object |
51
|
|
|
* @param RequestDescriptor $requestDescriptor Request descriptor object |
52
|
|
|
* @param int|null $timeout Timeout for event |
53
|
|
|
*/ |
54
|
51 |
|
public function __construct(LeCallbackInterface $callback, RequestDescriptor $requestDescriptor, $timeout) |
55
|
|
|
{ |
56
|
51 |
|
$this->handle = event_new(); |
57
|
51 |
|
$this->callback = $callback; |
58
|
51 |
|
$this->requestDescriptor = $requestDescriptor; |
59
|
51 |
|
$this->timeout = $timeout; |
60
|
51 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Destructor |
64
|
|
|
*/ |
65
|
51 |
|
public function __destruct() |
66
|
|
|
{ |
67
|
51 |
|
$this->destroy(); |
68
|
51 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return Timeout |
72
|
|
|
* |
73
|
|
|
* @return int|null |
74
|
|
|
*/ |
75
|
49 |
|
public function getTimeout() |
76
|
|
|
{ |
77
|
49 |
|
return $this->timeout; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return Handle |
82
|
|
|
* |
83
|
|
|
* @return resource |
84
|
|
|
*/ |
85
|
49 |
|
public function getHandle() |
86
|
|
|
{ |
87
|
49 |
|
return $this->handle; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return RequestDescriptor |
92
|
|
|
* |
93
|
|
|
* @return RequestDescriptor |
94
|
|
|
*/ |
95
|
49 |
|
public function getRequestDescriptor() |
96
|
|
|
{ |
97
|
49 |
|
return $this->requestDescriptor; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Fire event |
102
|
|
|
* |
103
|
|
|
* @param string $eventType Type of event, one of LeCallbackInterface::EVENT_* consts |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
49 |
|
public function fire($eventType) |
108
|
|
|
{ |
109
|
49 |
|
$this->callback->onEvent($this->requestDescriptor, $eventType); |
110
|
23 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Destroy event handle |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
51 |
|
private function destroy() |
118
|
|
|
{ |
119
|
51 |
|
if ($this->handle) { |
120
|
3 |
|
event_del($this->handle); |
121
|
3 |
|
event_free($this->handle); |
122
|
3 |
|
$this->handle = null; |
123
|
3 |
|
} |
124
|
51 |
|
} |
125
|
|
|
} |
126
|
|
|
|