gateway

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEndpointNotExists 该名称下不存在任何端点
	ErrEndpointNotExists = errors.New("gateway: endpoint not exists")
	// ErrGatewayClosed 网关已关闭
	ErrGatewayClosed = errors.New("gateway: gateway closed")
	// ErrGatewayRunning 网关正在运行
	ErrGatewayRunning = errors.New("gateway: gateway running")
	// ErrConnectionNotFount 该端点下不存在该连接
	ErrConnectionNotFount = errors.New("gateway: connection not found")
)
View Source
var DefaultEndpointReconnectInterval = time.Second

Functions

func MarshalGatewayInPacket added in v0.1.0

func MarshalGatewayInPacket(addr string, currentTime int64, packet []byte) ([]byte, error)

MarshalGatewayInPacket 将数据包转换为网关入网数据包

  • | ipv4(4) | port(2) | cost(4) | packet |

func MarshalGatewayOutPacket added in v0.1.0

func MarshalGatewayOutPacket(addr string, packet []byte) ([]byte, error)

MarshalGatewayOutPacket 将数据包转换为网关出网数据包

  • | identifier(4) | ipv4(4) | port(2) | packet |

func UnmarshalGatewayInPacket added in v0.1.0

func UnmarshalGatewayInPacket(data []byte) (addr string, sendTime int64, packet []byte, err error)

UnmarshalGatewayInPacket 将网关入网数据包转换为数据包

  • | ipv4(4) | port(2) | cost(4) | packet |

func UnmarshalGatewayOutPacket added in v0.1.0

func UnmarshalGatewayOutPacket(data []byte) (addr string, packet []byte, err error)

UnmarshalGatewayOutPacket 将网关出网数据包转换为数据包

  • | identifier(4) | ipv4(4) | port(2) | packet |

Types

type ConnectionClosedEventHandle added in v0.1.1

type ConnectionClosedEventHandle func(gateway *Gateway, conn *server.Conn)

type ConnectionOpenedEventHandle added in v0.1.1

type ConnectionOpenedEventHandle func(gateway *Gateway, conn *server.Conn)

type ConnectionReceivePacketEventHandle added in v0.1.1

type ConnectionReceivePacketEventHandle func(gateway *Gateway, conn *server.Conn, packet []byte)

type Endpoint

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

Endpoint 网关端点

func NewEndpoint

func NewEndpoint(name string, cli *client.Client, options ...EndpointOption) *Endpoint

NewEndpoint 创建网关端点

func (*Endpoint) Forward added in v0.1.1

func (slf *Endpoint) Forward(conn *server.Conn, packet []byte, callback ...func(err error))

Forward 转发数据包到该端点

  • 端点在处理数据包时,应区分数据包为普通直连数据包还是网关数据包。可通过 UnmarshalGatewayOutPacket 进行数据包解析,当解析失败且无其他数据包协议时,可认为该数据包为普通直连数据包。

func (*Endpoint) GetAddress added in v0.1.1

func (slf *Endpoint) GetAddress() string

GetAddress 获取端点地址

func (*Endpoint) GetName added in v0.1.1

func (slf *Endpoint) GetName() string

GetName 获取端点名称

func (*Endpoint) GetState added in v0.1.1

func (slf *Endpoint) GetState() float64

GetState 获取端点健康值

type EndpointConnectClosedEventHandle added in v0.1.1

type EndpointConnectClosedEventHandle func(gateway *Gateway, endpoint *Endpoint)

type EndpointConnectOpenedEventHandle added in v0.1.1

type EndpointConnectOpenedEventHandle func(gateway *Gateway, endpoint *Endpoint)

type EndpointConnectReceivePacketEventHandle added in v0.1.1

type EndpointConnectReceivePacketEventHandle func(gateway *Gateway, endpoint *Endpoint, conn *server.Conn, packet []byte)

type EndpointOption added in v0.0.30

type EndpointOption func(endpoint *Endpoint)

EndpointOption 网关端点选项

func WithEndpointStateEvaluator added in v0.0.30

func WithEndpointStateEvaluator(evaluator func(costUnixNano float64) float64) EndpointOption

WithEndpointStateEvaluator 设置端点健康值评估函数

func WithReconnectInterval added in v0.1.1

func WithReconnectInterval(interval time.Duration) EndpointOption

WithReconnectInterval 设置端点重连间隔

  • 默认为 DefaultEndpointReconnectInterval
  • 端点在连接失败后会在该间隔后重连,如果 <= 0 则不会重连

type EndpointSelector added in v0.1.1

type EndpointSelector func(endpoints []*Endpoint) *Endpoint

EndpointSelector 端点选择器,用于从多个端点中选择一个可用的端点,如果没有可用的端点则返回 nil

type Gateway

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

Gateway 网关

func NewGateway

func NewGateway(srv *server.Server, scanner Scanner, options ...Option) *Gateway

NewGateway 基于 server.Server 创建网关服务器

  • behaviorController 行为控制函数决定了客户端与网关服务器建立连接及接收数据包后的行为

func (*Gateway) GetEndpoint added in v0.1.1

func (slf *Gateway) GetEndpoint(name string) (*Endpoint, error)

GetEndpoint 获取一个可用的端点

  • name: 端点名称

func (Gateway) OnConnectionClosedEvent added in v0.1.1

func (slf Gateway) OnConnectionClosedEvent(gateway *Gateway, conn *server.Conn)

func (Gateway) OnConnectionOpenedEvent added in v0.1.1

func (slf Gateway) OnConnectionOpenedEvent(gateway *Gateway, conn *server.Conn)

func (Gateway) OnConnectionReceivePacketEvent added in v0.1.1

func (slf Gateway) OnConnectionReceivePacketEvent(gateway *Gateway, conn *server.Conn, packet []byte)

func (Gateway) OnEndpointConnectClosedEvent added in v0.1.1

func (slf Gateway) OnEndpointConnectClosedEvent(gateway *Gateway, endpoint *Endpoint)

func (Gateway) OnEndpointConnectOpenedEvent added in v0.1.1

func (slf Gateway) OnEndpointConnectOpenedEvent(gateway *Gateway, endpoint *Endpoint)

func (Gateway) OnEndpointConnectReceivePacketEvent added in v0.1.1

func (slf Gateway) OnEndpointConnectReceivePacketEvent(gateway *Gateway, endpoint *Endpoint, conn *server.Conn, packet []byte)

func (Gateway) RegConnectionClosedEventHandle added in v0.1.1

func (slf Gateway) RegConnectionClosedEventHandle(handle ConnectionClosedEventHandle, priority ...int)

RegConnectionClosedEventHandle 注册客户端连接关闭事件处理函数

func (Gateway) RegConnectionOpenedEventHandle added in v0.1.1

func (slf Gateway) RegConnectionOpenedEventHandle(handle ConnectionOpenedEventHandle, priority ...int)

RegConnectionOpenedEventHandle 注册客户端连接打开事件处理函数

func (Gateway) RegConnectionReceivePacketEventHandle added in v0.1.1

func (slf Gateway) RegConnectionReceivePacketEventHandle(handle ConnectionReceivePacketEventHandle, priority ...int)

RegConnectionReceivePacketEventHandle 注册客户端连接接收数据包事件处理函数

func (Gateway) RegEndpointConnectClosedEventHandle added in v0.1.1

func (slf Gateway) RegEndpointConnectClosedEventHandle(handle EndpointConnectClosedEventHandle, priority ...int)

RegEndpointConnectClosedEventHandle 注册端点连接关闭事件处理函数

func (Gateway) RegEndpointConnectOpenedEventHandle added in v0.1.1

func (slf Gateway) RegEndpointConnectOpenedEventHandle(handle EndpointConnectOpenedEventHandle, priority ...int)

RegEndpointConnectOpenedEventHandle 注册端点连接打开事件处理函数

func (Gateway) RegEndpointConnectReceivePacketEventHandle added in v0.1.1

func (slf Gateway) RegEndpointConnectReceivePacketEventHandle(handle EndpointConnectReceivePacketEventHandle, priority ...int)

RegEndpointConnectReceivePacketEventHandle 注册端点连接接收数据包事件处理函数

func (*Gateway) Run

func (slf *Gateway) Run(addr string) error

Run 运行网关

func (*Gateway) Server added in v0.1.1

func (slf *Gateway) Server() *server.Server

Server 获取网关服务器核心

func (*Gateway) Shutdown

func (slf *Gateway) Shutdown()

Shutdown 关闭网关

type Option

type Option func(gateway *Gateway)

Option 网关选项

func WithEndpointSelector

func WithEndpointSelector(selector EndpointSelector) Option

WithEndpointSelector 设置端点选择器

  • 默认情况下,网关会随机选择一个端点作为目标,如果需要自定义端点选择器,可以通过该选项设置

type Scanner added in v0.1.1

type Scanner interface {
	// GetEndpoints 获取端点列表
	GetEndpoints() ([]*Endpoint, error)
	// GetInterval 获取扫描间隔
	GetInterval() time.Duration
}

Scanner 端点扫描器

Jump to

Keyboard shortcuts

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