mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-05 04:47:40 +03:00
Fix usage of PowerUnregisterSuspendResumeNotification
This commit is contained in:
parent
7beca62e4f
commit
08e8c02fb1
4 changed files with 30 additions and 3 deletions
|
@ -11,6 +11,7 @@ func TestPowerEvents(t *testing.T) {
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
t.SkipNow()
|
t.SkipNow()
|
||||||
}
|
}
|
||||||
|
t.Parallel()
|
||||||
listener, err := NewEventListener(func(event int) {})
|
listener, err := NewEventListener(func(event int) {})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, listener)
|
require.NotNil(t, listener)
|
||||||
|
|
|
@ -42,6 +42,7 @@ var suspendResumeNotificationCallback = common.OnceValue(func() uintptr {
|
||||||
})
|
})
|
||||||
|
|
||||||
type powerEventListener struct {
|
type powerEventListener struct {
|
||||||
|
pinner myPinner
|
||||||
callback EventCallback
|
callback EventCallback
|
||||||
handle uintptr
|
handle uintptr
|
||||||
}
|
}
|
||||||
|
@ -61,6 +62,7 @@ func NewEventListener(callback EventCallback) (EventListener, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *powerEventListener) Start() error {
|
func (l *powerEventListener) Start() error {
|
||||||
|
l.pinner.Pin(&l.callback)
|
||||||
type DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct {
|
type DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct {
|
||||||
callback uintptr
|
callback uintptr
|
||||||
context unsafe.Pointer
|
context unsafe.Pointer
|
||||||
|
@ -77,15 +79,21 @@ func (l *powerEventListener) Start() error {
|
||||||
uintptr(unsafe.Pointer(&l.handle)),
|
uintptr(unsafe.Pointer(&l.handle)),
|
||||||
)
|
)
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
|
l.pinner.Unpin()
|
||||||
return errno
|
return errno
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *powerEventListener) Close() error {
|
func (l *powerEventListener) Close() error {
|
||||||
_, _, errno := syscall.SyscallN(procPowerUnregisterSuspendResumeNotification.Addr(), uintptr(unsafe.Pointer(&l.handle)))
|
if l.handle == 0 {
|
||||||
if errno != 0 {
|
return nil
|
||||||
return errno
|
|
||||||
}
|
}
|
||||||
|
defer l.pinner.Unpin()
|
||||||
|
r0, _, _ := syscall.SyscallN(procPowerUnregisterSuspendResumeNotification.Addr(), l.handle)
|
||||||
|
if r0 != windows.NO_ERROR {
|
||||||
|
return syscall.Errno(r0)
|
||||||
|
}
|
||||||
|
l.handle = 0
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
7
common/winpowrprof/pinner.go
Normal file
7
common/winpowrprof/pinner.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
//go:build go1.21
|
||||||
|
|
||||||
|
package winpowrprof
|
||||||
|
|
||||||
|
import "runtime"
|
||||||
|
|
||||||
|
type myPinner = runtime.Pinner
|
11
common/winpowrprof/pinner_compat.go
Normal file
11
common/winpowrprof/pinner_compat.go
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
//go:build !go1.21
|
||||||
|
|
||||||
|
package winpowrprof
|
||||||
|
|
||||||
|
type myPinner struct{}
|
||||||
|
|
||||||
|
func (p *myPinner) Pin(pointer any) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *myPinner) Unpin() {
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue