Class: GReactor::BasicIO

Inherits:
Object
  • Object
show all
Defined in:
lib/greactor/io_wrappers.rb

Overview

a basic IO socket wrapper.

Direct Known Subclasses

ListenerIO, SSLBasicIO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, params = {}) ⇒ BasicIO

initialize the listener with the actual socket and the properties.


15
16
17
18
19
20
21
22
23
24
# File 'lib/greactor/io_wrappers.rb', line 15

def initialize io, params = {}
  @io = io
  @params = params
  @cache = {}
  @locker = Mutex.new()
  @send_locker = Mutex.new()
  @timeout = params[:timeout]
  @t_active = Time.now
  params[:handler] = params[:handler].new(self) if params[:handler].is_a?(Class)
end

Instance Attribute Details

#cacheObject (readonly)

attribute readers


12
13
14
# File 'lib/greactor/io_wrappers.rb', line 12

def cache
  @cache
end

#ioObject (readonly)

attribute readers


12
13
14
# File 'lib/greactor/io_wrappers.rb', line 12

def io
  @io
end

#lockerObject (readonly)

attribute readers


12
13
14
# File 'lib/greactor/io_wrappers.rb', line 12

def locker
  @locker
end

#paramsObject (readonly)

attribute readers


12
13
14
# File 'lib/greactor/io_wrappers.rb', line 12

def params
  @params
end

#timeoutObject

timeout accessor


9
10
11
# File 'lib/greactor/io_wrappers.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#[](key) ⇒ Object

Access data stored in the IO's wrapper cache.


90
91
92
# File 'lib/greactor/io_wrappers.rb', line 90

def [] key
  @cache[key]
end

#[]=(key, val) ⇒ Object

Store data in the IO's wrapper cache.


94
95
96
# File 'lib/greactor/io_wrappers.rb', line 94

def []= key, val
  @cache[key] = val
end

#callObject

accepts a new connection, adding it to the IO stack, so that it will be reviewed.


40
41
42
43
# File 'lib/greactor/io_wrappers.rb', line 40

def call
  touch
  @locker.synchronize { @params[:handler].call(self) } unless @locker.locked?
end

#clear?Boolean

returns true if the socket is closed or the object can be cleared.


27
28
29
# File 'lib/greactor/io_wrappers.rb', line 27

def clear?
  @io.closed? || timeout?  || (@io.stat.socket? && @io.stat.mode < 0140666) rescue true
end

#closeObject Also known as: disconnect

closes the IO object using the GReactor.close_io.


50
51
52
53
# File 'lib/greactor/io_wrappers.rb', line 50

def close
  GReactor.close_io @io
  nil
end

#closed?Boolean

returns true if the socket is closed for both read and write.


31
32
33
# File 'lib/greactor/io_wrappers.rb', line 31

def closed?
  @io.closed?
end

#on_disconnectObject


44
45
46
47
# File 'lib/greactor/io_wrappers.rb', line 44

def on_disconnect
  (@params[:handler].on_disconnect self) if @params[:handler] && @params[:handler].respond_to?( :on_disconnect )
  true
end

#read(size = 1048576) ⇒ Object

reads from the IO up to the specified number of bytes (defaults to 1,048,576 bytes).


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/greactor/io_wrappers.rb', line 57

def read size = 1048576
  begin
    data = @io.recv_nonblock( size )
    return nil if data.to_s.empty?
    touch
    data
  rescue => e
    # GReactor.warn e
    nil
  end
end

#ssl?Boolean


98
99
100
# File 'lib/greactor/io_wrappers.rb', line 98

def ssl?
  false
end

#timeout?Boolean

returns true if the socket had timed-out.


35
36
37
# File 'lib/greactor/io_wrappers.rb', line 35

def timeout?
  @timeout && !@send_locker.locked? && ( (Time.now - @t_active) > @timeout )
end

#write(data) ⇒ Object Also known as: send, <<

this method, writes data to the socket / io object.


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/greactor/io_wrappers.rb', line 70

def write data
  begin
    @send_locker.synchronize do
      len = data.bytesize
      i = act = @io.send data, 0
      while len > act && i!=0
        act += (i = @io.send data.byteslice(act..-1) , 0)
      end
      touch
      act
    end
  rescue => e
    # GReactor.warn e
    close
  end
end