Class: XRBP::Overlay::Connection
- Inherits:
-
Object
- Object
- XRBP::Overlay::Connection
- 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.
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#node ⇒ Object
Returns the value of attribute node.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#close! ⇒ Object
(also: #close)
Close the connection to peer.
-
#closed? ⇒ Boolean
Indicates if the connection is closed.
-
#connect ⇒ Object
Initiate new connection to peer.
-
#initialize(host, port) ⇒ Connection
constructor
A new instance of Connection.
-
#read ⇒ Object
Read raw data from connection.
-
#read_frames ⇒ Object
Read frames from connection until closed, invoking passed block with each.
-
#write(data) ⇒ Object
Send raw data via this connection.
- #write_frame(msg) ⇒ Object
- #write_msg(data) ⇒ Object
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
22 23 24 |
# File 'lib/xrbp/overlay/connection.rb', line 22 def host @host end |
#node ⇒ Object
Returns the value of attribute node.
23 24 25 |
# File 'lib/xrbp/overlay/connection.rb', line 23 def node @node end |
#port ⇒ Object (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
37 38 39 |
# File 'lib/xrbp/overlay/connection.rb', line 37 def closed? socket.closed? end |
#connect ⇒ Object
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 |
#read ⇒ Object
Read raw data from connection
89 90 91 |
# File 'lib/xrbp/overlay/connection.rb', line 89 def read ssl_socket.gets end |
#read_frames ⇒ Object
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 |