Skip to content
This repository was archived by the owner on Sep 20, 2021. It is now read-only.

Commit 5454248

Browse files
committed
fix(node remote address) Handle correctly the node socket return path
1 parent 38ef12b commit 5454248

3 files changed

Lines changed: 19 additions & 20 deletions

File tree

Source/Connection/Connection.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ abstract class Connection extends Stream implements Stream\IStream\In, Stream\IS
114114
*/
115115
protected $_remote = false;
116116

117-
/**
118-
* Remote address.
119-
*/
120-
protected $_remoteAddress = null;
121-
122117
/**
123118
* Temporize selected connections when selecting.
124119
*/
@@ -284,7 +279,8 @@ public function quietAndMute(): bool
284279
*/
285280
public function disconnect(): void
286281
{
287-
$this->_disconnect = $this->close();
282+
$this->close();
283+
$this->_disconnect = true;
288284
}
289285

290286
/**
@@ -522,7 +518,11 @@ public function isRemoteAddressConsidered(): bool
522518
*/
523519
public function getRemoteAddress(): string
524520
{
525-
return $this->_remoteAddress;
521+
if (null === $this->_node) {
522+
return null;
523+
}
524+
525+
return $this->_node->getPeerName();
526526
}
527527

528528
/**
@@ -556,15 +556,7 @@ public function read(int $length, int $flags = 0)
556556
return stream_socket_recvfrom($this->getStream(), $length, $flags);
557557
}
558558

559-
$out = stream_socket_recvfrom(
560-
$this->getStream(),
561-
$length,
562-
$flags,
563-
$address
564-
);
565-
$this->_remoteAddress = !empty($address) ? $address : null;
566-
567-
return $out;
559+
return stream_socket_recvfrom($this->getStream(), $length, $flags);
568560
}
569561

570562
/**

Source/Node.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,12 @@ public function getEncryptionType(): ?int
121121
{
122122
return $this->_encryptionType;
123123
}
124+
125+
/**
126+
* Get peer connection name.
127+
*/
128+
public function getPeerName(bool $wantPeer = true): string
129+
{
130+
return stream_socket_get_name($this->getSocket(), $wantPeer);
131+
}
124132
}

Test/Unit/Connection/Connection.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,8 @@ public function case_read(): void
693693

694694
$this->calling($connection)->getStream = $stream,
695695
$this->calling($connection)->isEncrypted = false,
696-
$this->function->stream_socket_recvfrom = function ($_stream, $_length, $_flags, &$address) use ($self, &$called, $stream, $length, $flags) {
696+
$this->function->stream_socket_recvfrom = function ($_stream, $_length, $_flags) use ($self, &$called, $stream, $length, $flags) {
697697
$called = true;
698-
$address = '1.2.3.4';
699698

700699
$self
701700
->string($_stream)
@@ -714,8 +713,8 @@ public function case_read(): void
714713
->isEqualTo('hello')
715714
->boolean($called)
716715
->isTrue()
717-
->string($connection->getRemoteAddress())
718-
->isEqualTo('1.2.3.4');
716+
->variable($connection->getRemoteAddress())
717+
->isNull();
719718
}
720719

721720
public function case_read_string(): void

0 commit comments

Comments
 (0)