Class: Win32::Pipe::Server

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

Overview

The Pipe::Server class encapsulates the server 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?, #disconnect, #pending?, #read, #wait, #write

Constructor Details

#initialize(name, pipe_mode = 0, open_mode = Pipe::ACCESS_DUPLEX, pipe_buffer_size = DEFAULT_PIPE_BUFFER_SIZE) ⇒ Server

Creates and returns a new Pipe::Server instance, using name as the name for the pipe. Note that this does not actually connect the pipe. Use Pipe::Server#connect for that.

The default pipe_mode is PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT.

The default open_mode is Pipe::ACCESS_DUPLEX. – The default pipe_mode also happens to be 0.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/win32/pipe/server.rb', line 17

def initialize(name, pipe_mode = 0, open_mode = Pipe::ACCESS_DUPLEX, pipe_buffer_size = DEFAULT_PIPE_BUFFER_SIZE)
  super(name, pipe_mode, open_mode, pipe_buffer_size)

  @pipe = CreateNamedPipe(
    @name,
    @open_mode,
    @pipe_mode,
    PIPE_UNLIMITED_INSTANCES,
    pipe_buffer_size,
    pipe_buffer_size,
    PIPE_TIMEOUT,
    nil
  )

  if @pipe == INVALID_HANDLE_VALUE
    raise SystemCallError.new("CreateNamedPipe", FFI.errno)
  end

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Instance Method Details

#closeObject

Close the server. This will flush file buffers, disconnect the pipe, and close the pipe handle.



90
91
92
93
94
# File 'lib/win32/pipe/server.rb', line 90

def close
  FlushFileBuffers(@pipe)
  DisconnectNamedPipe(@pipe)
  super
end

#connectObject

Enables the named pipe server process to wait for a client process to connect to an instance of a named pipe. In other words, it puts the server in ‘connection wait’ status.

In synchronous mode always returns true on success. In asynchronous mode returns true if there is pending IO, or false otherwise.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/win32/pipe/server.rb', line 51

def connect
  if @asynchronous
    # An overlapped ConnectNamedPipe should return 0
    if ConnectNamedPipe(@pipe, @overlapped)
      raise SystemCallError.new("ConnectNamedPipe", FFI.errno)
    end

    error = GetLastError()

    case error
      when ERROR_IO_PENDING
        @pending_io = true
      when ERROR_PIPE_CONNECTED
        unless SetEvent(@event)
          raise Error, get_last_error(error)
        end
      when ERROR_PIPE_LISTENING, ERROR_SUCCESS
        # Do nothing
      else
        raise Error, get_last_error(error)
    end

    if @pending_io
      return false
    else
      return true
    end
  else
    unless ConnectNamedPipe(@pipe, nil)
      raise SystemCallError.new("ConnectNamedPipe", FFI.errno)
    end
  end

  return true
end