Class: ConcurrentSHM::Channel::SingleBuffered::Fixed

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

Overview

A single-buffered channel with fixed-width packets.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ConcurrentSHM::Channel

#close

Class Method Details

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

Allocates a single-unbuffered, fixed-width channel with the specified width.

Raises:

  • (RangeError)

    if width is less than 1

See Also:



174
175
176
177
178
179
180
181
182
# File 'lib/concurrent-shm/channel.rb', line 174

def self.new(shm, width:, offset: 0, autosize: true)
  raise RangeError, "Width must be > 0" unless width > 0

  alloc(shm, 2 + width, offset: offset, autosize: autosize) do |body|
    @state = body[0].as_intptr
    @closed = body[1].as_intptr
    @data = body[2..]
  end
end

Instance Method Details

#recvString

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

Returns:

  • (String)

    the packet



194
195
196
197
198
# File 'lib/concurrent-shm/channel.rb', line 194

def recv
  do_recv do
    @data.read
  end
end

#send(data) ⇒ nil

Send a fixed-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 width



185
186
187
188
189
190
191
# File 'lib/concurrent-shm/channel.rb', line 185

def send(data)
  raise RangeError, "Data is wider than channel" if data.size > @data.size

  do_send do
    @data.write(data)
  end
end