Skip to content

Commit

Permalink
feat: support skcp
Browse files Browse the repository at this point in the history
  • Loading branch information
hulucc committed Mar 9, 2023
1 parent 174ffba commit 561e172
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 13 deletions.
25 changes: 14 additions & 11 deletions controllers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const DiffNew = "New"
const DiffUpdated = "Updated"
const DiffDeleted = "Deleted"
const DiffUnchanged = "Unchanged"
const DiffRoomUpdated = "RoomUpdated"

type RoomKey struct {
ServerId string
Expand Down Expand Up @@ -174,11 +175,7 @@ func DiffRoomStatus(past *v1.RoomIngressStatus, curr *v1.RoomIngressStatus, opts
diff.Type = DiffNew
diff.Current = c
} else if cok && pok {
if o.uh(past, p, curr, c) {
diff.Type = DiffUpdated
} else {
diff.Type = DiffUnchanged
}
diff.Type = o.uh(past, p, curr, c)
diff.Current = c
diff.Past = p
} else if !cok && pok {
Expand All @@ -193,23 +190,29 @@ func DiffRoomStatus(past *v1.RoomIngressStatus, curr *v1.RoomIngressStatus, opts
}

func TokenUpdatedHandler() UpdatedHandler {
return func(past *v1.RoomIngressStatus, pp PlayerPos, curr *v1.RoomIngressStatus, cp PlayerPos) bool {
return func(past *v1.RoomIngressStatus, pp PlayerPos, curr *v1.RoomIngressStatus, cp PlayerPos) DiffType {
p := GetPlayerStatusByPos(past, pp)
c := GetPlayerStatusByPos(curr, cp)
if c.Player.Status == v1.PlayerStatusRetry {
return true
return DiffUpdated
}
if p.Room.Upstream != c.Room.Upstream {
return DiffRoomUpdated
}
if p.Player.Token != c.Player.Token {
return DiffUpdated
}
return p.Player.Token != c.Player.Token
return DiffUnchanged
}
}

func AlwaysUpdatedHandler() UpdatedHandler {
return func(past *v1.RoomIngressStatus, pp PlayerPos, curr *v1.RoomIngressStatus, cp PlayerPos) bool {
return true
return func(past *v1.RoomIngressStatus, pp PlayerPos, curr *v1.RoomIngressStatus, cp PlayerPos) DiffType {
return DiffUpdated
}
}

type UpdatedHandler func(past *v1.RoomIngressStatus, pp PlayerPos, curr *v1.RoomIngressStatus, cp PlayerPos) bool
type UpdatedHandler func(past *v1.RoomIngressStatus, pp PlayerPos, curr *v1.RoomIngressStatus, cp PlayerPos) DiffType

type diffOptions struct {
uh UpdatedHandler
Expand Down
3 changes: 3 additions & 0 deletions controllers/roomingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ func (it *RoomIngressReconciler) syncTokens(ring *v1.RoomIngress) (int, *time.Du
}
}
}
if diff.Type == DiffRoomUpdated {
n++
}
}
ring.Status = *curr
return n, min(requeue)
Expand Down
9 changes: 9 additions & 0 deletions controllers/roomingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,13 @@ func TestRoomIngressController(t *testing.T) {
p4 = GetPlayerStatusByKey(&ring1.Status, PlayerKey{RoomKey: room11, PlayerId: "player4"})
assert.Equal(t, v1.PlayerStatusExpired, p1.Player.Status)
assert.Nil(t, p4)

ring1.Spec.Rooms[0].Upstream = "127.0.0.2:4321"
n, u = rec.syncTokens(ring1)
assert.Equal(t, 3, n)
// _ = n
assert.Equal(t, "127.0.0.2:4321", ring1.Status.Rooms[0].Upstream)

n, _ = rec.syncTokens(ring1)
assert.Equal(t, 0, n)
}
2 changes: 2 additions & 0 deletions protocol/multiplex/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func Parser() protocol.FuncMultiplexParser {
fallthrough
case 'e':
fallthrough
case 's':
fallthrough
case 'x':
return b, nil
default:
Expand Down
20 changes: 20 additions & 0 deletions protocol/skcp/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package skcp

import (
"errors"
"encoding/binary"

"github.com/LilithGames/spiracle/protocol"
)


func Parser() protocol.FuncTokenParser {
size := 25
return func(data []byte) (uint32, error) {
if len(data) < size {
return 0, errors.New("invalid skcp data")
}
token := binary.LittleEndian.Uint32(data[1:])
return token, nil
}
}
17 changes: 17 additions & 0 deletions protocol/skcp/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package skcp

import (
"testing"

assert "github.com/stretchr/testify/require"
)


func TestSKcp(t *testing.T) {
p := Parser()
buffer := make([]byte, 26)
buffer[2] = 0x01
token, err := p(buffer[1:])
assert.Nil(t, err)
assert.Equal(t, uint32(1), token)
}
8 changes: 8 additions & 0 deletions services/roomproxy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (it *RoomProxy) multiplexing(buffer []byte) proxyHandler {
return proxyHandler{ch: ch, u: it.ukcp, d: it.dkcp}
case 'e':
return proxyHandler{ch: ch, u: it.uecho, d: it.decho}
case 's':
return proxyHandler{ch: ch, u: it.ukcp, d: it.dkcp}
default:
return proxyHandler{ch: ch, u: it.drop, d: it.drop}
}
Expand All @@ -51,6 +53,12 @@ func (it *RoomProxy) kcptoken(ch byte, buffer []byte) (uint32, error) {
return 0, fmt.Errorf("heartbeat.GetToken err: %w", err)
}
return token, nil
case 's':
token, err := it.skcp.GetToken(buffer[1:])
if err != nil {
return 0, fmt.Errorf("skcp.GetToken err: %w", err)
}
return token, nil
default:
return 0, errors.New("unknown kcp channel")
}
Expand Down
5 changes: 4 additions & 1 deletion services/roomproxy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package roomproxy

import (
"context"
"time"
"log"
"time"

"github.com/LilithGames/spiracle/protocol"
"github.com/LilithGames/spiracle/protocol/heartbeat"
"github.com/LilithGames/spiracle/protocol/kcp"
"github.com/LilithGames/spiracle/protocol/multiplex"
"github.com/LilithGames/spiracle/protocol/skcp"
"github.com/LilithGames/spiracle/proxy"
"github.com/LilithGames/spiracle/repos"
)
Expand All @@ -19,6 +20,7 @@ type RoomProxy struct {
name string
multiplex protocol.MultiplexParser
kcp protocol.TokenParser
skcp protocol.TokenParser
heartbeat protocol.TokenParser
}

Expand Down Expand Up @@ -47,6 +49,7 @@ func NewRoomProxy(ctx context.Context, name string, opts ...RoomProxyOption) (*R
name: name,
multiplex: protocol.NewFuncMultiplexParser(multiplex.Parser()),
kcp: protocol.NewFuncTokenParser(kcp.Parser()),
skcp: protocol.NewFuncTokenParser(skcp.Parser()),
heartbeat: protocol.NewFuncTokenParser(heartbeat.Parser()),
}
return rp, nil
Expand Down
2 changes: 1 addition & 1 deletion services/roomproxy/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func client(t *testing.T) *net.UDPConn {

func TestRoomProxyReal(t *testing.T) {
s := &proxy.Statd{}
go s.Tick(nil)
go s.Tick(func(s *proxy.Statd){})
ctx := proxy.WithStatd(context.TODO(), s)
name := "server1"
roomproxy, err := NewRoomProxy(ctx, name)
Expand Down

0 comments on commit 561e172

Please sign in to comment.