class BBS::Telnet::Parser
Telnet commands taken out of a byte stream, one byte at a time: a small state machine, shared by the boardβs own line (BBS::Telnet) and both directions of a BBS::Gateway::Link. Data bytes come out, a doubled IAC as one 255; each option command and subnegotiation goes to the block given to ::new.
parser = BBS::Telnet::Parser.new { |command, argument| p [command, argument] } parser.parse("hi\xFF\xFB\x01".b) # => "hi", and prints [251, 1]
The block gets (command, option) for WILL, WONT, DO and DONT, and (SB, bytes) for a subnegotiation, bytes an Array of Integer whose first is the option; a subnegotiation keeps at most MAX_SUBNEGOTIATION bytes, the rest is read and dropped. Other commands (NOP, GA β¦) are dropped.
Constants
- OPTION_COMMANDS
-
The commands followed by one option byte.
Public Class Methods
Source
# File lib/bbs/telnet/parser.rb, line 27 def initialize(&on_command) @on_command = on_command @state = :data end
Creates a parser in the data state; on_command is called as described above.
Public Instance Methods
Source
# File lib/bbs/telnet/parser.rb, line 34 def feed(byte) = send(@state, byte)
Takes the next byte (an Integer) of the stream. Returns it when it is data, nil when it belongs to a command.
Source
# File lib/bbs/telnet/parser.rb, line 37 def parse(bytes) = bytes.each_byte.filter_map { |byte| feed(byte) }.pack('C*')
Takes a String of bytes and returns its data bytes, a binary String.