dtas.git  about / heads / tags
duct tape audio suite for *nix
blob d4e56b30a25d1b1896200eb6cfd941fcc5989715 1310 bytes (raw)
$ git show mpd:lib/dtas/server_loop.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
# Copyright (C) 2015-2016 all contributors <dtas-all@nongnu.org>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
# frozen_string_literal: true

require_relative '../dtas'

# Used for mpd emulation currently, but dtas-player might use this eventually
class DTAS::ServerLoop
  def initialize(listeners, client_class)
    @rd = {}
    @wr = {}
    @rbuf = ''.b
    @client_class = client_class
    listeners.each { |l| @rd[l] = true }
  end

  def run_forever
    begin
      r = IO.select(@rd.keys, @wr.keys) or next
      r[0].each { |rd| do_read(rd) }
      r[1].each { |wr| do_write(wr) }
    end while true
  end

  def do_write(wr)
    case wr.dispatch_wr
    when :wait_readable
      @wr.delete(wr)
      @rd[wr] = true
    when nil
      @wr.delete(wr)
      wr.to_io.close
    # when :wait_writable # do nothing
    end
  end

  def do_read(rd)
    case rd
    when @client_class
      case rd.dispatch_rd(@rbuf)
      when :wait_writable
        @rd.delete(rd)
        @wr[rd] = true
      when nil
        @rd.delete(rd)
        rd.to_io.close
      # when :wait_readable : do nothing
      end
    else
      case io = rd.accept_nonblock(exception: false)
      when :wait_readable then break
      when IO then @rd[@client_class.new(io)] = true
      end while true
    end
  end
end

git clone git://80x24.org/dtas.git
git clone https://80x24.org/dtas.git