Class: Vizcore::Sync::OscReceiver

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/sync/osc_receiver.rb

Overview

Receives OSC UDP messages on a background thread.

Constant Summary collapse

DEFAULT_HOST =
"127.0.0.1"
MAX_PACKET_SIZE =
65_536

Instance Method Summary collapse

Constructor Details

#initialize(port:, host: DEFAULT_HOST, handler:, error_reporter: nil) ⇒ OscReceiver

Returns a new instance of OscReceiver.

Parameters:

  • port (Integer)
  • host (String) (defaults to: DEFAULT_HOST)
  • handler (#call)
  • error_reporter (#call, nil) (defaults to: nil)


17
18
19
20
21
22
23
24
25
# File 'lib/vizcore/sync/osc_receiver.rb', line 17

def initialize(port:, host: DEFAULT_HOST, handler:, error_reporter: nil)
  @host = host.to_s
  @port = Integer(port)
  @handler = handler
  @error_reporter = error_reporter
  @socket = nil
  @thread = nil
  @running = false
end

Instance Method Details

#startVizcore::Sync::OscReceiver



28
29
30
31
32
33
34
35
36
# File 'lib/vizcore/sync/osc_receiver.rb', line 28

def start
  return self if @thread&.alive?

  @socket = UDPSocket.new
  @socket.bind(@host, @port)
  @running = true
  @thread = Thread.new { receive_loop }
  self
end

#stopnil

Returns:

  • (nil)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vizcore/sync/osc_receiver.rb', line 39

def stop
  @running = false
  @socket&.close
  @thread&.join(1)
  nil
rescue StandardError
  nil
ensure
  @socket = nil
  @thread = nil
end