Module: EventMachine::Protocols::Zmq2::PackString

Included in:
PrePub, Pub, SocketConnection
Defined in:
lib/em/protocols/zmq2/socket_connection.rb

Overview

Main implementation peace, heart of protocol

Constant Summary collapse

BIG_PACK =
'CNNCa*'.freeze
SMALL_PACK =
'CCa*'.freeze

Instance Method Summary collapse

Instance Method Details

#pack_string(string, more = false) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/em/protocols/zmq2/socket_connection.rb', line 11

def pack_string(string, more = false)
  bytesize = string.bytesize + 1
  if bytesize <= 254
    [bytesize, more ? 1 : 0, string].pack(SMALL_PACK)
  else
    [255, 0, bytesize, more ? 1 : 0, string].pack(BIG_PACK)
  end
end

#prepare_message(message) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/em/protocols/zmq2/socket_connection.rb', line 20

def prepare_message(message)
  if String === message
    pack_string(message, false)
  else
    message = Array(message)
    buffer = ''
    i = 0
    last = message.size - 1
    while i < last
      buffer << pack_string(message[i].to_s, true)
      i += 1
    end
    buffer << pack_string(message[last].to_s, false)
  end
end