Module: RubyDNS::StreamTransport

Defined in:
lib/rubydns/transport.rb

Class Method Summary collapse

Class Method Details

.read_chunk(socket) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubydns/transport.rb', line 36

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



63
64
65
66
67
68
# File 'lib/rubydns/transport.rb', line 63

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



59
60
61
# File 'lib/rubydns/transport.rb', line 59

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