kit

package module
v0.0.0-...-7765a41 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2020 License: MIT Imports: 18 Imported by: 0

README

keep-in-touch 使用了nano的协议和部分代码

Documentation

Index

Constants

View Source
const (
	KitConnStatusCreated = iota
	KitConnStatusHandshake
	KitConnStatusWorking
	KitConnStatusClosed
)
View Source
const (
	MessageRequest  MessageType = 0x00
	MessageNotify               = 0x01
	MessageResponse             = 0x02
	MessagePush                 = 0x03
)

Message types

View Source
const (
	PacketHeadLength = 4
	PacketMaxSize    = 64 * 1024
)

Codec constants.

View Source
const (

	// Handshake represents a handshake: request(client) <====> handshake response(server)
	PacketHandshake = 0x01

	// HandshakeAck represents a handshake ack from client to server
	PacketHandshakeAck = 0x02

	// Heartbeat represents a heartbeat
	PacketHeartbeat = 0x03

	// Data represents a common data packet
	PacketData = 0x04

	// Kick represents a kick off packet
	PacketClose = 0x05 // disconnect message from server
)
View Source
const (
	SessionStatusNormal = iota
	SessionStatusClosed
)
View Source
const (
	KitConnWriteQueueSize = 128
)

Variables

View Source
var (
	ErrInvalidConnStatus = errors.New("write conn while not working status")
	ErrBufferExceed      = errors.New("session send buffer exceed")

	HeartbeatPacket []byte
	ConnClosePacket []byte
)
View Source
var (
	ErrWrongMessageType = errors.New("wrong message type")
	ErrInvalidMessage   = errors.New("invalid message")
)

Errors that could be occurred in message codec

View Source
var ErrPacketSizeExcced = errors.New("kit:packet size exceed")

ErrPacketSizeExcced is the error used for encode/decode.

View Source
var ErrWrongPacketType = errors.New("kit:wrong packet type")

ErrWrongPacketType represents a wrong packet type.

View Source
var SessionMaxDelayMsgCount = 100 // 必须比 KitConnWriteQueueSize 小

Functions

func SetDebug

func SetDebug(d bool)

Types

type Handler

type Handler struct {
	Receiver reflect.Value  // receiver of method
	Method   reflect.Method // method stub
	Type     reflect.Type   // low-level type of method
	IsRawArg bool           // whether the data need to serialize
}

type HandshakeHead

type HandshakeHead struct {
	SessionId string `json:"sid"`
}

type KitConn

type KitConn struct {
	Id      uint32
	Server  *Server
	Session *Session
	// contains filtered or unexported fields
}

func NewKitConn

func NewKitConn(server *Server, conn net.Conn) *KitConn

func (*KitConn) Close

func (c *KitConn) Close(reason string)

func (*KitConn) Handle

func (c *KitConn) Handle()

func (*KitConn) String

func (c *KitConn) String() string

func (*KitConn) WriteMsg

func (c *KitConn) WriteMsg(msg *Message) error

type Message

type Message struct {
	Type  MessageType // message type
	ID    uint        // unique id, zero while notify mode
	Route string      // route for locating service
	Data  []byte      // payload
}

Message represents a unmarshaled message or a message which to be marshaled

func DecodeMessageFromRaw

func DecodeMessageFromRaw(data []byte) (*Message, error)

Decode unmarshal the bytes slice to a message See ref: https://github.com/lonnng/nano/blob/master/docs/communication_protocol.md

func NewMessage

func NewMessage(t MessageType, id uint, route string, data interface{}) *Message

func (*Message) Encode

func (m *Message) Encode() ([]byte, error)

Encode marshals message to binary format. Different message types is corresponding to different message header, message types is identified by 2-4 bit of flag field. The relationship between message types and message header is presented as follows: ------------------------------------------ | type | flag | other | |----------|--------|--------------------| | request |----000-|<message id>|<route>| | notify |----001-|<route> | | response |----010-|<message id> | | push |----011-|<route> | ------------------------------------------ The figure above indicates that the bit does not affect the type of message. See ref: https://github.com/lonnng/nano/blob/master/docs/communication_protocol.md

func (*Message) String

func (m *Message) String() string

String, implementation of fmt.Stringer interface

type MessageType

type MessageType byte

Type represents the type of message, which could be Request/Notify/Response/Push

type Packet

type Packet struct {
	Type PacketType
	Data []byte
}

Packet represents a network packet.

func (*Packet) Encode

func (p *Packet) Encode() ([]byte, error)

Encode create a packet.Packet from the raw bytes slice and then encode to network bytes slice Protocol refs: https://github.com/NetEase/pomelo/wiki/Communication-Protocol

-<type>-|--------<length>--------|-<data>- --------|------------------------|-------- 1 byte packet type, 3 bytes packet data length(big end), and data segment

type PacketDecoder

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

A PacketDecoder reads and decodes network data slice

func NewPacketDecoder

func NewPacketDecoder() *PacketDecoder

NewPacketDecoder returns a new decoder that used for decode network bytes slice.

func (*PacketDecoder) Decode

func (c *PacketDecoder) Decode(data []byte) ([]*Packet, error)

Decode decode the network bytes slice to packet.Packet(s) TODO(Warning): shared slice

type PacketType

type PacketType byte

type RequestHead

type RequestHead struct {
	MsgId uint `json:"-"`
}

func (*RequestHead) GetMsgId

func (req *RequestHead) GetMsgId() uint

func (*RequestHead) SetMsgId

func (req *RequestHead) SetMsgId(id uint)

type RequestHeader

type RequestHeader interface {
	SetMsgId(id uint)
	GetMsgId() uint
}

type Route

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

func NewRoute

func NewRoute() *Route

func (*Route) Exec

func (r *Route) Exec(s *Session, msg *Message)

func (*Route) Reg

func (r *Route) Reg(prefix string, service interface{})

type Server

type Server struct {
	HeartbeatInterval time.Duration
	SessionManager    *SessionManager
	Route             *Route
}

func NewServer

func NewServer(route *Route) *Server

func (*Server) RunWebSocketServer

func (s *Server) RunWebSocketServer(path string, port int)

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Session

type Session struct {
	sync.RWMutex
	Manager        *SessionManager
	Id             string
	LostConnection time.Time
	// contains filtered or unexported fields
}

func (*Session) Close

func (s *Session) Close(reason string)

func (*Session) HasKey

func (s *Session) HasKey(key string) bool

func (*Session) Push

func (s *Session) Push(route string, v interface{}) error

func (*Session) Response

func (s *Session) Response(req RequestHeader, v interface{}) error

func (*Session) Set

func (s *Session) Set(key string, value interface{})

func (*Session) String

func (s *Session) String() string

func (*Session) Value

func (s *Session) Value(key string) interface{}

func (*Session) Write

func (s *Session) Write(t MessageType, msgId uint, route string, data interface{}) error

type SessionCloseEventListener

type SessionCloseEventListener interface {
	OnSessionClose(s *Session)
}

type SessionManager

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

func NewSessionManager

func NewSessionManager() *SessionManager

func (*SessionManager) CheckExpire

func (m *SessionManager) CheckExpire()

func (*SessionManager) GetSessionById

func (m *SessionManager) GetSessionById(id string) *Session

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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