-
Notifications
You must be signed in to change notification settings - Fork 1
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
tangtao
committed
Jul 15, 2019
0 parents
commit f523b2f
Showing
28 changed files
with
1,862 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 | ||
.glide/ | ||
|
||
.idea/ | ||
|
||
*.socket |
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,43 @@ | ||
# tgo-core | ||
|
||
## 简介 | ||
|
||
一款简单,高效,扩展性极强的现代通讯服务器,适用于即时通讯,物联网通讯,AI智能等等 | ||
|
||
## 启动TGO | ||
|
||
``` | ||
// 创建TGO | ||
tg := tgo.New(tgo.NewOptions()) | ||
... | ||
// 运行TGO | ||
tg.Run() | ||
``` | ||
|
||
## 自定义通信服务 | ||
|
||
``` | ||
tg.UseServer(tgo.NewServerTCP()) | ||
``` | ||
### 自定义数据协议 | ||
|
||
``` | ||
tg.UseProtocol(tgo.NewProtocolMQTT()) | ||
``` | ||
|
||
|
||
## 设置包处理者 | ||
|
||
``` | ||
tg.UseHandler(func(ctx tgo.Context) { | ||
}) | ||
``` | ||
|
||
## 自定义路由 | ||
|
||
``` | ||
tg.UseRouter(router) | ||
``` |
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,8 @@ | ||
module github.com/tgo-team/tgo-core | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/pkg/errors v0.8.1 | ||
github.com/stretchr/testify v1.3.0 | ||
) |
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,9 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
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 main | ||
|
||
import "github.com/tgo-team/tgo-core/src" | ||
|
||
func main() { | ||
|
||
// 创建TGO | ||
tg := tgo.New(tgo.NewOptions()) | ||
// 指定server | ||
tg.UseServer(tgo.NewServerTCP()) | ||
// 指定包处理者 | ||
tg.UseHandler(func(ctx tgo.Context) { | ||
|
||
}) | ||
|
||
// 开启TGO | ||
tg.Run() | ||
} |
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,24 @@ | ||
package tgo | ||
|
||
type Conn interface { | ||
Read(b []byte) (n int, err error) | ||
Write(b []byte) (n int, err error) | ||
} | ||
|
||
type ConnContext interface { | ||
// GetConn 获取连接 | ||
GetConn() Conn | ||
} | ||
|
||
type DefaultConnContext struct { | ||
conn Conn | ||
} | ||
|
||
func NewDefaultConnContext(conn Conn) *DefaultConnContext { | ||
|
||
return &DefaultConnContext{conn:conn} | ||
} | ||
|
||
func (c *DefaultConnContext) GetConn() Conn { | ||
return c.conn | ||
} |
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,22 @@ | ||
package tgo | ||
|
||
type Context interface { | ||
GetPacket() interface{} | ||
} | ||
|
||
type DefaultContext struct { | ||
packetCtx PacketContext | ||
connCtx ConnContext | ||
} | ||
|
||
func NewDefaultContext(packetCtx PacketContext, connCtx ConnContext) *DefaultContext { | ||
return &DefaultContext{ | ||
packetCtx: packetCtx, | ||
connCtx: connCtx, | ||
} | ||
} | ||
|
||
func (d *DefaultContext) GetPacket() interface{} { | ||
|
||
return d.packetCtx.GetPacket() | ||
} |
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,10 @@ | ||
package tgo | ||
|
||
type Options struct { | ||
|
||
} | ||
|
||
func NewOptions() *Options { | ||
|
||
return &Options{} | ||
} |
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,21 @@ | ||
package tgo | ||
|
||
type PacketContext interface { | ||
// GetMsg 获取消息 | ||
GetPacket() interface{} | ||
} | ||
|
||
type Packet interface { | ||
String() string | ||
} | ||
|
||
type DefaultPacketContext struct { | ||
p interface{} | ||
} | ||
|
||
func NewDefaultPacketContext(p interface{}) *DefaultPacketContext { | ||
return &DefaultPacketContext{p:p} | ||
} | ||
func (d *DefaultPacketContext) GetPacket() interface{} { | ||
return d.p | ||
} |
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,8 @@ | ||
package tgo | ||
|
||
type Protocol interface { | ||
// 解码消息 | ||
DecodePacket(connContext ConnContext) (interface{},error) | ||
// 编码消息 | ||
EncodePacket(packet interface{}) ([]byte,error) | ||
} |
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,135 @@ | ||
package mqtt | ||
|
||
import "io" | ||
|
||
func encodeBool(b bool) (i int) { | ||
if b { | ||
i = 1 | ||
} | ||
return | ||
} | ||
func boolToByte(b bool) byte { | ||
switch b { | ||
case true: | ||
return 1 | ||
default: | ||
return 0 | ||
} | ||
} | ||
|
||
func decodeBool(i int) (b bool) { | ||
if i > 0 { | ||
b = true | ||
} | ||
return | ||
} | ||
|
||
func encodeString(str string) []byte { | ||
buf := []byte(str) | ||
size := len(buf) | ||
|
||
return append(encodeInt16(size), buf...) | ||
} | ||
func encodeBinary(b []byte) []byte { | ||
size := len(b) | ||
return append(encodeInt16(size), b...) | ||
} | ||
func encodeVariable(size uint64) []byte { | ||
ret := []byte{} | ||
for size > 0 { | ||
digit := size % 0x80 | ||
size /= 0x80 | ||
if size > 0 { | ||
digit |= 0x80 | ||
} | ||
ret = append(ret, byte(digit)) | ||
} | ||
return ret | ||
} | ||
|
||
func decodeString(buffer []byte, start int) (string, int) { | ||
size := (int(buffer[start]) << 8) | int(buffer[start+1]) | ||
start += 2 | ||
data := string(buffer[start:(start + size)]) | ||
return data, start + size | ||
} | ||
func decodeBinary(buffer []byte, start int) ([]byte, int) { | ||
size := (int(buffer[start]) << 8) | int(buffer[start+1]) | ||
start += 2 | ||
data := buffer[start:(start + size)] | ||
return data, start + size | ||
} | ||
func decodeVariable(buffer []byte, start int) (uint64, int) { | ||
var ( | ||
b int | ||
size uint64 | ||
mul uint64 = 1 | ||
) | ||
for { | ||
b, start = decodeInt(buffer, start) | ||
size += uint64(b&0x7F) * mul | ||
mul *= 0x80 | ||
if b&0x80 == 0 { | ||
break | ||
} | ||
} | ||
return size, start | ||
} | ||
|
||
func decodeLength(r io.Reader) int { | ||
var rLength uint32 | ||
var multiplier uint32 | ||
b := make([]byte, 1) | ||
for multiplier < 27 { //fix: Infinite '(digit & 128) == 1' will cause the dead loop | ||
io.ReadFull(r, b) | ||
digit := b[0] | ||
rLength |= uint32(digit&127) << multiplier | ||
if (digit & 128) == 0 { | ||
break | ||
} | ||
multiplier += 7 | ||
} | ||
return int(rLength) | ||
} | ||
|
||
func encodeInt(v int) byte { | ||
return byte(v) | ||
} | ||
func encodeInt16(v int) []byte { | ||
return append([]byte{}, byte(v>>8), byte(v&0xFF)) | ||
} | ||
func encodeInt32(v int) []byte { | ||
return append([]byte{}, byte(v>>24), byte(v>>16), byte(v>>8), byte(v&0xFF)) | ||
} | ||
func encodeUint(v uint8) byte { | ||
return byte(v) | ||
} | ||
func encodeUint16(v uint16) []byte { | ||
iv := int(v) | ||
return append([]byte{}, byte(iv>>8), byte(iv&0xFF)) | ||
} | ||
func encodeUint32(v uint32) []byte { | ||
iv := int(v) | ||
return append([]byte{}, byte(iv>>24), byte(iv>>16), byte(iv>>8), byte(iv&0xFF)) | ||
} | ||
|
||
func decodeInt(buffer []byte, start int) (int, int) { | ||
return int(buffer[start]), start + 1 | ||
} | ||
func decodeInt16(buffer []byte, start int) (int, int) { | ||
return (int(buffer[start]) << 8) | int(buffer[start+1]), start + 2 | ||
} | ||
func decodeInt32(buffer []byte, start int) (int, int) { | ||
return (int(buffer[start]) << 24) | (int(buffer[start+1]) << 16) | (int(buffer[start+2]) << 8) | int(buffer[start+3]), start + 4 | ||
} | ||
func decodeUint(buffer []byte, start int) (uint8, int) { | ||
return uint8(buffer[start]), start + 1 | ||
} | ||
func decodeUint16(buffer []byte, start int) (uint16, int) { | ||
return uint16((int(buffer[start]) << 8) | int(buffer[start+1])), start + 2 | ||
} | ||
func decodeUint32(buffer []byte, start int) (uint32, int) { | ||
return uint32((int(buffer[start]) << 24) | (int(buffer[start+1]) << 16) | (int(buffer[start+2]) << 8) | int(buffer[start+3])), start + 4 | ||
} | ||
|
||
|
Oops, something went wrong.