-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
662 additions
and
8 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
roomproxy: | ||
servers: | ||
- name: dev | ||
port: 4000 | ||
- name: alpha | ||
port: 5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.