network

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultNoDelay           = 1
	DefaultIntervalMill      = 10
	DefaultResend            = 2
	DefaultCongestionControl = 1

	DefaultMtu          = 1400
	DefaultSndWndSize   = 4096
	DefaultRcvWndSize   = 4096
	DefaultStreamMode   = true
	DefaultDSCP         = 46
	DefaultDataShards   = 10
	DefaultParityShards = 0

	DefaultReadDeadlineMill  = 15 * time.Second
	DefaultWriteDeadlineMill = 15 * time.Second

	DefaultMaxConnNum = 20000
)
View Source
const (
	Default_ReadDeadline    = time.Second * 30 //默认读超时30s
	Default_WriteDeadline   = time.Second * 30 //默认写超时30s
	Default_MaxConnNum      = 1000000          //默认最大连接数
	Default_PendingWriteNum = 100000           //单连接写消息Channel容量
	Default_MinMsgLen       = 2                //最小消息长度2byte
	Default_LenMsgLen       = 2                //包头字段长度占用2byte
	Default_MaxMsgLen       = 65535            //最大消息长度
)

Variables

View Source
var DefaultMaxHeaderBytes = 1 << 20

Functions

This section is empty.

Types

type Agent

type Agent interface {
	Run()
	OnClose()
}

type CAFile

type CAFile struct {
	CertFile string
	Keyfile  string
}

type Conn

type Conn interface {
	ReadMsg() ([]byte, error)
	WriteMsg(args ...[]byte) error
	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	Close()
	Destroy()
	ReleaseReadMsg(byteBuff []byte)
}

type ConnSet

type ConnSet map[net.Conn]struct{}

type HttpServer

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

func (*HttpServer) Init

func (slf *HttpServer) Init(listenAddr string, handler http.Handler, readTimeout time.Duration, writeTimeout time.Duration)

func (*HttpServer) SetCAFile

func (slf *HttpServer) SetCAFile(caFile []CAFile)

func (*HttpServer) Start

func (slf *HttpServer) Start()

type KCPClient

type KCPClient struct {
	sync.Mutex
	Addr            string
	ConnNum         int
	ConnectInterval time.Duration
	PendingWriteNum int
	ReadDeadline    time.Duration
	WriteDeadline   time.Duration
	AutoReconnect   bool
	NewAgent        func(conn *NetConn) Agent

	// msg parser
	MsgParser
	// contains filtered or unexported fields
}

func (*KCPClient) Close

func (client *KCPClient) Close(waitDone bool)

func (*KCPClient) GetCloseFlag

func (client *KCPClient) GetCloseFlag() bool

func (*KCPClient) Start

func (client *KCPClient) Start()

type KCPServer

type KCPServer struct {
	NewAgent func(Conn) Agent
	// contains filtered or unexported fields
}

func (*KCPServer) Close

func (kp *KCPServer) Close()

func (*KCPServer) Init

func (kp *KCPServer) Init(kcpCfg *KcpCfg)

func (*KCPServer) Start

func (kp *KCPServer) Start() error

type KcpCfg

type KcpCfg struct {
	ListenAddr string // 监听地址
	MaxConnNum int    //最大连接数
	NoDelay    *NoDelayCfg

	Mtu               *int           // mtu大小
	SndWndSize        *int           // 发送窗口大小,默认1024
	RcvWndSize        *int           // 接收窗口大小,默认1024
	ReadDeadlineMill  *time.Duration // 读超时毫秒
	WriteDeadlineMill *time.Duration // 写超时毫秒
	StreamMode        *bool          // 是否打开流模式,默认true
	DSCP              *int           // 差分服务代码点,默认46
	ReadBuffSize      *int           // 读Buff大小,默认
	WriteBuffSize     *int           // 写Buff大小

	// 用于 FEC(前向纠错)的数据分片和校验分片数量,,默认10,0
	DataShards   *int
	ParityShards *int

	LittleEndian    bool   //是否小端序
	LenMsgLen       int    //消息头占用byte数量,只能是1byte,2byte,4byte。如果是4byte,意味着消息最大可以是math.MaxUint32(4GB)
	MinMsgLen       uint32 //最小消息长度
	MaxMsgLen       uint32 //最大消息长度,超过判定不合法,断开连接
	PendingWriteNum int    //写channel最大消息数量
}

type MsgParser

type MsgParser struct {
	LenMsgLen    int
	MinMsgLen    uint32
	MaxMsgLen    uint32
	LittleEndian bool

	bytespool.IBytesMemPool
}

MsgParser -------------- | len | data | --------------

func (*MsgParser) GetRecyclerReaderBytes

func (p *MsgParser) GetRecyclerReaderBytes() func(data []byte)

func (*MsgParser) Init

func (p *MsgParser) Init()

func (*MsgParser) Read

func (p *MsgParser) Read(r io.Reader) ([]byte, error)

goroutine safe

func (*MsgParser) Write

func (p *MsgParser) Write(conn io.Writer, args ...[]byte) error

goroutine safe

type NetConn

type NetConn struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*NetConn) Close

func (netConn *NetConn) Close()

func (*NetConn) Destroy

func (netConn *NetConn) Destroy()

func (*NetConn) GetRecyclerReaderBytes

func (netConn *NetConn) GetRecyclerReaderBytes() func(data []byte)

func (*NetConn) GetRemoteIp

func (netConn *NetConn) GetRemoteIp() string

func (*NetConn) IsConnected

func (netConn *NetConn) IsConnected() bool

func (*NetConn) LocalAddr

func (netConn *NetConn) LocalAddr() net.Addr

func (*NetConn) Read

func (netConn *NetConn) Read(b []byte) (int, error)

func (*NetConn) ReadMsg

func (netConn *NetConn) ReadMsg() ([]byte, error)

func (*NetConn) ReleaseReadMsg

func (netConn *NetConn) ReleaseReadMsg(byteBuff []byte)

func (*NetConn) RemoteAddr

func (netConn *NetConn) RemoteAddr() net.Addr

func (*NetConn) SetReadDeadline

func (netConn *NetConn) SetReadDeadline(d time.Duration)

func (*NetConn) SetWriteDeadline

func (netConn *NetConn) SetWriteDeadline(d time.Duration)

func (*NetConn) Write

func (netConn *NetConn) Write(b []byte) error

b must not be modified by the others goroutines

func (*NetConn) WriteMsg

func (netConn *NetConn) WriteMsg(args ...[]byte) error

func (*NetConn) WriteRawMsg

func (netConn *NetConn) WriteRawMsg(args []byte) error

type NoDelayCfg

type NoDelayCfg struct {
	NoDelay           int // 是否启用 nodelay模式,0不启用;1启用
	IntervalMill      int // 协议内部工作的 interval,单位毫秒,比如 10ms或者 20ms
	Resend            int // 快速重传模式,默认0关闭,可以设置2(2次ACK跨越将会直接重传)
	CongestionControl int // 是否关闭流控,默认是0代表不关闭,1代表关闭
}
NoDelayCfg

普通模式: ikcp_nodelay(kcp, 0, 40, 0, 0); 极速模式: ikcp_nodelay(kcp, 1, 10, 2, 1);

type TCPClient

type TCPClient struct {
	sync.Mutex
	Addr            string
	ConnNum         int
	ConnectInterval time.Duration
	PendingWriteNum int
	ReadDeadline    time.Duration
	WriteDeadline   time.Duration
	AutoReconnect   bool
	NewAgent        func(conn *NetConn) Agent

	// msg parser
	MsgParser
	// contains filtered or unexported fields
}

func (*TCPClient) Close

func (client *TCPClient) Close(waitDone bool)

func (*TCPClient) GetCloseFlag

func (client *TCPClient) GetCloseFlag() bool

func (*TCPClient) Start

func (client *TCPClient) Start()

type TCPServer

type TCPServer struct {
	Addr            string
	MaxConnNum      int
	PendingWriteNum int
	ReadDeadline    time.Duration
	WriteDeadline   time.Duration

	NewAgent func(conn Conn) Agent

	MsgParser
	// contains filtered or unexported fields
}

func (*TCPServer) Close

func (server *TCPServer) Close()

func (*TCPServer) GetNetMemPool

func (server *TCPServer) GetNetMemPool() bytespool.IBytesMemPool

func (*TCPServer) SetNetMemPool

func (server *TCPServer) SetNetMemPool(memPool bytespool.IBytesMemPool)

func (*TCPServer) Start

func (server *TCPServer) Start() error

type WSClient

type WSClient struct {
	sync.Mutex
	Addr             string
	ConnNum          int
	ConnectInterval  time.Duration
	PendingWriteNum  int
	MaxMsgLen        uint32
	MessageType      int
	HandshakeTimeout time.Duration
	AutoReconnect    bool
	NewAgent         func(*WSConn) Agent
	// contains filtered or unexported fields
}

func (*WSClient) Close

func (client *WSClient) Close()

func (*WSClient) Start

func (client *WSClient) Start()

type WSConn

type WSConn struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*WSConn) Close

func (wsConn *WSConn) Close()

func (*WSConn) Destroy

func (wsConn *WSConn) Destroy()

func (*WSConn) GetHeader

func (wsConn *WSConn) GetHeader() http.Header

func (*WSConn) LocalAddr

func (wsConn *WSConn) LocalAddr() net.Addr

func (*WSConn) ReadMsg

func (wsConn *WSConn) ReadMsg() ([]byte, error)

ReadMsg goroutine not safe

func (*WSConn) RemoteAddr

func (wsConn *WSConn) RemoteAddr() net.Addr

func (*WSConn) WriteMsg

func (wsConn *WSConn) WriteMsg(args ...[]byte) error

WriteMsg args must not be modified by the others goroutines

type WSHandler

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

func (*WSHandler) ServeHTTP

func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*WSHandler) SetMessageType

func (handler *WSHandler) SetMessageType(messageType int)

type WSServer

type WSServer struct {
	Addr            string
	MaxConnNum      int
	PendingWriteNum int
	MaxMsgLen       uint32
	HTTPTimeout     time.Duration
	CertFile        string
	KeyFile         string
	NewAgent        func(*WSConn) Agent
	// contains filtered or unexported fields
}

func (*WSServer) Close

func (server *WSServer) Close()

func (*WSServer) SetMessageType

func (server *WSServer) SetMessageType(messageType int)

func (*WSServer) Start

func (server *WSServer) Start() error

type WebsocketConnSet

type WebsocketConnSet map[*websocket.Conn]struct{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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