Search code examples
rubyregexsocketscelluloid

Reading on socket with EOT, SOH, STX among other characters


I'm using Celluloid IO to read from sockets. The incoming message has the following syntax

sometextsometextsometext

where

SOH = Hex 1
FS  = Hex 1C
STX = Hex 2
ETX = Hex 3
EOT = Hex 4

My read code is something like this -

message = ""
begin
  data = socket.readpartial(4096)
  message << data
end until message =~ /not sure what goes here/

I'm looking for a reliable way to read from the socket until EOT. Once the message is read, i'll regex out the relevant sections.

Some guidance on detecting the above mentioned hex characters in socket read stream and in regex would be very helpful. Guidance?


Solution

  • And this does the trick for me thanks

    def parse(message)
      if message =~ /\001(.*)\01C(.*)\002(.*)\003\004/
        return ($1,$2,$3)
      end
    end
    
    def read_until_eot(socket)
      eot_found = false
      message = ''
      begin
        data = socket.read()
        eot_found = !!data['\004']
        message << data
      end until eot_found
      message.chomp!
    end
    
    def handle_connection(socket)
      # read from socket until EOT
      message = read_until_eot(socket) # <-- need help with
      if (origin,target,payload) = parse(message) #message can be parsed
        #   process message
        output_message = process(payload)
      end
      # write to socket
      socket.write output_message
      # close socket
      socket.close
    end