session

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: Apache-2.0 Imports: 11 Imported by: 5

Documentation

Index

Constants

View Source
const (
	InvalidSessionId = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseConn

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

func (*BaseConn) Hash

func (conn *BaseConn) Hash() uint64

func (*BaseConn) Init

func (conn *BaseConn) Init(pConn net.Conn, fromClient bool) error

func (*BaseConn) IsClosed

func (conn *BaseConn) IsClosed() (bool, error)

func (*BaseConn) ToClosed

func (conn *BaseConn) ToClosed(reason error) bool

type BaseContext

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

func (*BaseContext) Deadline

func (ctx *BaseContext) Deadline() int64

func (*BaseContext) Id

func (ctx *BaseContext) Id() uint64

func (*BaseContext) Init

func (ctx *BaseContext) Init(id uint64)

func (*BaseContext) SetDeadline

func (ctx *BaseContext) SetDeadline(deadline int64)

type BaseSession

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

func (*BaseSession) Context

func (sess *BaseSession) Context() Context

func (*BaseSession) Id

func (sess *BaseSession) Id() uint64

func (*BaseSession) Register

func (sess *BaseSession) Register(context Context) error

type Conn

type Conn interface {
	net.Conn
	gnet.Reader
	Writer

	// Hash
	//	@Description: get conn hash code
	//	@return uint64
	//
	Hash() uint64

	// Context returns a user-defined context, it's not goroutine-safe,
	// you must invoke it within any method in EventHandler.
	Context() (ctx any)

	// SetContext sets a user-defined context, it's not goroutine-safe,
	// you must invoke it within any method in EventHandler.
	SetContext(ctx any)

	// IsClosed
	//	@Description: get connection status and closed reason
	//	@return bool closed or not
	//  @return error closed reason
	//
	IsClosed() (bool, error)
}

Conn

@Description: connection

type Context

type Context interface {
	Id() uint64      // 编号
	Deadline() int64 // 存活时间
}

Context

@Description: 会话上下文

type EmptyListener

type EmptyListener struct {
}

EmptyListener 空处理的会话监听器

func (*EmptyListener) OnClosed

func (listener *EmptyListener) OnClosed(session Session)

func (*EmptyListener) OnOpened

func (listener *EmptyListener) OnOpened(session Session)

func (*EmptyListener) OnReceive

func (listener *EmptyListener) OnReceive(session Session, msg any, msgLen int) (err error)

func (*EmptyListener) OnReceiveMulti

func (listener *EmptyListener) OnReceiveMulti(session Session, msg []any, totalLen int) (err error)

func (*EmptyListener) OnSend

func (listener *EmptyListener) OnSend(session Session, msg any, msgLen int) (err error)

func (*EmptyListener) OnSendMulti

func (listener *EmptyListener) OnSendMulti(session Session, msg []any, totalLen int) (err error)

type Listener

type Listener interface {

	// OnOpened
	//	@Description: 当会话开启
	//	@param session
	//
	OnOpened(session Session)

	// OnClosed
	//	@Description: 当会话关闭
	//	@param session
	//
	OnClosed(session Session)

	// OnReceive
	//	@Description: 成功接收消息
	//	@param session
	//	@param msg 接收的消息体
	//	@param msgLen 消息长度
	//	@return err
	//
	OnReceive(session Session, msg any, msgLen int) (err error)

	// OnReceiveMulti
	//	@Description: 成功接收消息
	//	@param session
	//	@param msg 接收的消息体
	//	@param totalLen 消息长度
	//	@return err
	//
	OnReceiveMulti(session Session, msg []any, totalLen int) (err error)

	// OnSend
	//	@Description: 成功发送消息
	//	@param session
	//	@param msg 要发送的消息体
	//	@param msgLen 消息长度
	//	@return err
	//
	OnSend(session Session, msg any, msgLen int) (err error)

	// OnSendMulti
	//	@Description: 成功发送消息
	//	@param session
	//	@param msg 要发送的消息体
	//	@param totalLen 消息长度
	//	@return out 经过处理传出的消息体
	//	@return err
	//
	OnSendMulti(session Session, msg []any, totalLen int) (err error)
}

Listener

@Description: 会话监听器

type Manager

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

func NewManager

func NewManager(name string, unregisterSessionLife int64) (*Manager, error)

func (*Manager) AddSession

func (manager *Manager) AddSession(svrSess Session) error

AddSession

@Description: 添加未注册的会话
@receiver manager
@param Session 会话
@return error

func (*Manager) CheckSessions

func (manager *Manager) CheckSessions()

CheckSessions

@Description: 检查会话的有效性
@receiver manager

func (*Manager) GetSession

func (manager *Manager) GetSession(sessionId uint64) Session

GetSession

@Description: 获取会话
@receiver manager
@param sessionId
@return Session

func (*Manager) RegisterSession

func (manager *Manager) RegisterSession(svrSess Session, context Context) error

RegisterSession

@Description: 注册会话
@receiver manager
@param Session 会话
@return error

func (*Manager) RemoveSession

func (manager *Manager) RemoveSession(svrSess Session)

RemoveSession

@Description: 移除会话
@receiver manager
@param svrSess

type Session

type Session interface {

	// Id
	//	@Description: 会话编号,仅关联后的会话有合法的id值,否则都返回 InvalidSessionId
	//	@return ID 非nil值
	//
	Id() uint64

	// Context
	//	@Description: 返回当前关联的业务对象
	//	@return Context
	//
	Context() Context

	// Register
	//  @Description: 注册会话
	//  @param context
	//	@return error
	//
	Register(context Context) error

	// Connection
	//	@Description: 连接
	//	@return Conn
	//
	Connection() Conn

	// Close
	//  @Description: 关闭会话
	//  @return error
	//
	Close() error

	// IsClosed
	//  @Description: 是否已关闭
	//  @return bool
	//
	IsClosed() bool

	// SendMessage
	//	@Description: 发送消息
	//	@param message
	//
	SendMessage(message any)

	// SendMessages
	//	@Description: 发送消息
	//	@param messages
	//
	SendMessages(messages ...any)
	// contains filtered or unexported methods
}

Session

@Description: 连接会话

type Writer

type Writer interface {
	io.Writer     // not goroutine-safe
	io.ReaderFrom // not goroutine-safe

	// Writev writes multiple byte slices to peer synchronously, it's not goroutine-safe,
	// you must invoke it within any method in EventHandler.
	Writev(bs [][]byte) (n int, err error)

	// Flush writes any buffered data to the underlying connection, it's not goroutine-safe,
	// you must invoke it within any method in EventHandler.
	Flush() (err error)

	// OutboundBuffered returns the number of bytes that can be read from the current buffer.
	// it's not goroutine-safe, you must invoke it within any method in EventHandler.
	OutboundBuffered() (n int)

	// AsyncWrite writes bytes to peer asynchronously, it's goroutine-safe,
	// you don't have to invoke it within any method in EventHandler,
	// usually you would call it in an individual goroutine.
	//
	// Note that it will go synchronously with UDP, so it is needless to call
	// this asynchronous method, we may disable this method for UDP and just
	// return ErrUnsupportedOp in the future, therefore, please don't rely on
	// this method to do something important under UDP, if you're working with UDP,
	// just call Conn.Write to send back your data.
	AsyncWrite(buf []byte, callback func(c Conn, err error) error) (err error)

	// AsyncWritev writes multiple byte slices to peer asynchronously,
	// you don't have to invoke it within any method in EventHandler,
	// usually you would call it in an individual goroutine.
	AsyncWritev(bs [][]byte, callback func(c Conn, err error) error) (err error)
}

Jump to

Keyboard shortcuts

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