Compare commits
2
Commits
update-api
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
312d979be1 | ||
|
|
137b96fae6 |
@@ -47,7 +47,7 @@ type BeamNGSDK struct {
|
|||||||
func NewBngSDK(opts Options) (*BeamNGSDK, error) {
|
func NewBngSDK(opts Options) (*BeamNGSDK, error) {
|
||||||
sdk := BeamNGSDK{
|
sdk := BeamNGSDK{
|
||||||
Opts: opts,
|
Opts: opts,
|
||||||
buffer: make([]byte, outgaugeSize),
|
buffer: make([]byte, OutgaugeSize),
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
@@ -184,6 +184,22 @@ func (sdk *BeamNGSDK) Update() (*Outgauge, error) {
|
|||||||
return &sdk.data, sdk.parseData(sdk.buffer)
|
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 {
|
func (sdk *BeamNGSDK) parseData(buffer []byte) error {
|
||||||
return sdk.data.ParseData(buffer)
|
return sdk.data.ParseData(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sdk *BeamNGSDK) GetSourceSize() int64 {
|
||||||
|
if sizer, ok := sdk.reader.(Sizer); ok {
|
||||||
|
return sizer.Size()
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|||||||
+12
-2
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
outgaugeSize = binary.Size(Outgauge{})
|
OutgaugeSize = int64(binary.Size(Outgauge{}))
|
||||||
ErrNotEnoughData = errors.New("buffer len is too short for Outgauge unpacking")
|
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 {
|
func (og *Outgauge) ParseData(buffer []byte) error {
|
||||||
if len(buffer) < outgaugeSize {
|
if int64(len(buffer)) < OutgaugeSize {
|
||||||
return ErrNotEnoughData
|
return ErrNotEnoughData
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,6 +171,11 @@ func (og *Outgauge) ABS() bool {
|
|||||||
return og.ShowLights&DL_ABS != 0
|
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]
|
// ShowLights - functions to check if a given dash light is on [END]
|
||||||
|
|
||||||
// DashLights - functions to check if a given dash light is provided [START]
|
// 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
|
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]
|
// DashLights - functions to check if a given dash light is provided [END]
|
||||||
|
|
||||||
// Flags - functions to check if a given flag is ON [START]
|
// Flags - functions to check if a given flag is ON [START]
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package bngsdk
|
|||||||
type BngImporter interface {
|
type BngImporter interface {
|
||||||
Reset() error
|
Reset() error
|
||||||
Next([]byte) (int, error)
|
Next([]byte) (int, error)
|
||||||
|
GetTotalRead() int64
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,15 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Sizer interface {
|
||||||
|
Size() int64
|
||||||
|
}
|
||||||
|
|
||||||
type GobReader struct {
|
type GobReader struct {
|
||||||
TotalRead int64
|
TotalRead int64
|
||||||
File *os.File
|
File *os.File
|
||||||
Buf []byte
|
Buf []byte
|
||||||
|
size int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOgBinReader(fp string) *GobReader {
|
func NewOgBinReader(fp string) *GobReader {
|
||||||
@@ -18,10 +23,16 @@ func NewOgBinReader(fp string) *GobReader {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info, err := bin.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return &GobReader{
|
return &GobReader{
|
||||||
TotalRead: 0,
|
TotalRead: 0,
|
||||||
File: bin,
|
File: bin,
|
||||||
Buf: make([]byte, unsafe.Sizeof(Outgauge{})),
|
Buf: make([]byte, unsafe.Sizeof(Outgauge{})),
|
||||||
|
size: info.Size(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,3 +66,11 @@ func (g *GobReader) Next(buffer []byte) (int, error) {
|
|||||||
|
|
||||||
return nBytes, nil
|
return nBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *GobReader) GetTotalRead() int64 {
|
||||||
|
return g.TotalRead
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GobReader) Size() int64 {
|
||||||
|
return g.size
|
||||||
|
}
|
||||||
|
|||||||
+5
-1
@@ -41,9 +41,13 @@ func (ogr *OgUDPReader) Next(buffer []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if enough data was received to fill our struct
|
// Check if enough data was received to fill our struct
|
||||||
if nBytes != outgaugeSize {
|
if int64(nBytes) != OutgaugeSize {
|
||||||
return 0, ErrInvalidOutgaugeData
|
return 0, ErrInvalidOutgaugeData
|
||||||
}
|
}
|
||||||
|
|
||||||
return nBytes, nil
|
return nBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ogr *OgUDPReader) GetTotalRead() int64 {
|
||||||
|
return ogr.udpConnection.GetTotalBytes()
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ type UDPTransport struct {
|
|||||||
address *net.UDPAddr
|
address *net.UDPAddr
|
||||||
connection *net.UDPConn
|
connection *net.UDPConn
|
||||||
dataChan chan frame
|
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) {
|
func NewUDPReader(ip string, port int) (*UDPTransport, error) {
|
||||||
@@ -110,7 +111,11 @@ func (ut *UDPTransport) udpSink() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ut *UDPTransport) Write(data []byte) (int, error) {
|
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) {
|
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])
|
nBytes := copy(buffer, latestFrame.Buf[:latestFrame.Len])
|
||||||
packetPool.Put(latestFrame.Buf)
|
packetPool.Put(latestFrame.Buf)
|
||||||
|
|
||||||
|
ut.totalBytes += int64(nBytes)
|
||||||
|
|
||||||
return nBytes, nil
|
return nBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,3 +140,7 @@ func (ut *UDPTransport) Close() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ut *UDPTransport) GetTotalBytes() int64 {
|
||||||
|
return ut.totalBytes
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package bngsdk
|
|||||||
|
|
||||||
type BngExporter interface {
|
type BngExporter interface {
|
||||||
Write([]byte) (int, error)
|
Write([]byte) (int, error)
|
||||||
|
GetTotalWritten() int64
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-2
@@ -6,7 +6,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type OgBinWriter struct {
|
type OgBinWriter struct {
|
||||||
file *os.File
|
file *os.File
|
||||||
|
totalBytes int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOgBinWriter(path string) (*OgBinWriter, error) {
|
func NewOgBinWriter(path string) (*OgBinWriter, error) {
|
||||||
@@ -34,5 +35,12 @@ func (ogw *OgBinWriter) Write(data []byte) (int, error) {
|
|||||||
return 0, err
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,3 +27,7 @@ func (sw *SocketWriter) Write(data []byte) (int, error) {
|
|||||||
// slog.Debug("Writing", "data", data)
|
// slog.Debug("Writing", "data", data)
|
||||||
return sw.udpConnection.Write(data)
|
return sw.udpConnection.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sw *SocketWriter) GetTotalWritten() int64 {
|
||||||
|
return sw.udpConnection.totalBytes
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user