Class: XRBP::Overlay::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/xrbp/overlay/connection.rb

Overview

Primary Overlay Connection Interface, use Connection to send and receive Peer-To-Peer data over the Overlay.

Examples:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Connection

Returns a new instance of Connection.



25
26
27
28
29
# File 'lib/xrbp/overlay/connection.rb', line 25

def initialize(host, port)
  @host = host
  @port = port
  @node = Crypto.node
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



22
23
24
# File 'lib/xrbp/overlay/connection.rb', line 22

def host
  @host
end

#nodeObject

Returns the value of attribute node.



23
24
25
# File 'lib/xrbp/overlay/connection.rb', line 23

def node
  @node
end

#portObject (readonly)

Returns the value of attribute port.



22
23
24
# File 'lib/xrbp/overlay/connection.rb', line 22

def port
  @port
end

Instance Method Details

#close!Object Also known as: close

Close the connection to peer



69
70
71
# File 'lib/xrbp/overlay/connection.rb', line 69

def close!
  ssl_socket.close
end

#closed?Boolean

Indicates if the connection is closed

Returns:

  • (Boolean)


37
38
39
# File 'lib/xrbp/overlay/connection.rb', line 37

def closed?
  socket.closed?
end

#connectObject

Initiate new connection to peer



63
64
65
66
# File 'lib/xrbp/overlay/connection.rb', line 63

def connect
  ssl_socket.connect
  handshake.execute!
end

#readObject

Read raw data from connection



89
90
91
# File 'lib/xrbp/overlay/connection.rb', line 89

def read
  ssl_socket.gets
end

#read_framesObject

Read frames from connection until closed, invoking passed block with each.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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

#write(data) ⇒ Object

Send raw data via this connection



76
77
78
# File 'lib/xrbp/overlay/connection.rb', line 76

def write(data)
  ssl_socket.puts(data)
end

#write_frame(msg) ⇒ Object



80
81
82
# File 'lib/xrbp/overlay/connection.rb', line 80

def write_frame(msg)
  write(Frame.from_msg(msg))
end

#write_msg(data) ⇒ Object



84
85
86
# File 'lib/xrbp/overlay/connection.rb', line 84

def write_msg(data)
  write_frame(Overlay.create_msg(data))
end