Class: Ticketing::Protocol::FrameParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ticketing/protocol.rb

Overview

Incremental frame parser fed by socket chunks. The A fixed header (8-byte token) is consumed by count because it may contain 0x0a; over-long frames are skipped. 소켓 청크를 먹는 증분 파서 — A의 8바이트 토큰은 개수 기준(0x0a 포함 가능), 초과 프레임 스킵.

Instance Method Summary collapse

Constructor Details

#initializeFrameParser

Returns a new instance of FrameParser.



61
62
63
# File 'lib/ticketing/protocol.rb', line 61

def initialize
  @buf = +"".b
end

Instance Method Details

#feed(chunk) ⇒ Object



65
# File 'lib/ticketing/protocol.rb', line 65

def feed(chunk) = @buf << chunk

#next_frameObject

Pops the next complete frame (without \n), "" for keep-alive, or nil if incomplete. 다음 프레임 — keep-alive는 "", 미완성은 nil.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ticketing/protocol.rb', line 82

def next_frame
  loop do
    return nil if @buf.empty?

    if @buf.getbyte(0) == NL
      @buf = @buf.byteslice(1..)
      return "".b
    end
    fixed = @buf.getbyte(0) == OP_ACQUIRE ? 8 : 0
    nl = @buf.index("\n", 1 + fixed)
    return nil if nl.nil?

    frame = @buf.byteslice(0, nl)
    @buf = @buf.byteslice(nl + 1..)
    next if frame.bytesize > 1 + fixed + MAX_LINE_LEN # skip over-long. 초과 스킵.

    return frame
  end
end

#next_lineObject

Pops the handshake line without \n, or nil if incomplete. 핸드셰이크 한 줄.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ticketing/protocol.rb', line 68

def next_line
  nl = @buf.index("\n")
  if nl.nil?
    raise TicketError.new(:protocol, "handshake line too long") if @buf.bytesize > MAX_HANDSHAKE_LINE

    return nil
  end
  line = @buf.byteslice(0, nl)
  @buf = @buf.byteslice(nl + 1..)
  line
end