spider

package module
v0.0.0-...-ad31191 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2023 License: MIT Imports: 16 Imported by: 0

README

spider

A simple tcp framework in Go.

Usage

只实现了服务器的部分,客户端的部分还没写。

因为设计的问题,客服端不想写了。

实在是太菜了,写不动了。

经过一段时间的学习,重新设计了一下,现在可以使用了。

实现了客户端和服务器的部分。

当前版本只有客户端进行请求,服务器进行响应。

规划的还有服务器的推送,和服务器请求客户端的功能。客户端可以推送和响应服务器的请求。

做到真正的双工通信。

功能

  1. 支持多种协议,目前只实现了TCP协议。
  2. 支持多种编码,目前实现了 JSON | protobuf 编码,智能选择对应协议。
  3. 支持消息路由,目前实现了基于消息ID的路由。类似 gin 的路由。
  4. 支持连接的创建管理,通过函数验证通过后才能保持连接。
  5. 支持消息注册中间件,可以在消息到达路由前进行处理。

希望有大佬来指点。

Documentation

Index

Constants

View Source
const ABORT int8 = 100

Variables

This section is empty.

Functions

This section is empty.

Types

type Call

type Call struct {
	Reply message.Message
	Error error      // After completion, the error status.
	Done  chan *Call // Strobes when call is complete.
	// contains filtered or unexported fields
}

Call represents an active req.

type ConnConfig

type ConnConfig struct {

	// 心跳控制 TODO
	HeartBeatOn       bool
	HeartBeatInterval time.Duration
	// contains filtered or unexported fields
}

type ConnConfigOption

type ConnConfigOption func(ConnConfig) ConnConfig

func WithAddr

func WithAddr(addr string) ConnConfigOption

WithAddr sets the server address.

func WithBinaryPoolSize

func WithBinaryPoolSize(min, max int) ConnConfigOption

WithBinaryPoolSize sets the binary pool size. default: min=512, max=512*1024

func WithHeartBeat

func WithHeartBeat(interval time.Duration) ConnConfigOption

WithHeartBeat sets the heart beat.

func WithMaxMsgNum

func WithMaxMsgNum(send, recv int32) ConnConfigOption

WithMaxMsgNum sets the max [send|recv] message number. default: send=1000, recv=10000

func WithOnConnHandle

func WithOnConnHandle(onConnHandle func(conn TcpConn) bool) ConnConfigOption

WithOnConnHandle sets the onConnHandle.

func WithProto

func WithProto(p proto.Proto) ConnConfigOption

WithProto sets the tcp server proto.

func WithReadTimeout

func WithReadTimeout(d time.Duration) ConnConfigOption

WithReadTimeout sets the read timeout.

func WithReconnection

func WithReconnection(reconnection bool) ConnConfigOption

WithReconnection sets the reconnection.

func WithRecvBufferSize

func WithRecvBufferSize(size int32) ConnConfigOption

WithRecvBufferSize sets the recv buffer size.

func WithTLSConfig

func WithTLSConfig(tlsConfig *tls.Config) ConnConfigOption

WithTLSConfig sets the tls config.

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) ConnConfigOption

WithWriteTimeout sets the write timeout.

type Context

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

func NewContext

func NewContext(ctx context.Context, reqMsg message.Message, conn TcpConn) *Context

func (*Context) Abort

func (c *Context) Abort()

Abort stop middleware chain

func (*Context) Bind

func (c *Context) Bind(dest any) error

Bind 自动反序列化

func (*Context) GetCtx

func (c *Context) GetCtx() context.Context

GetCtx 获取上下文

func (*Context) GetReqMsg

func (c *Context) GetReqMsg() message.Message

GetReqMsg 获取上下文

func (*Context) GetReqMsgId

func (c *Context) GetReqMsgId() uint32

GetReqMsgId 获取上下文

func (*Context) JSON

func (c *Context) JSON(msgId uint32, src interface{}, meatData ...map[string]string) error

JSON Reply to client using json marshaller. Whatever ctx.Packx.Marshaller.MarshalName is 'json' or not , message block will marshal its header and body by json marshaller.

func (*Context) MarshallerType

func (c *Context) MarshallerType() codec.MarshalType

MarshallerType 获取解析类型,请配合RawData使用

func (*Context) Next

func (c *Context) Next()

Next Since middlewares are divided into 3 kinds: global, messageIDSelfRelated, anchorType, offset can't be used straightly to control middlewares like middlewares[offset](). Thus, c.Next() means actually do nothing.

func (*Context) ProtoBuf

func (c *Context) ProtoBuf(msgId uint32, src interface{}, meatData ...map[string]string) error

ProtoBuf Reply to client using protobuf marshaller. Whatever ctx.Packx.Marshaller.MarshalName is 'protobuf' or not , message block will marshal its header and body by protobuf marshaller.

func (*Context) Raw

func (c *Context) Raw(msgId uint32, src interface{}, meatData ...map[string]string) error

Raw Reply to client using protobuf marshaller. Whatever ctx.Packx.Marshaller.MarshalName is 'protobuf' or not , message block will marshal its header and body by protobuf marshaller.

func (*Context) RawData

func (c *Context) RawData() []byte

RawData 获取原始数据,不做任何解析,请根据MarshallerType 配合使用

func (*Context) SetCtx

func (c *Context) SetCtx(ctx context.Context)

SetCtx 获取上下文

type MsgMiddleHandler

type MsgMiddleHandler struct {
	// 模块中间件
	ModelMiddles []func(ctx *Context)

	// 消息处理函数
	Handlers map[subMsgID]func(ctx *Context)
	// 消息处理函数的中间件
	HandlerMiddles map[subMsgID][]func(ctx *Context)
}

MsgMiddleHandler 模块处理函数

type Mux

type Mux struct {
	// 用于存储消息ID和消息处理函数的映射关系,key为模块ID,value为消息ID和消息处理函数的映射关系
	Handlers map[modelID]*MsgMiddleHandler

	// AllowAdd of allow routes to be added before starting.
	// 只允许在启动前添加路由
	AllowAdd bool

	// global-middles
	// 全局中间件
	GlobalMiddles []func(ctx *Context)
}

Mux is a multiplexer for network connections. 是一个网络路由复用器

func (*Mux) RegisterGlobalMiddle

func (m *Mux) RegisterGlobalMiddle(middles ...func(ctx *Context))

RegisterGlobalMiddle add global routing middle handlers.

func (*Mux) RegisterHandler

func (m *Mux) RegisterHandler(id modelID, subID subMsgID, handler func(ctx *Context), middles ...func(ctx *Context))

RegisterHandler add routing handlers by modelID and subMsgID.

func (*Mux) RegisterModelMiddle

func (m *Mux) RegisterModelMiddle(id modelID, middles ...func(ctx *Context))

RegisterModelMiddle add routing middle handlers by modelID.

type TcpClient

type TcpClient struct {
	TcpConn
	// contains filtered or unexported fields
}

func NewTcpClient

func NewTcpClient(addr string, cfgOptions ...ConnConfigOption) *TcpClient

func (*TcpClient) Call

func (t *TcpClient) Call(c context.Context, req message.Message) (resp message.Message, err error)

Call 发送消息,由客户端进行中间件的处理

func (*TcpClient) Close

func (t *TcpClient) Close()

func (*TcpClient) HandleHeartBeat

func (t *TcpClient) HandleHeartBeat(ctx *Context)

HandleHeartBeat 处理心跳消息

func (*TcpClient) HandlePush

func (t *TcpClient) HandlePush(ctx *Context)

HandlePush 处理推送消息

func (*TcpClient) HandleReply

func (t *TcpClient) HandleReply(ctx *Context)

HandleReply 处理响应消息

func (*TcpClient) IsClose

func (t *TcpClient) IsClose() bool

func (*TcpClient) RegisterGlobalMiddle

func (t *TcpClient) RegisterGlobalMiddle(middles ...func(ctx *Context))

RegisterGlobalMiddle add global routing middle handlers.

func (*TcpClient) Start

func (t *TcpClient) Start() error

Start connects to the address on the named network.

type TcpConn

type TcpConn interface {
	proto.Proto
	net.Conn

	// GetConnId returns the connection id.
	GetConnId() uint64
	SetConnId(uint64)

	Start()

	SendMsg(message.Message) error

	// StopNotifyChan 关闭的时候,需要被通知
	StopNotifyChan() chan struct{}
}

func NewTcpConn

func NewTcpConn(conn *net.TCPConn, cfg ConnConfig, handleFunc func(ctx *Context)) TcpConn

type TcpServer

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

func NewTcpX

func NewTcpX(cfgOptions ...ConnConfigOption) *TcpServer

func (*TcpServer) Close

func (t *TcpServer) Close()

Close 关闭服务

func (*TcpServer) HandleHeartBeat

func (t *TcpServer) HandleHeartBeat(ctx *Context)

HandleHeartBeat 处理心跳消息

func (*TcpServer) HandlePush

func (t *TcpServer) HandlePush(ctx *Context)

HandlePush 处理推送消息

func (*TcpServer) HandleReply

func (t *TcpServer) HandleReply(ctx *Context)

HandleReply 处理响应消息

func (*TcpServer) HandleRequest

func (t *TcpServer) HandleRequest(ctx *Context)

HandleRequest 处理请求消息

func (*TcpServer) IsClosed

func (t *TcpServer) IsClosed() bool

IsClosed 服务是否关闭

func (*TcpServer) ListenAndServe

func (t *TcpServer) ListenAndServe(network, addr string) error

ListenAndServe Start to listen. Serve can decode stream generated by packx. Support tcp and udp

func (*TcpServer) RegisterGlobalMiddle

func (t *TcpServer) RegisterGlobalMiddle(middles ...func(ctx *Context))

RegisterGlobalMiddle add global routing middle handlers.

func (*TcpServer) RegisterHandler

func (t *TcpServer) RegisterHandler(id modelID, subID subMsgID, handler func(ctx *Context), middles ...func(ctx *Context))

RegisterHandler add routing handlers by modelID and subMsgID.

func (*TcpServer) RegisterModelMiddle

func (t *TcpServer) RegisterModelMiddle(id modelID, middles ...func(ctx *Context))

RegisterModelMiddle add routing middle handlers by modelID.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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