transport

package
v0.2.32 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: LGPL-2.1 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnexpectedSeq = errors.New("gtp: unexpected sequence") // 收到非预期的消息序号,表示序号不连续
	ErrDiscardSeq    = errors.New("gtp: discard sequence")    // 收到已过期的消息序号,表示次消息已收到过
)
View Source
var (
	ErrTrans            = errors.New("gtp: trans")      // 传输错误
	ErrNetIO            = errors.New("gtp: net i/o")    // 网络io错误
	ErrRenew            = errors.New("gtp: renew conn") // 刷新链路错误
	ErrDeadlineExceeded = os.ErrDeadlineExceeded        // 网络io超时
	ErrClosed           = os.ErrClosed                  // 网络链路已关闭
	ErrShortBuffer      = io.ErrShortBuffer             // 缓冲区不足
	ErrShortWrite       = io.ErrShortWrite              // 短写
	ErrUnexpectedEOF    = io.ErrUnexpectedEOF           // 非预期的io结束
	EOF                 = io.EOF                        // io结束
)
View Source
var (
	ErrUnexpectedMsg = errors.New("gtp: unexpected msg") // 收到非预期的消息
)

Functions

This section is empty.

Types

type AuthAccept

type AuthAccept = generic.Func1[Event[gtp.MsgAuth], error] // 服务端确认客户端Auth请求

type ChangeCipherSpecAccept

type ChangeCipherSpecAccept = generic.PairFunc1[Event[gtp.MsgChangeCipherSpec], Event[gtp.MsgChangeCipherSpec], error] // 客户端确认服务端ChangeCipherSpec请求

type ChangeCipherSpecFin

type ChangeCipherSpecFin = generic.Func1[Event[gtp.MsgChangeCipherSpec], error] // 服务端获取客户端ChangeCipherSpec响应

type ContinueAccept

type ContinueAccept = generic.Func1[Event[gtp.MsgContinue], error] // 服务端确认客户端Continue请求

type CtrlProtocol

type CtrlProtocol struct {
	Transceiver      *Transceiver     // 消息事件收发器
	RetryTimes       int              // 网络io超时时的重试次数
	RstHandler       RstHandler       // Rst消息事件处理器
	SyncTimeHandler  SyncTimeHandler  // SyncTime消息事件处理器
	HeartbeatHandler HeartbeatHandler // Heartbeat消息事件处理器
}

CtrlProtocol 控制协议

func (*CtrlProtocol) HandleEvent

func (c *CtrlProtocol) HandleEvent(e IEvent) error

HandleEvent 消息事件处理器

func (*CtrlProtocol) RequestTime

func (c *CtrlProtocol) RequestTime(corrId int64) error

RequestTime 请求同步时间

func (*CtrlProtocol) SendPing

func (c *CtrlProtocol) SendPing() error

SendPing 发送ping

func (*CtrlProtocol) SendRst

func (c *CtrlProtocol) SendRst(err error) error

SendRst 发送Rst消息事件

type ECDHESecretKeyExchangeFin

type ECDHESecretKeyExchangeFin = generic.PairFunc1[Event[gtp.MsgECDHESecretKeyExchange], Event[gtp.MsgChangeCipherSpec], error] // 服务端获取客户端ECDHESecretKeyExchange响应

type ErrorHandler

type ErrorHandler = generic.DelegateAction1[error] // 错误处理器

type Event

type Event[T gtp.MsgReader] struct {
	Flags gtp.Flags // 标志位
	Seq   uint32    // 消息序号
	Ack   uint32    // 应答序号
	Msg   T         // 消息
}

Event 消息事件

func EventT added in v0.1.93

func EventT[T gtp.MsgReader](e IEvent) Event[T]

EventT 特化事件,转换为事件具体类型

func (Event[T]) Interface added in v0.1.93

func (e Event[T]) Interface() IEvent

Interface 泛化事件,转换为事件接口

type EventDispatcher

type EventDispatcher struct {
	Transceiver  *Transceiver // 消息事件收发器
	RetryTimes   int          // 网络io超时时的重试次数
	EventHandler EventHandler // 消息事件处理器列表
}

EventDispatcher 消息事件分发器

func (*EventDispatcher) Dispatching

func (d *EventDispatcher) Dispatching(ctx context.Context) error

Dispatching 分发事件

func (*EventDispatcher) Run

func (d *EventDispatcher) Run(ctx context.Context, errorHandler ...ErrorHandler)

Run 运行

type EventHandler

type EventHandler = generic.DelegateFunc1[IEvent, error] // 消息事件处理器

type FinishedAccept

type FinishedAccept = generic.Func1[Event[gtp.MsgFinished], error] // 客户端确认服务端Finished请求

type HandshakeProtocol

type HandshakeProtocol struct {
	Transceiver *Transceiver // 消息事件收发器
	RetryTimes  int          // 网络io超时时的重试次数
}

HandshakeProtocol 握手协议

func (*HandshakeProtocol) ClientAuth

func (h *HandshakeProtocol) ClientAuth(ctx context.Context, auth Event[gtp.MsgAuth]) (err error)

ClientAuth 客户端发起鉴权

func (*HandshakeProtocol) ClientContinue

func (h *HandshakeProtocol) ClientContinue(ctx context.Context, cont Event[gtp.MsgContinue]) (err error)

ClientContinue 客户端发起重连

func (*HandshakeProtocol) ClientFinished

func (h *HandshakeProtocol) ClientFinished(ctx context.Context, finishedAccept FinishedAccept) (err error)

ClientFinished 客户端握手结束

func (*HandshakeProtocol) ClientHello

func (h *HandshakeProtocol) ClientHello(ctx context.Context, hello Event[gtp.MsgHello], helloFin HelloFin) (err error)

ClientHello 客户端Hello

func (*HandshakeProtocol) ClientSecretKeyExchange

func (h *HandshakeProtocol) ClientSecretKeyExchange(ctx context.Context, secretKeyExchangeAccept SecretKeyExchangeAccept, changeCipherSpecAccept ChangeCipherSpecAccept) (err error)

ClientSecretKeyExchange 客户端交换秘钥

func (*HandshakeProtocol) ServerAuth

func (h *HandshakeProtocol) ServerAuth(ctx context.Context, authAccept AuthAccept) (err error)

ServerAuth 服务端验证鉴权

func (*HandshakeProtocol) ServerContinue

func (h *HandshakeProtocol) ServerContinue(ctx context.Context, continueAccept ContinueAccept) (err error)

ServerContinue 服务端处理重连

func (*HandshakeProtocol) ServerECDHESecretKeyExchange

func (h *HandshakeProtocol) ServerECDHESecretKeyExchange(ctx context.Context, secretKeyExchange Event[gtp.MsgECDHESecretKeyExchange], secretKeyExchangeFin ECDHESecretKeyExchangeFin, changeCipherSpecFin ChangeCipherSpecFin) (err error)

ServerECDHESecretKeyExchange 服务端交换秘钥(ECDHE)

func (*HandshakeProtocol) ServerFinished

func (h *HandshakeProtocol) ServerFinished(ctx context.Context, finished Event[gtp.MsgFinished]) (err error)

ServerFinished 服务端握手结束

func (*HandshakeProtocol) ServerHello

func (h *HandshakeProtocol) ServerHello(ctx context.Context, helloAccept HelloAccept) (err error)

ServerHello 服务端Hello

type HeartbeatHandler

type HeartbeatHandler = generic.DelegateFunc1[Event[gtp.MsgHeartbeat], error] // Heartbeat消息事件处理器

type HelloAccept

type HelloAccept = generic.PairFunc1[Event[gtp.MsgHello], Event[gtp.MsgHello], error] // 服务端确认客户端Hello请求

type HelloFin

type HelloFin = generic.Func1[Event[gtp.MsgHello], error] // 客户端获取服务端Hello响应

type IEvent added in v0.1.93

type IEvent = Event[gtp.MsgReader]

IEvent 消息事件接口

type ISynchronizer

type ISynchronizer interface {
	io.Writer
	io.WriterTo
	codec.IValidate
	// Synchronization 同步对端时序,对齐缓存序号
	Synchronization(remoteRecvSeq uint32) error
	// Ack 确认消息序号
	Ack(ack uint32)
	// SendSeq 发送消息序号
	SendSeq() uint32
	// RecvSeq 接收消息序号
	RecvSeq() uint32
	// AckSeq 当前ack序号
	AckSeq() uint32
	// Cap 缓存区容量
	Cap() int
	// Cached 已缓存大小
	Cached() int
	// Clean 清理
	Clean()
}

ISynchronizer 同步器

func NewSequencedSynchronizer

func NewSequencedSynchronizer(sendSeq, recvSeq uint32, cap int) ISynchronizer

NewSequencedSynchronizer 创建有时序同步器,支持缓存已发送的消息,在断连重连时同步时序并补发消息

func NewUnsequencedSynchronizer

func NewUnsequencedSynchronizer() ISynchronizer

NewUnsequencedSynchronizer 创建无时序同步器,不支持断连重连时同步时序

type PayloadHandler

type PayloadHandler = generic.DelegateFunc1[Event[gtp.MsgPayload], error] // Payload消息事件处理器

type Retry

type Retry struct {
	Transceiver *Transceiver
	Times       int
	Ctx         context.Context
}

Retry 网络io超时时重试

func (Retry) Recv

func (r Retry) Recv(e IEvent, err error) (IEvent, error)

Recv 重试接收

func (Retry) Send

func (r Retry) Send(err error) error

Send 重试发送

type RstError

type RstError struct {
	Code    gtp.Code // 错误码
	Message string   // 错误信息
}

RstError Rst错误提示

func CastRstErr

func CastRstErr(e Event[gtp.MsgRst]) *RstError

CastRstErr Rst错误事件转换为错误提示

func (RstError) Error

func (err RstError) Error() string

Error 错误信息

func (RstError) ToEvent added in v0.1.96

func (err RstError) ToEvent() Event[gtp.MsgRst]

ToEvent 转换为消息事件

type RstHandler

type RstHandler = generic.DelegateFunc1[Event[gtp.MsgRst], error] // Rst消息事件处理器

type SecretKeyExchangeAccept

type SecretKeyExchangeAccept = generic.PairFunc1[IEvent, IEvent, error] // 客户端确认服务端SecretKeyExchange请求,需要自己判断消息Id并处理,用于支持多种秘钥交换函数

type SequencedSynchronizer

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

SequencedSynchronizer 有时序同步器,支持缓存已发送的消息,在断连重连时同步时序并补发消息

func (*SequencedSynchronizer) Ack

func (s *SequencedSynchronizer) Ack(ack uint32)

Ack 确认消息序号

func (*SequencedSynchronizer) AckSeq

func (s *SequencedSynchronizer) AckSeq() uint32

AckSeq 当前ack序号

func (*SequencedSynchronizer) Cached

func (s *SequencedSynchronizer) Cached() int

Cached 已缓存大小

func (*SequencedSynchronizer) Cap

func (s *SequencedSynchronizer) Cap() int

Cap 缓存区容量

func (*SequencedSynchronizer) Clean

func (s *SequencedSynchronizer) Clean()

Clean 清理

func (*SequencedSynchronizer) RecvSeq

func (s *SequencedSynchronizer) RecvSeq() uint32

RecvSeq 接收消息序号

func (*SequencedSynchronizer) Reset

func (s *SequencedSynchronizer) Reset(sendSeq, recvSeq uint32, cap int)

Reset 重置缓存

func (*SequencedSynchronizer) SendSeq

func (s *SequencedSynchronizer) SendSeq() uint32

SendSeq 发送消息序号

func (*SequencedSynchronizer) Synchronization

func (s *SequencedSynchronizer) Synchronization(remoteRecvSeq uint32) error

Synchronization 同步对端时序,对齐缓存序号

func (*SequencedSynchronizer) Validate

func (s *SequencedSynchronizer) Validate(msgHead gtp.MsgHead, msgBuf []byte) error

Validate 验证消息包

func (*SequencedSynchronizer) Write

func (s *SequencedSynchronizer) Write(p []byte) (n int, err error)

Write implements io.Writer

func (*SequencedSynchronizer) WriteTo

func (s *SequencedSynchronizer) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriteTo

type SyncTimeHandler

type SyncTimeHandler = generic.DelegateFunc1[Event[gtp.MsgSyncTime], error] // SyncTime消息事件处理器

type TransProtocol

type TransProtocol struct {
	Transceiver    *Transceiver   // 消息事件收发器
	RetryTimes     int            // 网络io超时时的重试次数
	PayloadHandler PayloadHandler // Payload消息事件处理器
}

TransProtocol 传输协议

func (*TransProtocol) HandleEvent

func (t *TransProtocol) HandleEvent(e IEvent) error

HandleEvent 消息事件处理器

func (*TransProtocol) SendData

func (t *TransProtocol) SendData(data []byte) error

SendData 发送数据

type Transceiver

type Transceiver struct {
	Conn         net.Conn       // 网络连接
	Encoder      codec.IEncoder // 消息包编码器
	Decoder      codec.IDecoder // 消息包解码器
	Timeout      time.Duration  // 网络io超时时间
	Synchronizer ISynchronizer  // 同步器
	// contains filtered or unexported fields
}

Transceiver 消息事件收发器,线程安全

func (*Transceiver) Clean

func (t *Transceiver) Clean()

Clean 清理

func (*Transceiver) Continue

func (t *Transceiver) Continue()

Continue 继续收发消息

func (*Transceiver) GC

func (t *Transceiver) GC()

GC GC

func (*Transceiver) Pause

func (t *Transceiver) Pause()

Pause 暂停收发消息

func (*Transceiver) Recv

func (t *Transceiver) Recv(ctx context.Context) (IEvent, error)

Recv 接收消息事件

func (*Transceiver) Renew

func (t *Transceiver) Renew(conn net.Conn, remoteRecvSeq uint32) (sendReq, recvReq uint32, err error)

Renew 刷新链路

func (*Transceiver) Resend

func (t *Transceiver) Resend() error

Resend 重新发送未完整发送的消息事件

func (*Transceiver) Send

func (t *Transceiver) Send(e IEvent) error

Send 发送消息

func (*Transceiver) SendRst

func (t *Transceiver) SendRst(err error) error

SendRst 发送Rst消息事件

type UnsequencedSynchronizer

type UnsequencedSynchronizer struct {
	bytes.Buffer
}

UnsequencedSynchronizer 无时序同步器,不支持断连重连时补发消息

func (*UnsequencedSynchronizer) Ack

func (s *UnsequencedSynchronizer) Ack(ack uint32)

Ack 确认消息序号

func (*UnsequencedSynchronizer) AckSeq

func (s *UnsequencedSynchronizer) AckSeq() uint32

AckSeq 当前ack序号

func (*UnsequencedSynchronizer) Cached

func (s *UnsequencedSynchronizer) Cached() int

Cached 已缓存大小

func (*UnsequencedSynchronizer) Clean

func (s *UnsequencedSynchronizer) Clean()

Clean 清理

func (*UnsequencedSynchronizer) RecvSeq

func (s *UnsequencedSynchronizer) RecvSeq() uint32

RecvSeq 接收消息序号

func (*UnsequencedSynchronizer) SendSeq

func (s *UnsequencedSynchronizer) SendSeq() uint32

SendSeq 发送消息序号

func (*UnsequencedSynchronizer) Synchronization

func (s *UnsequencedSynchronizer) Synchronization(remoteRecvSeq uint32) error

Synchronization 同步对端时序,对齐缓存序号

func (*UnsequencedSynchronizer) Validate

func (s *UnsequencedSynchronizer) Validate(msgHead gtp.MsgHead, msgBuf []byte) error

Validate 验证消息包

func (*UnsequencedSynchronizer) WriteTo

func (s *UnsequencedSynchronizer) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriteTo

Jump to

Keyboard shortcuts

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