Class: OSC::TCPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/qcmd/core_ext/osc/tcp_client.rb

Constant Summary collapse

CHAR_END =

indicates end of packet

0300
CHAR_ESC =

indicates byte stuffing

0333
CHAR_ESC_END =

ESC ESC_END means END data byte

0334
CHAR_ESC_ESC =

ESC ESC_ESC means ESC data byte

0335
CHAR_END_ENC =

indicates end of packet

[0300].pack('C')
CHAR_ESC_ENC =

indicates byte stuffing

[0333].pack('C')
CHAR_ESC_END_ENC =

ESC ESC_END means END data byte

[0334].pack('C')
CHAR_ESC_ESC_ENC =

ESC ESC_ESC means ESC data byte

[0335].pack('C')

Instance Method Summary collapse

Constructor Details

#initialize(host, port, handler = nil) ⇒ TCPClient

Returns a new instance of TCPClient.



19
20
21
22
23
24
# File 'lib/qcmd/core_ext/osc/tcp_client.rb', line 19

def initialize host, port, handler=nil
  @host = host
  @port = port
  @handler = handler
  @so   = TCPSocket.new host, port
end

Instance Method Details

#closeObject



26
27
28
# File 'lib/qcmd/core_ext/osc/tcp_client.rb', line 26

def close
  @so.close unless closed?
end

#closed?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/qcmd/core_ext/osc/tcp_client.rb', line 30

def closed?
  @so.closed?
end

#responseObject



69
70
71
72
73
74
75
76
77
# File 'lib/qcmd/core_ext/osc/tcp_client.rb', line 69

def response
  if received_messages = receive_raw
    received_messages.map do |message|
      OSCPacket.messages_from_network(message)
    end.flatten
  else
    nil
  end
end

#send(msg) ⇒ Object

send an OSC::Message



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/qcmd/core_ext/osc/tcp_client.rb', line 35

def send msg
  enc_msg = msg.encode

  send_char CHAR_END

  enc_msg.bytes.each do |b|
    case b
    when CHAR_END
      send_char CHAR_ESC
      send_char CHAR_ESC_END
    when CHAR_ESC
      send_char CHAR_ESC
      send_char CHAR_ESC_ESC
    else
      send_char b
    end
  end

  send_char CHAR_END

  if block_given? || @handler
    messages = response
    if !messages.nil?
      messages.each do |message|
        if block_given?
          yield message
        else
          @handler.handle message
        end
      end
    end
  end
end

#to_sObject



79
80
81
# File 'lib/qcmd/core_ext/osc/tcp_client.rb', line 79

def to_s
  "#<OSC::TCPClient:#{ object_id } @host:#{ @host.inspect }, @port:#{ @port.inspect }, @handler:#{ @handler.to_s }>"
end