hpds_net_framework

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

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

Go to latest
Published: Aug 3, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

README

hpds_net_framework

网络开发整体框架

Documentation

Index

Constants

View Source
const (
	Msg = iota
	Conn
	Raw
)

回调传参常量

Variables

Functions

func NewTcpServer

func NewTcpServer(addr string, processor Processor, logger *logging.Logger) (s *tcpServer, err error)

NewTcpServer return new tcpServer

func SetConf

func SetConf(cfg *Data)

SetConf this before startup server

Types

type BroadcastNode

type BroadcastNode struct {
	// 节点ID
	NodeId string
	// 网络连接
	Connections map[interface{}]IConnection
	// contains filtered or unexported fields
}

BroadcastNode 广播转发节点

func NewBroadcastNode

func NewBroadcastNode(logger *logging.Logger) *BroadcastNode

NewBroadcastNode return a new BroadcastNode

func (*BroadcastNode) AddConn

func (bNode *BroadcastNode) AddConn(conn IConnection) error

AddConn by conn

func (*BroadcastNode) Complete

func (bNode *BroadcastNode) Complete() error

Complete sync

func (*BroadcastNode) DelConn

func (bNode *BroadcastNode) DelConn(key string) error

DelConn by key

func (*BroadcastNode) Destroy

func (bNode *BroadcastNode) Destroy() error

Destroy the node

func (*BroadcastNode) GetAllMessage

func (bNode *BroadcastNode) GetAllMessage() chan []interface{}

GetAllMessage return chan []interface{}

func (*BroadcastNode) OnProtocolMessage

func (bNode *BroadcastNode) OnProtocolMessage(msg interface{}) error

OnProtocolMessage interface

func (*BroadcastNode) OnRawMessage

func (bNode *BroadcastNode) OnRawMessage([]byte) error

OnRawMessage bytes

func (*BroadcastNode) Serve

func (bNode *BroadcastNode) Serve()

Serve the node

type Data

type Data struct {
	// 单个连接未处理消息包缓存队列大小
	// 注意:[超过这个大小,包将丢弃,视为当前系统无法处理,默认100]
	ConnUndoQueueSize int
	// 单个连接未写入消息包队列大小 [超过这个大小,包将丢弃,视为当前系统无法处理,默认为1]
	ConnWriteQueueSize int
	// 第一个包等待超市时间 (s) [默认5秒,连接上来未读到正确包,断开连接]
	FirstPackageTimeout int
	// 连接读取超时(s) [默认35秒, 超时等待时间内,请发送任何数据包,如心跳包]
	ConnReadTimeout int
	// 连接写超时(s) [默认5秒, 超时等待时间内,请发送任何数据包,如心跳包]
	ConnWriteTimeout int
	// 数据包最大限制,[默认2048]
	MaxDataPackageSize int
	// ws 最大header,[默认1024]
	MaxHeaderLen int
}

Data is the config struct

var (
	// Cfg is the config instance
	Cfg *Data
)

type Encryptor

type Encryptor interface {
	Encode(bs []byte) []byte
	Decode(bs []byte) []byte
}

Encryptor interface

type FrameNode

type FrameNode struct {
	// 节点ID
	NodeId string
	// 网络连接
	Connections map[interface{}]IConnection

	// 同步周期
	FrameTicker *time.Ticker

	// rand seed
	RandSeed int64
	// contains filtered or unexported fields
}

FrameNode 帧同步节点

func NewFrameNode

func NewFrameNode(logger *logging.Logger) *FrameNode

NewFrameNode return a new FrameNode

func (*FrameNode) AddConn

func (gr *FrameNode) AddConn(conn IConnection) error

AddConn conn

func (*FrameNode) Complete

func (gr *FrameNode) Complete() error

Complete sync

func (*FrameNode) DelConn

func (gr *FrameNode) DelConn(key string) error

DelConn by key

func (*FrameNode) Destroy

func (gr *FrameNode) Destroy() error

Destroy the node

func (*FrameNode) GetAllMessage

func (gr *FrameNode) GetAllMessage() chan []interface{}

GetAllMessage return chan []interface

func (*FrameNode) OnProtocolMessage

func (gr *FrameNode) OnProtocolMessage(interface{}) error

OnProtocolMessage interface

func (*FrameNode) OnRawMessage

func (gr *FrameNode) OnRawMessage(msg []byte) error

OnRawMessage msg

func (*FrameNode) Serve

func (gr *FrameNode) Serve()

Serve the node

type IConnection

type IConnection interface {
	GetUuid() string
	ReadMsg()
	WriteMsg(message interface{})
	Close() error
	AfterClose(func())
	//SetData 设置自定义数据
	SetData(interface{})
	GetData() interface{}
	//SetNode 设置节点
	SetNode(INode)
	GetNode() INode
	//IsClosed 是否关闭
	IsClosed() bool
}

IConnection 网络连接

type INode

type INode interface {
	AddConn(IConnection) error
	DelConn(string) error
	Serve()
	OnRawMessage([]byte) error
	OnProtocolMessage(interface{}) error
	GetAllMessage() chan []interface{}
	Destroy() error
	Complete() error
}

INode 网络同步节点,如消息节点,聊天室节点

type KCPConn

type KCPConn struct {
	*TCPConn
}

KCPConn 可靠的UDP,like TCP

func NewKcpConn

func NewKcpConn(conn net.Conn, processor Processor, logger *logging.Logger) *KCPConn

NewKcpConn get new kcp conn

type PbProcessor

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

PbProcessor one of Processor implement protoc --go_out=. *.proto

func NewPbProcessor

func NewPbProcessor(logger *logging.Logger) *PbProcessor

NewPbProcessor return PB processor

func (*PbProcessor) GetBigEndian

func (pbf *PbProcessor) GetBigEndian() bool

GetBigEndian of the order

func (*PbProcessor) OnReceivedPackage

func (pbf *PbProcessor) OnReceivedPackage(writer interface{}, body []byte) error

OnReceivedPackage 收到完整数据包, 返回解包错误

func (*PbProcessor) RegisterHandler

func (pbf *PbProcessor) RegisterHandler(id int, entity interface{}, handle func(args ...interface{}))

RegisterHandler for logic

func (*PbProcessor) SetBigEndian

func (pbf *PbProcessor) SetBigEndian()

SetBigEndian for order

func (*PbProcessor) SetEncryptor

func (pbf *PbProcessor) SetEncryptor(enc Encryptor)

SetEncryptor for processor

func (*PbProcessor) WrapIdMsg

func (pbf *PbProcessor) WrapIdMsg(id uint32, message interface{}) ([]byte, error)

WrapIdMsg format the interface message to []byte with id

func (*PbProcessor) WrapMsg

func (pbf *PbProcessor) WrapMsg(message interface{}) ([]byte, error)

WrapMsg format the interface message to []byte

func (*PbProcessor) WrapMsgNoHeader

func (pbf *PbProcessor) WrapMsgNoHeader(message interface{}) ([]byte, error)

WrapMsgNoHeader without header length

type Processor

type Processor interface {
	SetBigEndian()
	GetBigEndian() bool
	SetEncryptor(enc Encryptor)
	OnReceivedPackage(interface{}, []byte) error
	WrapMsg(interface{}) ([]byte, error)
	WrapIdMsg(id uint32, data interface{}) ([]byte, error)
	RegisterHandler(id int, entity interface{}, handle func(args ...interface{}))
}

Processor interface

type Protocol

type Protocol struct {
	Id      uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
	Content []byte `protobuf:"bytes,2,opt,name=content,proto3" json:"content,omitempty"`
	// contains filtered or unexported fields
}

协议消息

func (*Protocol) Descriptor deprecated

func (*Protocol) Descriptor() ([]byte, []int)

Deprecated: Use Protocol.ProtoReflect.Descriptor instead.

func (*Protocol) GetContent

func (x *Protocol) GetContent() []byte

func (*Protocol) GetId

func (x *Protocol) GetId() uint32

func (*Protocol) ProtoMessage

func (*Protocol) ProtoMessage()

func (*Protocol) ProtoReflect

func (x *Protocol) ProtoReflect() protoreflect.Message

func (*Protocol) Reset

func (x *Protocol) Reset()

func (*Protocol) String

func (x *Protocol) String() string

type ScFrame

type ScFrame struct {
	Frame     uint32   `protobuf:"varint,1,opt,name=frame,proto3" json:"frame,omitempty"`
	Protocols [][]byte `protobuf:"bytes,2,rep,name=protocols,proto3" json:"protocols,omitempty"`
	// contains filtered or unexported fields
}

帧消息

func (*ScFrame) Descriptor deprecated

func (*ScFrame) Descriptor() ([]byte, []int)

Deprecated: Use ScFrame.ProtoReflect.Descriptor instead.

func (*ScFrame) GetFrame

func (x *ScFrame) GetFrame() uint32

func (*ScFrame) GetProtocols

func (x *ScFrame) GetProtocols() [][]byte

func (*ScFrame) ProtoMessage

func (*ScFrame) ProtoMessage()

func (*ScFrame) ProtoReflect

func (x *ScFrame) ProtoReflect() protoreflect.Message

func (*ScFrame) Reset

func (x *ScFrame) Reset()

func (*ScFrame) String

func (x *ScFrame) String() string

type ScProtocolPack

type ScProtocolPack struct {
	Id   uint32      `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
	Pack []*Protocol `protobuf:"bytes,2,rep,name=pack,proto3" json:"pack,omitempty"`
	// contains filtered or unexported fields
}

打包消息

func (*ScProtocolPack) Descriptor deprecated

func (*ScProtocolPack) Descriptor() ([]byte, []int)

Deprecated: Use ScProtocolPack.ProtoReflect.Descriptor instead.

func (*ScProtocolPack) GetId

func (x *ScProtocolPack) GetId() uint32

func (*ScProtocolPack) GetPack

func (x *ScProtocolPack) GetPack() []*Protocol

func (*ScProtocolPack) ProtoMessage

func (*ScProtocolPack) ProtoMessage()

func (*ScProtocolPack) ProtoReflect

func (x *ScProtocolPack) ProtoReflect() protoreflect.Message

func (*ScProtocolPack) Reset

func (x *ScProtocolPack) Reset()

func (*ScProtocolPack) String

func (x *ScProtocolPack) String() string

type Server

type Server interface {
	Run() error
	Handle(conn net.Conn)
}

Server interface

type TCPConn

type TCPConn struct {
	sync.RWMutex

	net.Conn
	// contains filtered or unexported fields
}

TCPConn is warped tcp conn for luck

func NewTcpConn

func NewTcpConn(conn net.Conn, processor Processor, logger *logging.Logger) *TCPConn

NewTcpConn return new tcp conn

func (*TCPConn) AfterClose

func (tc *TCPConn) AfterClose(cb func())

AfterClose conn call back

func (*TCPConn) Close

func (tc *TCPConn) Close() error

Close the connection

func (*TCPConn) GetData

func (tc *TCPConn) GetData() interface{}

GetData from conn

func (*TCPConn) GetNode

func (tc *TCPConn) GetNode() INode

GetNode from conn

func (*TCPConn) GetUuid

func (tc *TCPConn) GetUuid() string

GetUuid get uuid of conn

func (*TCPConn) IsClosed

func (tc *TCPConn) IsClosed() bool

IsClosed return the status of conn

func (*TCPConn) ReadMsg

func (tc *TCPConn) ReadMsg()

ReadMsg read | write end -> write | read end -> conn end

func (*TCPConn) SetData

func (tc *TCPConn) SetData(data interface{})

SetData for conn

func (*TCPConn) SetNode

func (tc *TCPConn) SetNode(node INode)

SetNode for conn

func (*TCPConn) WriteMsg

func (tc *TCPConn) WriteMsg(message interface{})

WriteMsg warp msg base on connection's processor

Directories

Path Synopsis
examples
tcp

Jump to

Keyboard shortcuts

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