lucky

package module
v1.4.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2024 License: Apache-2.0 Imports: 37 Imported by: 0

README

Use Lucky to create server by simple, fast, security

Github <<=>> Gitee

Go Report Card

English README
介绍

一个简洁安全游戏/应用网络框架,支持protobuf,JSON 消息协议,基于HTTP/HTTPS,websocket或者socket(TCP,KCP,QUIC)进行数据传输, 支持对消息包加密解密。

数据包加密方式: AES128,AES192,AES256 以及Byte轻量级混淆加密。以帮助开发者朋友能高效率写出安全可靠的业务逻辑。

数据包读、写、执行逻辑处理分别在各自goroutine中, 可以对单个连接恶意发包进行限制超过ConnUndoQueueSize会被忽略,不停留在缓冲区。

使用者只需注册消息和消息对应的回调函数,在回调中处理具体逻辑。例如:

    //在处理器上注册(消息码,消息体,消息执行的逻辑代码)
	Processor.RegisterHandler(code.Hello, &protobuf.Hello{}, logic.Hello)
安装教程

go get github.com/helloh2o/lucky

使用说明

Go version > 1.71

  1. 设置配置参数或保持默认
lucky.SetConf(&lucky.Data{
		ConnUndoQueueSize:   100,
		ConnWriteQueueSize:  100,
		FirstPackageTimeout: 5,
		ConnReadTimeout:     30,
		ConnWriteTimeout:    5,
		MaxDataPackageSize:  2048,
		MaxHeaderLen:        1024,
	})
  1. 请参考example下的http, tcp, websocket, kcp, 以及kcp帧同步例子
  2. 帧同步部分还需要进一步完善,只是一个基础的实现
  3. 聊天室例子, 源码example/chatroom Image text
欢迎参与 :)
  1. 欢迎提交PR 和 Issue
  2. 开源不易,觉得不错就给个小星星✮吧
  3. 该框架正在多个 DAU>10w 商业项目中使用
  4. 新增utils.LazyQueue可对高频操作进行管理,如数据库写。
  5. 新增utils.RkPool可对任意数据进行排序,如排行榜。
  6. 新增Redis分布式锁utils.RDLockOp, 可在多进程间多协程的情景下安全操作数据
Redis分布式同步锁
// 初始化Redis
if _, err := cache.OpenRedis("redis://localhost:6379/?pwd=&db=0"); err != nil {
	panic(err)
}
// 同步操作
op := "lock_user_111_data"
// 获得同步锁
release := RDLockOpWait(op)
// 解锁!!!
defer release()
// 同步逻辑代码
// 安全操作

快速开始

Run as tcp server

package main

import (
	"github.com/helloh2o/lucky"
	"github.com/helloh2o/lucky/examples/comm/msg/code"
	"github.com/helloh2o/lucky/examples/comm/protobuf"
	"github.com/helloh2o/lucky/log"
)

// processor is protobuf processor
var processor = lucky.NewPBProcessor()

func init() {
	//passwd := little.RandLittlePassword()
	passwd := "EyEmxIhoYUFuEc8gDTBlbN/pVOs9Nu/hLCTSjW19Oii0mKNQ9xKPoGJqu1q5Mox4zDT/+MgicJ/j5Nt2sQwK2E8rY3ORVgMUU2v2hmQBb5cP00dettGeF6wvQ36vH2CpGLX9x6RIliP8WAtZqJ0xaT7ixnxxCIr5xRZbutXl8pXqRvSa1+Z/HcuTuFHze4T1ok5A1O4Gge1n6I4ZQjgeHHSSwYs7dQI8oYWQ0MMt3rOywvsVKgUESl2cquDapXrW3PH68MoOPyk1RCe3hxvJNxB3LnLNplVLzkmbTHnZv8AJRedfUoKAJTPsAN0HVzn+XBqUvE2Dvb6nia6tZpmrsA=="
	log.Release("TCP Server password %v", passwd)
	pwd, err := lucky.ParseLittlePassword(passwd)
	if err != nil {
		panic(err)
	}
	cipher := lucky.NewLittleCipher(pwd)
	// add encrypt cipher for processor
	processor.SetEncryptor(cipher)
	// register message && callback
	processor.RegisterHandler(code.Hello, &protobuf.Hello{}, func(args ...interface{}) {
		msg := args[lucky.Msg].(*protobuf.Hello)
		log.Release("Message => from client:: %s", msg.Hello)
		conn := args[lucky.Conn].(lucky.IConnection)
		conn.WriteMsg(msg)
	})
}

func main() {
	// run server
	if s, err := lucky.NewTcpServer("localhost:2021", processor); err != nil {
		panic(err)
	} else {
		log.Fatal("%v", s.Run())
	}
}

go tcp client

package main

import (
	"github.com/helloh2o/lucky"
	"github.com/helloh2o/lucky/examples/comm/msg/code"
	"github.com/helloh2o/lucky/examples/comm/protobuf"
	"github.com/helloh2o/lucky/log"
	"net"
)

// processor is protobuf processor
var processor = lucky.NewPBProcessor()

func init() {
	//passwd := lucky.RandLittlePassword()
	passwd := "EyEmxIhoYUFuEc8gDTBlbN/pVOs9Nu/hLCTSjW19Oii0mKNQ9xKPoGJqu1q5Mox4zDT/+MgicJ/j5Nt2sQwK2E8rY3ORVgMUU2v2hmQBb5cP00dettGeF6wvQ36vH2CpGLX9x6RIliP8WAtZqJ0xaT7ixnxxCIr5xRZbutXl8pXqRvSa1+Z/HcuTuFHze4T1ok5A1O4Gge1n6I4ZQjgeHHSSwYs7dQI8oYWQ0MMt3rOywvsVKgUESl2cquDapXrW3PH68MoOPyk1RCe3hxvJNxB3LnLNplVLzkmbTHnZv8AJRedfUoKAJTPsAN0HVzn+XBqUvE2Dvb6nia6tZpmrsA=="
	log.Release("TCP client password %v", passwd)
	pwd, err := lucky.ParseLittlePassword(passwd)
	if err != nil {
		panic(err)
	}
	cipher := lucky.NewLittleCipher(pwd)
	// add encrypt cipher for processor
	processor.SetEncryptor(cipher)
	// register message && callback
	processor.RegisterHandler(code.Hello, &protobuf.Hello{}, func(args ...interface{}) {
		msg := args[lucky.Msg].(*protobuf.Hello)
		log.Release("Message => from server:: %s", msg.Hello)
		conn := args[lucky.Conn].(lucky.IConnection)
		_ = conn.Close()
	})
}

func main() {
	conn, err := net.Dial("tcp", "localhost:2021")
	if err != nil {
		panic(err)
	}
	ic := lucky.NewTcpConn(conn, processor)
	ic.WriteMsg(&protobuf.Hello{Hello: "hello lucky."})
	ic.ReadMsg()
}

Documentation

Index

Constants

View Source
const (
	Msg = iota
	Conn
	Raw
)

回调传参常量

Variables

View Source
var (
	// UA is user agent
	UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.3578.98 Safari/537.36"
)

Functions

func AddHandler

func AddHandler(h context.Handler)

AddHandler for iris

func Delete

func Delete(path string, handlers ...context.Handler)

Delete add del router

func EnableCrossOrigin

func EnableCrossOrigin()

EnableCrossOrigin for client

func Get

func Get(path string, handlers ...context.Handler)

Get add get router

func Handle

func Handle(method string, relativePath string, handlers ...context.Handler)

Handle add router

func HandleDir

func HandleDir(requestPath string, fsOrDir interface{}, opts ...iris.DirOptions) (routes []*router.Route)

HandleDir static dir

func Iris

func Iris() *iris.Application

Iris return the instance

func NewKcpServer

func NewKcpServer(addr string, processor Processor) (s *kcpServer, err error)

NewKcpServer return a *kcpServer

func NewQUICServer

func NewQUICServer(addr string, processor Processor, config *tls.Config) (s *quicServer, err error)

NewQUICServer return new quicServer

func NewTcpServer

func NewTcpServer(addr string, processor Processor) (s *tcpServer, err error)

NewTcpServer return new tcpServer

func NewWsServer

func NewWsServer(addr string, processor Processor) (s *wsServer, err error)

NewWsServer return new wsServer

func ParseLittlePassword

func ParseLittlePassword(passwordString string) (*password, error)

ParseLittlePassword 解析采用base64编码的字符串获取密码

func Post

func Post(path string, handlers ...context.Handler)

Post add post router

func Put

func Put(path string, handlers ...context.Handler)

Put add put router

func RandLittlePassword

func RandLittlePassword() string

RandLittlePassword 产生 256个byte随机组合的 密码,最后会使用base64编码为字符串存储在配置文件中 不能出现任何一个重复的byte位,必须又 0-255 组成,并且都需要包含

func Run

func Run(addr string) error

Run the iris app

func SetConf

func SetConf(cfg *Data)

SetConf this before startup server

func SetLogLv

func SetLogLv(lv string)

SetLogLv for iris

func SetLogOutput

func SetLogOutput(w io.Writer)

SetLogOutput for io

Types

type AESCipher

type AESCipher struct {
	// contains filtered or unexported fields
}

AESCipher one of Encryptor implement

func NewAESCipher

func NewAESCipher(key string) *AESCipher

NewAESCipher return a AESCipher

func (*AESCipher) Decode

func (cipher *AESCipher) Decode(src []byte) []byte

Decode src

func (*AESCipher) Encode

func (cipher *AESCipher) Encode(src []byte) []byte

Encode src

type BroadcastNode

type BroadcastNode struct {
	// 节点ID
	NodeId string
	// 网络连接
	Connections map[interface{}]IConnection
	// contains filtered or unexported fields
}

BroadcastNode 广播转发节点

func NewBroadcastNode

func NewBroadcastNode() *BroadcastNode

NewBroadcastNode return a new BroadcastNode

func (*BroadcastNode) AddConn

func (bNode *BroadcastNode) AddConn(conn IConnection) error

AddConn by conn

func (*BroadcastNode) Complete

func (bNode *BroadcastNode) Complete() error

Complete sync

func (*BroadcastNode) DelConn

func (bNode *BroadcastNode) DelConn(key string) error

DelConn by key

func (*BroadcastNode) Destroy

func (bNode *BroadcastNode) Destroy() error

Destroy the node

func (*BroadcastNode) GetAllMessage

func (bNode *BroadcastNode) GetAllMessage() chan []interface{}

GetAllMessage return chan []interface{}

func (*BroadcastNode) OnProtocolMessage

func (bNode *BroadcastNode) OnProtocolMessage(msg interface{}) error

OnProtocolMessage interface

func (*BroadcastNode) OnRawMessage

func (bNode *BroadcastNode) OnRawMessage([]byte) error

OnRawMessage bytes

func (*BroadcastNode) Serve

func (bNode *BroadcastNode) Serve()

Serve the node

type Data

type Data struct {
	// 单个连接未处理消息包缓存队列大小
	// 注意:[超过这个大小,包将丢弃,视为当前系统无法处理,默认100]
	ConnUndoQueueSize int
	// 单个连接未写入消息包队列大小 [超过这个大小,包将丢弃,视为当前系统无法处理,默认为1]
	ConnWriteQueueSize int
	// 第一个包等待超市时间 (s) [默认5秒,连接上来未读到正确包,断开连接]
	FirstPackageTimeout int
	// 连接读取超时(s) [默认35秒, 超时等待时间内,请发送任何数据包,如心跳包]
	ConnReadTimeout int
	// 连接写超时(s) [默认5秒, 超时等待时间内,请发送任何数据包,如心跳包]
	ConnWriteTimeout int
	// 数据包最大限制,[默认2048]
	MaxDataPackageSize int
	// ws 最大header,[默认1024]
	MaxHeaderLen int
}

Data is the config struct

var (
	// C is the config instance
	C *Data
)

type Encryptor

type Encryptor interface {
	Encode(bs []byte) []byte
	Decode(bs []byte) []byte
}

Encryptor interface

type FrameNode

type FrameNode struct {
	// 节点ID
	NodeId string
	// 网络连接
	Connections map[interface{}]IConnection

	// 同步周期
	FrameTicker *time.Ticker

	// rand seed
	RandSeed int64
	// contains filtered or unexported fields
}

FrameNode 帧同步节点

func NewFrameNode

func NewFrameNode() *FrameNode

NewFrameNode return a new FrameNode

func (*FrameNode) AddConn

func (gr *FrameNode) AddConn(conn IConnection) error

AddConn conn

func (*FrameNode) Complete

func (gr *FrameNode) Complete() error

Complete sync

func (*FrameNode) DelConn

func (gr *FrameNode) DelConn(key string) error

DelConn by key

func (*FrameNode) Destroy

func (gr *FrameNode) Destroy() error

Destroy the node

func (*FrameNode) GetAllMessage

func (gr *FrameNode) GetAllMessage() chan []interface{}

GetAllMessage return chan []interface

func (*FrameNode) OnProtocolMessage

func (gr *FrameNode) OnProtocolMessage(interface{}) error

OnProtocolMessage interface

func (*FrameNode) OnRawMessage

func (gr *FrameNode) OnRawMessage(msg []byte) error

OnRawMessage msg

func (*FrameNode) Serve

func (gr *FrameNode) Serve()

Serve the node

type IConnection

type IConnection interface {
	GetUuid() string
	ReadMsg()
	WriteMsg(message interface{})
	Close() error
	AfterClose(func())
	// 设置自定义数据
	SetData(interface{})
	GetData() interface{}
	// 设置节点
	SetNode(INode)
	GetNode() INode
	// 是否关闭
	IsClosed() bool
}

IConnection 网络连接

type INode

type INode interface {
	AddConn(IConnection) error
	DelConn(string) error
	Serve()
	OnRawMessage([]byte) error
	OnProtocolMessage(interface{}) error
	GetAllMessage() chan []interface{}
	Destroy() error
	Complete() error
}

INode 网络同步节点,如游戏房间节点,聊天室节点

type Jar

type Jar struct {
	// contains filtered or unexported fields
}

Jar store the cookies

func (*Jar) Cookies

func (jar *Jar) Cookies(u *url.URL) []*http.Cookie

Cookies get all cookies

func (*Jar) SetCookies

func (jar *Jar) SetCookies(u *url.URL, cookies []*http.Cookie)

SetCookies new cookies

type JsonProcessor

type JsonProcessor struct {
	// contains filtered or unexported fields
}

JsonProcessor one of Processor implement

func NewJSONProcessor

func NewJSONProcessor() *JsonProcessor

NewJSONProcessor return new JsonProcessor

func (*JsonProcessor) GetBigEndian

func (jp *JsonProcessor) GetBigEndian() bool

GetBigEndian of the order

func (*JsonProcessor) OnReceivedPackage

func (jp *JsonProcessor) OnReceivedPackage(writer interface{}, body []byte) error

OnReceivedPackage 收到完整数据包

func (*JsonProcessor) RegisterHandler

func (jp *JsonProcessor) RegisterHandler(id int, entity interface{}, handle func(args ...interface{}))

RegisterHandler for logic

func (*JsonProcessor) SetBigEndian

func (jp *JsonProcessor) SetBigEndian()

SetBigEndian for order

func (*JsonProcessor) SetEncryptor

func (jp *JsonProcessor) SetEncryptor(enc Encryptor)

SetEncryptor for processor

func (*JsonProcessor) WrapIdMsg added in v1.1.8

func (jp *JsonProcessor) WrapIdMsg(id uint32, message interface{}) ([]byte, error)

WrapIdMsg format the interface message to []byte with id

func (*JsonProcessor) WrapMsg

func (jp *JsonProcessor) WrapMsg(message interface{}) ([]byte, error)

WrapMsg format the interface message to []byte

type JsonProtocol

type JsonProtocol struct {
	Id      int         `json:"id"`
	Content interface{} `json:"content"`
}

JsonProtocol is the protocol for JsonProcessor

type KCPConn

type KCPConn struct {
	*TCPConn
}

KCPConn 可靠的UDP,like TCP

func NewKcpConn

func NewKcpConn(conn net.Conn, processor Processor) *KCPConn

NewKcpConn get new kcp conn

type LittleCipher

type LittleCipher struct {
	// contains filtered or unexported fields
}

LittleCipher one of Encryptor implement

func NewLittleCipher

func NewLittleCipher(encodePassword *password) *LittleCipher

NewLittleCipher 新建一个编码解码器

func (*LittleCipher) Decode

func (cipher *LittleCipher) Decode(bs []byte) []byte

Decode 解码加密后的数据到原数据

func (*LittleCipher) Encode

func (cipher *LittleCipher) Encode(bs []byte) []byte

Encode 加密原数据

type PbfProcessor

type PbfProcessor struct {
	// contains filtered or unexported fields
}

PbfProcessor one of Processor implement protoc --go_out=. *.proto

func NewPBProcessor

func NewPBProcessor() *PbfProcessor

NewPBProcessor return PB processor

func (*PbfProcessor) GetBigEndian

func (pbf *PbfProcessor) GetBigEndian() bool

GetBigEndian of the order

func (*PbfProcessor) OnReceivedPackage

func (pbf *PbfProcessor) OnReceivedPackage(writer interface{}, body []byte) error

OnReceivedPackage 收到完整数据包, 返回解包错误

func (*PbfProcessor) RegisterHandler

func (pbf *PbfProcessor) RegisterHandler(id int, entity interface{}, handle func(args ...interface{}))

RegisterHandler for logic

func (*PbfProcessor) SetBigEndian

func (pbf *PbfProcessor) SetBigEndian()

SetBigEndian for order

func (*PbfProcessor) SetEncryptor

func (pbf *PbfProcessor) SetEncryptor(enc Encryptor)

SetEncryptor for processor

func (*PbfProcessor) WrapIdMsg added in v1.1.8

func (pbf *PbfProcessor) WrapIdMsg(id uint32, message interface{}) ([]byte, error)

WrapIdMsg format the interface message to []byte with id

func (*PbfProcessor) WrapMsg

func (pbf *PbfProcessor) WrapMsg(message interface{}) ([]byte, error)

WrapMsg format the interface message to []byte

func (*PbfProcessor) WrapMsgNoHeader

func (pbf *PbfProcessor) WrapMsgNoHeader(message interface{}) ([]byte, error)

WrapMsgNoHeader without header length

type Processor

type Processor interface {
	SetBigEndian()
	GetBigEndian() bool
	SetEncryptor(enc Encryptor)
	OnReceivedPackage(interface{}, []byte) error
	WrapMsg(interface{}) ([]byte, error)
	WrapIdMsg(id uint32, data interface{}) ([]byte, error)
	RegisterHandler(id int, entity interface{}, handle func(args ...interface{}))
}

Processor interface

type Protocol

type Protocol struct {
	Id      uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
	Content []byte `protobuf:"bytes,2,opt,name=content,proto3" json:"content,omitempty"`
	// contains filtered or unexported fields
}

协议消息

func (*Protocol) Descriptor deprecated

func (*Protocol) Descriptor() ([]byte, []int)

Deprecated: Use Protocol.ProtoReflect.Descriptor instead.

func (*Protocol) GetContent

func (x *Protocol) GetContent() []byte

func (*Protocol) GetId

func (x *Protocol) GetId() uint32

func (*Protocol) ProtoMessage

func (*Protocol) ProtoMessage()

func (*Protocol) ProtoReflect

func (x *Protocol) ProtoReflect() protoreflect.Message

func (*Protocol) Reset

func (x *Protocol) Reset()

func (*Protocol) String

func (x *Protocol) String() string

type QuicStream

type QuicStream struct {
	sync.RWMutex

	quic.Stream
	// contains filtered or unexported fields
}

QuicStream is warped udp conn for luck

func NewQuicStream

func NewQuicStream(stream quic.Stream, processor Processor) *QuicStream

NewQuicStream return new udp conn

func (*QuicStream) AfterClose

func (s *QuicStream) AfterClose(cb func())

AfterClose conn call back

func (*QuicStream) Close

func (s *QuicStream) Close() error

Close the conn

func (*QuicStream) GetData

func (s *QuicStream) GetData() interface{}

GetData from conn

func (*QuicStream) GetNode

func (s *QuicStream) GetNode() INode

GetNode from conn

func (*QuicStream) GetUuid

func (s *QuicStream) GetUuid() string

GetUuid get uuid of conn

func (*QuicStream) IsClosed

func (s *QuicStream) IsClosed() bool

IsClosed return the status of conn

func (*QuicStream) ReadMsg

func (s *QuicStream) ReadMsg()

ReadMsg read | write end -> write | read end -> conn end

func (*QuicStream) SetData

func (s *QuicStream) SetData(data interface{})

SetData for conn

func (*QuicStream) SetNode

func (s *QuicStream) SetNode(node INode)

SetNode for conn

func (*QuicStream) WriteMsg

func (s *QuicStream) WriteMsg(message interface{})

WriteMsg warp msg base on conn's processor

type Req

type Req struct {
	http.Client
	// contains filtered or unexported fields
}

Req is http client req

func NewHttpClient

func NewHttpClient(proxyUrl ...string) *Req

NewHttpClient get new client

func (*Req) DoReq

func (req *Req) DoReq(method string, targetUrl string, callback func(*url.URL, io.Reader))

DoReq client request target url

type ScFrame

type ScFrame struct {
	Frame     uint32   `protobuf:"varint,1,opt,name=frame,proto3" json:"frame,omitempty"`
	Protocols [][]byte `protobuf:"bytes,2,rep,name=protocols,proto3" json:"protocols,omitempty"`
	// contains filtered or unexported fields
}

帧消息

func (*ScFrame) Descriptor deprecated

func (*ScFrame) Descriptor() ([]byte, []int)

Deprecated: Use ScFrame.ProtoReflect.Descriptor instead.

func (*ScFrame) GetFrame

func (x *ScFrame) GetFrame() uint32

func (*ScFrame) GetProtocols

func (x *ScFrame) GetProtocols() [][]byte

func (*ScFrame) ProtoMessage

func (*ScFrame) ProtoMessage()

func (*ScFrame) ProtoReflect

func (x *ScFrame) ProtoReflect() protoreflect.Message

func (*ScFrame) Reset

func (x *ScFrame) Reset()

func (*ScFrame) String

func (x *ScFrame) String() string

type ScProtocolPack

type ScProtocolPack struct {
	Id   uint32      `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
	Pack []*Protocol `protobuf:"bytes,2,rep,name=pack,proto3" json:"pack,omitempty"`
	// contains filtered or unexported fields
}

打包消息

func (*ScProtocolPack) Descriptor deprecated

func (*ScProtocolPack) Descriptor() ([]byte, []int)

Deprecated: Use ScProtocolPack.ProtoReflect.Descriptor instead.

func (*ScProtocolPack) GetId

func (x *ScProtocolPack) GetId() uint32

func (*ScProtocolPack) GetPack

func (x *ScProtocolPack) GetPack() []*Protocol

func (*ScProtocolPack) ProtoMessage

func (*ScProtocolPack) ProtoMessage()

func (*ScProtocolPack) ProtoReflect

func (x *ScProtocolPack) ProtoReflect() protoreflect.Message

func (*ScProtocolPack) Reset

func (x *ScProtocolPack) Reset()

func (*ScProtocolPack) String

func (x *ScProtocolPack) String() string

type Server

type Server interface {
	Run() error
	Handle(conn net.Conn)
}

Server interface

type TCPConn

type TCPConn struct {
	sync.RWMutex

	net.Conn
	// contains filtered or unexported fields
}

TCPConn is warped tcp conn for luck

func NewTcpConn

func NewTcpConn(conn net.Conn, processor Processor) *TCPConn

NewTcpConn return new tcp conn

func (*TCPConn) AfterClose

func (tc *TCPConn) AfterClose(cb func())

AfterClose conn call back

func (*TCPConn) Close

func (tc *TCPConn) Close() error

Close the conn

func (*TCPConn) GetData

func (tc *TCPConn) GetData() interface{}

GetData from conn

func (*TCPConn) GetNode

func (tc *TCPConn) GetNode() INode

GetNode from conn

func (*TCPConn) GetUuid

func (tc *TCPConn) GetUuid() string

GetUuid get uuid of conn

func (*TCPConn) IsClosed

func (tc *TCPConn) IsClosed() bool

IsClosed return the status of conn

func (*TCPConn) ReadMsg

func (tc *TCPConn) ReadMsg()

ReadMsg read | write end -> write | read end -> conn end

func (*TCPConn) SetData

func (tc *TCPConn) SetData(data interface{})

SetData for conn

func (*TCPConn) SetNode

func (tc *TCPConn) SetNode(node INode)

SetNode for conn

func (*TCPConn) WriteMsg

func (tc *TCPConn) WriteMsg(message interface{})

WriteMsg warp msg base on conn's processor

type WSConn

type WSConn struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

WSConn is warped tcp conn for luck

func NewWSConn

func NewWSConn(conn *websocket.Conn, processor Processor) *WSConn

NewWSConn return new ws conn

func (*WSConn) AfterClose

func (wc *WSConn) AfterClose(cb func())

AfterClose conn call back

func (*WSConn) Close

func (wc *WSConn) Close() error

Close the conn

func (*WSConn) GetData

func (wc *WSConn) GetData() interface{}

GetData from conn

func (*WSConn) GetNode

func (wc *WSConn) GetNode() INode

GetNode from conn

func (*WSConn) GetUuid

func (wc *WSConn) GetUuid() string

GetUuid get uuid of conn

func (*WSConn) IsClosed

func (wc *WSConn) IsClosed() bool

IsClosed return the status of conn

func (*WSConn) ReadMsg

func (wc *WSConn) ReadMsg()

ReadMsg read | write end -> write | read end -> conn end

func (*WSConn) SetData

func (wc *WSConn) SetData(data interface{})

SetData for conn

func (*WSConn) SetNode

func (wc *WSConn) SetNode(node INode)

SetNode for conn

func (*WSConn) WriteMsg

func (wc *WSConn) WriteMsg(message interface{})

WriteMsg warp msg base on conn's processor

type XORCipher

type XORCipher struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

XORCipher one of Encryptor implement

func NewXORCipher

func NewXORCipher(key string) *XORCipher

NewXORCipher 简单的混淆加密 maxBufferLen 加密数据最大长度

func (*XORCipher) Decode

func (xor *XORCipher) Decode(src []byte) []byte

Decode the src

func (*XORCipher) Encode

func (xor *XORCipher) Encode(src []byte) []byte

Encode the src

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL