class Transport
A simple DNS message stream encoder/decoder.
Definitions
def initialize(socket)
Create a new transport.
Signature
-
parameter
socket
IO
The socket to read/write from.
Implementation
def initialize(socket)
@socket = socket
end
def write_message(message)
Write a message to the socket.
Signature
-
parameter
message
Resolv::DNS::Message
The message to write.
Implementation
def write_message(message)
write_chunk(message.encode)
end
def read_chunk
Read a chunk from the socket.
Signature
-
returns
String
The data read from the socket.
Implementation
def read_chunk
if size_data = @socket.read(2)
# Read in the length, the first two bytes:
size = size_data.unpack('n')[0]
return @socket.read(size)
end
end
def write_chunk(data)
Write a chunk to the socket.
Signature
-
parameter
data
String
The data to write.
Implementation
def write_chunk(data)
size_data = [data.bytesize].pack('n')
@socket.write(size_data)
@socket.write(data)
@socket.flush
end