kim

package module
v0.0.0-...-69995ca Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

README

KIM

King IM CLoud

简介

kim 是一个高性能分布式即时通信系统。

structure.png

环境准备

中间件安装

Kim依赖mysql、Consul和Redis。因此,在本地测试时需要准备相应环境。这里提供两种方式:

方式一: 通过docker-compose启动

docker-compose -f "docker-compose.yml" up -d --build

方式二: docker分别启动

docker run -itd --name kim_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

docker run \
    -d \
    -p 8500:8500 \
    -p 8600:8600/udp \
    --name=kim_consul \
    consul agent -server -ui -node=server-1 -bootstrap-expect=1 -client=0.0.0.0

docker run -itd --name kim_redis -p 6379:6379 redis
数据准备
  1. 进入Mysql,修改访问权限:
    1. docker exec -it kim_mysql /bin/sh
    2. mysql -uroot -p123456
    3. GRANT ALL ON . TO 'root'@'%';
    4. flush privileges;
  2. 创建数据库
    1. create database kim_base default character set utf8mb4 collate utf8mb4_unicode_ci;
    2. create database kim_message default character set utf8mb4 collate utf8mb4_unicode_ci;

启动服务

首先进入services中,分别启动三个服务:

go run main.go gateway
go run main.go server
go run main.go royal

或者,通过docker-compose启动:

docker-compose -f "docker-compose-kim.yml" up -d --build

访问Consul,可以查看服务启动状态:

http://localhost:8500/ui

Documentation

Overview

Package kim is a generated GoMock package.

Package kim is a generated GoMock package.

Package kim is a generated GoMock package.

Index

Constants

View Source
const (
	DefaultReadWait  = time.Minute * 3
	DefaultWriteWait = time.Second * 10
	DefaultLoginWait = time.Second * 10
	DefaultHeartbeat = time.Second * 55
)
View Source
const (
	// 定义读取消息的默认goroutine池大小
	DefaultMessageReadPool = 5000
	DefaultConnectionPool  = 5000
)

Variables

View Source
var ErrSessionLost = errors.New("err:session lost")
View Source
var ErrSessionNil = errors.New("err:session nil")

ErrNil

Functions

func FromRequest

func FromRequest(r *http.Request) string

FromRequest return client's real public IP address from http request headers.

func GetLocalIP

func GetLocalIP() string

GetLocalIP GetLocalIP

func RealIP

func RealIP(r *http.Request) string

RealIP is depreciated, use FromRequest instead

Types

type Acceptor

type Acceptor interface {
	// Accept 返回一个握手完成的Channel对象或者一个error。
	// 业务层需要处理不同协议和网络环境下的连接握手协议
	Accept(Conn, time.Duration) (string, Meta, error)
}

Acceptor 连接接收器

type Agent

type Agent interface {
	ID() string
	Push([]byte) error
	GetMeta() Meta
}

Agent is interface of client side

type Channel

type Channel interface {
	Conn
	Agent
	// Close 关闭连接
	Close() error
	Readloop(lst MessageListener) error
	// SetWriteWait 设置写超时
	SetWriteWait(time.Duration)
	SetReadWait(time.Duration)
}

Channel is interface of client side

func NewChannel

func NewChannel(id string, meta Meta, conn Conn, gpool *ants.Pool) Channel

NewChannel NewChannel

type ChannelImpl

type ChannelImpl struct {
	Conn
	// contains filtered or unexported fields
}

ChannelImpl is a websocket implement of channel

func (*ChannelImpl) Close

func (ch *ChannelImpl) Close() error

Close 关闭连接

func (*ChannelImpl) GetMeta

func (ch *ChannelImpl) GetMeta() Meta

func (*ChannelImpl) ID

func (ch *ChannelImpl) ID() string

ID id simpling server

func (*ChannelImpl) Push

func (ch *ChannelImpl) Push(payload []byte) error

Push 异步写数据

func (*ChannelImpl) Readloop

func (ch *ChannelImpl) Readloop(lst MessageListener) error

Declare a function called Readloop that belongs to the ChannelImpl struct. This function takes in a MessageListener as a parameter and returns an error (if there is one).

func (*ChannelImpl) SetReadWait

func (ch *ChannelImpl) SetReadWait(readwait time.Duration)

func (*ChannelImpl) SetWriteWait

func (ch *ChannelImpl) SetWriteWait(writeWait time.Duration)

SetWriteWait 设置写超时

type ChannelMap

type ChannelMap interface {
	Add(channel Channel)
	Remove(id string)
	Get(id string) (channel Channel, ok bool)
	All() []Channel
}

ChannelMap ChannelMap

func NewChannels

func NewChannels(num int) ChannelMap

NewChannels NewChannels

type ChannelsImpl

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

ChannelsImpl ChannelMap

func (*ChannelsImpl) Add

func (ch *ChannelsImpl) Add(channel Channel)

Add addChannel

func (*ChannelsImpl) All

func (ch *ChannelsImpl) All() []Channel

All return channels

func (*ChannelsImpl) Get

func (ch *ChannelsImpl) Get(id string) (Channel, bool)

Get Get

func (*ChannelsImpl) Remove

func (ch *ChannelsImpl) Remove(id string)

Remove addChannel

type Client

type Client interface {
	Service
	// connect to server
	Connect(string) error
	// SetDialer 设置拨号处理器
	SetDialer(Dialer)
	Send([]byte) error
	Read() (Frame, error)
	// Close 关闭
	Close()
}

Client is interface of client side

type Conn

type Conn interface {
	net.Conn
	ReadFrame() (Frame, error)
	WriteFrame(OpCode, []byte) error
	Flush() error
}

Conn Connection

type Context

type Context interface {
	Dispatcher
	SessionStorage
	Header() *pkt.Header
	ReadBody(val proto.Message) error
	Session() Session
	RespWithError(status pkt.Status, err error) error
	Resp(status pkt.Status, body proto.Message) error
	Dispatch(body proto.Message, recvs ...*Location) error
	Next()
}

func BuildContext

func BuildContext() Context

type ContextImpl

type ContextImpl struct {
	sync.Mutex
	Dispatcher
	SessionStorage
	// contains filtered or unexported fields
}

ContextImpl is the most important part of kim

func (*ContextImpl) Dispatch

func (c *ContextImpl) Dispatch(body proto.Message, recvs ...*Location) error

Dispatch the packet to the Destination of request, the header flag of this packet will be set with FlagDelivery exceptMe: exclude self if self is false

func (*ContextImpl) Header

func (c *ContextImpl) Header() *pkt.Header

func (*ContextImpl) Next

func (c *ContextImpl) Next()

Next execute next handler

func (*ContextImpl) ReadBody

func (c *ContextImpl) ReadBody(val proto.Message) error

func (*ContextImpl) Resp

func (c *ContextImpl) Resp(status pkt.Status, body proto.Message) error

Resp send a response message to sender, the header of packet copied from request

func (*ContextImpl) RespWithError

func (c *ContextImpl) RespWithError(status pkt.Status, err error) error

RespWithError response with error

func (*ContextImpl) Session

func (c *ContextImpl) Session() Session

type DefaultServer

type DefaultServer struct {
	Upgrader

	ServiceRegistration
	ChannelMap
	Acceptor
	MessageListener
	StateListener
	// contains filtered or unexported fields
}

DefaultServer is a websocket implement of the DefaultServer

func NewServer

func NewServer(listen string, service ServiceRegistration, upgrader Upgrader, options ...ServerOption) *DefaultServer

NewServer NewServer

func (*DefaultServer) Push

func (s *DefaultServer) Push(id string, data []byte) error

string channelID []byte data

func (*DefaultServer) SetAcceptor

func (s *DefaultServer) SetAcceptor(acceptor Acceptor)

SetAcceptor SetAcceptor

func (*DefaultServer) SetChannelMap

func (s *DefaultServer) SetChannelMap(channels ChannelMap)

SetChannels SetChannels

func (*DefaultServer) SetMessageListener

func (s *DefaultServer) SetMessageListener(listener MessageListener)

SetMessageListener SetMessageListener

func (*DefaultServer) SetReadWait

func (s *DefaultServer) SetReadWait(Readwait time.Duration)

SetReadWait set read wait duration

func (*DefaultServer) SetStateListener

func (s *DefaultServer) SetStateListener(listener StateListener)

SetStateListener SetStateListener

func (*DefaultServer) Shutdown

func (s *DefaultServer) Shutdown(ctx context.Context) error

Shutdown Shutdown

func (*DefaultServer) Start

func (s *DefaultServer) Start() error

Start server

type Dialer

type Dialer interface {
	DialAndHandshake(DialerContext) (net.Conn, error)
}

Dialer Dialer

type DialerContext

type DialerContext struct {
	Id      string
	Name    string
	Address string
	Timeout time.Duration
}

type Dispatcher

type Dispatcher interface {
	Push(gateway string, channels []string, p *pkt.LogicPkt) error
}

Dispatcher defined a component how a message be dispatched to gateway

type Event

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

Event represents a one-time event that may occur in the future.

func NewEvent

func NewEvent() *Event

NewEvent returns a new, ready-to-use Event.

func (*Event) Done

func (e *Event) Done() <-chan struct{}

Done returns a channel that will be closed when Fire is called.

func (*Event) Fire

func (e *Event) Fire() bool

Fire causes e to complete. It is safe to call multiple times, and concurrently. It returns true iff this call to Fire caused the signaling channel returned by Done to close.

func (*Event) HasFired

func (e *Event) HasFired() bool

HasFired returns true if Fire has been called.

type Frame

type Frame interface {
	SetOpCode(OpCode)
	GetOpCode() OpCode
	SetPayload([]byte)
	GetPayload() []byte
}

Frame Frame

type FuncTree

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

FuncTree is a tree structure

func NewTree

func NewTree() *FuncTree

NewTree NewTree

func (*FuncTree) Add

func (t *FuncTree) Add(path string, handlers ...HandlerFunc)

Add a handler to tree

func (*FuncTree) Get

func (t *FuncTree) Get(path string) (HandlersChain, bool)

Get a handler from tree

type HandlerFunc

type HandlerFunc func(Context)

HandlerFunc defines the handler used

type HandlersChain

type HandlersChain []HandlerFunc

HandlersChain HandlersChain

type Location

type Location struct {
	ChannelId string
	GateId    string
}

func (*Location) Bytes

func (loc *Location) Bytes() []byte

func (*Location) Unmarshal

func (loc *Location) Unmarshal(data []byte) (err error)

type MessageListener

type MessageListener interface {
	// 收到消息回调
	Receive(Agent, []byte)
}

MessageListener 监听消息

type Meta

type Meta map[string]string

type MockAcceptor

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

MockAcceptor is a mock of Acceptor interface.

func NewMockAcceptor

func NewMockAcceptor(ctrl *gomock.Controller) *MockAcceptor

NewMockAcceptor creates a new mock instance.

func (*MockAcceptor) Accept

func (m *MockAcceptor) Accept(arg0 Conn, arg1 time.Duration) (string, Meta, error)

Accept mocks base method.

func (*MockAcceptor) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

type MockAcceptorMockRecorder

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

MockAcceptorMockRecorder is the mock recorder for MockAcceptor.

func (*MockAcceptorMockRecorder) Accept

func (mr *MockAcceptorMockRecorder) Accept(arg0, arg1 interface{}) *gomock.Call

Accept indicates an expected call of Accept.

type MockAgent

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

MockAgent is a mock of Agent interface.

func NewMockAgent

func NewMockAgent(ctrl *gomock.Controller) *MockAgent

NewMockAgent creates a new mock instance.

func (*MockAgent) EXPECT

func (m *MockAgent) EXPECT() *MockAgentMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockAgent) GetMeta

func (m *MockAgent) GetMeta() Meta

GetMeta mocks base method.

func (*MockAgent) ID

func (m *MockAgent) ID() string

ID mocks base method.

func (*MockAgent) Push

func (m *MockAgent) Push(arg0 []byte) error

Push mocks base method.

type MockAgentMockRecorder

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

MockAgentMockRecorder is the mock recorder for MockAgent.

func (*MockAgentMockRecorder) GetMeta

func (mr *MockAgentMockRecorder) GetMeta() *gomock.Call

GetMeta indicates an expected call of GetMeta.

func (*MockAgentMockRecorder) ID

func (mr *MockAgentMockRecorder) ID() *gomock.Call

ID indicates an expected call of ID.

func (*MockAgentMockRecorder) Push

func (mr *MockAgentMockRecorder) Push(arg0 interface{}) *gomock.Call

Push indicates an expected call of Push.

type MockChannel

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

MockChannel is a mock of Channel interface.

func NewMockChannel

func NewMockChannel(ctrl *gomock.Controller) *MockChannel

NewMockChannel creates a new mock instance.

func (*MockChannel) Close

func (m *MockChannel) Close() error

Close mocks base method.

func (*MockChannel) EXPECT

func (m *MockChannel) EXPECT() *MockChannelMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockChannel) Flush

func (m *MockChannel) Flush() error

Flush mocks base method.

func (*MockChannel) GetMeta

func (m *MockChannel) GetMeta() Meta

GetMeta mocks base method.

func (*MockChannel) ID

func (m *MockChannel) ID() string

ID mocks base method.

func (*MockChannel) LocalAddr

func (m *MockChannel) LocalAddr() net.Addr

LocalAddr mocks base method.

func (*MockChannel) Push

func (m *MockChannel) Push(arg0 []byte) error

Push mocks base method.

func (*MockChannel) Read

func (m *MockChannel) Read(b []byte) (int, error)

Read mocks base method.

func (*MockChannel) ReadFrame

func (m *MockChannel) ReadFrame() (Frame, error)

ReadFrame mocks base method.

func (*MockChannel) Readloop

func (m *MockChannel) Readloop(lst MessageListener) error

Readloop mocks base method.

func (*MockChannel) RemoteAddr

func (m *MockChannel) RemoteAddr() net.Addr

RemoteAddr mocks base method.

func (*MockChannel) SetDeadline

func (m *MockChannel) SetDeadline(t time.Time) error

SetDeadline mocks base method.

func (*MockChannel) SetReadDeadline

func (m *MockChannel) SetReadDeadline(t time.Time) error

SetReadDeadline mocks base method.

func (*MockChannel) SetReadWait

func (m *MockChannel) SetReadWait(arg0 time.Duration)

SetReadWait mocks base method.

func (*MockChannel) SetWriteDeadline

func (m *MockChannel) SetWriteDeadline(t time.Time) error

SetWriteDeadline mocks base method.

func (*MockChannel) SetWriteWait

func (m *MockChannel) SetWriteWait(arg0 time.Duration)

SetWriteWait mocks base method.

func (*MockChannel) Write

func (m *MockChannel) Write(b []byte) (int, error)

Write mocks base method.

func (*MockChannel) WriteFrame

func (m *MockChannel) WriteFrame(arg0 OpCode, arg1 []byte) error

WriteFrame mocks base method.

type MockChannelMockRecorder

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

MockChannelMockRecorder is the mock recorder for MockChannel.

func (*MockChannelMockRecorder) Close

func (mr *MockChannelMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close.

func (*MockChannelMockRecorder) Flush

func (mr *MockChannelMockRecorder) Flush() *gomock.Call

Flush indicates an expected call of Flush.

func (*MockChannelMockRecorder) GetMeta

func (mr *MockChannelMockRecorder) GetMeta() *gomock.Call

GetMeta indicates an expected call of GetMeta.

func (*MockChannelMockRecorder) ID

ID indicates an expected call of ID.

func (*MockChannelMockRecorder) LocalAddr

func (mr *MockChannelMockRecorder) LocalAddr() *gomock.Call

LocalAddr indicates an expected call of LocalAddr.

func (*MockChannelMockRecorder) Push

func (mr *MockChannelMockRecorder) Push(arg0 interface{}) *gomock.Call

Push indicates an expected call of Push.

func (*MockChannelMockRecorder) Read

func (mr *MockChannelMockRecorder) Read(b interface{}) *gomock.Call

Read indicates an expected call of Read.

func (*MockChannelMockRecorder) ReadFrame

func (mr *MockChannelMockRecorder) ReadFrame() *gomock.Call

ReadFrame indicates an expected call of ReadFrame.

func (*MockChannelMockRecorder) Readloop

func (mr *MockChannelMockRecorder) Readloop(lst interface{}) *gomock.Call

Readloop indicates an expected call of Readloop.

func (*MockChannelMockRecorder) RemoteAddr

func (mr *MockChannelMockRecorder) RemoteAddr() *gomock.Call

RemoteAddr indicates an expected call of RemoteAddr.

func (*MockChannelMockRecorder) SetDeadline

func (mr *MockChannelMockRecorder) SetDeadline(t interface{}) *gomock.Call

SetDeadline indicates an expected call of SetDeadline.

func (*MockChannelMockRecorder) SetReadDeadline

func (mr *MockChannelMockRecorder) SetReadDeadline(t interface{}) *gomock.Call

SetReadDeadline indicates an expected call of SetReadDeadline.

func (*MockChannelMockRecorder) SetReadWait

func (mr *MockChannelMockRecorder) SetReadWait(arg0 interface{}) *gomock.Call

SetReadWait indicates an expected call of SetReadWait.

func (*MockChannelMockRecorder) SetWriteDeadline

func (mr *MockChannelMockRecorder) SetWriteDeadline(t interface{}) *gomock.Call

SetWriteDeadline indicates an expected call of SetWriteDeadline.

func (*MockChannelMockRecorder) SetWriteWait

func (mr *MockChannelMockRecorder) SetWriteWait(arg0 interface{}) *gomock.Call

SetWriteWait indicates an expected call of SetWriteWait.

func (*MockChannelMockRecorder) Write

func (mr *MockChannelMockRecorder) Write(b interface{}) *gomock.Call

Write indicates an expected call of Write.

func (*MockChannelMockRecorder) WriteFrame

func (mr *MockChannelMockRecorder) WriteFrame(arg0, arg1 interface{}) *gomock.Call

WriteFrame indicates an expected call of WriteFrame.

type MockClient

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

MockClient is a mock of Client interface.

func NewMockClient

func NewMockClient(ctrl *gomock.Controller) *MockClient

NewMockClient creates a new mock instance.

func (*MockClient) Close

func (m *MockClient) Close()

Close mocks base method.

func (*MockClient) Connect

func (m *MockClient) Connect(arg0 string) error

Connect mocks base method.

func (*MockClient) EXPECT

func (m *MockClient) EXPECT() *MockClientMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockClient) GetMeta

func (m *MockClient) GetMeta() map[string]string

GetMeta mocks base method.

func (*MockClient) Read

func (m *MockClient) Read() (Frame, error)

Read mocks base method.

func (*MockClient) Send

func (m *MockClient) Send(arg0 []byte) error

Send mocks base method.

func (*MockClient) ServiceID

func (m *MockClient) ServiceID() string

ServiceID mocks base method.

func (*MockClient) ServiceName

func (m *MockClient) ServiceName() string

ServiceName mocks base method.

func (*MockClient) SetDialer

func (m *MockClient) SetDialer(arg0 Dialer)

SetDialer mocks base method.

type MockClientMockRecorder

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

MockClientMockRecorder is the mock recorder for MockClient.

func (*MockClientMockRecorder) Close

func (mr *MockClientMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close.

func (*MockClientMockRecorder) Connect

func (mr *MockClientMockRecorder) Connect(arg0 interface{}) *gomock.Call

Connect indicates an expected call of Connect.

func (*MockClientMockRecorder) GetMeta

func (mr *MockClientMockRecorder) GetMeta() *gomock.Call

GetMeta indicates an expected call of GetMeta.

func (*MockClientMockRecorder) Read

func (mr *MockClientMockRecorder) Read() *gomock.Call

Read indicates an expected call of Read.

func (*MockClientMockRecorder) Send

func (mr *MockClientMockRecorder) Send(arg0 interface{}) *gomock.Call

Send indicates an expected call of Send.

func (*MockClientMockRecorder) ServiceID

func (mr *MockClientMockRecorder) ServiceID() *gomock.Call

ServiceID indicates an expected call of ServiceID.

func (*MockClientMockRecorder) ServiceName

func (mr *MockClientMockRecorder) ServiceName() *gomock.Call

ServiceName indicates an expected call of ServiceName.

func (*MockClientMockRecorder) SetDialer

func (mr *MockClientMockRecorder) SetDialer(arg0 interface{}) *gomock.Call

SetDialer indicates an expected call of SetDialer.

type MockConn

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

MockConn is a mock of Conn interface.

func NewMockConn

func NewMockConn(ctrl *gomock.Controller) *MockConn

NewMockConn creates a new mock instance.

func (*MockConn) Close

func (m *MockConn) Close() error

Close mocks base method.

func (*MockConn) EXPECT

func (m *MockConn) EXPECT() *MockConnMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockConn) Flush

func (m *MockConn) Flush() error

Flush mocks base method.

func (*MockConn) LocalAddr

func (m *MockConn) LocalAddr() net.Addr

LocalAddr mocks base method.

func (*MockConn) Read

func (m *MockConn) Read(b []byte) (int, error)

Read mocks base method.

func (*MockConn) ReadFrame

func (m *MockConn) ReadFrame() (Frame, error)

ReadFrame mocks base method.

func (*MockConn) RemoteAddr

func (m *MockConn) RemoteAddr() net.Addr

RemoteAddr mocks base method.

func (*MockConn) SetDeadline

func (m *MockConn) SetDeadline(t time.Time) error

SetDeadline mocks base method.

func (*MockConn) SetReadDeadline

func (m *MockConn) SetReadDeadline(t time.Time) error

SetReadDeadline mocks base method.

func (*MockConn) SetWriteDeadline

func (m *MockConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline mocks base method.

func (*MockConn) Write

func (m *MockConn) Write(b []byte) (int, error)

Write mocks base method.

func (*MockConn) WriteFrame

func (m *MockConn) WriteFrame(arg0 OpCode, arg1 []byte) error

WriteFrame mocks base method.

type MockConnMockRecorder

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

MockConnMockRecorder is the mock recorder for MockConn.

func (*MockConnMockRecorder) Close

func (mr *MockConnMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close.

func (*MockConnMockRecorder) Flush

func (mr *MockConnMockRecorder) Flush() *gomock.Call

Flush indicates an expected call of Flush.

func (*MockConnMockRecorder) LocalAddr

func (mr *MockConnMockRecorder) LocalAddr() *gomock.Call

LocalAddr indicates an expected call of LocalAddr.

func (*MockConnMockRecorder) Read

func (mr *MockConnMockRecorder) Read(b interface{}) *gomock.Call

Read indicates an expected call of Read.

func (*MockConnMockRecorder) ReadFrame

func (mr *MockConnMockRecorder) ReadFrame() *gomock.Call

ReadFrame indicates an expected call of ReadFrame.

func (*MockConnMockRecorder) RemoteAddr

func (mr *MockConnMockRecorder) RemoteAddr() *gomock.Call

RemoteAddr indicates an expected call of RemoteAddr.

func (*MockConnMockRecorder) SetDeadline

func (mr *MockConnMockRecorder) SetDeadline(t interface{}) *gomock.Call

SetDeadline indicates an expected call of SetDeadline.

func (*MockConnMockRecorder) SetReadDeadline

func (mr *MockConnMockRecorder) SetReadDeadline(t interface{}) *gomock.Call

SetReadDeadline indicates an expected call of SetReadDeadline.

func (*MockConnMockRecorder) SetWriteDeadline

func (mr *MockConnMockRecorder) SetWriteDeadline(t interface{}) *gomock.Call

SetWriteDeadline indicates an expected call of SetWriteDeadline.

func (*MockConnMockRecorder) Write

func (mr *MockConnMockRecorder) Write(b interface{}) *gomock.Call

Write indicates an expected call of Write.

func (*MockConnMockRecorder) WriteFrame

func (mr *MockConnMockRecorder) WriteFrame(arg0, arg1 interface{}) *gomock.Call

WriteFrame indicates an expected call of WriteFrame.

type MockDialer

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

MockDialer is a mock of Dialer interface.

func NewMockDialer

func NewMockDialer(ctrl *gomock.Controller) *MockDialer

NewMockDialer creates a new mock instance.

func (*MockDialer) DialAndHandshake

func (m *MockDialer) DialAndHandshake(arg0 DialerContext) (net.Conn, error)

DialAndHandshake mocks base method.

func (*MockDialer) EXPECT

func (m *MockDialer) EXPECT() *MockDialerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

type MockDialerMockRecorder

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

MockDialerMockRecorder is the mock recorder for MockDialer.

func (*MockDialerMockRecorder) DialAndHandshake

func (mr *MockDialerMockRecorder) DialAndHandshake(arg0 interface{}) *gomock.Call

DialAndHandshake indicates an expected call of DialAndHandshake.

type MockDispatcher

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

MockDispatcher is a mock of Dispatcher interface.

func NewMockDispatcher

func NewMockDispatcher(ctrl *gomock.Controller) *MockDispatcher

NewMockDispatcher creates a new mock instance.

func (*MockDispatcher) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockDispatcher) Push

func (m *MockDispatcher) Push(gateway string, channels []string, p *pkt.LogicPkt) error

Push mocks base method.

type MockDispatcherMockRecorder

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

MockDispatcherMockRecorder is the mock recorder for MockDispatcher.

func (*MockDispatcherMockRecorder) Push

func (mr *MockDispatcherMockRecorder) Push(gateway, channels, p interface{}) *gomock.Call

Push indicates an expected call of Push.

type MockFrame

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

MockFrame is a mock of Frame interface.

func NewMockFrame

func NewMockFrame(ctrl *gomock.Controller) *MockFrame

NewMockFrame creates a new mock instance.

func (*MockFrame) EXPECT

func (m *MockFrame) EXPECT() *MockFrameMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockFrame) GetOpCode

func (m *MockFrame) GetOpCode() OpCode

GetOpCode mocks base method.

func (*MockFrame) GetPayload

func (m *MockFrame) GetPayload() []byte

GetPayload mocks base method.

func (*MockFrame) SetOpCode

func (m *MockFrame) SetOpCode(arg0 OpCode)

SetOpCode mocks base method.

func (*MockFrame) SetPayload

func (m *MockFrame) SetPayload(arg0 []byte)

SetPayload mocks base method.

type MockFrameMockRecorder

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

MockFrameMockRecorder is the mock recorder for MockFrame.

func (*MockFrameMockRecorder) GetOpCode

func (mr *MockFrameMockRecorder) GetOpCode() *gomock.Call

GetOpCode indicates an expected call of GetOpCode.

func (*MockFrameMockRecorder) GetPayload

func (mr *MockFrameMockRecorder) GetPayload() *gomock.Call

GetPayload indicates an expected call of GetPayload.

func (*MockFrameMockRecorder) SetOpCode

func (mr *MockFrameMockRecorder) SetOpCode(arg0 interface{}) *gomock.Call

SetOpCode indicates an expected call of SetOpCode.

func (*MockFrameMockRecorder) SetPayload

func (mr *MockFrameMockRecorder) SetPayload(arg0 interface{}) *gomock.Call

SetPayload indicates an expected call of SetPayload.

type MockMessageListener

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

MockMessageListener is a mock of MessageListener interface.

func NewMockMessageListener

func NewMockMessageListener(ctrl *gomock.Controller) *MockMessageListener

NewMockMessageListener creates a new mock instance.

func (*MockMessageListener) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockMessageListener) Receive

func (m *MockMessageListener) Receive(arg0 Agent, arg1 []byte)

Receive mocks base method.

type MockMessageListenerMockRecorder

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

MockMessageListenerMockRecorder is the mock recorder for MockMessageListener.

func (*MockMessageListenerMockRecorder) Receive

func (mr *MockMessageListenerMockRecorder) Receive(arg0, arg1 interface{}) *gomock.Call

Receive indicates an expected call of Receive.

type MockServer

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

MockServer is a mock of Server interface.

func NewMockServer

func NewMockServer(ctrl *gomock.Controller) *MockServer

NewMockServer creates a new mock instance.

func (*MockServer) DialURL

func (m *MockServer) DialURL() string

DialURL mocks base method.

func (*MockServer) EXPECT

func (m *MockServer) EXPECT() *MockServerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockServer) GetMeta

func (m *MockServer) GetMeta() map[string]string

GetMeta mocks base method.

func (*MockServer) GetNamespace

func (m *MockServer) GetNamespace() string

GetNamespace mocks base method.

func (*MockServer) GetProtocol

func (m *MockServer) GetProtocol() string

GetProtocol mocks base method.

func (*MockServer) GetTags

func (m *MockServer) GetTags() []string

GetTags mocks base method.

func (*MockServer) PublicAddress

func (m *MockServer) PublicAddress() string

PublicAddress mocks base method.

func (*MockServer) PublicPort

func (m *MockServer) PublicPort() int

PublicPort mocks base method.

func (*MockServer) Push

func (m *MockServer) Push(arg0 string, arg1 []byte) error

Push mocks base method.

func (*MockServer) ServiceID

func (m *MockServer) ServiceID() string

ServiceID mocks base method.

func (*MockServer) ServiceName

func (m *MockServer) ServiceName() string

ServiceName mocks base method.

func (*MockServer) SetAcceptor

func (m *MockServer) SetAcceptor(arg0 Acceptor)

SetAcceptor mocks base method.

func (*MockServer) SetChannelMap

func (m *MockServer) SetChannelMap(arg0 ChannelMap)

SetChannelMap mocks base method.

func (*MockServer) SetMessageListener

func (m *MockServer) SetMessageListener(arg0 MessageListener)

SetMessageListener mocks base method.

func (*MockServer) SetReadWait

func (m *MockServer) SetReadWait(arg0 time.Duration)

SetReadWait mocks base method.

func (*MockServer) SetStateListener

func (m *MockServer) SetStateListener(arg0 StateListener)

SetStateListener mocks base method.

func (*MockServer) Shutdown

func (m *MockServer) Shutdown(arg0 context.Context) error

Shutdown mocks base method.

func (*MockServer) Start

func (m *MockServer) Start() error

Start mocks base method.

func (*MockServer) String

func (m *MockServer) String() string

String mocks base method.

type MockServerMockRecorder

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

MockServerMockRecorder is the mock recorder for MockServer.

func (*MockServerMockRecorder) DialURL

func (mr *MockServerMockRecorder) DialURL() *gomock.Call

DialURL indicates an expected call of DialURL.

func (*MockServerMockRecorder) GetMeta

func (mr *MockServerMockRecorder) GetMeta() *gomock.Call

GetMeta indicates an expected call of GetMeta.

func (*MockServerMockRecorder) GetNamespace

func (mr *MockServerMockRecorder) GetNamespace() *gomock.Call

GetNamespace indicates an expected call of GetNamespace.

func (*MockServerMockRecorder) GetProtocol

func (mr *MockServerMockRecorder) GetProtocol() *gomock.Call

GetProtocol indicates an expected call of GetProtocol.

func (*MockServerMockRecorder) GetTags

func (mr *MockServerMockRecorder) GetTags() *gomock.Call

GetTags indicates an expected call of GetTags.

func (*MockServerMockRecorder) PublicAddress

func (mr *MockServerMockRecorder) PublicAddress() *gomock.Call

PublicAddress indicates an expected call of PublicAddress.

func (*MockServerMockRecorder) PublicPort

func (mr *MockServerMockRecorder) PublicPort() *gomock.Call

PublicPort indicates an expected call of PublicPort.

func (*MockServerMockRecorder) Push

func (mr *MockServerMockRecorder) Push(arg0, arg1 interface{}) *gomock.Call

Push indicates an expected call of Push.

func (*MockServerMockRecorder) ServiceID

func (mr *MockServerMockRecorder) ServiceID() *gomock.Call

ServiceID indicates an expected call of ServiceID.

func (*MockServerMockRecorder) ServiceName

func (mr *MockServerMockRecorder) ServiceName() *gomock.Call

ServiceName indicates an expected call of ServiceName.

func (*MockServerMockRecorder) SetAcceptor

func (mr *MockServerMockRecorder) SetAcceptor(arg0 interface{}) *gomock.Call

SetAcceptor indicates an expected call of SetAcceptor.

func (*MockServerMockRecorder) SetChannelMap

func (mr *MockServerMockRecorder) SetChannelMap(arg0 interface{}) *gomock.Call

SetChannelMap indicates an expected call of SetChannelMap.

func (*MockServerMockRecorder) SetMessageListener

func (mr *MockServerMockRecorder) SetMessageListener(arg0 interface{}) *gomock.Call

SetMessageListener indicates an expected call of SetMessageListener.

func (*MockServerMockRecorder) SetReadWait

func (mr *MockServerMockRecorder) SetReadWait(arg0 interface{}) *gomock.Call

SetReadWait indicates an expected call of SetReadWait.

func (*MockServerMockRecorder) SetStateListener

func (mr *MockServerMockRecorder) SetStateListener(arg0 interface{}) *gomock.Call

SetStateListener indicates an expected call of SetStateListener.

func (*MockServerMockRecorder) Shutdown

func (mr *MockServerMockRecorder) Shutdown(arg0 interface{}) *gomock.Call

Shutdown indicates an expected call of Shutdown.

func (*MockServerMockRecorder) Start

func (mr *MockServerMockRecorder) Start() *gomock.Call

Start indicates an expected call of Start.

func (*MockServerMockRecorder) String

func (mr *MockServerMockRecorder) String() *gomock.Call

String indicates an expected call of String.

type MockService

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

MockService is a mock of Service interface.

func NewMockService

func NewMockService(ctrl *gomock.Controller) *MockService

NewMockService creates a new mock instance.

func (*MockService) EXPECT

func (m *MockService) EXPECT() *MockServiceMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockService) GetMeta

func (m *MockService) GetMeta() map[string]string

GetMeta mocks base method.

func (*MockService) ServiceID

func (m *MockService) ServiceID() string

ServiceID mocks base method.

func (*MockService) ServiceName

func (m *MockService) ServiceName() string

ServiceName mocks base method.

type MockServiceMockRecorder

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

MockServiceMockRecorder is the mock recorder for MockService.

func (*MockServiceMockRecorder) GetMeta

func (mr *MockServiceMockRecorder) GetMeta() *gomock.Call

GetMeta indicates an expected call of GetMeta.

func (*MockServiceMockRecorder) ServiceID

func (mr *MockServiceMockRecorder) ServiceID() *gomock.Call

ServiceID indicates an expected call of ServiceID.

func (*MockServiceMockRecorder) ServiceName

func (mr *MockServiceMockRecorder) ServiceName() *gomock.Call

ServiceName indicates an expected call of ServiceName.

type MockServiceRegistration

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

MockServiceRegistration is a mock of ServiceRegistration interface.

func NewMockServiceRegistration

func NewMockServiceRegistration(ctrl *gomock.Controller) *MockServiceRegistration

NewMockServiceRegistration creates a new mock instance.

func (*MockServiceRegistration) DialURL

func (m *MockServiceRegistration) DialURL() string

DialURL mocks base method.

func (*MockServiceRegistration) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockServiceRegistration) GetMeta

func (m *MockServiceRegistration) GetMeta() map[string]string

GetMeta mocks base method.

func (*MockServiceRegistration) GetNamespace

func (m *MockServiceRegistration) GetNamespace() string

GetNamespace mocks base method.

func (*MockServiceRegistration) GetProtocol

func (m *MockServiceRegistration) GetProtocol() string

GetProtocol mocks base method.

func (*MockServiceRegistration) GetTags

func (m *MockServiceRegistration) GetTags() []string

GetTags mocks base method.

func (*MockServiceRegistration) PublicAddress

func (m *MockServiceRegistration) PublicAddress() string

PublicAddress mocks base method.

func (*MockServiceRegistration) PublicPort

func (m *MockServiceRegistration) PublicPort() int

PublicPort mocks base method.

func (*MockServiceRegistration) ServiceID

func (m *MockServiceRegistration) ServiceID() string

ServiceID mocks base method.

func (*MockServiceRegistration) ServiceName

func (m *MockServiceRegistration) ServiceName() string

ServiceName mocks base method.

func (*MockServiceRegistration) String

func (m *MockServiceRegistration) String() string

String mocks base method.

type MockServiceRegistrationMockRecorder

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

MockServiceRegistrationMockRecorder is the mock recorder for MockServiceRegistration.

func (*MockServiceRegistrationMockRecorder) DialURL

DialURL indicates an expected call of DialURL.

func (*MockServiceRegistrationMockRecorder) GetMeta

GetMeta indicates an expected call of GetMeta.

func (*MockServiceRegistrationMockRecorder) GetNamespace

func (mr *MockServiceRegistrationMockRecorder) GetNamespace() *gomock.Call

GetNamespace indicates an expected call of GetNamespace.

func (*MockServiceRegistrationMockRecorder) GetProtocol

func (mr *MockServiceRegistrationMockRecorder) GetProtocol() *gomock.Call

GetProtocol indicates an expected call of GetProtocol.

func (*MockServiceRegistrationMockRecorder) GetTags

GetTags indicates an expected call of GetTags.

func (*MockServiceRegistrationMockRecorder) PublicAddress

func (mr *MockServiceRegistrationMockRecorder) PublicAddress() *gomock.Call

PublicAddress indicates an expected call of PublicAddress.

func (*MockServiceRegistrationMockRecorder) PublicPort

PublicPort indicates an expected call of PublicPort.

func (*MockServiceRegistrationMockRecorder) ServiceID

ServiceID indicates an expected call of ServiceID.

func (*MockServiceRegistrationMockRecorder) ServiceName

func (mr *MockServiceRegistrationMockRecorder) ServiceName() *gomock.Call

ServiceName indicates an expected call of ServiceName.

func (*MockServiceRegistrationMockRecorder) String

String indicates an expected call of String.

type MockSessionStorage

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

MockSessionStorage is a mock of SessionStorage interface.

func NewMockSessionStorage

func NewMockSessionStorage(ctrl *gomock.Controller) *MockSessionStorage

NewMockSessionStorage creates a new mock instance.

func (*MockSessionStorage) Add

func (m *MockSessionStorage) Add(session *pkt.Session) error

Add mocks base method.

func (*MockSessionStorage) Delete

func (m *MockSessionStorage) Delete(account, channelId string) error

Delete mocks base method.

func (*MockSessionStorage) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockSessionStorage) Get

func (m *MockSessionStorage) Get(channelId string) (*pkt.Session, error)

Get mocks base method.

func (*MockSessionStorage) GetLocation

func (m *MockSessionStorage) GetLocation(account, device string) (*Location, error)

GetLocation mocks base method.

func (*MockSessionStorage) GetLocations

func (m *MockSessionStorage) GetLocations(account ...string) ([]*Location, error)

GetLocations mocks base method.

type MockSessionStorageMockRecorder

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

MockSessionStorageMockRecorder is the mock recorder for MockSessionStorage.

func (*MockSessionStorageMockRecorder) Add

func (mr *MockSessionStorageMockRecorder) Add(session interface{}) *gomock.Call

Add indicates an expected call of Add.

func (*MockSessionStorageMockRecorder) Delete

func (mr *MockSessionStorageMockRecorder) Delete(account, channelId interface{}) *gomock.Call

Delete indicates an expected call of Delete.

func (*MockSessionStorageMockRecorder) Get

func (mr *MockSessionStorageMockRecorder) Get(channelId interface{}) *gomock.Call

Get indicates an expected call of Get.

func (*MockSessionStorageMockRecorder) GetLocation

func (mr *MockSessionStorageMockRecorder) GetLocation(account, device interface{}) *gomock.Call

GetLocation indicates an expected call of GetLocation.

func (*MockSessionStorageMockRecorder) GetLocations

func (mr *MockSessionStorageMockRecorder) GetLocations(account ...interface{}) *gomock.Call

GetLocations indicates an expected call of GetLocations.

type MockStateListener

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

MockStateListener is a mock of StateListener interface.

func NewMockStateListener

func NewMockStateListener(ctrl *gomock.Controller) *MockStateListener

NewMockStateListener creates a new mock instance.

func (*MockStateListener) Disconnect

func (m *MockStateListener) Disconnect(arg0 string) error

Disconnect mocks base method.

func (*MockStateListener) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

type MockStateListenerMockRecorder

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

MockStateListenerMockRecorder is the mock recorder for MockStateListener.

func (*MockStateListenerMockRecorder) Disconnect

func (mr *MockStateListenerMockRecorder) Disconnect(arg0 interface{}) *gomock.Call

Disconnect indicates an expected call of Disconnect.

type OpCode

type OpCode byte

OpCode OpCode

const (
	OpContinuation OpCode = 0x0
	OpText         OpCode = 0x1
	OpBinary       OpCode = 0x2
	OpClose        OpCode = 0x8
	OpPing         OpCode = 0x9
	OpPong         OpCode = 0xa
)

Opcode type

type Router

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

Router defines

func NewRouter

func NewRouter() *Router

NewRouter NewRouter

func (*Router) Handle

func (r *Router) Handle(command string, handlers ...HandlerFunc)

Handle register a command handler

func (*Router) Serve

func (r *Router) Serve(packet *pkt.LogicPkt, dispatcher Dispatcher, cache SessionStorage, session Session) error

Serve a packet from client

func (*Router) Use

func (r *Router) Use(handlers ...HandlerFunc)

type Server

type Server interface {
	ServiceRegistration
	// SetAcceptor 设置Acceptor
	SetAcceptor(Acceptor)
	//SetMessageListener 设置上行消息监听器
	SetMessageListener(MessageListener)
	//SetStateListener 设置连接状态监听服务
	SetStateListener(StateListener)
	// SetReadWait 设置读超时
	SetReadWait(time.Duration)
	// SetChannelMap 设置Channel管理服务
	SetChannelMap(ChannelMap)

	// Start 用于在内部实现网络端口的监听和接收连接,
	// 并完成一个Channel的初始化过程。
	Start() error
	// Push 消息到指定的Channel中
	//  string channelID
	//  []byte 序列化之后的消息数据
	Push(string, []byte) error
	// Shutdown 服务下线,关闭连接
	Shutdown(context.Context) error
}

Server 定义了一个tcp/websocket不同协议通用的服务端的接口

type ServerOption

type ServerOption func(*ServerOptions)

func WithConnectionGPool

func WithConnectionGPool(val int) ServerOption

func WithMessageGPool

func WithMessageGPool(val int) ServerOption

type ServerOptions

type ServerOptions struct {
	Loginwait       time.Duration //登录超时
	Readwait        time.Duration //读超时
	Writewait       time.Duration //写超时
	MessageGPool    int
	ConnectionGPool int
}

ServerOptions ServerOptions

type Service

type Service interface {
	ServiceID() string
	ServiceName() string
	GetMeta() map[string]string
}

定义了基础服务的抽象接口

type ServiceRegistration

type ServiceRegistration interface {
	Service
	PublicAddress() string
	PublicPort() int
	DialURL() string
	GetTags() []string
	GetProtocol() string
	GetNamespace() string
	String() string
}

定义服务注册的抽象接口

type Session

type Session interface {
	GetChannelId() string
	GetGateId() string
	GetAccount() string
	GetRemoteIP() string
	GetApp() string
	GetTags() []string
}

Session read only

type SessionStorage

type SessionStorage interface {
	// Add a session
	Add(session *pkt.Session) error
	// Delete a session
	Delete(account string, channelId string) error
	// Get session by channelId
	Get(channelId string) (*pkt.Session, error)
	// Get Locations by accounts
	GetLocations(account ...string) ([]*Location, error)
	// Get Location by account and device
	GetLocation(account string, device string) (*Location, error)
}

SessionStorage defined a session storage which provides based functions as save,delete,find a session

type StateListener

type StateListener interface {
	// 连接断开回调
	Disconnect(string) error
}

StateListener 状态监听器

type Upgrader

type Upgrader interface {
	Name() string
	Upgrade(rawconn net.Conn, rd *bufio.Reader, wr *bufio.Writer) (Conn, error)
}

Jump to

Keyboard shortcuts

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