Class: Milight::V6::Socket
- Inherits:
-
Object
- Object
- Milight::V6::Socket
- Defined in:
- lib/milight/v6/socket.rb
Constant Summary collapse
- READ_TIMEOUT =
5
Instance Method Summary collapse
-
#initialize(host, port) ⇒ Socket
constructor
A new instance of Socket.
- #receive_bytes ⇒ Object
- #send_bytes(bytes) ⇒ Object
Constructor Details
#initialize(host, port) ⇒ Socket
Returns a new instance of Socket.
11 12 13 14 15 16 17 |
# File 'lib/milight/v6/socket.rb', line 11 def initialize(host, port) @socket = UDPSocket.new @socket.connect(host, port) @logger = Logger.new(STDOUT) @logger.level = Logger::INFO if ENV["MILIGHT_DEBUG"] != "1" end |
Instance Method Details
#receive_bytes ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/milight/v6/socket.rb', line 25 def receive_bytes response = @socket.recvfrom_nonblock(128).first bytes = response.unpack('C*') @logger.debug("Received: #{format_bytes_as_hex(bytes)}") bytes rescue IO::WaitReadable ready = IO.select([@socket], nil, nil, READ_TIMEOUT) retry if ready return false end |
#send_bytes(bytes) ⇒ Object
19 20 21 22 23 |
# File 'lib/milight/v6/socket.rb', line 19 def send_bytes(bytes) @logger.debug("Sending: #{format_bytes_as_hex(bytes)}") @socket.send(bytes.pack('C*'), 0) end |