Class: ObjectSocket
- Inherits:
-
Object
- Object
- ObjectSocket
- Defined in:
- lib/rails_parallel/object_socket.rb
Constant Summary collapse
- BLOCK_SIZE =
4096
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #blocking(&block) ⇒ Object
- #close ⇒ Object
- #each_object(&block) ⇒ Object
-
#initialize(socket) ⇒ ObjectSocket
constructor
A new instance of ObjectSocket.
- #next_object ⇒ Object
- #nonblock=(val) ⇒ Object
- #nonblocking(&block) ⇒ Object
Constructor Details
#initialize(socket) ⇒ ObjectSocket
Returns a new instance of ObjectSocket.
13 14 15 16 |
# File 'lib/rails_parallel/object_socket.rb', line 13 def initialize(socket) @socket = socket @buffer = '' end |
Instance Attribute Details
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
7 8 9 |
# File 'lib/rails_parallel/object_socket.rb', line 7 def socket @socket end |
Class Method Details
.pair ⇒ Object
9 10 11 |
# File 'lib/rails_parallel/object_socket.rb', line 9 def self.pair Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0).map { |s| new(s) } end |
Instance Method Details
#<<(obj) ⇒ Object
51 52 53 54 55 |
# File 'lib/rails_parallel/object_socket.rb', line 51 def <<(obj) data = Marshal.dump(obj) @socket.syswrite [data.size, data].pack('Na*') self # chainable end |
#blocking(&block) ⇒ Object
29 30 31 |
# File 'lib/rails_parallel/object_socket.rb', line 29 def blocking(&block) with_nonblock(false, &block) end |
#close ⇒ Object
22 23 24 |
# File 'lib/rails_parallel/object_socket.rb', line 22 def close @socket.close end |
#each_object(&block) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rails_parallel/object_socket.rb', line 33 def each_object(&block) first = true loop do process_buffer(&block) if first first = false @buffer += @nonblock ? @socket.read_nonblock(BLOCK_SIZE) : @socket.readpartial(BLOCK_SIZE) process_buffer(&block) end rescue Errno::EAGAIN # end of nonblocking data end |
#next_object ⇒ Object
46 47 48 49 |
# File 'lib/rails_parallel/object_socket.rb', line 46 def next_object each_object { |o| return o } nil # no pending data in nonblock mode end |
#nonblock=(val) ⇒ Object
18 19 20 |
# File 'lib/rails_parallel/object_socket.rb', line 18 def nonblock=(val) @nonblock = val end |
#nonblocking(&block) ⇒ Object
26 27 28 |
# File 'lib/rails_parallel/object_socket.rb', line 26 def nonblocking(&block) with_nonblock(true, &block) end |