1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 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\Event; |
11
|
|
|
|
12
|
|
|
use AsyncSockets\RequestExecutor\RequestExecutorInterface; |
13
|
|
|
use AsyncSockets\Socket\SocketInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class TimeoutEvent |
17
|
|
|
*/ |
18
|
|
|
class TimeoutEvent extends Event |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Timeout during connection |
22
|
|
|
*/ |
23
|
|
|
const DURING_CONNECTION = 'connection'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Timeout during I/O operation |
27
|
|
|
*/ |
28
|
|
|
const DURING_IO = 'io'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Stage when occured timeout |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $when; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Flag whether we can enable one more I/O attempt for this socket |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $isNextAttemptEnabled = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* TimeoutEvent constructor. |
46
|
|
|
* |
47
|
|
|
* @param RequestExecutorInterface $executor Request executor object |
48
|
|
|
* @param SocketInterface $socket Socket for this request |
49
|
|
|
* @param mixed $context Any optional user data for event |
50
|
|
|
* @param string $when One of DURING_* constants |
51
|
|
|
*/ |
52
|
25 |
|
public function __construct(RequestExecutorInterface $executor, SocketInterface $socket, $context, $when) |
53
|
|
|
{ |
54
|
25 |
|
parent::__construct($executor, $socket, $context, EventType::TIMEOUT); |
55
|
25 |
|
$this->when = $when; |
56
|
25 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return stage when timeout occurred |
60
|
|
|
* |
61
|
|
|
* @return string one of DURING_* consts |
62
|
|
|
*/ |
63
|
3 |
|
public function when() |
64
|
|
|
{ |
65
|
3 |
|
return $this->when; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Mark operation for try-again |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
3 |
|
public function enableOneMoreAttempt() |
74
|
|
|
{ |
75
|
3 |
|
$this->isNextAttemptEnabled = true; |
76
|
3 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return true if we can try to connect / I/O once again |
80
|
|
|
* |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
14 |
|
public function isNextAttemptEnabled() |
84
|
|
|
{ |
85
|
14 |
|
return $this->isNextAttemptEnabled; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|