migrate counter

This commit is contained in:
Mark Puha 2025-02-09 09:36:41 +01:00
parent 553060f804
commit e08105f85b
2 changed files with 10 additions and 5 deletions

View file

@ -3,13 +3,15 @@ package adapter
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"sync/atomic"
"github.com/aarzilli/golua/lua" "github.com/aarzilli/golua/lua"
) )
// TODO: aSec sync is enough? // TODO: aSec sync is enough?
type Lua struct { type Lua struct {
state *lua.State state *lua.State
packetCounter atomic.Int64
} }
type LuaParams struct { type LuaParams struct {
@ -36,12 +38,15 @@ func (l *Lua) Close() {
l.state.Close() l.state.Close()
} }
func (l *Lua) Generate(msgType int64, data []byte, counter int64) ([]byte, error) { func (l *Lua) Generate(
msgType int64,
data []byte,
) ([]byte, error) {
l.state.GetGlobal("d_gen") l.state.GetGlobal("d_gen")
l.state.PushInteger(msgType) l.state.PushInteger(msgType)
l.state.PushBytes(data) l.state.PushBytes(data)
l.state.PushInteger(counter) l.state.PushInteger(l.packetCounter.Add(1))
if err := l.state.Call(3, 1); err != nil { if err := l.state.Call(3, 1); err != nil {
return nil, fmt.Errorf("Error calling Lua function: %v\n", err) return nil, fmt.Errorf("Error calling Lua function: %v\n", err)

View file

@ -26,7 +26,7 @@ func TestLua_Generate(t *testing.T) {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
l := newLua() l := newLua()
defer l.Close() defer l.Close()
got, err := l.Generate(1, []byte("test"), 10) got, err := l.Generate(1, []byte("test"))
if err != nil { if err != nil {
t.Errorf( t.Errorf(
"Lua.Generate() error = %v, wantErr %v", "Lua.Generate() error = %v, wantErr %v",
@ -36,7 +36,7 @@ func TestLua_Generate(t *testing.T) {
return return
} }
want := "10headertest" want := "1headertest"
if string(got) != want { if string(got) != want {
t.Errorf("Lua.Generate() = %v, want %v", string(got), want) t.Errorf("Lua.Generate() = %v, want %v", string(got), want)
} }