Documentation ¶
Index ¶
- type Handle
- func (f *Handle) Close() error
- func (f *Handle) Custom(fn func(windows.Handle, []byte, *uint32, *windows.Overlapped) error, b []byte, ...) (int, error)
- func (f *Handle) Read(b []byte) (int, error)
- func (f *Handle) ReadAt(b []byte, off int64) (int, error)
- func (f *Handle) ReadTimeout(b []byte, milliseconds int) (int, error)
- func (f *Handle) Write(b []byte) (int, error)
- func (f *Handle) WriteAt(b []byte, off int64) (int, error)
- func (f *Handle) WriteTimeout(b []byte, milliseconds int) (int, error)
- type PositionHandle
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handle ¶
type Handle struct { // DefaultTimeout allows changing the default timeout used for Write() and Read() functions. Defaults to -1 (blocking) DefaultTimeout int // contains filtered or unexported fields }
func New ¶
New returns a new overlapped.Handle that wraps the original handle, allowing both asynchronous and synchronous I/O.
func (*Handle) Close ¶
Close cancels all currently waiting reads/writes, closes the wrapped handle, as well as all event handles created for overlapped I/O.
func (*Handle) Custom ¶ added in v1.0.3
func (f *Handle) Custom(fn func(windows.Handle, []byte, *uint32, *windows.Overlapped) error, b []byte, milliseconds int) (int, error)
Custom allows I/O using function that has the same signature as windows.ReadFile/windows.WriteFile
Example ¶
f, h, err := newFile() if err != nil { return } defer os.Remove(f.Name()) defer windows.CloseHandle(h) oh := New(h) defer oh.Close() go func() { fmt.Fprint(oh, "Bacon") }() time.Sleep(time.Millisecond * 10) b := make([]byte, 10) fn := func(h windows.Handle, b []byte, n *uint32, o *windows.Overlapped) error { *n = uint32(copy(b, []byte("Foobar"))) return nil } n, _ := oh.Custom(fn, b, -1) fmt.Print(string(b[:n]))
Output: Foobar
func (*Handle) Read ¶
Read reads from the wrapped handle, using the timeout set in Handle.DefaultTimeout (default: -1)
Example ¶
f, h, err := newFile() if err != nil { return } defer os.Remove(f.Name()) defer windows.CloseHandle(h) oh := New(h) defer oh.Close() go func() { fmt.Fprint(oh, "Foobar") }() time.Sleep(time.Millisecond * 10) b := make([]byte, 10) n, _ := oh.Read(b) fmt.Print(string(b[:n]))
Output: Foobar
func (*Handle) ReadAt ¶
ReadAt reads from specific offset, using the timeout set in Handle.DefaultTimeout (default: -1)
Example ¶
f, h, _ := newFile() defer os.Remove(f.Name()) oh := New(h) defer oh.Close() fmt.Fprint(oh, "Bacon") b := make([]byte, 3) var pos int64 = 0 for i := 0; i < 3; i++ { n, err := oh.ReadAt(b, pos) pos += int64(n) fmt.Println(string(b[:n]), err)
Output: Bac <nil> on EOF EOF
func (*Handle) ReadTimeout ¶
ReadTimeout reads from the wrapped handle, allowing custom timeout to be set
-1: blocking 0: non-blocking >0: with timeout
func (*Handle) Write ¶
Read writes to the wrapped handle, using the timeout set in Handle.DefaultTimeout (default: -1)
type PositionHandle ¶
type PositionHandle struct { *Handle // contains filtered or unexported fields }
Positionhandle wraps overlapped.Handle, keeping a pointer at the location where last read/write ended. The position is shared between writes and reads, so a PositionHandle should not be shared between different uses, instead a new PositionHandle can be created for each client if needed.
func (*PositionHandle) Read ¶
func (f *PositionHandle) Read(b []byte) (int, error)
Read reads forward from the current position and moves the position forward the amount read
Example ¶
f, h, _ := newFile() defer os.Remove(f.Name()) oh := New(h) defer oh.Close() fmt.Fprint(oh, "Bacon") oh2 := &PositionHandle{Handle: New(h)} b := make([]byte, 3) for i := 0; i < 3; i++ { n, err := oh2.Read(b) fmt.Println(string(b[:n]), err)
Output: Bac <nil> on <nil> EOF
func (*PositionHandle) Write ¶
func (f *PositionHandle) Write(b []byte) (int, error)
Write writes to the current position and moves the position forward the amount written
Example ¶
f, h, err := newFile() if err != nil { return } defer os.Remove(f.Name()) defer windows.CloseHandle(h) oh := New(h) ph := &PositionHandle{Handle: oh} defer oh.Close() ph.Write([]byte("Ba")) ph.Write([]byte("con")) rh := &PositionHandle{Handle: oh} b, _ := ioutil.ReadAll(rh) fmt.Print(string(b))
Output: Bacon