Last active
November 1, 2017 23:40
-
-
Save rickhull/757f0c1740c8e9f612f26c9f90c696e6 to your computer and use it in GitHub Desktop.
Opens a named pipe in given path, creates the named pipe -file if needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Open one end of named pipe | |
# @argument pipe_path - path to fifo | |
# @argument flag - [r, w, w+, ... | |
# @argument wait_on_str - continue if can read] | |
# @returns named_pipe -handle | |
def named_pipe( pipe_path, flag, wait_on_str=nil ) | |
if File.exists? pipe_path | |
raise "bad #{pipe_path}" unless File.pipe? pipe_path | |
end | |
pipe_handle = File.mkfifo pipe_path | |
pipe_handle.open(flag) # ? | |
### not sure this is right, but this is the simpler strategy above ### | |
if wait_on_str | |
msg = pipe_handle.gets # blocks until content | |
unless msg&.include? wait_on_str | |
error = "no handshake #{msg} in #{pipe_path} - FAIL" | |
p_dbg error, self | |
raise error | |
end | |
end | |
pipe_handle | |
end | |
# Reads from a named pipe | |
# @agument timeout_secs_int - timeout for waiting on something to read | |
# @argument readlen_bytes - number of bytes to read | |
# @return nil if nothing to read | |
def read_bytes_nonblocking( pipe_handle_io, timeout_secs_int=0, readlen_bytes=2 ) | |
msg = nil | |
rwe = IO.select([pipe_handle_io], [], [], timeout_secs_int) | |
msg = rwe[0][0].read(readlen_bytes) unless rwe == nil | |
msg.strip if msg != nil | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment