phpnats icon indicating copy to clipboard operation
phpnats copied to clipboard

\Nats\Connection::handleMSG doesnt read trailing CR+LF

Open VRciF opened this issue 9 years ago • 5 comments

As the documentation states, a MSG has a CR+LF after the payload: MSG [reply-to] <#bytes>\r\n[payload]\r\n

The handleMSG method only reads the payload and leaves the trailing \r\n in the socket buffer. I understand that this is not much of a problem, because Connection::wait just ignores such empty lines, but in my case this behaviour causes a big problem.

I'm using nats as a platform to deliver messages TO and FROM a websocket connection. This means i use stream_socket_select to listen for incoming websocket messages and concurrently for incoming nats messages by abusing streamSocket which i extracted out of the Connection class using Reflection since streamSocket is private. Now the problem is, that since the newline is left in the buffer my stream_socket_select notices that a messages is waiting. Thus my call to wait(1) blocks indefinitely because it only reads those \r\n and waits for the message. This also means that my websocket connection is blocked.

So i would really appreciate it, if you could add something like $this->receive(2); in line 387 of \Nats\Connection to have correct protocol parsing. Many thanks in advance!

VRciF avatar Jun 11 '16 21:06 VRciF

Hi @VRciF, I've discovered a bug when doing the fread call, where it doesn't correctly use a sensible chunk size. I've a fix in the works and have a feeling it will fix your issue too. See #59

byrnedo avatar Jun 15 '16 11:06 byrnedo

Amazing! :muscle: Send me a PR and I will be happy to merge.

repejota avatar Jun 15 '16 11:06 repejota

Closing as it is solved on #59 Thanks all! 😄

repejota avatar Jun 15 '16 17:06 repejota

Hmm I'm not entirely sure that my fix for fread has solved this. The fread reads the whole message payload according to length in the header, presumably including the CR+LF. Then I strip the last 2 bytes.

So I guess that means the fget messages (whichever ones fall into that category) will not have those bytes read off the wire.

byrnedo avatar Jun 16 '16 08:06 byrnedo

Ok, I'm reopening this issue and i will try to look further.

repejota avatar Jun 16 '16 08:06 repejota