road

package
v0.0.0-...-e2d8c84 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: Apache-2.0 Imports: 20 Imported by: 2

README


0x01 road

改编自pitaya v1.1.1版本的golang网络库。

为什么不继续使用pitaya?包括:

  1. pitaya使用一个全局的handlerService对象,该对象会把所有接受到的消息,随机分发到一个goroutine中处理。这样做的一个结果是:来自同一个玩家的消息很可能在不同的goroutine处理,从而无法保证按接收消息的顺序处理,这在很多场景中是不可接受的。
  2. pitaya使用了gorilla这个websocket库,其中的flushFrame()方法必须同步执行,否则会引发panic。目前pitaya中提供的Kick()等接口直接调用了WSConn的Write()方法,这有可能导致未知道的panic。
  3. 当session.OnClose()回调方法中,如果意外调用session.Kick()方法会导致deadlock

因为,几经考虑,我决定基于pitaya的代码改一个简化的版本出来。


0x09 参考文献

  1. A Million WebSockets and Go
  2. https://github.com/eranyanay/1m-go-websockets
  3. https://github.com/smallnest/epoller
  4. https://github.com/smallnest/1m-go-tcp-server/

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyHandler = NewError("EmptyHandler", "handler is empty")
View Source
var ErrInvalidArgument = NewError("InvalidArgument", "argument is not valid")
View Source
var ErrInvalidRoute = NewError("InvalidRoute", "route is not valid")
View Source
var ErrNilSerde = NewError("NilSerde", "serde is nil")

Functions

func SendDefault

func SendDefault(session Session, data any) error

func SendStream

func SendStream(session Session, text string, done bool) error

Types

type Acceptor

type Acceptor interface {
	Listen()
	GetLinkChan() chan intern.Link
}

type App

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

func NewApp

func NewApp(accept Acceptor, opts ...AppOption) *App

func (*App) AddInterceptor

func (my *App) AddInterceptor(interceptor InterceptorFunc)

func (*App) Documentation

func (my *App) Documentation(getPtrNames bool) (map[string]any, error)

Documentation returns handler and remotes documentation

func (*App) Listen

func (my *App) Listen()

Listen 初始化完成后才启动Listen, 才可以接受session连接进来

func (*App) OnHandShaken

func (my *App) OnHandShaken(handler func(session Session))

OnHandShaken 暴露一个OnConnected()事件暂时没有看到很大的意义,因为handshake必须是第一个消息 如果需要接入握手事件的话, 可以自己注册OnHandShaken事件

func (*App) RangeSessions

func (my *App) RangeSessions(handler func(session Session))

RangeSessions 设计这个方法的目的是为了排查如下bug: 2个相同的uid登录了server, playerManager中好像有player丢失了

func (*App) Register

func (my *App) Register(comp component.Component, opts ...component.Option) error

type AppOption

type AppOption func(*appOptions)

func WithHeartbeatInterval

func WithHeartbeatInterval(interval time.Duration) AppOption

func WithKickInterval

func WithKickInterval(interval time.Duration) AppOption

func WithSerdeBuilder

func WithSerdeBuilder(name string, builder serdeBuilder) AppOption

type Attachment

type Attachment interface {
	Set(key any, value any)
	UInt32(key any) uint32
	Int32(key any) int32
	UInt64(key any) uint64
	Int64(key any) int64
	Int(key any) int
	Float32(key any) float32
	Float64(key any) float64
	Bool(key any) bool
	String(key any) string
	Get1(key any) any
	Get2(key any) (any, bool)
}

type AttachmentImpl

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

func (*AttachmentImpl) Bool

func (my *AttachmentImpl) Bool(key any) bool

func (*AttachmentImpl) Float32

func (my *AttachmentImpl) Float32(key any) float32

func (*AttachmentImpl) Float64

func (my *AttachmentImpl) Float64(key any) float64

func (*AttachmentImpl) Get1

func (my *AttachmentImpl) Get1(key any) any

func (*AttachmentImpl) Get2

func (my *AttachmentImpl) Get2(key any) (any, bool)

func (*AttachmentImpl) Int

func (my *AttachmentImpl) Int(key any) int

func (*AttachmentImpl) Int32

func (my *AttachmentImpl) Int32(key any) int32

func (*AttachmentImpl) Int64

func (my *AttachmentImpl) Int64(key any) int64

func (*AttachmentImpl) Range

func (my *AttachmentImpl) Range(f func(key, value any) bool)

func (*AttachmentImpl) Set

func (my *AttachmentImpl) Set(key any, value any)

func (*AttachmentImpl) String

func (my *AttachmentImpl) String(key any) string

func (*AttachmentImpl) UInt32

func (my *AttachmentImpl) UInt32(key any) uint32

func (*AttachmentImpl) UInt64

func (my *AttachmentImpl) UInt64(key any) uint64

type Error

type Error struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

func NewError

func NewError(code string, format string, args ...interface{}) *Error

func (*Error) Error

func (err *Error) Error() string

type InterceptorFunc

type InterceptorFunc func(session Session, route string) error

type Manager

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

func (*Manager) AddHandler

func (my *Manager) AddHandler(route string, handler *component.Handler)

func (*Manager) AddInterceptor

func (my *Manager) AddInterceptor(interceptor InterceptorFunc)

func (*Manager) AddSerdeBuilder

func (my *Manager) AddSerdeBuilder(name string, builder serdeBuilder)

func (*Manager) CloneRouteKinds

func (my *Manager) CloneRouteKinds() map[string]int32

func (*Manager) CreateSerde

func (my *Manager) CreateSerde(name string, session Session) serde.Serde

func (*Manager) GetHandlerByKind

func (my *Manager) GetHandlerByKind(kind int32) *component.Handler

func (*Manager) NewSession

func (my *Manager) NewSession(link intern.Link) Session

func (*Manager) RebuildHandlerKinds

func (my *Manager) RebuildHandlerKinds()

type Session

type Session interface {
	Handshake() error         // server主动向client发送服务器的配置信息
	Kick(reason string) error // server主动踢client
	Send(route string, v any) error
	Echo(handler func()) error

	OnHandShaken(handler func()) // 握手完成后
	OnClosed(handler func())     // 连接关闭后

	Id() int64
	RemoteAddr() net.Addr
	Attachment() Attachment
	Nonce() int32
}

func GetSessionFromCtx

func GetSessionFromCtx(ctx context.Context) Session

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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