dtas.git  about / heads / tags
duct tape audio suite for *nix
blob 63d3ce0240c3f24328beeb2b7c419f82ac21ef95 1524 bytes (raw)
$ git show HEAD:lib/dtas/unix_accepted.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 
# Copyright (C) all contributors <dtas-all@nongnu.org>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
# frozen_string_literal: true
require 'socket'
require 'io/wait'

# an accepted (client) socket in dtas-player server
class DTAS::UNIXAccepted # :nodoc:
  attr_reader :to_io

  def initialize(sock)
    @to_io = sock
    @sbuf = []
  end

  # public API (for DTAS::Player)
  # returns :wait_readable on success
  def emit(msg)
    if @sbuf.empty?
      case rv = @to_io.sendmsg_nonblock(msg, 0, exception: false)
      when :wait_writable
        @sbuf << msg
        rv
      else
        :wait_readable
      end
    else
      @sbuf << msg
      :wait_writable
    end
  rescue => e
    e
  end

  # flushes pending data if it got buffered
  def writable_iter
    case @to_io.sendmsg_nonblock(@sbuf[0], 0, exception: false)
    when :wait_writable then return :wait_writable
    else
      @sbuf.shift
      @sbuf.empty? ? :wait_readable : :wait_writable
    end
  rescue => e
    e
  end

  def readable_iter
    nread = @to_io.nread

    # EOF, assume no spurious wakeups for SOCK_SEQPACKET
    return nil if nread == 0

    case msg = @to_io.recv_nonblock(nread, exception: false)
    when :wait_readable then return msg
    when '', nil then return nil # EOF
    else
      yield(self, msg) # DTAS::Player deals with this
    end
    @sbuf.empty? ? :wait_readable : :wait_writable
  rescue SystemCallError
    nil
  end

  def close
    @to_io.close
  end

  def closed?
    @to_io.closed?
  end
end

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