connection

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StatusStart 开始阶段
	StatusStart int32 = iota + 1
	// StatusPrepare 准备阶段
	StatusPrepare
	// StatusPending 等待工作阶段
	StatusPending
	// StatusWorking 工作阶段
	StatusWorking
	// StatusClosed 连接关闭
	StatusClosed
)
View Source
const DefaultGroupName = "DEFAULT_GROUP"
View Source
const SerialNetwork = "serial"

Variables

View Source
var (
	ErrCloseClosedSession = errors.New("close closed session")
	// ErrBrokenPipe represents the low-level connection has broken.
	ErrBrokenPipe = errors.New("broken low-level pipe")
)
View Source
var (
	ErrCloseClosedGroup        = errors.New("close closed group")
	ErrClosedGroup             = errors.New("group closed")
	DeleteDefaultGroupNotAllow = errors.New("delete default group not allow")
)

Functions

func NewSerialAddr added in v1.0.0

func NewSerialAddr(addr string, rate int, dataBits int, stopBits int, parity string) net.Addr

func NewSerialConn added in v1.0.0

func NewSerialConn(port serial.Port, cfg *serial.Config) net.Conn

NewSerialConn 新建 SerialConn

Types

type Channel

type Channel interface {
	PushFront(h Func)
	PushBack(h Func)
	Process(c *Connection, v interface{}) error
}

type Config

type Config struct {
	LogDebug  bool
	LogPrefix string
}

type ConnType

type ConnType int
const (
	ConnTypeTCP ConnType = iota // TCP connection
	ConnTypeWS                  // Websocket connection
)

type Connection

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

func NewConnection

func NewConnection(
	id int64,
	conn net.Conn,
	pool *ants.Pool,
	conf Config,
	packerBuilder packet.PackerBuilder,
	serializer ndef.Serializer,
	pipeline Pipeline,
	handleFn packetFn) *Connection

func (*Connection) Close

func (r *Connection) Close() error

func (*Connection) Conn

func (r *Connection) Conn() (net.Conn, ConnType)

func (*Connection) DieChan

func (r *Connection) DieChan() chan struct{}

func (*Connection) ID

func (r *Connection) ID() int64

func (*Connection) LastMID

func (r *Connection) LastMID() uint64

func (*Connection) Send

func (r *Connection) Send(header, payload interface{}) (err error)

func (*Connection) SendBytes

func (r *Connection) SendBytes(data []byte) (err error)

func (*Connection) Serve

func (r *Connection) Serve()

func (*Connection) Session

func (r *Connection) Session() *session.Session

func (*Connection) SetLastMID

func (r *Connection) SetLastMID(mid uint64)

func (*Connection) SetStatus

func (r *Connection) SetStatus(s int32)

func (*Connection) Status

func (r *Connection) Status() int32

type Func

type Func func(c *Connection, v interface{}) error

type Group

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

func NewGroup

func NewGroup(name string) *Group

func (*Group) Add

func (g *Group) Add(c *Connection) error

Add session to group

func (*Group) Close

func (g *Group) Close() error

Close destroy group, which will release all resource in the group

func (*Group) Contains

func (g *Group) Contains(uid string) bool

Contains check whether a UID is contained in current group or not

func (*Group) Count

func (g *Group) Count() int

Count get current member amount in the group

func (*Group) Leave

func (g *Group) Leave(c *Connection) error

Leave remove specified UID related session from group

func (*Group) LeaveAll

func (g *Group) LeaveAll() error

LeaveAll clear all sessions in the group

func (*Group) LeaveByUID

func (g *Group) LeaveByUID(uid string) error

func (*Group) Member

func (g *Group) Member(uid string) (*Connection, bool)

Member returns connection by specified uid

func (*Group) MemberBySID

func (g *Group) MemberBySID(id int64) (*Connection, bool)

MemberBySID returns specified sId's connection

func (*Group) Members

func (g *Group) Members() []*Connection

func (*Group) PeekMembers

func (g *Group) PeekMembers(fn func(sId int64, c *Connection) bool)

PeekMembers returns all members in current group fn 返回true跳过循环,反之一直循环

type Manager

type Manager struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewManager

func NewManager() *Manager

func (*Manager) FindConn

func (m *Manager) FindConn(id int64) (*Connection, bool)

FindConn 根据连接ID找到连接

func (*Manager) FindConnByUID

func (m *Manager) FindConnByUID(uid string) (*Connection, bool)

FindConnByUID 根据连接绑定的UID找到连接

func (*Manager) FindGroup

func (m *Manager) FindGroup(name string) (*Group, bool)

FindGroup 查找分组

func (*Manager) NewGroup

func (m *Manager) NewGroup(name string) *Group

NewGroup 新增分组,若分组已存在,则返回现有分组

func (*Manager) PeekConn

func (m *Manager) PeekConn(fn func(id int64, c *Connection) bool)

PeekConn 循环所有连接 fn 返回true跳过循环,反之一直循环

func (*Manager) Remove

func (m *Manager) Remove(c *Connection) error

func (*Manager) RemoveFromGroup

func (m *Manager) RemoveFromGroup(groupName string, c *Connection) error

func (*Manager) Store

func (m *Manager) Store(groupName string, c *Connection) error

Store 保存连接,同时加入到指定分组,若给定分组名为空,则不进行分组操作

type PendingMessage

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

type Pipeline

type Pipeline interface {
	Outbound() Channel
	Inbound() Channel
}

Pipeline 消息管道

func NewPipeline

func NewPipeline() Pipeline

type SerialAddr added in v1.0.0

type SerialAddr struct {
	Address  string
	BaudRate int
	DataBits int
	StopBits int
	Parity   string
}

func (*SerialAddr) Network added in v1.0.0

func (r *SerialAddr) Network() string

func (*SerialAddr) String added in v1.0.0

func (r *SerialAddr) String() string

String form of address (for example, "/dev/ttyS0|19200|8|1|N", "com1|19200|8|1|N")

type SerialConn added in v1.0.0

type SerialConn struct {
	serial.Port
	// contains filtered or unexported fields
}

SerialConn serial connection wrapper

func (*SerialConn) LocalAddr added in v1.0.0

func (c *SerialConn) LocalAddr() net.Addr

func (*SerialConn) RemoteAddr added in v1.0.0

func (c *SerialConn) RemoteAddr() net.Addr

func (*SerialConn) SetDeadline added in v1.0.0

func (c *SerialConn) SetDeadline(_ time.Time) error

func (*SerialConn) SetReadDeadline added in v1.0.0

func (c *SerialConn) SetReadDeadline(_ time.Time) error

func (*SerialConn) SetWriteDeadline added in v1.0.0

func (c *SerialConn) SetWriteDeadline(_ time.Time) error

type WSConn

type WSConn struct {
	*websocket.Conn
}

WSConn 封装 websocket.Conn 并实现所有 net.Conn 接口 兼容所有使用 net.Conn 的方法

func NewWSConn

func NewWSConn(conn *websocket.Conn) *WSConn

NewWSConn 新建wsConn

func (*WSConn) Read

func (c *WSConn) Read(b []byte) (int, error)

Read reads data from the connection. Read can be made to time out and return an Error with Timeout() == true after a fixed time limit; see SetDeadline and SetReadDeadline.

func (*WSConn) SetDeadline

func (c *WSConn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.

A deadline is an absolute time after which I/O operations fail with a timeout (see type Error) instead of blocking. The deadline applies to all future and pending I/O, not just the immediately following call to Read or Write. After a deadline has been exceeded, the connection can be refreshed by setting a deadline in the future.

An idle timeout can be implemented by repeatedly extending the deadline after successful Read or Write calls.

A zero value for t means I/O operations will not time out.

func (*WSConn) Write

func (c *WSConn) Write(b []byte) (int, error)

Write writes data to the connection. Write can be made to time out and return an Error with Timeout() == true after a fixed time limit; see SetDeadline and SetWriteDeadline.

Jump to

Keyboard shortcuts

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