network

package
v0.0.0-...-12adc20 Latest Latest
Warning

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

Go to latest
Published: May 10, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Time allowed to write a message to the peer.
	WriteWait = 10 * time.Second
	// Time allowed to read the next pong message from the peer.
	PongWait = 60 * time.Second
	// Send pings to peer with this period. Must be less than pongWait.
	PingPeriod = (PongWait * 9) / 10
	// Maximum message size allowed from peer.
	MaxMessageSize int64 = 64 * 1024
)

Functions

func Pack

func Pack(message Message) (packageData []byte, err error)

Types

type BaseRouter

type BaseRouter struct{}

func (*BaseRouter) Handle

func (br *BaseRouter) Handle(request Request) (err error)

func (*BaseRouter) PostHandle

func (br *BaseRouter) PostHandle(request Request) (err error)

func (*BaseRouter) PreHandle

func (br *BaseRouter) PreHandle(request Request) (err error)

type Connection

type Connection interface {

	// get connecton ID
	GetID() uint64

	// establish a connection between server and client
	Open()

	// close a connection
	Close()

	// reading message from websocket connection
	StartReader()

	// prepare for writing message into websocket connection
	StartWriter()

	// server send message to client by connection
	SendMsg(data []byte) error

	Context() context.Context
}

func NewEchoConnection

func NewEchoConnection(id uint64, server Server, conn *websocket.Conn) Connection

type ConnectionManager

type ConnectionManager interface {

	// add a connection into connection manager
	Add(Connection)

	// remove a connection from connection manager
	Remove(Connection)

	// get a connection by connection ID
	Get(uint64) (Connection, error)

	// count how many connections in total
	Count() int

	// disconnect all connections
	Clear()
}

func NewConnectionManager

func NewConnectionManager() ConnectionManager

type Homey

type Homey struct {
	ConnManager ConnectionManager

	MsgHandler MessageHandler

	RedirectMsgChan chan *[]byte

	OnInit func(context.Context)

	OnConnOpen func(Connection) error

	OnConnClose func(Connection)
	// contains filtered or unexported fields
}

func NewHomey

func NewHomey(messageType int) *Homey

func (*Homey) AddRouter

func (h *Homey) AddRouter(msgID uint32, router Router)

func (*Homey) CallOnConnClose

func (h *Homey) CallOnConnClose(conn Connection)

func (*Homey) CallOnConnOpen

func (h *Homey) CallOnConnOpen(conn Connection) (err error)

func (*Homey) CallOnInit

func (h *Homey) CallOnInit(ctx context.Context)

func (*Homey) ConnectionManager

func (h *Homey) ConnectionManager() ConnectionManager

func (*Homey) Context

func (h *Homey) Context() context.Context

func (*Homey) Distribute

func (h *Homey) Distribute()

func (*Homey) Echo

func (h *Homey) Echo() echo.HandlerFunc

func (*Homey) GetMsgType

func (h *Homey) GetMsgType() int

func (*Homey) MessageHandler

func (h *Homey) MessageHandler() MessageHandler

func (*Homey) RedirectMsgHandler

func (h *Homey) RedirectMsgHandler()

func (*Homey) SetOnConnClose

func (h *Homey) SetOnConnClose(hookFunc func(Connection))

func (*Homey) SetOnConnOpen

func (h *Homey) SetOnConnOpen(hookFunc func(Connection) error)

func (*Homey) SetOnInit

func (h *Homey) SetOnInit(hookFunc func(context.Context))

func (*Homey) Stop

func (h *Homey) Stop()

func (*Homey) SubscribeWorldChannel

func (h *Homey) SubscribeWorldChannel()

type Message

type Message interface {

	// get send message connection id
	GetConnID() uint64

	// get package type
	GetDataType() uint32

	// get message data
	GetData() []byte

	// get message data length
	GetDataLength() uint32

	// set eventually send message connection id
	SetConnID(connID uint64)

	// set package type
	SetDataType(messageType uint32)

	// set message data
	SetData(data []byte)

	// set message data length
	SetDataLength(dataLength uint32)
}

func NewMessage

func NewMessage(packageType uint32, data []byte) Message

func UnPack

func UnPack(binaryData []byte, isForward bool) (Message, error)

type MessageHandler

type MessageHandler interface {
	// execute handler function
	ExecHandler(request Request)

	// add router
	AddRouter(msgType uint32, router Router)

	// start work pool
	StartWorkPool()

	// send message to task queue, the message would be handled by worker
	SendMsgToTaskQueue(Request)

	String()
}

func NewMessageHandler

func NewMessageHandler() MessageHandler

type Request

type Request interface {

	// get request belong connection
	GetConnection() Connection

	// get custom message type which was bound on handler
	GetMsgDataType() uint32

	// get message data
	GetMsgData() []byte
}

type Router

type Router interface {
	// hook function
	PreHandle(request Request) error

	Handle(request Request) error

	PostHandle(request Request) error
}

type Server

type Server interface {
	Context() context.Context
	// get message type
	GetMsgType() int

	// get connection manager
	ConnectionManager() ConnectionManager

	MessageHandler() MessageHandler

	// set a function it would be called on http request arrive
	SetOnInit(func(context.Context))

	// set a function it would be called on a connection openning
	SetOnConnOpen(func(conn Connection) error)

	// set a function it would be called on a connection closing
	SetOnConnClose(func(conn Connection))

	// call this function on http request arrive
	CallOnInit(context.Context)

	// call this function on connection openning
	CallOnConnOpen(Connection) error

	// call this function on connection closing
	CallOnConnClose(Connection)
}

Jump to

Keyboard shortcuts

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