conn

package
v1.3.0 Latest Latest
Warning

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

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

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")

	ErrSendPayload = errors.New("serializer is nil, but payload type not []byte")
	ErrSendMarshal = errors.New("message body marshal err")
	ErrSend        = errors.New("send err")
	ErrSendWSType  = errors.New("websocket message type err")

	ErrPack     = errors.New("pack err")
	ErrUnpack   = errors.New("unPacker err")
	ErrNoPacker = errors.New("no packer")

	ErrReceiveZero = errors.New("receive zero")
)
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.1.4

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

func NewSerialConn added in v1.1.4

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

NewSerialConn 新建 SerialConn

Types

type ConnManager

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

func NewConnManager added in v1.1.4

func NewConnManager() *ConnManager

func (*ConnManager) FindConn added in v1.1.4

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

FindConn 根据连接ID找到连接

func (*ConnManager) FindConnByUID added in v1.1.4

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

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

func (*ConnManager) FindGroup added in v1.1.4

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

FindGroup 查找分组

func (*ConnManager) NewGroup added in v1.1.4

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

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

func (*ConnManager) PeekConn added in v1.1.4

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

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

func (*ConnManager) Remove added in v1.1.4

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

func (*ConnManager) RemoveFromGroup added in v1.1.4

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

func (*ConnManager) Store added in v1.1.4

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

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

type ConnType added in v1.1.4

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

type Connection added in v1.1.4

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

func NewConnection added in v1.1.4

func NewConnection(id int64, rawC net.Conn, sendSize, writeSize int) *Connection

func (*Connection) ChDie added in v1.1.4

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

func (*Connection) ChSend added in v1.1.4

func (r *Connection) ChSend() chan PendingMessage

func (*Connection) ChWrite added in v1.1.4

func (r *Connection) ChWrite() chan []byte

func (*Connection) Close added in v1.1.4

func (r *Connection) Close() error

func (*Connection) Conn added in v1.1.4

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

func (*Connection) ID added in v1.1.4

func (r *Connection) ID() int64

func (*Connection) LastMID added in v1.1.4

func (r *Connection) LastMID() uint64

func (*Connection) Send added in v1.1.4

func (r *Connection) Send(header, payload any) (err error)

func (*Connection) SendBytes added in v1.1.4

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

func (*Connection) SerialConn added in v1.1.4

func (r *Connection) SerialConn() *SerialConn

func (*Connection) Session added in v1.1.4

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

func (*Connection) SetLastMID added in v1.1.4

func (r *Connection) SetLastMID(mid uint64)

func (*Connection) SetStatus added in v1.1.4

func (r *Connection) SetStatus(s int32)

func (*Connection) Status added in v1.1.4

func (r *Connection) Status() int32

func (*Connection) Type added in v1.1.4

func (r *Connection) Type() ConnType

func (*Connection) WsConn added in v1.1.4

func (r *Connection) WsConn() *WSConn

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 PendingMessage added in v1.1.4

type PendingMessage struct {
	Header  any
	Payload any
}

type SerialAddr added in v1.1.4

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

func (*SerialAddr) Network added in v1.1.4

func (r *SerialAddr) Network() string

func (*SerialAddr) String added in v1.1.4

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.1.4

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

SerialConn serial connection wrapper

func (*SerialConn) LocalAddr added in v1.1.4

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

func (*SerialConn) RemoteAddr added in v1.1.4

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

func (*SerialConn) SetDeadline added in v1.1.4

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

func (*SerialConn) SetReadDeadline added in v1.1.4

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

func (*SerialConn) SetWriteDeadline added in v1.1.4

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

type WSConn added in v1.1.4

type WSConn struct {
	*websocket.Conn
}

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

func NewWSConn added in v1.1.4

func NewWSConn(conn *websocket.Conn) *WSConn

NewWSConn 新建wsConn

func (*WSConn) Read added in v1.1.4

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 added in v1.1.4

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 added in v1.1.4

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