2 Commits
Author SHA1 Message Date
esilva 312d979be1 added some new API calls
mainly around getting the file size and data read for convenience for
other tools
2026-09-16 23:47:35 +01:00
esilva 137b96fae6 Merge pull request 'Update api' (#2) from update-api into master
Reviewed-on: #2
2026-09-16 21:39:30 +01:00
9 changed files with 81 additions and 7 deletions
+17 -1
View File
@@ -47,7 +47,7 @@ type BeamNGSDK struct {
func NewBngSDK(opts Options) (*BeamNGSDK, error) {
sdk := BeamNGSDK{
Opts: opts,
buffer: make([]byte, outgaugeSize),
buffer: make([]byte, OutgaugeSize),
}
var err error
@@ -184,6 +184,22 @@ func (sdk *BeamNGSDK) Update() (*Outgauge, error) {
return &sdk.data, sdk.parseData(sdk.buffer)
}
func (sdk *BeamNGSDK) GetTotalRead() int64 {
return sdk.reader.GetTotalRead()
}
func (sdk *BeamNGSDK) GetTotalWritten() int64 {
return sdk.writer.GetTotalWritten()
}
func (sdk *BeamNGSDK) parseData(buffer []byte) error {
return sdk.data.ParseData(buffer)
}
func (sdk *BeamNGSDK) GetSourceSize() int64 {
if sizer, ok := sdk.reader.(Sizer); ok {
return sizer.Size()
}
return 0
}
+12 -2
View File
@@ -7,7 +7,7 @@ import (
)
var (
outgaugeSize = binary.Size(Outgauge{})
OutgaugeSize = int64(binary.Size(Outgauge{}))
ErrNotEnoughData = errors.New("buffer len is too short for Outgauge unpacking")
)
@@ -64,7 +64,7 @@ type Outgauge struct {
}
func (og *Outgauge) ParseData(buffer []byte) error {
if len(buffer) < outgaugeSize {
if int64(len(buffer)) < OutgaugeSize {
return ErrNotEnoughData
}
@@ -171,6 +171,11 @@ func (og *Outgauge) ABS() bool {
return og.ShowLights&DL_ABS != 0
}
// Spare reports whether spare was flipped
func (og *Outgauge) Spare() bool {
return og.ShowLights&DL_SPARE != 0
}
// ShowLights - functions to check if a given dash light is on [END]
// DashLights - functions to check if a given dash light is provided [START]
@@ -231,6 +236,11 @@ func (og *Outgauge) HasABSLight() bool {
return og.DashLights&DL_ABS != 0
}
// HasSpare reports whether spare was flipped
func (og *Outgauge) HasSpare() bool {
return og.DashLights&DL_SPARE != 0
}
// DashLights - functions to check if a given dash light is provided [END]
// Flags - functions to check if a given flag is ON [START]
+1
View File
@@ -3,6 +3,7 @@ package bngsdk
type BngImporter interface {
Reset() error
Next([]byte) (int, error)
GetTotalRead() int64
Close() error
}
+19
View File
@@ -6,10 +6,15 @@ import (
"unsafe"
)
type Sizer interface {
Size() int64
}
type GobReader struct {
TotalRead int64
File *os.File
Buf []byte
size int64
}
func NewOgBinReader(fp string) *GobReader {
@@ -18,10 +23,16 @@ func NewOgBinReader(fp string) *GobReader {
return nil
}
info, err := bin.Stat()
if err != nil {
return nil
}
return &GobReader{
TotalRead: 0,
File: bin,
Buf: make([]byte, unsafe.Sizeof(Outgauge{})),
size: info.Size(),
}
}
@@ -55,3 +66,11 @@ func (g *GobReader) Next(buffer []byte) (int, error) {
return nBytes, nil
}
func (g *GobReader) GetTotalRead() int64 {
return g.TotalRead
}
func (g *GobReader) Size() int64 {
return g.size
}
+5 -1
View File
@@ -41,9 +41,13 @@ func (ogr *OgUDPReader) Next(buffer []byte) (int, error) {
}
// Check if enough data was received to fill our struct
if nBytes != outgaugeSize {
if int64(nBytes) != OutgaugeSize {
return 0, ErrInvalidOutgaugeData
}
return nBytes, nil
}
func (ogr *OgUDPReader) GetTotalRead() int64 {
return ogr.udpConnection.GetTotalBytes()
}
+12 -1
View File
@@ -32,6 +32,7 @@ type UDPTransport struct {
address *net.UDPAddr
connection *net.UDPConn
dataChan chan frame
totalBytes int64 // returns total bytes read in case of reader and written in case of writer
}
func NewUDPReader(ip string, port int) (*UDPTransport, error) {
@@ -110,7 +111,11 @@ func (ut *UDPTransport) udpSink() {
}
func (ut *UDPTransport) Write(data []byte) (int, error) {
return ut.connection.Write(data)
nBytes, err := ut.connection.Write(data)
ut.totalBytes += int64(nBytes)
return nBytes, err
}
func (ut *UDPTransport) Read(buffer []byte) (int, error) {
@@ -123,6 +128,8 @@ func (ut *UDPTransport) Read(buffer []byte) (int, error) {
nBytes := copy(buffer, latestFrame.Buf[:latestFrame.Len])
packetPool.Put(latestFrame.Buf)
ut.totalBytes += int64(nBytes)
return nBytes, nil
}
@@ -133,3 +140,7 @@ func (ut *UDPTransport) Close() error {
return nil
}
func (ut *UDPTransport) GetTotalBytes() int64 {
return ut.totalBytes
}
+1
View File
@@ -2,6 +2,7 @@ package bngsdk
type BngExporter interface {
Write([]byte) (int, error)
GetTotalWritten() int64
Close() error
}
+10 -2
View File
@@ -6,7 +6,8 @@ import (
)
type OgBinWriter struct {
file *os.File
file *os.File
totalBytes int64
}
func NewOgBinWriter(path string) (*OgBinWriter, error) {
@@ -34,5 +35,12 @@ func (ogw *OgBinWriter) Write(data []byte) (int, error) {
return 0, err
}
return len(data), nil
nBytes := len(data)
ogw.totalBytes += int64(nBytes)
return nBytes, nil
}
func (ogw *OgBinWriter) GetTotalWritten() int64 {
return ogw.totalBytes
}
+4
View File
@@ -27,3 +27,7 @@ func (sw *SocketWriter) Write(data []byte) (int, error) {
// slog.Debug("Writing", "data", data)
return sw.udpConnection.Write(data)
}
func (sw *SocketWriter) GetTotalWritten() int64 {
return sw.udpConnection.totalBytes
}