Class: Webmidi::Network::AppleMIDI::Session
- Inherits:
-
Object
- Object
- Webmidi::Network::AppleMIDI::Session
- Defined in:
- lib/webmidi/network/apple_midi.rb
Instance Attribute Summary collapse
-
#control_port ⇒ Object
readonly
Returns the value of attribute control_port.
-
#data_port ⇒ Object
readonly
Returns the value of attribute data_port.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ssrc ⇒ Object
readonly
Returns the value of attribute ssrc.
Instance Method Summary collapse
- #connect_to(host, port, data_port: port + 1) ⇒ Object
-
#initialize(port:, name:, mode: :server) ⇒ Session
constructor
A new instance of Session.
- #on_error(&block) ⇒ Object
- #on_message(&block) ⇒ Object
- #peers ⇒ Object
- #send(message) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object (also: #close)
Constructor Details
#initialize(port:, name:, mode: :server) ⇒ Session
Returns a new instance of Session.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/webmidi/network/apple_midi.rb', line 24 def initialize(port:, name:, mode: :server) @requested_control_port = port @requested_data_port = port.zero? ? 0 : port + 1 @name = name @mode = mode @ssrc = SecureRandom.random_number(0xFFFFFFFF) @rtp = RTP::Session.new(port: @requested_data_port, name: name, mode: mode, ssrc: @ssrc) @pending_tokens = {} @control_peers = [] @mutex = Mutex.new @running = false @control_socket = nil @control_thread = nil @data_subscription = nil end |
Instance Attribute Details
#control_port ⇒ Object (readonly)
Returns the value of attribute control_port.
22 23 24 |
# File 'lib/webmidi/network/apple_midi.rb', line 22 def control_port @control_port end |
#data_port ⇒ Object (readonly)
Returns the value of attribute data_port.
22 23 24 |
# File 'lib/webmidi/network/apple_midi.rb', line 22 def data_port @data_port end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
22 23 24 |
# File 'lib/webmidi/network/apple_midi.rb', line 22 def name @name end |
#ssrc ⇒ Object (readonly)
Returns the value of attribute ssrc.
22 23 24 |
# File 'lib/webmidi/network/apple_midi.rb', line 22 def ssrc @ssrc end |
Instance Method Details
#connect_to(host, port, data_port: port + 1) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/webmidi/network/apple_midi.rb', line 65 def connect_to(host, port, data_port: port + 1) start unless @running token = SecureRandom.random_number(0xFFFF_FFFF) peer = {host: host, control_port: port, data_port: data_port} @mutex.synchronize { @pending_tokens[token] = peer } send_control(RTP::ControlPacket.invitation(token: token, ssrc: @ssrc, name: @name), host, port) self end |
#on_error(&block) ⇒ Object
83 84 85 |
# File 'lib/webmidi/network/apple_midi.rb', line 83 def on_error(&block) @rtp.on_error(&block) end |
#on_message(&block) ⇒ Object
79 80 81 |
# File 'lib/webmidi/network/apple_midi.rb', line 79 def (&block) @rtp.(&block) end |
#peers ⇒ Object
87 88 89 |
# File 'lib/webmidi/network/apple_midi.rb', line 87 def peers @rtp.peers end |
#send(message) ⇒ Object
75 76 77 |
# File 'lib/webmidi/network/apple_midi.rb', line 75 def send() @rtp.send() end |
#start ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/webmidi/network/apple_midi.rb', line 40 def start return self if @running @rtp.start @data_port = @rtp.port @data_subscription = @rtp.on_control_packet { |packet, peer| handle_control_packet(packet, peer, :data) } @control_socket = UDPSocket.new @control_socket.bind("0.0.0.0", @requested_control_port) @control_port = @control_socket.addr[1] @running = true @control_thread = Thread.new { control_loop } self end |
#stop ⇒ Object Also known as: close
54 55 56 57 58 59 60 61 |
# File 'lib/webmidi/network/apple_midi.rb', line 54 def stop @running = false @control_socket&.close @control_thread&.join(1) @data_subscription&.unsubscribe @rtp.stop self end |