network

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTCPOptions = TCPOptions{
	NoDelay: true,
	Linger:  -1,
}

Functions

func Discard

func Discard(client IClient) (err error)

func GetLocalAddrs

func GetLocalAddrs() (result []*net.IPNet)

获取所有局域网IP,除了127.0.0.1,未排序

func GetTCPAddr

func GetTCPAddr(addr net.Addr) (tcpAddr *net.TCPAddr)

将网络地址转为TCP地址

func GetUDPAddr

func GetUDPAddr(addr net.Addr) (udpAddr *net.UDPAddr)

将网络地址转为UDP地址

func NewTCPAddr

func NewTCPAddr(host string, port uint16) *net.TCPAddr

根据IP和端口创建TCP地址

func NewUnixAddr

func NewUnixAddr(filename string) (addr *net.UnixAddr)

将文件名转为Unix socket地址

func RandomGUID

func RandomGUID() string

创建全局唯一的字符串ID

func Reconnect

func Reconnect(client IClient, force bool, retries int) (n int, err error)

按需重连服务端

func SendData

func SendData(client IClient, data []byte) (err error)

func TrySendData

func TrySendData(client IClient, data []byte, retries int) (n int, err error)

Types

type CloseFunc

type CloseFunc func(c *Conn) error

type Conn

type Conn struct {
	Session *Session
	// 下发指令专用,回复请直接调用Write
	Output    chan []byte
	ReadOnly  bool
	IsActive  bool
	LastError error
	// contains filtered or unexported fields
}

func NewTCPConn

func NewTCPConn(conn *net.TCPConn) *Conn

func NewUDPConn

func NewUDPConn(conn *net.UDPConn) *Conn

func NewUnixConn

func NewUnixConn(conn *net.UnixConn) *Conn

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) GetKind

func (c *Conn) GetKind() string

func (*Conn) GetLocalAddr

func (c *Conn) GetLocalAddr() net.Addr

func (*Conn) GetRawConn

func (c *Conn) GetRawConn() IPckConn

返回原始的网络连接 注意:调用过Peek()操作后,原始连接不能再用于读(会丢失前n个字节)

func (*Conn) GetReader

func (c *Conn) GetReader() *bufio.Reader

func (*Conn) GetRemoteAddr

func (c *Conn) GetRemoteAddr() net.Addr

func (*Conn) GetSessId

func (c *Conn) GetSessId() string

func (*Conn) Peek

func (c *Conn) Peek(n int) ([]byte, error)

往前读n个字节,但不移动游标 注意:原始conn的读游标会向前移动,所有读的地方,用GetReader()代替GetConn()

func (*Conn) QuickSend

func (c *Conn) QuickSend(data []byte) error

快速发送数据,但连接断开也不会去重连

type DialPlan

type DialPlan struct {
	Timeout    time.Duration
	LocalAddr  net.Addr
	RemoteAddr net.Addr
}

拨号计划

func NewDialPlan

func NewDialPlan(remote, local net.Addr, timeout int) *DialPlan

func (*DialPlan) Dial

func (dp *DialPlan) Dial(kind string) (net.Conn, error)

拨号得到网络连接

func (*DialPlan) DialTCP

func (dp *DialPlan) DialTCP() (*net.TCPConn, error)

拨号得到TCP连接

func (*DialPlan) DialUDP

func (dp *DialPlan) DialUDP() (*net.UDPConn, error)

拨号得到UDP连接

func (*DialPlan) DialUnix

func (dp *DialPlan) DialUnix() (*net.UnixConn, error)

拨号得到Unix连接

func (*DialPlan) SetPortRemote

func (dp *DialPlan) SetPortRemote(host string, port uint16)

func (*DialPlan) SetUnixRemote

func (dp *DialPlan) SetUnixRemote(filename string)

type EachFunc

type EachFunc func(k string, c *Conn) bool

type Events

type Events struct {
	Tick    func(t time.Time)
	Serving func(s *Server)
	Opened  func(s *Server, c *Conn) error
	Closed  func(s *Server, c *Conn, err error)
	Process ProcessFunc
	Prepare func(c *Conn, input chan<- []byte) error
	Receive func(c *Conn, data []byte, saved bool) string
	Send    func(c *Conn, data []byte)
}

事件集

type IClient

type IClient interface {
	GetPlan() *DialPlan
	GetConn() *Conn
	SetConn(conn *Conn)
	Dialing() (*Conn, error)
	Close() error
}

type IPckConn

type IPckConn interface {
	SetReadBuffer(bytes int) error
	SetWriteBuffer(bytes int) error
	net.Conn
}

type KeepAlive

type KeepAlive struct {
	Idle, Count, Interval int
}

检测TCP连接是否断开 Idle: 没有数据往来(上行、下行都没有)多少秒后,发第一个检测包 Count: 最大检测次数 Interval: 相邻两个检测之间的时间间隔,单位:秒

func (KeepAlive) ApplyTo

func (ka KeepAlive) ApplyTo(c *net.TCPConn) (err error)

为TCP连接设置保活时间

type LocalAddrRing

type LocalAddrRing struct {
	LocalAddrs []*net.IPNet
	TCPAddrs   []*net.TCPAddr
	// contains filtered or unexported fields
}

局域网IP的循环列表

func NewLocalAddrRing

func NewLocalAddrRing() *LocalAddrRing

func (*LocalAddrRing) GetTCPAddrs

func (r *LocalAddrRing) GetTCPAddrs() []*net.TCPAddr

func (*LocalAddrRing) NextAddr

func (r *LocalAddrRing) NextAddr() (addr *net.IPNet)

func (*LocalAddrRing) NextTCPAddr

func (r *LocalAddrRing) NextTCPAddr() (addr *net.TCPAddr)

func (*LocalAddrRing) Translate

func (r *LocalAddrRing) Translate(i, stop int)

type MapData

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

会话数据,只使用string作为key

func (*MapData) Get

func (d *MapData) Get(key string) (interface{}, bool)

func (*MapData) Init

func (d *MapData) Init() error

func (*MapData) Put

func (d *MapData) Put(key string, value interface{})

func (*MapData) Reset

func (d *MapData) Reset() error

type Options

type Options struct {
	ReadBuffer  int
	WriteBuffer int
	Deadline    int
}

func (Options) ApplyConn

func (opts Options) ApplyConn(c IPckConn) (err error)

设置UDP连接参数

type ProcessFunc

type ProcessFunc func(s *Server, c *Conn)

type Registry

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

网络连接集合

func (*Registry) Cleanup

func (r *Registry) Cleanup(closer CloseFunc)

删除所有网络连接

func (*Registry) CloseConn

func (r *Registry) CloseConn(c *Conn, key string) (err error)

关闭网络连接,先执行Closed事件

func (*Registry) Each

func (r *Registry) Each(op EachFunc)

遍历所有item

func (*Registry) IsOverride

func (r *Registry) IsOverride(c *Conn, key string) bool

同一个设备,旧连接将被新连接覆盖(会话ID不一样)

func (*Registry) LoadConn

func (r *Registry) LoadConn(key string) *Conn

获取网络连接,key一般是设备(唯一)ID

func (*Registry) SaveConn

func (r *Registry) SaveConn(c *Conn, key string) bool

保存网络连接,key一般是设备(唯一)ID

type SafeMapData

type SafeMapData struct {
	sync.Map
}

跨进程安全会话数据

func (*SafeMapData) Get

func (d *SafeMapData) Get(key string) (interface{}, bool)

func (*SafeMapData) Init

func (d *SafeMapData) Init() error

func (*SafeMapData) Put

func (d *SafeMapData) Put(key string, value interface{})

func (*SafeMapData) Reset

func (d *SafeMapData) Reset() error

type Server

type Server struct {
	Address net.Addr
	Ticker  <-chan time.Time
	*Registry
}

服务器

func NewAddrServer

func NewAddrServer(addr net.Addr) *Server

创建服务器

func NewPortServer

func NewPortServer(host string, port uint16) *Server

创建TCP/UDP服务器

func NewUnixServer

func NewUnixServer(filename string) *Server

创建unix socket服务器

func (*Server) Execute

func (s *Server) Execute(events Events, c *Conn)

func (*Server) Finish

func (s *Server) Finish(events Events, c *Conn) error

关闭客户端

func (*Server) Process

func (s *Server) Process(events Events, c *Conn)

处理单个连接

func (*Server) SendTo

func (s *Server) SendTo(key string, data []byte) bool

根据设备id下发数据

func (*Server) SetTickInterval

func (s *Server) SetTickInterval(secs int)

func (*Server) Trigger

func (s *Server) Trigger(events Events)

执行Tick事件

type Session

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

会话,拥有唯一的sid

func NewSession

func NewSession(safe bool) *Session

func (*Session) Clear

func (s *Session) Clear()

func (*Session) GetData

func (s *Session) GetData(key string) (interface{}, bool)

读取数据,可能不存在

func (*Session) GetId

func (s *Session) GetId() string

func (*Session) GetString

func (s *Session) GetString(key string) string

读取字符串数据

func (*Session) PutData

func (s *Session) PutData(key string, value interface{})

写入数据

type SessionData

type SessionData interface {
	Get(key string) (interface{}, bool)
	Put(key string, value interface{})
	Init() error
	Reset() error
}

type TCPOptions

type TCPOptions struct {
	NoDelay bool // default true
	Linger  int  // default -1
	KeepAlive
	Options
}

func (TCPOptions) ApplyTCP

func (opts TCPOptions) ApplyTCP(c *net.TCPConn) (err error)

设置TCP连接参数

Jump to

Keyboard shortcuts

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