Method: NATS#receive_data

Defined in:
lib/nats/client.rb

#receive_data(data) ⇒ Object

:nodoc:



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
# File 'lib/nats/client.rb', line 872

def receive_data(data) #:nodoc:
  @buf = @buf ? @buf << data : data

  while (@buf)
    case @parse_state
    when AWAITING_INFO_LINE
      case @buf
      when INFO
        @buf = $'
        process_connect_init($1)
      else
        # If we are here we do not have a complete line yet that we understand.
        return
      end
    when AWAITING_CONTROL_LINE
      case @buf
      when MSG
        @buf = $'
        @sub, @sid, @reply, @needed = $1, $2.to_i, $4, $5.to_i
        @parse_state = AWAITING_MSG_PAYLOAD
      when OK # No-op right now
        @buf = $'
      when ERR
        @buf = $'
        current = server_pool.first
        current[:error_received] = true
        if current[:auth_required] && !current[:auth_ok]
          err_cb.call(NATS::AuthError.new($1))
        else
          err_cb.call(NATS::ServerError.new($1))
        end
      when PING
        @pings += 1
        @buf = $'
        send_command(PONG_RESPONSE)
      when PONG
        @buf = $'
        cb = @pongs.shift
        cb.call if cb
      when INFO
        @buf = $'
        process_info($1)
      when UNKNOWN
        @buf = $'
        err_cb.call(NATS::ServerError.new("Unknown protocol: #{$1}"))
      else
        # If we are here we do not have a complete line yet that we understand.
        return
      end
      @buf = nil if (@buf && @buf.empty?)

    when AWAITING_MSG_PAYLOAD
      return unless (@needed && @buf.bytesize >= (@needed + CR_LF_SIZE))
      on_msg(@sub, @sid, @reply, @buf.slice(0, @needed))
      @buf = @buf.slice((@needed + CR_LF_SIZE), @buf.bytesize)
      @sub = @sid = @reply = @needed = nil
      @parse_state = AWAITING_CONTROL_LINE
      @buf = nil if (@buf && @buf.empty?)
    end
  end
end