Skip to content

Commit

Permalink
feat: roomproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
hulucc committed May 27, 2021
1 parent a81f66c commit be1116f
Show file tree
Hide file tree
Showing 27 changed files with 662 additions and 8 deletions.
1 change: 0 additions & 1 deletion cache/db.go

This file was deleted.

6 changes: 6 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
roomproxy:
servers:
- name: dev
port: 4000
- name: alpha
port: 5000
23 changes: 23 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

import "github.com/jinzhu/configor"

type Config struct {
RoomProxy struct {
Servers []struct {
Name string
Host string `default:"0.0.0.0"`
Port int
}
}
}

func Load(path ...string) (*Config, error) {
c := &Config{}
cc := &configor.Config{Debug: false, Verbose: false}
err := configor.New(cc).Load(c, path...)
if err != nil {
return nil, err
}
return c, nil
}
15 changes: 15 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config

import (
"fmt"
"testing"

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

func TestConfig(t *testing.T) {
c, err := Load("../config.yaml")
assert.Nil(t, err)
fmt.Printf("%+v\n", c)
assert.Equal(t, 2, len(c.RoomProxy.Servers))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/buraksezer/olric v0.4.0-beta.2.0.20210502130407-be0cf149f919 // indirect
github.com/jinzhu/configor v1.2.1 // indirect
github.com/libp2p/go-reuseport v0.0.2 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da h1:8GUt8eRujhVEGZFFEjBj46YV4rDjvGrNxb0KMWYkL2I=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down Expand Up @@ -40,6 +42,8 @@ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/memberlist v0.1.5 h1:AYBsgJOW9gab/toO5tEB8lWetVgDKZycqkebJ8xxpqM=
github.com/hashicorp/memberlist v0.1.5/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/jinzhu/configor v1.2.1 h1:OKk9dsR8i6HPOCZR8BcMtcEImAFjIhbJFZNyn5GCZko=
github.com/jinzhu/configor v1.2.1/go.mod h1:nX89/MOmDba7ZX7GCyU/VIaQ2Ar2aizBl2d3JLF/rDc=
github.com/libp2p/go-reuseport v0.0.2 h1:XSG94b1FJfGA01BUrT82imejHQyTxO4jEWqheyCXYvU=
github.com/libp2p/go-reuseport v0.0.2/go.mod h1:SPD+5RwGC7rcnzngoYC86GjPzjSywuQyMVAheVBD9nQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down
41 changes: 41 additions & 0 deletions infra/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package db

import (
"context"
"errors"
"log"

"github.com/buraksezer/olric"
"github.com/buraksezer/olric/config"
)

func ProvideLocal(ctx context.Context) (*olric.Olric, error) {
ready := make(chan struct{})
done := make(chan error)
c := config.New("local")
c.BindAddr = "127.0.0.1"
c.MemberlistConfig.BindAddr = "127.0.0.1"
c.Started = func() {
defer close(ready)
log.Println("Olric is ready to accept connections")
}
db, err := olric.New(c)
if err != nil {
return nil, err
}
go func() {
defer close(done)
done <- db.Start()
}()
select {
case <-ready:
return db, nil
case err, ok := <-done:
if !ok {
return nil, errors.New("unknown error")
}
return nil, err
case <-ctx.Done():
return nil, ctx.Err()
}
}
26 changes: 26 additions & 0 deletions protocol/heartbeat/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package heartbeat

import (
"bytes"
"errors"
"fmt"
"strconv"

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

func Parser() protocol.FuncParserHandler {
return func(data []byte) (interface{}, error) {
index := bytes.IndexByte(data, ' ')
if index < 0 {
return nil, errors.New("miss token delimiter")
}

s := string(data[1:index])
token, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("parser token %w", err)
}
return uint32(token), err
}
}
17 changes: 17 additions & 0 deletions protocol/heartbeat/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package heartbeat

import (
"testing"
"github.com/stretchr/testify/assert"
)

func TestParser(t *testing.T) {
p := Parser()
token, err := p([]byte("x1234 xxxxx"))
assert.Nil(t, err)
assert.Equal(t, uint32(1234), token.(uint32))
token, err = p([]byte("x1234xxxxx"))
assert.NotNil(t, err)
token, err = p([]byte("xhello xxxxx"))
assert.NotNil(t, err)
}
19 changes: 19 additions & 0 deletions protocol/kcp/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kcp

import (
"encoding/binary"
"errors"

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

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

import (
"testing"

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

func TestKcpBasic(t *testing.T) {
p := Parser()
buffer := make([]byte, 25)
buffer[1] = 0x01
token, err := p(buffer[1:])
assert.Nil(t, err)
assert.Equal(t, uint32(1), token.(uint32))
}
25 changes: 25 additions & 0 deletions protocol/multiplex/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package multiplex

import (
"errors"

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

func Parser() protocol.FuncParserHandler {
minSize := 1
return func(data []byte) (interface{}, error) {
if len(data) < minSize {
return nil, errors.New("invalid multiplex buffer")
}
b := data[0]
switch b {
case 0x01:
fallthrough
case 'x':
return b, nil
default:
return nil, errors.New("unknown channel")
}
}
}
19 changes: 19 additions & 0 deletions protocol/multiplex/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package multiplex

import (
"testing"

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

func TestParser(t *testing.T) {
p := Parser()
token, err := p([]byte{0x01})
assert.Nil(t, err)
assert.Equal(t, byte(0x01), token.(byte))
token, err = p([]byte{'x'})
assert.Nil(t, err)
assert.Equal(t, byte('x'), token.(byte))
token, err = p([]byte{0x00})
assert.NotNil(t, err)
}
18 changes: 18 additions & 0 deletions protocol/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package protocol

// Parser interface
type Parser interface {
GetToken(data []byte) (interface{}, error)
}

type FuncParserHandler func(data []byte) (interface{}, error)
type funcParser struct{
f FuncParserHandler
}
func NewFuncParser(f FuncParserHandler) Parser {
return &funcParser{f: f}
}

func (it *funcParser) GetToken(data []byte) (interface{}, error) {
return it.f(data)
}
2 changes: 1 addition & 1 deletion proxy/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (it *conn) RunReadLoop(ctx context.Context) error {
log.Fatalf("conn read loop err: %v\n", err)
}
for i := 0; i < n; i++ {
msg := Msg{Buffer: msgs[i].Buffers[0][:msgs[i].N], Addr: msgs[i].Addr}
msg := Msg{Buffer: msgs[i].Buffers[0][:msgs[i].N], Addr: msgs[i].Addr.(*net.UDPAddr)}
select {
case it.rch <- &msg:
case <-ctx.Done():
Expand Down
2 changes: 1 addition & 1 deletion proxy/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Msgs = []ipv4.Message

type UdpMsg struct {
Buffer []byte
Addr net.Addr
Addr *net.UDPAddr
}

func (it *UdpMsg) Drop(pool *Pool) {
Expand Down
2 changes: 1 addition & 1 deletion proxy/proxy_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func BenchRecv() ProxyHandler {
}
}

func BenchSend(target net.Addr) ProxyHandler {
func BenchSend(target *net.UDPAddr) ProxyHandler {
return func(ctx *ProxyContext, pes *ProxyEndpoints) error {
s := GetStatd(ctx.Context)
for {
Expand Down
2 changes: 1 addition & 1 deletion proxy/proxy_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net"
)

func Forward(addr net.Addr) ProxyHandler {
func Forward(addr *net.UDPAddr) ProxyHandler {
return func(ctx *ProxyContext, pes *ProxyEndpoints) error {
for {
select {
Expand Down
5 changes: 5 additions & 0 deletions repos/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package repos

import "github.com/buraksezer/olric"

var ErrKeyNotFound = olric.ErrKeyNotFound
11 changes: 8 additions & 3 deletions cache/db_test.go → repos/db_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cache
package repos

import (
"fmt"
"log"
"net"
"sync"
"testing"

Expand Down Expand Up @@ -45,15 +47,18 @@ func TestOlric(t *testing.T) {
db2 := serve(c2, wg)
wg.Wait()

addr, _ := net.ResolveUDPAddr("udp4", "127.0.0.1:8454")
items1, err := db1.NewDMap("items")
assert.Nil(t, err)
err = items1.Put("name", "hulucc")
err = items1.Put("name", addr)
assert.Nil(t, err)

items2, err := db2.NewDMap("items")
assert.Nil(t, err)
v, err := items2.Get("name")
assert.Nil(t, err)
assert.Equal(t, "hulucc", v)
fmt.Printf("%#v\n", v.(net.Addr))
// assert.Equal(t, "hulucc", v)
}

func BenchmarkDB(b *testing.B) {
Expand Down
Loading

0 comments on commit be1116f

Please sign in to comment.