Class: Async::DNS::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/async/dns/transport.rb

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Transport

Returns a new instance of Transport.



33
34
35
# File 'lib/async/dns/transport.rb', line 33

def initialize(socket)
	@stream = IO::Stream.new(socket)
end

Instance Method Details

#read_chunkObject



41
42
43
44
45
46
47
48
# File 'lib/async/dns/transport.rb', line 41

def read_chunk
	if size_data = @stream.read(2)
		# Read in the length, the first two bytes:
		size = size_data.unpack('n')[0]
		
		return @stream.read(size)
	end
end

#write_chunk(output_data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/async/dns/transport.rb', line 50

def write_chunk(output_data)
	size_data = [output_data.bytesize].pack('n')
	
	@stream.write(size_data)
	@stream.write(output_data)
	
	@stream.flush
	
	return output_data.bytesize
end

#write_message(message) ⇒ Object



37
38
39
# File 'lib/async/dns/transport.rb', line 37

def write_message(message)
	write_chunk(message.encode)
end