Class: GrpcKit::Session::SendBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/grpc_kit/session/send_buffer.rb

Instance Method Summary collapse

Constructor Details

#initializeSendBuffer

Returns a new instance of SendBuffer.



6
7
8
9
10
11
# File 'lib/grpc_kit/session/send_buffer.rb', line 6

def initialize
  @buffer = ''.b
  @end_write = false
  @deferred_read = false
  @mutex = Mutex.new
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/grpc_kit/session/send_buffer.rb', line 40

def empty?
  @mutex.synchronize { @buffer.empty? }
end

#end_writevoid

This method returns an undefined value.



31
32
33
# File 'lib/grpc_kit/session/send_buffer.rb', line 31

def end_write
  @end_write = true
end

#end_write?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/grpc_kit/session/send_buffer.rb', line 36

def end_write?
  @end_write
end

#eof?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/grpc_kit/session/send_buffer.rb', line 68

def eof?
  end_write? && @mutex.synchronize { @buffer.empty? }
end

#need_resume?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/grpc_kit/session/send_buffer.rb', line 22

def need_resume?
  @deferred_read
end

#no_resumeObject



26
27
28
# File 'lib/grpc_kit/session/send_buffer.rb', line 26

def no_resume
  @deferred_read = false
end

#read(size = nil) ⇒ nil, ...

Parameters:

  • size (Integer, nil) (defaults to: nil)

Returns:

  • (nil, DS9::ERR_DEFERRED, String)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grpc_kit/session/send_buffer.rb', line 46

def read(size = nil)
  buf = do_read(size)
  if buf
    @deferred_read = false
    return buf
  end

  if end_write?
    # Call again because #write invokes `@buffer << data` before calling #end_write
    if (buf = do_read(size))
      @deferred_read = false
      return buf
    end

    @deferred_read = false
    return nil # EOF
  end

  @deferred_read = true
  DS9::ERR_DEFERRED
end

#write(data, last: false) ⇒ void

This method returns an undefined value.

Parameters:

  • data (String)
  • last (Boolean) (defaults to: false)


16
17
18
19
# File 'lib/grpc_kit/session/send_buffer.rb', line 16

def write(data, last: false)
  @mutex.synchronize { @buffer << data }
  end_write if last
end