The live data also works now, but I have some refactoring to do

This commit is contained in:
2024-10-29 14:52:40 +00:00
parent dbf30c1d4e
commit 422d90e98a
7 changed files with 338 additions and 161 deletions
+38 -87
View File
@@ -1,108 +1,59 @@
//go:build windows && cgo
// +build windows,cgo
// I should rename winutils to something else but what this package does
// is interface some windows stuff that we need for the:
// - Broadcast Channel
// - Valid Data Event windows thing
package winutils
import (
"fmt"
"sync"
"ibtReader/sharedMem"
"io"
"time"
"unsafe"
"golang.org/x/sys/windows"
)
const (
WAIT_OBJECT_0 = 0
WAIT_TIMEOUT = 258
)
type Reader interface {
io.Reader
io.ReaderAt
io.ReadCloser
}
var (
once sync.Once
user32DLL *windows.LazyDLL = nil
)
type IRacingWinUtils struct {
Utils *utils
}
// openEvent opens a windows.Handle for a given event
func OpenEvent(eventName string) (windows.Handle, error) {
name, err := windows.UTF16PtrFromString(eventName)
func Init() (*IRacingWinUtils, error) {
u, err := newUtils()
if err != nil {
return 0, err
return nil, err
}
return &IRacingWinUtils{u}, nil
}
handle, err := windows.OpenEvent(windows.SYNCHRONIZE, false, name)
func (u *IRacingWinUtils) Close() {
u.Utils.Close()
}
// OpenMemMap returns a Reader interface that can be used to read the data
// No need to encapsulate it
func OpenMemMap(path string, size uint32) (Reader, error) {
file, err := sharedMem.Open(path, size)
if err != nil {
return 0, fmt.Errorf("error opening event %s: %w", eventName, err)
return nil, err
}
return handle, nil
return file, nil
}
// closeEvent closes a given windows.Handle
func CloseEvent(h windows.Handle) {
windows.CloseHandle(h)
// OpenWinEvent will open the named windows event
func (u *IRacingWinUtils) OpenWinEvent(name string) error {
return u.Utils.OpenEvent(name)
}
// openEvent waits for a good response for some given time
func CheckValidDataEvent(handle windows.Handle, timeout time.Duration) bool {
t0 := time.Now().UnixNano()
timeoutInt := uint32(timeout / time.Millisecond)
result, err := windows.WaitForSingleObject(handle, timeoutInt)
if err != nil {
remaining := timeoutInt - uint32((time.Now().UnixNano()-t0)/1000000)
if remaining > 0 {
time.Sleep(time.Duration(remaining) * time.Millisecond)
}
return false
}
// Check the result of the wait
if result == WAIT_OBJECT_0 {
return true
} else if result == WAIT_TIMEOUT {
return false
}
return false
// OpenWinEvent will open the broadcast channel
func (u *IRacingWinUtils) OpenBroadcastChannel(name string) error {
return u.Utils.OpenBroadcastChannel(name)
}
// loadUser32DLL loads the user32.dll which is used to create some processes
func loadUser32DLL() {
user32DLL = windows.NewLazyDLL("user32.dll")
}
// OpenBroadcastChannel opens up a broadcast channel to send commands to iracing
func OpenBroadcastChannel(name string) (uintptr, error) {
if user32DLL == nil {
once.Do(loadUser32DLL)
}
registerWindowsMessageW := user32DLL.NewProc("RegisterWindowMessageW")
msgPtr, err := windows.UTF16PtrFromString(name)
if err != nil {
return 0, err
}
ret, _, err := registerWindowsMessageW.Call(uintptr(unsafe.Pointer(msgPtr)))
if ret == 0 {
return 0, err
}
return ret, nil
}
// SendBroadcastMessage sends a message trough the broadcast channel
func SendBroadcastMessage(id, p1, p2 uintptr) error {
if user32DLL == nil {
once.Do(loadUser32DLL)
}
sendMsg := user32DLL.NewProc("SendNotifyMessageW")
ret, _, err := sendMsg.Call(0xffff, id, p1, p2)
if ret == 1 {
return nil
} else {
return err
}
// CheckValidDataEvent checks if our windows even is telling us we are good to go
func (u *IRacingWinUtils) CheckValidDataEvent(timeout time.Duration) bool {
return u.Utils.CheckValidDataEvent(timeout)
}