Skip to content

Commit

Permalink
fix : The semantics of the readBytes interface and other interfaces s…
Browse files Browse the repository at this point in the history
…uch as readInt32 are inconsistent #30
  • Loading branch information
pojol committed Sep 19, 2024
1 parent 0fb5671 commit 68c0984
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions driver/script/module/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"fmt"
"io"

lua "github.com/yuin/gopher-lua"
)
Expand All @@ -17,8 +18,8 @@ type Message struct {
buff []byte
byteSort string
msglen int
br *bytes.Reader
bw *bytes.Buffer
br *bytes.Reader // 用于读取
bw *bytes.Buffer // 用于写入
bs binary.ByteOrder
}

Expand Down Expand Up @@ -139,15 +140,22 @@ func readInt64(L *lua.LState) int {
}

func readString(L *lua.LState) int {

p := checkPerson(L)
begin := L.CheckNumber(2)
end := L.CheckNumber(3)
length := L.CheckInt(2)

if end == -1 {
L.Push(lua.LString(p.buff[int(begin):]))
if length == -1 {
// 读取剩余的所有数据
bytes, _ := io.ReadAll(p.br)
L.Push(lua.LString(bytes))
} else {
L.Push(lua.LString(p.buff[int(begin):int(end)]))
bytes := make([]byte, length)
_, err := io.ReadFull(p.br, bytes)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LString(bytes))
}

return 1
Expand Down

0 comments on commit 68c0984

Please sign in to comment.