Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Goのシリアライザの32bit対応 #77

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/wsnet2-dashboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- run: go install golang.org/dl/go1.18.5@latest && go1.18.5 download

- run: go1.18.5 test wsnet2/binary
- run: GOARCH=386 go1.18.5 test wsnet2/binary

- run: GOARCH=386 go1.18.5 build

Expand Down
6 changes: 3 additions & 3 deletions server/binary/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func UnmarshalEvent(data []byte) (Event, int, error) {
if len(data) < 4 {
return nil, 0, xerrors.Errorf("data length not enough: %v", len(data))
}
seq := get32(data)
seq := int(get32(data))
data = data[4:]

return &RegularEvent{et, data}, seq, nil
Expand Down Expand Up @@ -194,7 +194,7 @@ func UnmarshalEvPeerReadyPayload(payload []byte) (int, error) {
// - dict: last msg timestamps of each player.
func NewEvPong(pingtime uint64, watchers uint32, lastMsg Dict) *SystemEvent {
payload := MarshalULong(pingtime)
payload = append(payload, MarshalUInt(int(watchers))...)
payload = append(payload, MarshalUInt(int64(watchers))...)
payload = append(payload, MarshalDict(lastMsg)...)

return &SystemEvent{
Expand Down Expand Up @@ -225,7 +225,7 @@ func UnmarshalEvPongPayload(payload []byte) (*EvPongPayload, error) {
if e != nil {
return nil, xerrors.Errorf("Invalid EvPong payload (watchers): %w", e)
}
pp.Watchers = uint32(d.(int))
pp.Watchers = uint32(d.(int64))
payload = payload[l:]

// lastmsg
Expand Down
87 changes: 44 additions & 43 deletions server/binary/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,31 +201,31 @@ func unmarshalShort(src []byte) (int, int, error) {
}

// MarshalUInt marshals unsigned 32bit integer
func MarshalUInt(val int) []byte {
func MarshalUInt(val int64) []byte {
v := clamp(int64(val), 0, math.MaxUint32)
buf := make([]byte, 1+UIntDataSize)
buf[0] = byte(TypeUInt)
put32(buf[1:], v)
return buf
}

func unmarshalUInt(src []byte) (int, int, error) {
func unmarshalUInt(src []byte) (int64, int, error) {
if len(src) < 1+UIntDataSize {
return 0, 0, xerrors.Errorf("Unmarshal UInt error: not enough data (%v)", len(src))
}
return get32(src[1:]), 1 + UIntDataSize, nil
}

// MarshalInt marshals signed 32bit integer comparably
func MarshalInt(val int) []byte {
func MarshalInt(val int64) []byte {
v := clamp(int64(val), math.MinInt32, math.MaxInt32)
buf := make([]byte, 1+IntDataSize)
buf[0] = byte(TypeInt)
put32(buf[1:], v-math.MinInt32)
return buf
}

func unmarshalInt(src []byte) (int, int, error) {
func unmarshalInt(src []byte) (int64, int, error) {
if len(src) < 1+IntDataSize {
return 0, 0, xerrors.Errorf("Unmarshal Int error: not enough data (%v)", len(src))
}
Expand Down Expand Up @@ -773,7 +773,7 @@ func unmarshalUShorts(src []byte) ([]int, int, error) {
// - TypeInts
// - 16bit count
// - repeat: 32bit BE integer...
func MarshalInts(vals []int) []byte {
func MarshalInts(vals []int64) []byte {
if vals == nil {
return MarshalNull()
}
Expand All @@ -787,14 +787,14 @@ func MarshalInts(vals []int) []byte {
put16(buf[1:], int64(count))

for i := 0; i < count; i++ {
v := clamp(int64(vals[i]), math.MinInt32, math.MaxInt32) - math.MinInt32
v := clamp(vals[i], math.MinInt32, math.MaxInt32) - math.MinInt32
put32(buf[3+i*IntDataSize:], v)
}

return buf
}

func unmarshalInts(src []byte) ([]int, int, error) {
func unmarshalInts(src []byte) ([]int64, int, error) {
if len(src) < 3 {
return nil, 0, xerrors.Errorf("Unmarshal Intts error: not enough data (%v)", len(src))
}
Expand All @@ -803,7 +803,7 @@ func unmarshalInts(src []byte) ([]int, int, error) {
if len(src) < l {
return nil, 0, xerrors.Errorf("Unmarshal Ints error: not enough data (%v)", len(src))
}
vals := make([]int, count)
vals := make([]int64, count)
for i := 0; i < count; i++ {
vals[i] = get32(src[3+i*IntDataSize:]) + math.MinInt32
}
Expand All @@ -815,7 +815,7 @@ func unmarshalInts(src []byte) ([]int, int, error) {
// - TypeUInts
// - 16bit count
// - repeat: 32bit BE integer...
func MarshalUInts(vals []int) []byte {
func MarshalUInts(vals []int64) []byte {
if vals == nil {
return MarshalNull()
}
Expand All @@ -829,14 +829,14 @@ func MarshalUInts(vals []int) []byte {
put16(buf[1:], int64(count))

for i := 0; i < count; i++ {
v := clamp(int64(vals[i]), 0, math.MaxUint32)
v := clamp(vals[i], 0, math.MaxUint32)
put32(buf[3+i*UIntDataSize:], v)
}

return buf
}

func unmarshalUInts(src []byte) ([]int, int, error) {
func unmarshalUInts(src []byte) ([]int64, int, error) {
if len(src) < 3 {
return nil, 0, xerrors.Errorf("Unmarshal UInts error: not enough data (%v)", len(src))
}
Expand All @@ -845,7 +845,7 @@ func unmarshalUInts(src []byte) ([]int, int, error) {
if len(src) < l {
return nil, 0, xerrors.Errorf("Unmarshal UInts error: not enough data (%v)", len(src))
}
vals := make([]int, count)
vals := make([]int64, count)
for i := 0; i < count; i++ {
vals[i] = get32(src[3+i*UIntDataSize:])
}
Expand Down Expand Up @@ -1182,62 +1182,63 @@ func get8(src []byte) int {
}

func put16(dst []byte, val int64) {
dst[0] = byte((val & 0xff00) >> 8)
dst[1] = byte(val & 0xff)
dst[0] = byte((val & 0xff00) >> 8)
}

func get16(src []byte) int {
v := int(src[0]) << 8
v += int(src[1])
v := int(src[1])
v |= int(src[0]) << 8
return v
}

func put24(dst []byte, val int64) {
dst[0] = byte((val & 0xff0000) >> 16)
dst[1] = byte((val & 0xff00) >> 8)
dst[2] = byte(val & 0xff)
dst[1] = byte((val & 0xff00) >> 8)
dst[0] = byte((val & 0xff0000) >> 16)
}

func get24(src []byte) int {
i := int(src[0]) << 16
i += int(src[1]) << 8
i += int(src[2])
i := int(src[2])
i |= int(src[1]) << 8
i |= int(src[0]) << 16
return i
}

func put32(dst []byte, val int64) {
dst[0] = byte((val & 0xff000000) >> 24)
dst[1] = byte((val & 0xff0000) >> 16)
dst[2] = byte((val & 0xff00) >> 8)
dst[3] = byte(val & 0xff)
dst[2] = byte((val & 0xff00) >> 8)
dst[1] = byte((val & 0xff0000) >> 16)
dst[0] = byte((val & 0xff000000) >> 24)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

binary.BigEndian.PutUint32(dst, uint32(val))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最適化を標準ライブラリに任せたほうがいいですねたしかに。

}
func get32(src []byte) int {
i := int(src[0]) << 24
i += int(src[1]) << 16
i += int(src[2]) << 8
i += int(src[3])

func get32(src []byte) int64 {
i := int64(src[3])
i |= int64(src[2]) << 8
i |= int64(src[1]) << 16
i |= int64(src[0]) << 24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return int64(binary.BigEndian.Uint32(src))

return i
}

func put64(dst []byte, val uint64) {
dst[0] = byte((val & 0xff00000000000000) >> 56)
dst[1] = byte((val & 0xff000000000000) >> 48)
dst[2] = byte((val & 0xff0000000000) >> 40)
dst[3] = byte((val & 0xff00000000) >> 32)
dst[4] = byte((val & 0xff000000) >> 24)
dst[5] = byte((val & 0xff0000) >> 16)
dst[6] = byte((val & 0xff00) >> 8)
dst[7] = byte(val & 0xff)
dst[6] = byte((val & 0xff00) >> 8)
dst[5] = byte((val & 0xff0000) >> 16)
dst[4] = byte((val & 0xff000000) >> 24)
dst[3] = byte((val & 0xff00000000) >> 32)
dst[2] = byte((val & 0xff0000000000) >> 40)
dst[1] = byte((val & 0xff000000000000) >> 48)
dst[0] = byte((val & 0xff00000000000000) >> 56)
}

func get64(src []byte) uint64 {
i := uint64(src[0]) << 56
i += uint64(src[1]) << 48
i += uint64(src[2]) << 40
i += uint64(src[3]) << 32
i += uint64(src[4]) << 24
i += uint64(src[5]) << 16
i += uint64(src[6]) << 8
i += uint64(src[7])
i := uint64(src[7])
i |= uint64(src[6]) << 8
i |= uint64(src[5]) << 16
i |= uint64(src[4]) << 24
i |= uint64(src[3]) << 32
i |= uint64(src[2]) << 40
i |= uint64(src[1]) << 48
i |= uint64(src[0]) << 56
return i
}
80 changes: 70 additions & 10 deletions server/binary/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,33 @@ func TestMarshalInteger(t *testing.T) {
{MarshalShort, 1, 1, []byte{byte(TypeShort), 0x80, 0x01}},
{MarshalShort, 0x7fff, 0x7fff, []byte{byte(TypeShort), 0xff, 0xff}},
{MarshalShort, 0x8000, 0x7fff, []byte{byte(TypeShort), 0xff, 0xff}},
}
for _, test := range tests {
b := test.marshal(test.in)
if !reflect.DeepEqual(b, test.buf) {
fname := runtime.FuncForPC(reflect.ValueOf(test.marshal).Pointer()).Name()
t.Fatalf("%s(%x):\n%#v\n%#v", fname, test.in, b, test.buf)
}
r, l, e := Unmarshal(b)
if e != nil {
fname := runtime.FuncForPC(reflect.ValueOf(test.marshal).Pointer()).Name()
t.Fatalf("%s(%x): Unmarshal error: %v", fname, test.in, e)
}
if r != test.out || l != len(test.buf) {
fname := runtime.FuncForPC(reflect.ValueOf(test.marshal).Pointer()).Name()
t.Fatalf("%s(%x): Unmarshal = %v (len=%v) wants %v (len=%v)",
fname, test.in, r, l, test.out, len(test.buf))
}
}
}

func TestMarshalInteger32(t *testing.T) {
tests := []struct {
marshal func(int64) []byte
in int64
out int64
buf []byte
}{
{MarshalUInt, -1, 0, []byte{byte(TypeUInt), 0x00, 0x00, 0x00, 0x00}},
{MarshalUInt, 0, 0, []byte{byte(TypeUInt), 0x00, 0x00, 0x00, 0x00}},
{MarshalUInt, 0x01020304, 0x01020304, []byte{byte(TypeUInt), 0x01, 0x02, 0x03, 0x04}},
Expand Down Expand Up @@ -611,32 +637,66 @@ func TestMarshalIntegers(t *testing.T) {
[]byte{byte(TypeUShorts), 0, 6, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x80, 0x00, 0xff, 0xff, 0xff, 0xff},
},

{MarshalInts, []int{}, []int{}, []byte{byte(TypeInts), 0, 0}},
{MarshalSBytes, nil, nil, []byte{byte(TypeNull)}},
{MarshalBytes, nil, nil, []byte{byte(TypeNull)}},
{MarshalShorts, nil, nil, []byte{byte(TypeNull)}},
{MarshalUShorts, nil, nil, []byte{byte(TypeNull)}},
}
for _, test := range tests {
b := test.marshal(test.in)
if diff := cmp.Diff(b, test.buf); diff != "" {
fname := runtime.FuncForPC(reflect.ValueOf(test.marshal).Pointer()).Name()
t.Fatalf("%s(%#v): Marshal (-got +want)\n%s", fname, test.in, diff)
}
r, l, e := Unmarshal(b)
if e != nil {
fname := runtime.FuncForPC(reflect.ValueOf(test.marshal).Pointer()).Name()
t.Fatalf("%s(%#v): Unmarshal error: %v", fname, test.in, e)
}
if !(test.out == nil && r == nil) {
if diff := cmp.Diff(r, test.out); diff != "" {
fname := runtime.FuncForPC(reflect.ValueOf(test.marshal).Pointer()).Name()
t.Fatalf("%s(%#v): Unmarshal (-got +want)\n%s", fname, test.in, diff)
}
}
if l != len(test.buf) {
fname := runtime.FuncForPC(reflect.ValueOf(test.marshal).Pointer()).Name()
t.Fatalf("%s(%#v): Unmarshal len=%v wants %v", fname, test.in, l, len(test.buf))
}
}
}

func TestMarshalIntegers32(t *testing.T) {
tests := []struct {
marshal func([]int64) []byte
in []int64
out []int64
buf []byte
}{
{MarshalInts, []int64{}, []int64{}, []byte{byte(TypeInts), 0, 0}},
{MarshalInts,
[]int{0, 1, math.MinInt32 - 1, math.MinInt32, math.MaxInt32, math.MaxInt32 + 1},
[]int{0, 1, math.MinInt32, math.MinInt32, math.MaxInt32, math.MaxInt32},
[]int64{0, 1, math.MinInt32 - 1, math.MinInt32, math.MaxInt32, math.MaxInt32 + 1},
[]int64{0, 1, math.MinInt32, math.MinInt32, math.MaxInt32, math.MaxInt32},
[]byte{byte(TypeInts), 0, 6,
0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
},

{MarshalUInts, []int{}, []int{}, []byte{byte(TypeUInts), 0, 0}},
{MarshalUInts, []int64{}, []int64{}, []byte{byte(TypeUInts), 0, 0}},
{MarshalUInts,
[]int{0, 1, -1, math.MaxInt32 + 1, math.MaxUint32, math.MaxUint32 + 1},
[]int{0, 1, 0, math.MaxInt32 + 1, math.MaxUint32, math.MaxUint32},
[]int64{0, 1, -1, math.MaxInt32 + 1, math.MaxUint32, math.MaxUint32 + 1},
[]int64{0, 1, 0, math.MaxInt32 + 1, math.MaxUint32, math.MaxUint32},
[]byte{byte(TypeUInts), 0, 6,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
},
{MarshalSBytes, nil, nil, []byte{byte(TypeNull)}},
{MarshalBytes, nil, nil, []byte{byte(TypeNull)}},
{MarshalShorts, nil, nil, []byte{byte(TypeNull)}},
{MarshalUShorts, nil, nil, []byte{byte(TypeNull)}},

{MarshalInts, nil, nil, []byte{byte(TypeNull)}},
{MarshalUInts, nil, nil, []byte{byte(TypeNull)}},
}

for _, test := range tests {
b := test.marshal(test.in)
if diff := cmp.Diff(b, test.buf); diff != "" {
Expand Down
8 changes: 4 additions & 4 deletions server/binary/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func UnmarshalPingPayload(payload []byte) (uint64, error) {

// NewMsgNodeCount constructs MsgNodeCount
func NewMsgNodeCount(count uint32) Msg {
payload := MarshalUInt(int(count))
payload := MarshalUInt(int64(count))
return &nonregularMsg{
mtype: MsgTypeNodeCount,
payload: payload,
Expand All @@ -207,7 +207,7 @@ func UnmarshalNodeCountPayload(payload []byte) (uint32, error) {
if e != nil {
return 0, xerrors.Errorf("Invalid MsgNodeCount payload (node count): %w", e)
}
return uint32(d.(int)), nil
return uint32(d.(int64)), nil
}

// MarshalLeavePayload marshals MsgLeave payload
Expand Down Expand Up @@ -270,7 +270,7 @@ func MarshalRoomPropPayload(visible, joinable, watchable bool, searchGroup, maxP
}
p := make([]byte, 0, 15)
p = append(p, MarshalByte(flg)...)
p = append(p, MarshalUInt(int(searchGroup))...)
p = append(p, MarshalUInt(int64(searchGroup))...)
p = append(p, MarshalUShort(int(maxPlayer))...)
p = append(p, MarshalUShort(int(clientDeadline))...)
p = append(p, MarshalDict(publicProps)...)
Expand Down Expand Up @@ -300,7 +300,7 @@ func UnmarshalRoomPropPayload(payload []byte) (*MsgRoomPropPayload, error) {
if e != nil {
return nil, xerrors.Errorf("Invalid MsgRoomProp payload (search group): %w", e)
}
rpp.SearchGroup = uint32(d.(int))
rpp.SearchGroup = uint32(d.(int64))
payload = payload[l:]

// max players
Expand Down
4 changes: 2 additions & 2 deletions server/binary/unmarshal_recursive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func TestUnmarshalRecursive(t *testing.T) {
}{
{
[]byte{byte(binary.TypeInt), 0x80, 0x00, 0x00, 0x01},
int(1),
int64(1),
},
{
[]byte{byte(binary.TypeUInt), 0x01, 0x02, 0x03, 0x04, byte(binary.TypeStr8), 0x03, 'a', 'b', 'c'},
[]interface{}{int(0x1020304), "abc"},
[]interface{}{int64(0x1020304), "abc"},
},
{
[]byte{
Expand Down
Loading