Sha256: bfdac259f0f5d26754704e7882193e48be3c9eeb6ffdf980c07ec2d5a971ca87

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

module Skylight
  module Worker
    class Connection
      FRAME_HDR_LEN = 8

      attr_reader :sock

      def initialize(sock)
        @sock = sock
        @len  = nil
        @buf  = ""
      end

      def read
        if msg = maybe_read_message
          return msg
        end

        if chunk = read_sock
          @buf << chunk

          if !@len && @buf.bytesize >= FRAME_HDR_LEN
            @len = read_len
          end

          maybe_read_message
        end
      end

    private

      def read_len
        if len = @buf[4, 4]
          len.unpack("L")[0]
        end
      end

      def read_message_id
        if win = @buf[0, 4]
          win.unpack("L")[0]
        end
      end

      def maybe_read_message
        if @len && @buf.bytesize >= @len + FRAME_HDR_LEN
          mid   = read_message_id
          klass = Messages::ID_TO_KLASS.fetch(mid) do
            raise IpcProtoError, "unknown message `#{mid}`"
          end
          data  = @buf[FRAME_HDR_LEN, @len]
          @buf  = @buf[(FRAME_HDR_LEN + @len)..-1] || ""

          if @buf.bytesize >= FRAME_HDR_LEN
            @len = read_len
          else
            @len = nil
          end

          begin
            return klass.deserialize(data)
          rescue Exception => e
            # reraise protobuf decoding exceptions
            raise IpcProtoError, e.message
          end
        end
      end

      def read_sock
        @sock.read_nonblock(CHUNK_SIZE)
      rescue Errno::EAGAIN, Errno::EWOULDBLOCK
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
skylight-0.3.7 lib/skylight/worker/connection.rb
skylight-0.3.6 lib/skylight/worker/connection.rb
skylight-0.3.3 lib/skylight/worker/connection.rb
skylight-0.3.2 lib/skylight/worker/connection.rb
skylight-0.3.1 lib/skylight/worker/connection.rb
skylight-0.3.0 lib/skylight/worker/connection.rb
skylight-0.3.0.rc.6 lib/skylight/worker/connection.rb
skylight-0.3.0.rc.5 lib/skylight/worker/connection.rb