win32-pipe 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,65 +1,65 @@
1
- # The Win32 module serves as a namespace only
2
- module Win32
3
-
4
- # The Pipe::Client class encapsulates the client side of a named pipe
5
- # connection.
6
- #
7
- class Pipe::Client < Pipe
8
-
9
- # Create and return a new Pipe::Client instance. The +name+, +pipe_mode+,
10
- # and +open_mode+ are passed to the Win32::Pipe superclass.
11
- #
12
- # The default +pipe_mode+ is NOWAIT.
13
- #
14
- # The default +open_mode+ is FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH.
15
- #
16
- # In block form the client object is yield, and is automatically
17
- # disconnected and closed at the end of the block.
18
- #
19
- # Example:
20
- #
21
- # require 'win32/pipe'
22
- #
23
- # Pipe::Client.new('foo') do |pipe|
24
- # puts "Connected..."
25
- # pipe.write("Ruby rocks!")
26
- # data = pipe.read
27
- # puts "Got [#{data}] back from pipe server"
28
- # end
29
- #
30
- def initialize(name, pipe_mode = DEFAULT_PIPE_MODE, open_mode = DEFAULT_OPEN_MODE, pipe_buffer_size = DEFAULT_PIPE_BUFFER_SIZE)
31
- super(name, pipe_mode, open_mode, pipe_buffer_size)
32
-
33
- @pipe = CreateFile(
34
- @name,
35
- GENERIC_READ | GENERIC_WRITE,
36
- FILE_SHARE_READ | FILE_SHARE_WRITE,
37
- nil,
38
- OPEN_EXISTING,
39
- @open_mode,
40
- 0
41
- )
42
-
43
- error = FFI.errno
44
-
45
- if error == ERROR_PIPE_BUSY
46
- unless WaitNamedPipe(@name, NMPWAIT_WAIT_FOREVER)
47
- raise SystemCallError.new("WaitNamedPipe", error)
48
- end
49
- end
50
-
51
- if @pipe == INVALID_HANDLE_VALUE
52
- raise SystemCallError.new("CreateFile", error)
53
- end
54
-
55
- if block_given?
56
- begin
57
- yield self
58
- ensure
59
- disconnect
60
- close
61
- end
62
- end
63
- end
64
- end
65
- end
1
+ # The Win32 module serves as a namespace only
2
+ module Win32
3
+
4
+ # The Pipe::Client class encapsulates the client side of a named pipe
5
+ # connection.
6
+ #
7
+ class Pipe::Client < Pipe
8
+
9
+ # Create and return a new Pipe::Client instance. The +name+, +pipe_mode+,
10
+ # and +open_mode+ are passed to the Win32::Pipe superclass.
11
+ #
12
+ # The default +pipe_mode+ is NOWAIT.
13
+ #
14
+ # The default +open_mode+ is FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH.
15
+ #
16
+ # In block form the client object is yield, and is automatically
17
+ # disconnected and closed at the end of the block.
18
+ #
19
+ # Example:
20
+ #
21
+ # require 'win32/pipe'
22
+ #
23
+ # Pipe::Client.new('foo') do |pipe|
24
+ # puts "Connected..."
25
+ # pipe.write("Ruby rocks!")
26
+ # data = pipe.read
27
+ # puts "Got [#{data}] back from pipe server"
28
+ # end
29
+ #
30
+ def initialize(name, pipe_mode = DEFAULT_PIPE_MODE, open_mode = DEFAULT_OPEN_MODE, pipe_buffer_size = DEFAULT_PIPE_BUFFER_SIZE)
31
+ super(name, pipe_mode, open_mode, pipe_buffer_size)
32
+
33
+ @pipe = CreateFile(
34
+ @name,
35
+ GENERIC_READ | GENERIC_WRITE,
36
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
37
+ nil,
38
+ OPEN_EXISTING,
39
+ @open_mode,
40
+ 0
41
+ )
42
+
43
+ error = FFI.errno
44
+
45
+ if error == ERROR_PIPE_BUSY
46
+ unless WaitNamedPipe(@name, NMPWAIT_WAIT_FOREVER)
47
+ raise SystemCallError.new("WaitNamedPipe", error)
48
+ end
49
+ end
50
+
51
+ if @pipe == INVALID_HANDLE_VALUE
52
+ raise SystemCallError.new("CreateFile", error)
53
+ end
54
+
55
+ if block_given?
56
+ begin
57
+ yield self
58
+ ensure
59
+ disconnect
60
+ close
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,96 +1,96 @@
1
- # The Win32 module serves as a namespace only.
2
- module Win32
3
- # The Pipe::Server class encapsulates the server side of a named pipe
4
- # connection.
5
- class Pipe::Server < Pipe
6
-
7
- # Creates and returns a new Pipe::Server instance, using +name+ as the
8
- # name for the pipe. Note that this does not actually connect the pipe.
9
- # Use Pipe::Server#connect for that.
10
- #
11
- # The default pipe_mode is PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT.
12
- #
13
- # The default open_mode is Pipe::ACCESS_DUPLEX.
14
- #--
15
- # The default pipe_mode also happens to be 0.
16
- #
17
- def initialize(name, pipe_mode = 0, open_mode = Pipe::ACCESS_DUPLEX, pipe_buffer_size = DEFAULT_PIPE_BUFFER_SIZE)
18
- super(name, pipe_mode, open_mode, pipe_buffer_size)
19
-
20
- @pipe = CreateNamedPipe(
21
- @name,
22
- @open_mode,
23
- @pipe_mode,
24
- PIPE_UNLIMITED_INSTANCES,
25
- pipe_buffer_size,
26
- pipe_buffer_size,
27
- PIPE_TIMEOUT,
28
- nil
29
- )
30
-
31
- if @pipe == INVALID_HANDLE_VALUE
32
- raise SystemCallError.new("CreateNamedPipe", FFI.errno)
33
- end
34
-
35
- if block_given?
36
- begin
37
- yield self
38
- ensure
39
- close
40
- end
41
- end
42
- end
43
-
44
- # Enables the named pipe server process to wait for a client process
45
- # to connect to an instance of a named pipe. In other words, it puts
46
- # the server in 'connection wait' status.
47
- #
48
- # In synchronous mode always returns true on success. In asynchronous
49
- # mode returns true if there is pending IO, or false otherwise.
50
- #
51
- def connect
52
- if @asynchronous
53
- # An overlapped ConnectNamedPipe should return 0
54
- if ConnectNamedPipe(@pipe, @overlapped)
55
- raise SystemCallError.new("ConnectNamedPipe", FFI.errno)
56
- end
57
-
58
- error = GetLastError()
59
-
60
- case error
61
- when ERROR_IO_PENDING
62
- @pending_io = true
63
- when ERROR_PIPE_CONNECTED
64
- unless SetEvent(@event)
65
- raise Error, get_last_error(error)
66
- end
67
- when ERROR_PIPE_LISTENING, ERROR_SUCCESS
68
- # Do nothing
69
- else
70
- raise Error, get_last_error(error)
71
- end
72
-
73
- if @pending_io
74
- return false
75
- else
76
- return true
77
- end
78
- else
79
- unless ConnectNamedPipe(@pipe, nil)
80
- raise SystemCallError.new("ConnectNamedPipe", FFI.errno)
81
- end
82
- end
83
-
84
- return true
85
- end
86
-
87
- # Close the server. This will flush file buffers, disconnect the
88
- # pipe, and close the pipe handle.
89
- #
90
- def close
91
- FlushFileBuffers(@pipe)
92
- DisconnectNamedPipe(@pipe)
93
- super
94
- end
95
- end
96
- end
1
+ # The Win32 module serves as a namespace only.
2
+ module Win32
3
+ # The Pipe::Server class encapsulates the server side of a named pipe
4
+ # connection.
5
+ class Pipe::Server < Pipe
6
+
7
+ # Creates and returns a new Pipe::Server instance, using +name+ as the
8
+ # name for the pipe. Note that this does not actually connect the pipe.
9
+ # Use Pipe::Server#connect for that.
10
+ #
11
+ # The default pipe_mode is PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT.
12
+ #
13
+ # The default open_mode is Pipe::ACCESS_DUPLEX.
14
+ #--
15
+ # The default pipe_mode also happens to be 0.
16
+ #
17
+ def initialize(name, pipe_mode = 0, open_mode = Pipe::ACCESS_DUPLEX, pipe_buffer_size = DEFAULT_PIPE_BUFFER_SIZE)
18
+ super(name, pipe_mode, open_mode, pipe_buffer_size)
19
+
20
+ @pipe = CreateNamedPipe(
21
+ @name,
22
+ @open_mode,
23
+ @pipe_mode,
24
+ PIPE_UNLIMITED_INSTANCES,
25
+ pipe_buffer_size,
26
+ pipe_buffer_size,
27
+ PIPE_TIMEOUT,
28
+ nil
29
+ )
30
+
31
+ if @pipe == INVALID_HANDLE_VALUE
32
+ raise SystemCallError.new("CreateNamedPipe", FFI.errno)
33
+ end
34
+
35
+ if block_given?
36
+ begin
37
+ yield self
38
+ ensure
39
+ close
40
+ end
41
+ end
42
+ end
43
+
44
+ # Enables the named pipe server process to wait for a client process
45
+ # to connect to an instance of a named pipe. In other words, it puts
46
+ # the server in 'connection wait' status.
47
+ #
48
+ # In synchronous mode always returns true on success. In asynchronous
49
+ # mode returns true if there is pending IO, or false otherwise.
50
+ #
51
+ def connect
52
+ if @asynchronous
53
+ # An overlapped ConnectNamedPipe should return 0
54
+ if ConnectNamedPipe(@pipe, @overlapped)
55
+ raise SystemCallError.new("ConnectNamedPipe", FFI.errno)
56
+ end
57
+
58
+ error = GetLastError()
59
+
60
+ case error
61
+ when ERROR_IO_PENDING
62
+ @pending_io = true
63
+ when ERROR_PIPE_CONNECTED
64
+ unless SetEvent(@event)
65
+ raise Error, get_last_error(error)
66
+ end
67
+ when ERROR_PIPE_LISTENING, ERROR_SUCCESS
68
+ # Do nothing
69
+ else
70
+ raise Error, get_last_error(error)
71
+ end
72
+
73
+ if @pending_io
74
+ return false
75
+ else
76
+ return true
77
+ end
78
+ else
79
+ unless ConnectNamedPipe(@pipe, nil)
80
+ raise SystemCallError.new("ConnectNamedPipe", FFI.errno)
81
+ end
82
+ end
83
+
84
+ return true
85
+ end
86
+
87
+ # Close the server. This will flush file buffers, disconnect the
88
+ # pipe, and close the pipe handle.
89
+ #
90
+ def close
91
+ FlushFileBuffers(@pipe)
92
+ DisconnectNamedPipe(@pipe)
93
+ super
94
+ end
95
+ end
96
+ end
@@ -1,46 +1,46 @@
1
- require 'ffi'
2
-
3
- module Windows
4
- module Constants
5
- include FFI::Library
6
-
7
- PIPE_WAIT = 0x00000000
8
- PIPE_NOWAIT = 0x00000001
9
- PIPE_ACCESS_INBOUND = 0x00000001
10
- PIPE_ACCESS_OUTBOUND = 0x00000002
11
- PIPE_ACCESS_DUPLEX = 0x00000003
12
- PIPE_TYPE_BYTE = 0x00000000
13
- PIPE_TYPE_MESSAGE = 0x00000004
14
- PIPE_READMODE_BYTE = 0x00000000
15
- PIPE_READMODE_MESSAGE = 0x00000002
16
- PIPE_CLIENT_END = 0x00000000
17
- PIPE_SERVER_END = 0x00000001
18
-
19
- PIPE_UNLIMITED_INSTANCES = 255
20
-
21
- FILE_FLAG_OVERLAPPED = 0x40000000
22
- FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000
23
- FILE_FLAG_WRITE_THROUGH = 0x80000000
24
- FILE_ATTRIBUTE_NORMAL = 0x00000080
25
-
26
- INFINITE = 0xFFFFFFFF
27
- NMPWAIT_WAIT_FOREVER = 0xFFFFFFFF
28
-
29
- ERROR_PIPE_BUSY = 231
30
- ERROR_IO_PENDING = 997
31
- ERROR_PIPE_CONNECTED = 535
32
- ERROR_PIPE_LISTENING = 536
33
- ERROR_SUCCESS = 0
34
-
35
- WAIT_TIMEOUT = 0x102
36
- WAIT_OBJECT_0 = 0
37
-
38
- GENERIC_READ = 0x80000000
39
- GENERIC_WRITE = 0x40000000
40
- FILE_SHARE_READ = 1
41
- FILE_SHARE_WRITE = 2
42
- OPEN_EXISTING = 3
43
-
44
- INVALID_HANDLE_VALUE = FFI::Pointer.new(-1).address
45
- end
46
- end
1
+ require 'ffi'
2
+
3
+ module Windows
4
+ module Constants
5
+ include FFI::Library
6
+
7
+ PIPE_WAIT = 0x00000000
8
+ PIPE_NOWAIT = 0x00000001
9
+ PIPE_ACCESS_INBOUND = 0x00000001
10
+ PIPE_ACCESS_OUTBOUND = 0x00000002
11
+ PIPE_ACCESS_DUPLEX = 0x00000003
12
+ PIPE_TYPE_BYTE = 0x00000000
13
+ PIPE_TYPE_MESSAGE = 0x00000004
14
+ PIPE_READMODE_BYTE = 0x00000000
15
+ PIPE_READMODE_MESSAGE = 0x00000002
16
+ PIPE_CLIENT_END = 0x00000000
17
+ PIPE_SERVER_END = 0x00000001
18
+
19
+ PIPE_UNLIMITED_INSTANCES = 255
20
+
21
+ FILE_FLAG_OVERLAPPED = 0x40000000
22
+ FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000
23
+ FILE_FLAG_WRITE_THROUGH = 0x80000000
24
+ FILE_ATTRIBUTE_NORMAL = 0x00000080
25
+
26
+ INFINITE = 0xFFFFFFFF
27
+ NMPWAIT_WAIT_FOREVER = 0xFFFFFFFF
28
+
29
+ ERROR_PIPE_BUSY = 231
30
+ ERROR_IO_PENDING = 997
31
+ ERROR_PIPE_CONNECTED = 535
32
+ ERROR_PIPE_LISTENING = 536
33
+ ERROR_SUCCESS = 0
34
+
35
+ WAIT_TIMEOUT = 0x102
36
+ WAIT_OBJECT_0 = 0
37
+
38
+ GENERIC_READ = 0x80000000
39
+ GENERIC_WRITE = 0x40000000
40
+ FILE_SHARE_READ = 1
41
+ FILE_SHARE_WRITE = 2
42
+ OPEN_EXISTING = 3
43
+
44
+ INVALID_HANDLE_VALUE = FFI::Pointer.new(-1).address
45
+ end
46
+ end
@@ -1,37 +1,37 @@
1
- require 'ffi'
2
-
3
- module Windows
4
- module Functions
5
- extend FFI::Library
6
-
7
- typedef :ulong, :dword
8
- typedef :uintptr_t, :handle
9
- typedef :pointer, :ptr
10
- typedef :string, :str
11
-
12
- ffi_lib :kernel32
13
-
14
- module FFI::Library
15
- # Wrapper method for attach_function + private
16
- def attach_pfunc(*args)
17
- attach_function(*args)
18
- private args[0]
19
- end
20
- end
21
-
22
- attach_pfunc :CloseHandle, [:handle], :bool
23
- attach_pfunc :ConnectNamedPipe, [:handle, :ptr], :bool
24
- attach_pfunc :CreateEvent, :CreateEventA, [:ptr, :bool, :bool, :str], :handle
25
- attach_pfunc :CreateFile, :CreateFileA, [:str, :dword, :dword, :ptr, :dword, :dword, :handle], :handle
26
- attach_pfunc :CreateNamedPipe, :CreateNamedPipeA, [:str, :dword, :dword, :dword, :dword, :dword, :dword, :ptr], :handle
27
- attach_pfunc :CreatePipe, [:ptr, :ptr, :ptr, :dword], :bool
28
- attach_pfunc :DisconnectNamedPipe, [:handle], :bool
29
- attach_pfunc :FlushFileBuffers, [:handle], :bool
30
- attach_pfunc :GetLastError, [], :dword
31
- attach_pfunc :GetOverlappedResult, [:handle, :ptr, :ptr, :bool], :bool
32
- attach_pfunc :ReadFile, [:handle, :buffer_out, :dword, :ptr, :ptr], :bool
33
- attach_pfunc :WaitForSingleObject, [:handle, :dword], :dword
34
- attach_pfunc :WaitNamedPipe, :WaitNamedPipeA, [:str, :dword], :bool
35
- attach_pfunc :WriteFile, [:handle, :buffer_in, :dword, :ptr, :ptr], :bool
36
- end
37
- end
1
+ require 'ffi'
2
+
3
+ module Windows
4
+ module Functions
5
+ extend FFI::Library
6
+
7
+ typedef :ulong, :dword
8
+ typedef :uintptr_t, :handle
9
+ typedef :pointer, :ptr
10
+ typedef :string, :str
11
+
12
+ ffi_lib :kernel32
13
+
14
+ module FFI::Library
15
+ # Wrapper method for attach_function + private
16
+ def attach_pfunc(*args)
17
+ attach_function(*args)
18
+ private args[0]
19
+ end
20
+ end
21
+
22
+ attach_pfunc :CloseHandle, [:handle], :bool
23
+ attach_pfunc :ConnectNamedPipe, [:handle, :ptr], :bool
24
+ attach_pfunc :CreateEvent, :CreateEventA, [:ptr, :int, :int, :str], :handle
25
+ attach_pfunc :CreateFile, :CreateFileA, [:str, :dword, :dword, :ptr, :dword, :dword, :handle], :handle
26
+ attach_pfunc :CreateNamedPipe, :CreateNamedPipeA, [:str, :dword, :dword, :dword, :dword, :dword, :dword, :ptr], :handle
27
+ attach_pfunc :CreatePipe, [:ptr, :ptr, :ptr, :dword], :bool
28
+ attach_pfunc :DisconnectNamedPipe, [:handle], :bool
29
+ attach_pfunc :FlushFileBuffers, [:handle], :bool
30
+ attach_pfunc :GetLastError, [], :dword
31
+ attach_pfunc :GetOverlappedResult, [:handle, :ptr, :ptr, :int], :bool
32
+ attach_pfunc :ReadFile, [:handle, :buffer_out, :dword, :ptr, :ptr], :bool
33
+ attach_pfunc :WaitForSingleObject, [:handle, :dword], :dword
34
+ attach_pfunc :WaitNamedPipe, :WaitNamedPipeA, [:str, :dword], :bool
35
+ attach_pfunc :WriteFile, [:handle, :buffer_in, :dword, :ptr, :ptr], :bool
36
+ end
37
+ end