proto

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PING uint8 = iota + 1 // 1
	ENTER
	REQUEST
	RESPONSE
	PUSH
	ACK
	CLOSE
	ERR = 0xff
)
View Source
const (
	BYTE_PREFIX = 0xfa
)

Variables

This section is empty.

Functions

func EncodeTcpRequest

func EncodeTcpRequest(method string, params []interface{}) []byte

Types

type AuthConnection

type AuthConnection interface {
	Connection

	DoAuth(session *TcpSession, auth *LoginAuth) *Response
	OnAck(msg *Message)
}

type ClientConnection

type ClientConnection interface {
	Connection

	OnAuthSuccess(session *TcpSession)
	HandleResponse(msgHead MsgHead, resp map[string]interface{})
	HandlePushResponse(msgHead MsgHead, method string, resp map[string]interface{})
}

type Connection

type Connection interface {
	OnConnected(session *TcpSession)
	OnDisconnected(session *TcpSession)
	HandleMessage(session *TcpSession, msg *Message)
}

type LoginAuth

type LoginAuth struct {
	UserId int64  `json:"userId"`
	Token  string `json:"token"`
}

type Message

type Message struct {
	MsgHead
	Body []byte
}

func NewMessage

func NewMessage() *Message

func (*Message) Decode

func (this *Message) Decode(br *bufio.Reader) error

func (*Message) Encode

func (this *Message) Encode() []byte

type MsgHead

type MsgHead struct {
	Version uint8
	Cmd     uint8
	Flag    uint8
	Ext     uint8
	Length  int
	MsgId   string
}

*

  • TCP包头信息
  • 头格式:23字节(固定值0xfa[1字节] + 版本号[1字节] + 指令[1字节] + Flag[1字节] + 预留位[1字节] + length[2字节] + msgId[16字节])
  • 版本号: 0x01 - JSON序列化
  • 指令 0x01 请求消息 0x02 ACK消息
  • Flag: 留给客户端标记的字节
  • 预留位: 1字节
  • length: Body的长度,不包括包头长度
  • msgId: 消息ID[MD5值, 如 0830b69ce8af7a5e6b21658bbf5f0eeb]转换规则为每两位作为一个字节,0x08,0x30

type Response

type Response struct {
	sync.RWMutex
	Code string
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse() *Response

func (*Response) Build

func (this *Response) Build()

func (*Response) Get

func (this *Response) Get(key string) interface{}

func (*Response) IsSuccess

func (this *Response) IsSuccess() bool

func (*Response) Put

func (this *Response) Put(key string, value interface{})

func (*Response) String

func (this *Response) String() string

func (*Response) Value

func (this *Response) Value() map[string]interface{}

type TcpRequest

type TcpRequest struct {
	Method string
	Data   []byte
}

*

  • method length[1字节] + method name + params length[2字节] + params

func (*TcpRequest) Decode

func (this *TcpRequest) Decode(data []byte)

func (*TcpRequest) Encode

func (this *TcpRequest) Encode(paramData string) []byte

type TcpSession

type TcpSession struct {
	Conn               *net.TCPConn // socket
	BR                 *bufio.Reader
	GmtConnect         time.Time
	RemoteAddr         string
	UniqueKey          string
	ConnectionListener Connection
	// contains filtered or unexported fields
}

TcpSession represent a user connection's context

func NewTcpSession

func NewTcpSession(conn *net.TCPConn, connListener Connection) *TcpSession

func (*TcpSession) CheckIfLogined

func (this *TcpSession) CheckIfLogined()

func (*TcpSession) Close

func (this *TcpSession) Close()

func (*TcpSession) CloseSafely

func (this *TcpSession) CloseSafely()

func (*TcpSession) GetUserId

func (this *TcpSession) GetUserId() int64

func (*TcpSession) IsClosed

func (this *TcpSession) IsClosed() bool

func (*TcpSession) Read

func (this *TcpSession) Read() *Message

return false will close the conn

func (*TcpSession) SetUserId

func (this *TcpSession) SetUserId(userId int64)

func (*TcpSession) Write

func (this *TcpSession) Write(msg *Message) error

when write data error, close the connection

func (*TcpSession) WriteError

func (this *TcpSession) WriteError(msg *Message, body string) error

Jump to

Keyboard shortcuts

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