neo4j-php / Bolt

PHP library to provide connectivity to graph database over TCP socket with Bolt specification
https://packagist.org/packages/stefanak-michal/bolt
MIT License
73 stars 10 forks source link

Refactor reading from connection #86

Closed stefanak-michal closed 2 years ago

stefanak-michal commented 2 years ago

If you have problem with receiving data from server. Can it happen you will get only a part of chunk? what about something like this?

$msg = '';
while (true) {
  $header = $this->connection->read(2);
  $length = unpack('n', $header)[1] ?? 0; //because 0x00 0x00 end mark is chunk of size 0

  if ($length > 0) {
    $chunk = '';
    $size = mb_strlen($chunk, '8bit');
    while ($size < $length) {
      $chunk .= $this->connection->read($length - $size);
    }
    $msg .= $chunk;
  } else {
    break;
  }
}

not tested.. written in this comment.

Originally posted by @stefanak-michal in https://github.com/neo4j-php/Bolt/issues/85#issuecomment-1057084967

Also you should read header until you readed length 2.

stefanak-michal commented 2 years ago

@transistive I added you to make you notified about updates

stefanak-michal commented 2 years ago

I've made some changes in StreamSocket. I believe you are using this connection class. It's about reading until requested length. In this way it's in Socket. Maybe it will help with this issue.

https://github.com/neo4j-php/Bolt/blob/eba79bfa755d6ee3ae47402f300794bda8d4cd48/src/connection/StreamSocket.php#L109-L127

Same thing I have applied to write.