Kcpnet

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 21, 2020 License: MIT Imports: 25 Imported by: 0

README

make fast net for game by kcp.

by stefan.

Documentation

Index

Constants

View Source
const (
	EnMessage_MainIDPackLen = 2  //主命令
	EnMessage_SubIDPackLen  = 2  //次命令
	EnMessage_PostType      = 2  //发送类型
	EnMessage_DataPackLen   = 6  //真实数据长度 (主命令+次命令+发送类型)
	EnMessage_NoDataLen     = 10 //非data数据长度(包体之前的)->(主命令+次命令+发送类型+datalen)

	//2020.04.15 Identify 会考虑换成 userid 减小包长度
	EnMessage_SvrDataPackLen  = 50 //真实数据长度 (主命令+次命令+发送类型+ Identify长度 + Identify内容+RemoteAddr 长度+RemoteAddr 内容)
	EnMessage_SvrNoDataLen    = 54 //非data数据长度(包体之前的)->(主命令+次命令+发送类型+ Identify长度 + Identify内容+datalen+RemoteAddr 长度+RemoteAddr 内容)
	EnMessage_IdentifyEixst   = 1  //Identify 长度
	EnMessage_IdentifyLen     = 21 //Identify 内容
	EnMessage_RemoteAddrEixst = 1  //RemoteAddr 长度
	EnMessage_RemoteAddrLen   = 21 //RemoteAddr 内容
)

- 协议格式,小端字节序

|主命令号 | 次命令号 | 发送类型 |包长度 | 包体 ---------|-------- | -------- | -------- | ---- 2字节 |2字节 | 2字节 | 4字节 | 包体

View Source
const (
	MsgPostType_Single    = uint16(1)
	MsgPostType_Broadcast = uint16(2)
)

message Post type

Variables

This section is empty.

Functions

func GetAllMessageIDs

func GetAllMessageIDs() (msgs []uint32)

func RegisterMessage

func RegisterMessage(mainID, subID uint16, proc interface{})

func RegisterMessageRet

func RegisterMessageRet(session TcpSession) (succ bool, err error)

func ResponseHeartBeat

func ResponseHeartBeat(session TcpSession) (succ bool, err error)

func SpecialLoginMsgFilter

func SpecialLoginMsgFilter(main, sub uint16) (ok bool)

func UnPackExternalMsg

func UnPackExternalMsg(c net.Conn, pack IMessagePack) (succ bool)

@func: UnPackExternalMsg 解服务器外部消息(客户端,clientsession 注册消息) @parma1: 连接对象 c net.Conn @param2: 解包对象 pack IMessagePack

func UnPackInnerMsg

func UnPackInnerMsg(c net.Conn, pack IMessagePack) (succ bool)

@func: UnPackInnerMsg 解服务器内部消息(server 间客户端发来的请求或者其他rpc消息传递) @parma1: 连接对象 c net.Conn @param2: 解包对象 pack IMessagePack

Types

type IMessagePack

type IMessagePack interface {
	PackAction(Output []byte) (err error)
	PackAction4Client(Output []byte) (err error)
	PackData(msg proto.Message) (data []byte, err error)
	UnPackMsg4Client(InData []byte) (pos int, err error)
	UnPackData() (msg proto.Message, cb reflect.Value, err error, exist bool)
	GetRouteID() (route uint16)
	GetMessageID() (mainID uint16, subID uint16)
	Clean()
	SetCmd(mainid, subid uint16, data []byte)
	PackInnerMsg(mainid, subid uint16, msg proto.Message) (out []byte, err error)
	PackClientMsg(mainid, subid uint16, msg proto.Message) (out []byte, err error)
	GetSrcMsg() (data []byte)
	SetIdentify(identify string)
	GetIdentify() string
	UnPackMsg4Svr(InData []byte) (pos int, err error)
	GetDataLen() (datalen uint32)
	SetRemoteAddr(addr string)
	GetRemoteAddr() (addr string)
	SetPostType(pt uint16)
	GetPostType() (pt uint16)
}

type KcpClient

type KcpClient struct {
	Addr string
	// contains filtered or unexported fields
}

func NewKcpClient

func NewKcpClient(addr, pprofAddr string, name string) *KcpClient

func (*KcpClient) Run

func (this *KcpClient) Run()

type KcpClientProtocol

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

model: KcpClientProtocol Client to Server, message

func (*KcpClientProtocol) Clean

func (this *KcpClientProtocol) Clean()

func (*KcpClientProtocol) GetDataLen

func (this *KcpClientProtocol) GetDataLen() (datalen uint32)

func (*KcpClientProtocol) GetIdentify

func (this *KcpClientProtocol) GetIdentify() string

func (*KcpClientProtocol) GetMessageID

func (this *KcpClientProtocol) GetMessageID() (mainID uint16, subID uint16)

func (*KcpClientProtocol) GetPostType

func (this *KcpClientProtocol) GetPostType() (pt uint16)

func (*KcpClientProtocol) GetRemoteAddr

func (this *KcpClientProtocol) GetRemoteAddr() (addr string)

func (*KcpClientProtocol) GetRouteID

func (this *KcpClientProtocol) GetRouteID() (route uint16)

func (*KcpClientProtocol) GetSrcMsg

func (this *KcpClientProtocol) GetSrcMsg() (data []byte)

func (*KcpClientProtocol) PackAction

func (this *KcpClientProtocol) PackAction(Output []byte) (err error)

func (*KcpClientProtocol) PackAction4Client

func (this *KcpClientProtocol) PackAction4Client(Output []byte) (err error)

func (*KcpClientProtocol) PackClientMsg

func (this *KcpClientProtocol) PackClientMsg(mainid, subid uint16, msg proto.Message) (out []byte, err error)

func (*KcpClientProtocol) PackData

func (this *KcpClientProtocol) PackData(msg proto.Message) (data []byte, err error)

func (*KcpClientProtocol) PackInnerMsg

func (this *KcpClientProtocol) PackInnerMsg(mainid, subid uint16, msg proto.Message) (out []byte, err error)

func (*KcpClientProtocol) SetCmd

func (this *KcpClientProtocol) SetCmd(mainid, subid uint16, data []byte)

func (*KcpClientProtocol) SetIdentify

func (this *KcpClientProtocol) SetIdentify(identify string)

func (*KcpClientProtocol) SetPostType

func (this *KcpClientProtocol) SetPostType(pt uint16)

func (*KcpClientProtocol) SetRemoteAddr

func (this *KcpClientProtocol) SetRemoteAddr(addr string)

func (*KcpClientProtocol) UnPackData

func (this *KcpClientProtocol) UnPackData() (msg proto.Message, cb reflect.Value, err error, exist bool)

func (*KcpClientProtocol) UnPackMsg4Client

func (this *KcpClientProtocol) UnPackMsg4Client(InData []byte) (pos int, err error)

func (*KcpClientProtocol) UnPackMsg4Svr

func (this *KcpClientProtocol) UnPackMsg4Svr(InData []byte) (pos int, err error)

type KcpClientSession

type KcpClientSession struct {
	SvrType     define.ERouteId
	RegPoint    define.ERouteId
	StrIdentify string
	Name        string
	// contains filtered or unexported fields
}

func NewKcpClientSession

func NewKcpClientSession(c net.Conn, offCh chan *KcpClientSession) *KcpClientSession

func (*KcpClientSession) Alive

func (this *KcpClientSession) Alive() bool

func (*KcpClientSession) GetIdentify

func (this *KcpClientSession) GetIdentify() string

func (*KcpClientSession) GetModuleName

func (this *KcpClientSession) GetModuleName() string

func (*KcpClientSession) GetPack

func (this *KcpClientSession) GetPack() (obj IMessagePack)

func (*KcpClientSession) GetRegPoint

func (this *KcpClientSession) GetRegPoint() (RegPoint define.ERouteId)

func (*KcpClientSession) GetRemoteAddr

func (this *KcpClientSession) GetRemoteAddr() string

func (*KcpClientSession) Handler

func (this *KcpClientSession) Handler()

func (*KcpClientSession) IsUser

func (this *KcpClientSession) IsUser() bool

func (*KcpClientSession) Offline

func (this *KcpClientSession) Offline()

func (*KcpClientSession) Push

func (this *KcpClientSession) Push(RegPoint define.ERouteId)

func (*KcpClientSession) RefreshHeartBeat

func (this *KcpClientSession) RefreshHeartBeat(mainid, subid uint16) bool

func (*KcpClientSession) SendInnerBroadcastMsg

func (this *KcpClientSession) SendInnerBroadcastMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)

func (*KcpClientSession) SendInnerClientMsg

func (this *KcpClientSession) SendInnerClientMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)

func (*KcpClientSession) SendInnerSvrMsg

func (this *KcpClientSession) SendInnerSvrMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)

func (*KcpClientSession) SendSvrClientMsg

func (this *KcpClientSession) SendSvrClientMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)

func (*KcpClientSession) SetIdentify

func (this *KcpClientSession) SetIdentify(StrIdentify string)

func (*KcpClientSession) SetSendCache

func (this *KcpClientSession) SetSendCache(data []byte)

func (*KcpClientSession) WriteMessage added in v0.0.5

func (this *KcpClientSession) WriteMessage(data []byte) (succ bool)

type KcpServer

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

func NewKcpServer

func NewKcpServer(Name string, addr string, pprofAddr string) *KcpServer

func (*KcpServer) Run

func (this *KcpServer) Run()

type KcpServerProtocol

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

model: KcpServerProtocol server to server, message

func (*KcpServerProtocol) Clean

func (this *KcpServerProtocol) Clean()

func (*KcpServerProtocol) GetDataLen

func (this *KcpServerProtocol) GetDataLen() (datalen uint32)

func (*KcpServerProtocol) GetIdentify

func (this *KcpServerProtocol) GetIdentify() string

func (*KcpServerProtocol) GetMessageID

func (this *KcpServerProtocol) GetMessageID() (mainID uint16, subID uint16)

func (*KcpServerProtocol) GetPostType

func (this *KcpServerProtocol) GetPostType() (pt uint16)

func (*KcpServerProtocol) GetRemoteAddr

func (this *KcpServerProtocol) GetRemoteAddr() (addr string)

func (*KcpServerProtocol) GetRouteID

func (this *KcpServerProtocol) GetRouteID() (route uint16)

func (*KcpServerProtocol) GetSrcMsg

func (this *KcpServerProtocol) GetSrcMsg() (data []byte)

func (*KcpServerProtocol) PackAction

func (this *KcpServerProtocol) PackAction(Output []byte) (err error)

func (*KcpServerProtocol) PackAction4Client

func (this *KcpServerProtocol) PackAction4Client(Output []byte) (err error)

func (*KcpServerProtocol) PackClientMsg

func (this *KcpServerProtocol) PackClientMsg(mainid, subid uint16, msg proto.Message) (out []byte, err error)

func (*KcpServerProtocol) PackData

func (this *KcpServerProtocol) PackData(msg proto.Message) (data []byte, err error)

func (*KcpServerProtocol) PackInnerMsg

func (this *KcpServerProtocol) PackInnerMsg(mainid, subid uint16, msg proto.Message) (out []byte, err error)

func (*KcpServerProtocol) SetCmd

func (this *KcpServerProtocol) SetCmd(mainid, subid uint16, data []byte)

func (*KcpServerProtocol) SetIdentify

func (this *KcpServerProtocol) SetIdentify(identify string)

func (*KcpServerProtocol) SetPostType

func (this *KcpServerProtocol) SetPostType(pt uint16)

func (*KcpServerProtocol) SetRemoteAddr

func (this *KcpServerProtocol) SetRemoteAddr(addr string)

func (*KcpServerProtocol) UnPackData

func (this *KcpServerProtocol) UnPackData() (msg proto.Message, cb reflect.Value, err error, exist bool)

func (*KcpServerProtocol) UnPackMsg4Client

func (this *KcpServerProtocol) UnPackMsg4Client(InData []byte) (pos int, err error)

func (*KcpServerProtocol) UnPackMsg4Svr

func (this *KcpServerProtocol) UnPackMsg4Svr(InData []byte) (pos int, err error)

type KcpServerSession

type KcpServerSession struct {
	RemoteAddr string

	SvrType  define.ERouteId
	RegPoint define.ERouteId

	StrIdentify string
	Name        string
	// contains filtered or unexported fields
}

func NewKcpSvrSession

func NewKcpSvrSession(c net.Conn, offCh chan *KcpServerSession, kcpcfg *KcpSvrConfig) *KcpServerSession

func (*KcpServerSession) Alive added in v0.0.5

func (this *KcpServerSession) Alive() bool

func (*KcpServerSession) GetIdentify

func (this *KcpServerSession) GetIdentify() string

func (*KcpServerSession) GetModuleName added in v0.0.5

func (this *KcpServerSession) GetModuleName() string

func (*KcpServerSession) GetPack

func (this *KcpServerSession) GetPack() (obj IMessagePack)

func (*KcpServerSession) GetRegPoint

func (this *KcpServerSession) GetRegPoint() (RegPoint define.ERouteId)

func (*KcpServerSession) GetRemoteAddr

func (this *KcpServerSession) GetRemoteAddr() string

func (*KcpServerSession) Handler

func (this *KcpServerSession) Handler()

func (*KcpServerSession) IsUser added in v0.0.5

func (this *KcpServerSession) IsUser() bool

func (*KcpServerSession) Offline

func (this *KcpServerSession) Offline()

func (*KcpServerSession) Push

func (this *KcpServerSession) Push(RegPoint define.ERouteId)

func (*KcpServerSession) RefreshHeartBeat added in v0.0.5

func (this *KcpServerSession) RefreshHeartBeat(mainid, subid uint16) bool

func (*KcpServerSession) SendInnerBroadcastMsg

func (this *KcpServerSession) SendInnerBroadcastMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)

func (*KcpServerSession) SendInnerClientMsg

func (this *KcpServerSession) SendInnerClientMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)

func (*KcpServerSession) SendInnerSvrMsg

func (this *KcpServerSession) SendInnerSvrMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)

func (*KcpServerSession) SendSvrClientMsg

func (this *KcpServerSession) SendSvrClientMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)

func (*KcpServerSession) SetIdentify

func (this *KcpServerSession) SetIdentify(StrIdentify string)

func (*KcpServerSession) SetSendCache

func (this *KcpServerSession) SetSendCache(data []byte)

func (*KcpServerSession) WriteMessage added in v0.0.5

func (this *KcpServerSession) WriteMessage(data []byte) (succ bool)

type KcpSvrConfig

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

type SessionMgr

type SessionMgr struct {
	// contains filtered or unexported fields
}
var (
	GServer2ServerSession *SessionMgr
	GClient2ServerSession *SessionMgr
)

func (*SessionMgr) AddSession

func (this *SessionMgr) AddSession(key interface{}, sess TcpSession)

func (*SessionMgr) GetAllSession

func (this *SessionMgr) GetAllSession() (sess sync.Map)

func (*SessionMgr) GetSession

func (this *SessionMgr) GetSession(key interface{}) (sess TcpSession)

func (*SessionMgr) GetSessionByIdentify

func (this *SessionMgr) GetSessionByIdentify(key interface{}) (sess TcpSession)

func (*SessionMgr) RemoveSession

func (this *SessionMgr) RemoveSession(key interface{}) (exist bool)

type TMessageProc

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

func GetMessageInfo

func GetMessageInfo(mainID, subID uint16) (proc *TMessageProc, finded bool)

type TcpSession

type TcpSession interface {
	GetRemoteAddr() string
	GetRegPoint() (RegPoint define.ERouteId)
	GetIdentify() string
	SetSendCache(data []byte)
	Push(RegPoint define.ERouteId)
	SetIdentify(StrIdentify string)
	SendInnerSvrMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)
	SendSvrClientMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)
	SendInnerClientMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)
	SendInnerBroadcastMsg(mainid, subid uint16, msg proto.Message) (succ bool, err error)
	WriteMessage(data []byte) (succ bool)
	Alive() bool
	GetPack() (obj IMessagePack)
	IsUser() bool
	RefreshHeartBeat(mainid, subid uint16) bool
	GetModuleName() string
}

Jump to

Keyboard shortcuts

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