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_MODE, FIRST_PIPE_INSTANCE, NOWAIT, OVERLAPPED, PIPE_BUFFER_SIZE, PIPE_TIMEOUT, READMODE_BYTE, READMODE_MESSAGE, TYPE_BYTE, TYPE_MESSAGE, VERSION, WAIT, WRITE_THROUGH

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) ⇒ 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)
   super(name, pipe_mode, open_mode)
   
   @pipe = CreateNamedPipe(
      @name,
      @open_mode,
      @pipe_mode,
      PIPE_UNLIMITED_INSTANCES,
      PIPE_BUFFER_SIZE,
      PIPE_BUFFER_SIZE,
      PIPE_TIMEOUT,
      0
   )
   
   if @pipe == INVALID_HANDLE_VALUE
      raise Error, get_last_error
   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 Error, get_last_error
      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
            # 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 Error, get_last_error
      end
   end

   return true
end