Class: DTAS::UNIXAccepted

Inherits:
Object
  • Object
show all
Defined in:
lib/dtas/unix_accepted.rb

Overview

an accepted (client) socket in dtas-player server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock) ⇒ UNIXAccepted

Returns a new instance of UNIXAccepted.


11
12
13
14
# File 'lib/dtas/unix_accepted.rb', line 11

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

Instance Attribute Details

#to_ioObject (readonly)

:nodoc:


9
10
11
# File 'lib/dtas/unix_accepted.rb', line 9

def to_io
  @to_io
end

Instance Method Details

#closeObject


65
66
67
# File 'lib/dtas/unix_accepted.rb', line 65

def close
  @to_io.close
end

#closed?Boolean

Returns:

  • (Boolean)

69
70
71
# File 'lib/dtas/unix_accepted.rb', line 69

def closed?
  @to_io.closed?
end

#emit(msg) ⇒ Object

public API (for DTAS::Player) returns :wait_readable on success


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dtas/unix_accepted.rb', line 18

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

#readable_iterObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dtas/unix_accepted.rb', line 48

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

#recv_nonblock(len) ⇒ Object


78
79
80
# File 'lib/dtas/unix_accepted.rb', line 78

def recv_nonblock(len)
  @to_io.recv_nonblock(len, exception: false)
end

#sendmsg_nonblock(msg) ⇒ Object


74
75
76
# File 'lib/dtas/unix_accepted.rb', line 74

def sendmsg_nonblock(msg)
  @to_io.sendmsg_nonblock(msg, Socket::MSG_EOR, exception: false)
end

#writable_iterObject

flushes pending data if it got buffered


37
38
39
40
41
42
43
44
45
46
# File 'lib/dtas/unix_accepted.rb', line 37

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