Module: Async::DNS::StreamTransport

Defined in:
lib/async/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
63
64
65
66
67
68
69
70
# File 'lib/async/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
		if data = socket.read(1)
			buffer.write data
		else
			raise EOFError, "Could not read message size!"
		end
	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:
		if data = socket.read(required)
			buffer.write data
		else
			raise EOFError, "Could not read message data!"
		end
	end
	
	return buffer.string.byteslice(2, length)
end

.write_chunk(socket, output_data) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/async/dns/transport.rb', line 76

def self.write_chunk(socket, output_data)
	size_data = [output_data.bytesize].pack('n')
	
	# TODO: Validate/check for data written correctly
	count = socket.write(size_data)
	count = socket.write(output_data)
	
	return output_data.bytesize
end

.write_message(socket, message) ⇒ Object



72
73
74
# File 'lib/async/dns/transport.rb', line 72

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