forked from jstarks/npiperelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlappedfile.go
97 lines (86 loc) · 1.97 KB
/
overlappedfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
43
44
45
46
47
48
49
50
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
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"io"
"os"
"sync"
"unsafe"
"golang.org/x/sys/windows"
)
var getOverlappedResultFunc = windows.MustLoadDLL("kernel32.dll").MustFindProc("GetOverlappedResult")
type overlappedFile struct {
h windows.Handle
m sync.Mutex
e []windows.Handle
}
func (f *overlappedFile) getEvent() windows.Handle {
f.m.Lock()
if len(f.e) == 0 {
f.m.Unlock()
e, err := windows.CreateEvent(nil, 0, 0, nil)
if err != nil {
panic(err)
}
return e
}
e := f.e[len(f.e)-1]
f.e = f.e[:len(f.e)-1]
f.m.Unlock()
return e
}
func (f *overlappedFile) putEvent(e windows.Handle) {
windows.ResetEvent(e)
f.m.Lock()
f.e = append(f.e, e)
f.m.Unlock()
}
func (f *overlappedFile) asyncIo(fn func(h windows.Handle, n *uint32, o *windows.Overlapped) error) (uint32, error) {
o := &windows.Overlapped{}
e := f.getEvent()
defer f.putEvent(e)
o.HEvent = e
var n uint32
err := fn(f.h, &n, o)
if err == windows.ERROR_IO_PENDING {
r, _, err := getOverlappedResultFunc.Call(uintptr(f.h), uintptr(unsafe.Pointer(o)), uintptr(unsafe.Pointer(&n)), 1)
if r == 0 {
return 0, err
}
} else if err != nil {
return 0, err
}
return n, nil
}
func (f *overlappedFile) Read(b []byte) (int, error) {
n, err := f.asyncIo(func(h windows.Handle, n *uint32, o *windows.Overlapped) error {
return windows.ReadFile(h, b, n, o)
})
err = os.NewSyscallError("read", err)
if err == nil && n == 0 && len(b) > 0 {
err = io.EOF
}
return int(n), err
}
func (f *overlappedFile) Write(b []byte) (int, error) {
n, err := f.asyncIo(func(h windows.Handle, n *uint32, o *windows.Overlapped) error {
return windows.WriteFile(h, b, n, o)
})
return int(n), os.NewSyscallError("write", err)
}
func (f *overlappedFile) Close() error {
err := windows.Close(f.h)
if err != nil {
panic(err)
}
f.h = 0
for _, h := range f.e {
err := windows.Close(h)
if err != nil {
panic(err)
}
}
f.e = nil
return nil
}
func newOverlappedFile(h windows.Handle) *overlappedFile {
return &overlappedFile{h: h}
}