Class: EventMachine::Protocols::LineAndTextProtocol

### Home Classes Methods

### In Files

- lib/em/protocols/line_and_text.rb

### Parent

EventMachine::Connection

### Methods

- ::new
- #receive_data
- #set_binary_mode
- #unbind

### Files

- ChangeLog
- DEFERRABLES
- EPOLL
- INSTALL
- KEYBOARD
- LEGAL
- LIGHTWEIGHT_CONCURRENCY
- PURE_RUBY
- RELEASE_NOTES
- SMTP
- SPAWNED_PROCESSES
- TODO

### Class/Module Index

Quicksearch

- EventMachine
- EventMachine
- EventMachine::Channel
- EventMachine::Completion
- EventMachine::Connection
- EventMachine::DNS
- EventMachine::DNS::Request
- EventMachine::DNS::RequestIdAlreadyUsed
- EventMachine::DNS::Resolver
- EventMachine::DNS::Socket
- EventMachine::DatagramObject
- EventMachine::DefaultDeferrable
- EventMachine::Deferrable
- EventMachine::DeferrableChildProcess
- EventMachine::Error
- EventMachine::EvmaKeyboard
- EventMachine::EvmaTCPClient
- EventMachine::EvmaTCPServer
- EventMachine::EvmaUDPSocket
- EventMachine::EvmaUNIXClient
- EventMachine::EvmaUNIXServer
- EventMachine::FileNotFoundException
- EventMachine::FileStreamer
- EventMachine::FileWatch
- EventMachine::Iterator
- EventMachine::LoopbreakReader
- EventMachine::PeriodicTimer
- EventMachine::Pool
- EventMachine::ProcessWatch
- EventMachine::Protocols
- EventMachine::Protocols
- EventMachine::Protocols::HeaderAndContentProtocol
- EventMachine::Protocols::HttpClient
- EventMachine::Protocols::HttpClient2
- EventMachine::Protocols::HttpClient2::Request
- EventMachine::Protocols::LineAndTextProtocol
- EventMachine::Protocols::LineProtocol
- EventMachine::Protocols::LineText2
- EventMachine::Protocols::Memcache
- EventMachine::Protocols::Memcache::ParserError
- EventMachine::Protocols::ObjectProtocol
- EventMachine::Protocols::Postgres3
- EventMachine::Protocols::SASLauth
- EventMachine::Protocols::SASLauthclient
- EventMachine::Protocols::SmtpClient
- EventMachine::Protocols::SmtpServer
- EventMachine::Protocols::Socks4
- EventMachine::Protocols::Stomp
- EventMachine::Protocols::Stomp::Message
- EventMachine::Protocols::TcpConnectTester
- EventMachine::Queue
- EventMachine::Reactor
- EventMachine::Selectable
- EventMachine::SpawnedProcess
- EventMachine::StreamObject
- EventMachine::SystemCmd
- EventMachine::ThreadedResource
- EventMachine::TickLoop
- EventMachine::Timer
- EventMachine::UuidGenerator
- EventMachine::YieldBlockFromSpawnedProcess
- Object
- Object::EM
- Object::P
- BufferedTokenizer
- IO
- StringIO
- TestConnection

No matching classes.

# EventMachine::Protocols::LineAndTextProtocol

A protocol that handles line-oriented data with interspersed binary text.

This version is optimized for performance. See EventMachine::Protocols::LineText2 for a version which is optimized for correctness with regard to binary text blocks that can switch back to line mode.

### Constants

MaxBinaryLength

### Public Class Methods

new(*args) click to toggle source

    # File lib/em/protocols/line_and_text.rb, line 37
    def initialize *args
      super
      lbp_init_line_state
    end

### Public Instance Methods

receive_data(data) click to toggle source

    # File lib/em/protocols/line_and_text.rb, line 41
    def receive_data data
      if @lbp_mode == :lines
        begin
          @lpb_buffer.extract(data).each do |line|
            receive_line(line.chomp) if respond_to?(:receive_line)
          end
        rescue Exception
          receive_error('overlength line') if respond_to?(:receive_error)
          close_connection
          return
        end
      else
        if @lbp_binary_limit > 0
          wanted = @lbp_binary_limit - @lbp_binary_bytes_received
          chunk = nil
          if data.length > wanted
            chunk = data.slice!(0...wanted)
          else
            chunk = data
            data = ""
          end
          @lbp_binary_buffer[@lbp_binary_bytes_received...(@lbp_binary_bytes_received+chunk.length)] = chunk
          @lbp_binary_bytes_received += chunk.length
          if @lbp_binary_bytes_received == @lbp_binary_limit
            receive_binary_data(@lbp_binary_buffer) if respond_to?(:receive_binary_data)
            lbp_init_line_state
          end
          receive_data(data) if data.length > 0
        else
          receive_binary_data(data) if respond_to?(:receive_binary_data)
          data = ""
        end
      end
    end

set_binary_mode(size = nil) click to toggle source

Set up to read the supplied number of binary bytes. This recycles all the data currently waiting in the line buffer, if any. If the limit is nil, then ALL subsequent data will be treated as binary data and passed to the upstream protocol handler as we receive it. If a limit is given, we'll hold the incoming binary data and not pass it upstream until we've seen it all, or until there is an unbind (in which case we'll pass up a partial). Specifying nil for the limit (the default) means there is no limit. Specifiyng zero for the limit will cause an immediate transition back to line mode.

    # File lib/em/protocols/line_and_text.rb, line 94
    def set_binary_mode size = nil
      if @lbp_mode == :lines
        if size == 0
          receive_binary_data("") if respond_to?(:receive_binary_data)
          # Do no more work here. Stay in line mode and keep consuming data.
        else
          @lbp_binary_limit = size.to_i # (nil will be stored as zero)
          if @lbp_binary_limit > 0
            raise "Overlength" if @lbp_binary_limit > MaxBinaryLength # arbitrary sanity check
            @lbp_binary_buffer = "\00"" * @lbp_binary_limit
            @lbp_binary_bytes_received = 0
          end

          @lbp_mode = :binary
          receive_data @lpb_buffer.flush
        end
      else
        raise "invalid operation"
      end
    end

unbind() click to toggle source

    # File lib/em/protocols/line_and_text.rb, line 76
    def unbind
      if @lbp_mode == :binary and @lbp_binary_limit > 0
        if respond_to?(:receive_binary_data)
          receive_binary_data( @lbp_binary_buffer[0...@lbp_binary_bytes_received] )
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.