Class: Revactor::UNIX::Socket

Inherits:
Rev::UNIXSocket
  • Object
show all
Defined in:
lib/revactor/unix.rb

Overview

UNIX socket class, returned by Revactor::UNIX.connect and Revactor::UNIX::Listener#accept

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, options = {}) ⇒ Socket

Returns a new instance of Socket.



80
81
82
83
84
85
86
87
88
89
# File 'lib/revactor/unix.rb', line 80

def initialize(socket, options = {})
  super(socket)

  @active ||= options[:active] || false
  @controller ||= options[:controller] || Actor.current
  @filterset ||= [*initialize_filter(options[:filter])]

  @receiver = @controller
  @read_buffer = IO::Buffer.new
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



52
53
54
# File 'lib/revactor/unix.rb', line 52

def controller
  @controller
end

Class Method Details

.connect(path, options = {}) ⇒ Object

Connect to the specified path. Accepts the following options:

:active - Controls how data is read from the socket.  See the
          documentation for #active=

:controller - The controlling actor, default Actor.current

:filter - An symbol/class or array of symbols/classes which
          implement #encode and #decode methods to transform
          data sent and received data respectively via
          Revactor::UNIX::Socket. See the "Filters" section
          in the README for more information


68
69
70
71
72
73
74
75
76
77
# File 'lib/revactor/unix.rb', line 68

def connect(path, options = {})
  options[:active]     ||= false
  options[:controller] ||= Actor.current

  super.instance_eval {
    @active, @controller = options[:active], options[:controller]
    @filterset = [*initialize_filter(options[:filter])]
    self
  }
end

Instance Method Details

#active=(state) ⇒ Object

Enable or disable active mode data reception. State can be any of the following:

true - All received data is sent to the controlling actor
false - Receiving data is disabled
:once - A single message will be sent to the controlling actor
        then active mode will be disabled


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/revactor/unix.rb', line 103

def active=(state)
  unless @receiver == @controller
    raise "cannot change active state during a synchronous call"
  end

  unless [true, false, :once].include? state
    raise ArgumentError, "must be true, false, or :once"
  end

  if [true, :once].include?(state)
    unless @read_buffer.empty?
      @receiver << [:unix, self, @read_buffer.read]
      return if state == :once
    end

    enable unless enabled?
  end

  @active = state
end

#active?Boolean

Is the socket in active mode?

Returns:

  • (Boolean)


125
# File 'lib/revactor/unix.rb', line 125

def active?; @active; end

#inspectObject



91
92
93
# File 'lib/revactor/unix.rb', line 91

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} #@address_family:#@path"
end

#read(length = nil) ⇒ Object

Read data from the socket synchronously. If a length is specified then the call blocks until the given length has been read. Otherwise the call blocks until it receives any data.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/revactor/unix.rb', line 139

def read(length = nil)
  # Only one synchronous call allowed at a time
  @receiver == @controller or
    raise "already being called synchronously"

  unless @read_buffer.empty? or (length and @read_buffer.size < length)
    return @read_buffer.read(length)
  end

  active = @active
  @active = :once
  @receiver = Actor.current
  enable unless enabled?

  loop do
    Actor.receive do |filter|
      filter.when(T[:unix, self]) do |_, _, data|
        if length.nil?
          @receiver = @controller
          @active = active
          enable if @active

          return data
        end

        @read_buffer << data

        if @read_buffer.size >= length
          @receiver = @controller
          @active = active
          enable if @active

          return @read_buffer.read(length)
        end
      end

      filter.when(T[:unix_closed, self]) do
        unless @receiver == @controller
          @receiver = @controller
          @receiver << T[:unix_closed, self]
        end

        raise EOFError, "connection closed"
      end
    end
  end
end

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

Write data to the socket. The call blocks until all data has been written.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/revactor/unix.rb', line 189

def write(data)
  # Only one synchronous call allowed at a time
  @receiver == @controller or
    raise "already being called synchronously"

  active = @active
  @active = false
  @receiver = Actor.current
  disable if @active

  super(encode(data))

  Actor.receive do |filter|
    filter.when(T[:unix_write_complete, self]) do
      @receiver = @controller
      @active = active
      enable if @active and not enabled?

      return data.size
    end

    filter.when(T[:unix_closed, self]) do
      @active = false
      raise EOFError, "connection closed"
    end
  end
end