Class: ConcurrentSHM::Channel::SingleBuffered::Variable

Inherits:
ConcurrentSHM::Channel::SingleBuffered show all
Defined in:
lib/concurrent-shm/channel.rb

Overview

A single-buffered channel with variable-width packets.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ConcurrentSHM::Channel

#close

Class Method Details

.new(shm, capacity:, offset: 0, autosize: true) ⇒ Object

Allocates a single-buffered, variable-width channel with the specified capacity.

Raises:

  • (RangeError)

    if capacity is less than 1 or greater than 2<sup>32</sup>-1

See Also:



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

#recvString

Receive a variable-width packet over the channel. Blocks if the buffer is empty.

Returns:

  • (String)

    the packet



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.

Parameters:

  • data (String)

    the packet

Returns:

  • (nil)

Raises:

  • (ClosedWriteError)

    if the channel is closed

  • (RangeError)

    if the packet size exceeds the channel capacity



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