|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Seasx\SeasLogger\Kafka\Socket; |
|
5
|
|
|
|
|
6
|
|
|
use Co; |
|
7
|
|
|
use Co\Socket; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class SocketIO |
|
12
|
|
|
* @package Seasx\SeasLogger\Kafka\Socket |
|
13
|
|
|
*/ |
|
14
|
|
|
class SocketIO |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var bool */ |
|
17
|
|
|
private $recv = false; |
|
18
|
|
|
/** @var Socket */ |
|
19
|
|
|
private $connection; |
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
private $config = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $data |
|
25
|
|
|
* @param float $timeout |
|
26
|
|
|
* @return int |
|
27
|
|
|
* @throws Exception |
|
28
|
|
|
*/ |
|
29
|
|
|
public function send(string $data, float $timeout = -1): int |
|
30
|
|
|
{ |
|
31
|
|
|
$ln = strlen($data); |
|
32
|
|
|
while ($data && $ln > 0) { |
|
33
|
|
|
$result = $this->connection->sendAll($data, $timeout); |
|
|
|
|
|
|
34
|
|
|
if (!is_int($result)) { |
|
35
|
|
|
$this->reconnect(); |
|
36
|
|
|
} |
|
37
|
|
|
$data = substr($data, $result); |
|
38
|
|
|
} |
|
39
|
|
|
$this->recv = false; |
|
40
|
|
|
return $ln; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @throws Exception |
|
45
|
|
|
*/ |
|
46
|
|
|
public function reconnect(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->createConnection(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $config |
|
53
|
|
|
* @throws Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
public function createConnection(array $config = []): void |
|
56
|
|
|
{ |
|
57
|
|
|
!empty($config) && ($this->config = $config); |
|
58
|
|
|
$client = new Socket(AF_INET, SOCK_STREAM, 0); |
|
59
|
|
|
list($host, $port) = explode(':', $this->config['uri']); |
|
60
|
|
|
$maxRetry = $this->config['retry']; |
|
61
|
|
|
$reconnectCount = 0; |
|
62
|
|
|
while (true) { |
|
63
|
|
|
$isConnect = $this->config['timeout'] ? $client->connect($host, (int)$port, |
|
|
|
|
|
|
64
|
|
|
$this->config['timeout']) : $client->connect($host, (int)$port); |
|
|
|
|
|
|
65
|
|
|
if (!$isConnect) { |
|
66
|
|
|
$reconnectCount++; |
|
67
|
|
|
if ($maxRetry > 0 && $reconnectCount >= $maxRetry) { |
|
68
|
|
|
$error = sprintf('Service connect fail error=%s host=%s port=%s', socket_strerror($client->errCode), |
|
69
|
|
|
$host, $port); |
|
70
|
|
|
throw new Exception($error); |
|
71
|
|
|
} |
|
72
|
|
|
Co::sleep($this->config['sleep']); |
|
73
|
|
|
} else { |
|
74
|
|
|
break; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
$this->connection = $client; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param int $length |
|
82
|
|
|
* @param float $timeout |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function recv(int $length = 65535, float $timeout = -1): string |
|
86
|
|
|
{ |
|
87
|
|
|
$data = $this->connection->recvAll($length, $timeout); |
|
|
|
|
|
|
88
|
|
|
return $data; |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function check(): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->connection->errCode === 0; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
public function close(): bool |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->connection->close(); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.