|
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\Socket\Io; |
|
11
|
|
|
|
|
12
|
|
|
use AsyncSockets\Exception\NetworkSocketException; |
|
13
|
|
|
use AsyncSockets\Socket\SocketInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class AbstractIo |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractIo implements IoInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Socket buffer size |
|
22
|
|
|
*/ |
|
23
|
|
|
const SOCKET_BUFFER_SIZE = 8192; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Amount of attempts to set data |
|
27
|
|
|
*/ |
|
28
|
|
|
const IO_ATTEMPTS = 10; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Socket |
|
32
|
|
|
* |
|
33
|
|
|
* @var SocketInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $socket; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* AbstractIo constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param SocketInterface $socket Socket object |
|
41
|
|
|
*/ |
|
42
|
183 |
|
public function __construct(SocketInterface $socket) |
|
43
|
|
|
{ |
|
44
|
183 |
|
$this->socket = $socket; |
|
45
|
183 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Throw network operation exception |
|
49
|
|
|
* |
|
50
|
|
|
* @param bool $condition Condition, which must evaluates to true for throwing exception |
|
51
|
|
|
* @param string $message Exception message |
|
52
|
|
|
* @param bool $includeLastError Flag whether to include php error message |
|
53
|
|
|
* |
|
54
|
|
|
* @return void |
|
55
|
|
|
* @throws NetworkSocketException |
|
56
|
|
|
*/ |
|
57
|
39 |
|
protected function throwNetworkSocketExceptionIf($condition, $message, $includeLastError = false) |
|
58
|
|
|
{ |
|
59
|
39 |
|
if ($condition) { |
|
60
|
10 |
|
$lastError = $includeLastError ? error_get_last() : null; |
|
61
|
10 |
|
if ($lastError) { |
|
62
|
|
|
$phpMessage = explode(':', $lastError['message'], 2); |
|
63
|
|
|
$phpMessage = trim(trim(end($phpMessage)), '.') . '.'; |
|
64
|
|
|
$message .= ' ' . $phpMessage; |
|
65
|
|
|
} |
|
66
|
10 |
|
throw new NetworkSocketException($this->socket, $message); |
|
67
|
|
|
} |
|
68
|
33 |
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|