Class: Win32::Pipe::Client

Inherits:
Win32::Pipe show all
Defined in:
lib/win32/pipe/client.rb

Overview

The Pipe::Client class encapsulates the client side of a named pipe connection.

Constant Summary

Constants inherited from Win32::Pipe

ACCESS_DUPLEX, ACCESS_INBOUND, ACCESS_OUTBOUND, DEFAULT_OPEN_MODE, DEFAULT_PIPE_BUFFER_SIZE, DEFAULT_PIPE_MODE, FIRST_PIPE_INSTANCE, NOWAIT, OVERLAPPED, PIPE_TIMEOUT, READMODE_BYTE, READMODE_MESSAGE, TYPE_BYTE, TYPE_MESSAGE, VERSION, WAIT, WRITE_THROUGH

Constants included from Windows::Constants

Windows::Constants::ERROR_IO_PENDING, Windows::Constants::ERROR_PIPE_BUSY, Windows::Constants::ERROR_PIPE_CONNECTED, Windows::Constants::ERROR_PIPE_LISTENING, Windows::Constants::ERROR_SUCCESS, Windows::Constants::FILE_ATTRIBUTE_NORMAL, Windows::Constants::FILE_FLAG_FIRST_PIPE_INSTANCE, Windows::Constants::FILE_FLAG_OVERLAPPED, Windows::Constants::FILE_FLAG_WRITE_THROUGH, Windows::Constants::FILE_SHARE_READ, Windows::Constants::FILE_SHARE_WRITE, Windows::Constants::GENERIC_READ, Windows::Constants::GENERIC_WRITE, Windows::Constants::INFINITE, Windows::Constants::INVALID_HANDLE_VALUE, Windows::Constants::NMPWAIT_WAIT_FOREVER, Windows::Constants::OPEN_EXISTING, Windows::Constants::PIPE_ACCESS_DUPLEX, Windows::Constants::PIPE_ACCESS_INBOUND, Windows::Constants::PIPE_ACCESS_OUTBOUND, Windows::Constants::PIPE_CLIENT_END, Windows::Constants::PIPE_NOWAIT, Windows::Constants::PIPE_READMODE_BYTE, Windows::Constants::PIPE_READMODE_MESSAGE, Windows::Constants::PIPE_SERVER_END, Windows::Constants::PIPE_TYPE_BYTE, Windows::Constants::PIPE_TYPE_MESSAGE, Windows::Constants::PIPE_UNLIMITED_INSTANCES, Windows::Constants::PIPE_WAIT, Windows::Constants::WAIT_OBJECT_0, Windows::Constants::WAIT_TIMEOUT

Instance Attribute Summary

Attributes inherited from Win32::Pipe

#buffer, #name, #open_mode, #pipe_mode, #size, #transferred

Instance Method Summary collapse

Methods inherited from Win32::Pipe

#asynchronous?, #close, #disconnect, #pending?, #read, #wait, #write

Constructor Details

#initialize(name, pipe_mode = DEFAULT_PIPE_MODE, open_mode = DEFAULT_OPEN_MODE, pipe_buffer_size = DEFAULT_PIPE_BUFFER_SIZE) ⇒ Client

Create and return a new Pipe::Client instance. The name, pipe_mode, and open_mode are passed to the Win32::Pipe superclass.

The default pipe_mode is NOWAIT.

The default open_mode is FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH.

In block form the client object is yield, and is automatically disconnected and closed at the end of the block.

Example:

 require 'win32/pipe'

 Pipe::Client.new('foo') do |pipe|
   puts "Connected..."
   pipe.write("Ruby rocks!")
   data = pipe.read
   puts "Got [#{data}] back from pipe server"
end


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/win32/pipe/client.rb', line 30

def initialize(name, pipe_mode = DEFAULT_PIPE_MODE, open_mode = DEFAULT_OPEN_MODE, pipe_buffer_size = DEFAULT_PIPE_BUFFER_SIZE)
  super(name, pipe_mode, open_mode, pipe_buffer_size)

  @pipe = CreateFile(
    @name,
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    nil,
    OPEN_EXISTING,
    @open_mode,
    0
  )

  error = FFI.errno

  if error == ERROR_PIPE_BUSY
    unless WaitNamedPipe(@name, NMPWAIT_WAIT_FOREVER)
      raise SystemCallError.new("WaitNamedPipe", error)
    end
  end

  if @pipe == INVALID_HANDLE_VALUE
    raise SystemCallError.new("CreateFile", error)
  end

  if block_given?
    begin
      yield self
    ensure
      disconnect
      close
    end
  end
end