adding support for length prefixed json on the client socket

As @portertech & I agreed on IRC, I am adding support for length prefixed JSON to the client socket while keeping the existing behavior as it is.

Thanks,
praddy
It will fail because the JSON would fail to parse as we are removing any
extra bytes. This is reported in the logs and the client gets the 'invalid'
message.

Regarding data format prefix notation, not sure whether it makes sense here
as I think JSON is a pretty generic data format and I don't think there
would be a genuine need to support more. I could be wrong, do you know of a
use case? And the checksum, I personally think, is good for data integrity
check but is an overhead in this use case because these listeners (udp &
tcp) are on the loopback interface where we don't expect the data to get
corrupted or truncated, unless you overrun your socket buffers, in which
case (& for most of cases) your JSON won't parse (or length won't match)
and would result in an error. The same would happen in case of a checksum.
On Jul 15, 2014 7:10 AM, "Jesse Kempf" <notifications@github.com> wrote:

> In lib/sensu/socket.rb:
>
> > +        if @first_packet
> > +          if data =~ /^(\d+)\n/n
> > +            @expected_data_len = $1.to_i
> > +            @logger.debug("expecting data of length #{@expected_data_len}")
> > +            data = data[data.index("\n")+1..-1]
> > +          end
> > +          @first_packet = false
> > +        end
> > +        if @expected_data_len and @data.length < @expected_data_len
> > +          @data += data
> > +          # Check whether this last packet completes the data
> > +          if @data.length < @expected_data_len
> > +            @logger.debug("expecting #{@expected_data_len - @data.length} bytes more")
> > +            return
> > +          end
> > +          # Remove any extra bytes the client might have sent
>
> Shouldn't it be a fatal error if the client says "I'm about to send you 10
> bytes." and then goes ahead and sends 17?
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/sensu/sensu/pull/752/files#r14913960>.
>