dtas.git  about / heads / tags
duct tape audio suite for *nix
blob ec7f3ef8b3fd8d638c98f44c0ffeab58123b25c1 2051 bytes (raw)
$ git show v0.18.0: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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
 
# Copyright (C) 2013-2020 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
    @send_buf = []
  end

  # public API (for DTAS::Player)
  # returns :wait_readable on success
  def emit(msg)
    buffered = @send_buf.size
    if buffered == 0
      case rv = sendmsg_nonblock(msg)
      when :wait_writable
        @send_buf << msg
        rv
      else
        :wait_readable
      end
    else # buffered > 0
      @send_buf << msg
      :wait_writable
    end
  rescue => e
    e
  end

  # flushes pending data if it got buffered
  def writable_iter
    case sendmsg_nonblock(@send_buf[0])
    when :wait_writable then return :wait_writable
    else
      @send_buf.shift
      @send_buf.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 = recv_nonblock(nread)
    when :wait_readable then return msg
    when '', nil then return nil # EOF
    else
      yield(self, msg) # DTAS::Player deals with this
    end
    @send_buf.empty? ? :wait_readable : :wait_writable
  rescue SystemCallError
    nil
  end

  def close
    @to_io.close
  end

  def closed?
    @to_io.closed?
  end

  if RUBY_VERSION.to_f >= 2.3
    def sendmsg_nonblock(msg)
      @to_io.sendmsg_nonblock(msg, Socket::MSG_EOR, exception: false)
    end

    def recv_nonblock(len)
      @to_io.recv_nonblock(len, exception: false)
    end
  else
    def sendmsg_nonblock(msg)
      @to_io.sendmsg_nonblock(msg, Socket::MSG_EOR)
    rescue IO::WaitWritable
      :wait_writable
    end

    def recv_nonblock(len)
      @to_io.recv_nonblock(len)
    rescue IO::WaitReadable
      :wait_readable
    rescue EOFError
      nil
    end
  end
end

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