class XRBP::Overlay::Connection
Primary Overlay
Connection
Interface, use Connection
to send and receive Peer-To-Peer data over the Overlay
.
@example establishing a connection, reading frames
overlay = XRBP::Overlay::Connection.new "127.0.0.1", 51235 overlay.connect overlay.read_frames do |frame| puts "Message: #{frame.type_name} (#{frame.size} bytes)" end
Attributes
host[R]
node[RW]
port[R]
Public Class Methods
new(host, port)
click to toggle source
# File lib/xrbp/overlay/connection.rb, line 25 def initialize(host, port) @host = host @port = port @node = Crypto.node end
Public Instance Methods
close!()
click to toggle source
Close the connection to peer
# File lib/xrbp/overlay/connection.rb, line 69 def close! ssl_socket.close end
Also aliased as: close
closed?()
click to toggle source
Indicates if the connection is closed
# File lib/xrbp/overlay/connection.rb, line 37 def closed? socket.closed? end
connect()
click to toggle source
Initiate new connection to peer
# File lib/xrbp/overlay/connection.rb, line 63 def connect ssl_socket.connect handshake.execute! end
handshake()
click to toggle source
@private
# File lib/xrbp/overlay/connection.rb, line 56 def handshake @handshake ||= Handshake.new self end
read()
click to toggle source
Read raw data from connection
# File lib/xrbp/overlay/connection.rb, line 89 def read ssl_socket.gets end
read_frames() { |frame| ... }
click to toggle source
Read frames from connection until closed, invoking passed block with each.
# File lib/xrbp/overlay/connection.rb, line 95 def read_frames frame = nil remaining = nil while !closed? read_sockets, _, _ = IO.select([ssl_socket], nil, nil, 0.1) if read_sockets && read_sockets[0] out = ssl_socket.read_nonblock(1024) if frame.nil? type = Frame::TYPE_INFER.decode(out) frame = Frame.new type["type"], type["size"] out = out[Frame::TYPE_INFER.size..-1] end _, remaining = frame << out if frame.complete? yield frame frame = nil end # static assertion: should have no more data raise unless remaining.nil? || remaining.empty? end end end
socket()
click to toggle source
@private
# File lib/xrbp/overlay/connection.rb, line 32 def socket @socket ||= TCPSocket.open(host, port) end
ssl_socket()
click to toggle source
@private
# File lib/xrbp/overlay/connection.rb, line 42 def ssl_socket @ssl_socket ||= begin ssl_context = OpenSSL::SSL::SSLContext.new ssl_context.ssl_version = :SSLv23 ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE _ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context) _ssl_socket.sync_close = true _ssl_socket end end
write(data)
click to toggle source
Send raw data via this connection
# File lib/xrbp/overlay/connection.rb, line 76 def write(data) ssl_socket.puts(data) end
write_frame(msg)
click to toggle source
# File lib/xrbp/overlay/connection.rb, line 80 def write_frame(msg) write(Frame.from_msg(msg)) end
write_msg(data)
click to toggle source
# File lib/xrbp/overlay/connection.rb, line 84 def write_msg(data) write_frame(Overlay.create_msg(data)) end