Class: ConcurrentSHM::Channel::SingleBuffered::Variable
- Inherits:
-
ConcurrentSHM::Channel::SingleBuffered
- Object
- ConcurrentSHM::Channel
- ConcurrentSHM::Channel::SingleBuffered
- ConcurrentSHM::Channel::SingleBuffered::Variable
- Defined in:
- lib/concurrent-shm/channel.rb
Overview
A single-buffered channel with variable-width packets.
Class Method Summary collapse
-
.new(shm, capacity:, offset: 0, autosize: true) ⇒ Object
Allocates a single-buffered, variable-width channel with the specified capacity.
Instance Method Summary collapse
-
#recv ⇒ String
Receive a variable-width packet over the channel.
-
#send(data) ⇒ nil
Send a variable-width packet over the channel.
Methods inherited from ConcurrentSHM::Channel
Class Method Details
.new(shm, capacity:, offset: 0, autosize: true) ⇒ Object
Allocates a single-buffered, variable-width channel with the specified capacity.
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/concurrent-shm/channel.rb', line 207 def self.new(shm, capacity:, offset: 0, autosize: true) raise RangeError, "Capacity must be > 0" unless capacity > 0 cbytes = bytes_for(capacity) { raise RangeError, "Capacity must be less than 2^32" } alloc(shm, cbytes + 1 + capacity, offset: offset, autosize: autosize) do |body| @width = body[0, cbytes].as_uintptr @closed = body[cbytes].as_intptr @data = body[cbytes+1..] end end |
Instance Method Details
#recv ⇒ String
Receive a variable-width packet over the channel. Blocks if the buffer is empty.
234 235 236 237 238 |
# File 'lib/concurrent-shm/channel.rb', line 234 def recv do_recv do @data[0...@width[]].read end end |
#send(data) ⇒ nil
Send a variable-width packet over the channel. Blocks if the buffer is full.
223 224 225 226 227 228 229 230 |
# File 'lib/concurrent-shm/channel.rb', line 223 def send(data) raise RangeError, "Data is wider than channel" if data.size > @data.size do_send do @width[] = data.bytesize @data.write(data) end end |