Module: Celluloid::DNS::StreamTransport

Defined in:
lib/celluloid/dns/transport.rb

Class Method Summary collapse

Class Method Details

.read_chunk(socket) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/celluloid/dns/transport.rb', line 41

def self.read_chunk(socket)
  # The data buffer:
  buffer = BinaryStringIO.new
  
  # First we need to read in the length of the packet
  while buffer.size < 2
    buffer.write socket.readpartial(1)
  end
  
  # Read in the length, the first two bytes:
  length = buffer.string.byteslice(0, 2).unpack('n')[0]
  
  # Read data until we have the amount specified:
  while (buffer.size - 2) < length
    required = (2 + length) - buffer.size
    
    # Read precisely the required amount:
    buffer.write socket.readpartial(required)
  end
  
  return buffer.string.byteslice(2, length)
end

.write_chunk(socket, output_data) ⇒ Object



68
69
70
71
72
73
# File 'lib/celluloid/dns/transport.rb', line 68

def self.write_chunk(socket, output_data)
  socket.write([output_data.bytesize].pack('n'))
  socket.write(output_data)
  
  return output_data.bytesize
end

.write_message(socket, message) ⇒ Object



64
65
66
# File 'lib/celluloid/dns/transport.rb', line 64

def self.write_message(socket, message)
  write_chunk(socket, message.encode)
end