Skip to content

Commit c23091b

Browse files
committed
Added cheks to avoid socket* calls to boolean
1 parent 4a72cc6 commit c23091b

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"require": {
1010
"php": ">=7.1",
11+
"ext-sockets": "*",
1112
"apache/thrift": "0.11.*",
1213
"psr/log": "^1.0.2"
1314
},

src/Transport/TUDPTransport.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ public function isOpen(): bool
2929
return true;
3030
}
3131

32-
public function open()
32+
public function open(): void
3333
{
3434
}
3535

36-
public function close()
36+
public function close(): void
3737
{
3838
if (null === $this->socket) {
3939
return;
4040
}
41-
4241
\socket_close($this->socket);
4342
$this->socket = null;
4443
}
@@ -48,28 +47,29 @@ public function read($len): string
4847
return '';
4948
}
5049

51-
public function write($buf)
50+
public function write($buf): void
5251
{
5352
$this->buffer .= $buf;
5453
}
5554

56-
public function flush()
55+
public function flush(): void
5756
{
57+
parent::flush();
5858
if ('' === $this->buffer) {
5959
return;
6060
}
61-
6261
$this->doWrite($this->buffer);
6362
$this->buffer = '';
6463
}
6564

66-
private function doWrite($buf)
65+
private function doWrite($buf): void
6766
{
68-
$socket = $this->getConnectedSocket();
69-
67+
if (null === ($socket = $this->connect())) {
68+
return;
69+
}
7070
$length = \strlen($buf);
7171
while (true) {
72-
if (false === $result = @\socket_write($socket, $buf)) {
72+
if (false === ($result = @\socket_write($socket, $buf))) {
7373
break;
7474
}
7575
if ($result >= $length) {
@@ -80,16 +80,19 @@ private function doWrite($buf)
8080
}
8181
}
8282

83-
private function getConnectedSocket()
83+
private function connect()
8484
{
85-
if (null === $this->socket) {
86-
if (false !== $socket = \socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
85+
$count = 0;
86+
while (false === \is_resource($this->socket) && $count < 5) {
87+
if (false !== ($socket = \socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))) {
8788
@\socket_connect($socket, $this->host, $this->port);
89+
$this->socket = $socket;
90+
break;
8891
}
89-
90-
$this->socket = $socket;
92+
$count++;
93+
usleep(10);
9194
}
9295

93-
return $this->socket;
96+
return $this->socket ?: null;
9497
}
9598
}

0 commit comments

Comments
 (0)