nets

package
v0.0.0-...-d853fe5 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlagDataChecksumIEEE uint32 = 0x01
)
View Source
const MaxCloseWaitTime = 10
View Source
const (
	MaxPacketBodyLength = bytespool.MaxBufferCap
)

Variables

View Source
var DefaultOptions = &Options{
	network:                        TCP,
	address:                        "",
	CACertFile:                     "",
	TLSCertFile:                    "",
	TLSKeyFile:                     "",
	CompressType:                   bbq.CompressType_None,
	ContentType:                    bbq.ContentType_Proto,
	CheckFlags:                     0,
	maxSendPacketSize:              0,
	writeBufferSize:                0,
	readBufferSize:                 0,
	connectionTimeout:              5 * time.Second,
	RequestTimeout:                 5 * time.Second,
	MaxCloseWaitTime:               10,
	NetNoDelay:                     true,
	NetKeepAlive:                   true,
	KcpSetStreamMode:               true,
	KcpSetWriteDelay:               true,
	KcpSetAckNoDelay:               true,
	KcpInternalUpdateTimerInterval: 10,
	KcpEnableFastResend:            2,
	KcpDisableCongestionControl:    1,
	KcpMTU:                         1400,
	PacketHandler:                  nil,
	ConnCallback:                   nil,
}

Functions

func HasFlags

func HasFlags(v, flags uint32) bool

Has reports whether f contains all (0 or more) flags in v.

func NewNetService

func NewNetService(opts ...Option) *service

func ReplayError

func ReplayError(pkt *Packet, e error)

Types

type Client

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

func Connect

func Connect(netname NetWorkName, ip, port string, ops ...Option) (*Client, error)

func (*Client) GetConn

func (ct *Client) GetConn() *Conn

func (*Client) SendPacket

func (ct *Client) SendPacket(pkt *Packet) error

type Config

type Config struct {
}

func DefaultConfig

func DefaultConfig() *Config

type Conn

type Conn struct {
	PacketHandler PacketHandler
	ConnCallbacks []ConnCallback
	// contains filtered or unexported fields
}

func (*Conn) Close

func (cn *Conn) Close(closeChan chan struct{}) (e error)

func (*Conn) Name

func (cn *Conn) Name() string

func (*Conn) SendPacket

func (cn *Conn) SendPacket(p *Packet) (err error)

AsyncWritePacket async writes a packet, this method will never block

func (*Conn) Serve

func (cn *Conn) Serve()

type ConnCallback

type ConnCallback interface {
	HandleClose(*Conn)
	HandleEOF(*Conn)
	HandleTimeOut(*Conn)
	HandleFail(*Conn)
}

type KCPListener

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

func NewDefaultKCPListener

func NewDefaultKCPListener(opts *Options) *KCPListener

func (*KCPListener) Accept

func (kl *KCPListener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

func (*KCPListener) Addr

func (kl *KCPListener) Addr() net.Addr

Addr returns the listener's network address.

func (*KCPListener) Close

func (kl *KCPListener) Close() error

Close closes the listener. Any blocked Accept operations will be unblocked and return errors.

func (*KCPListener) Listen

func (kl *KCPListener) Listen(network NetWorkName, address string) (net.Listener, error)

func (*KCPListener) Name

func (kl *KCPListener) Name() NetWorkName

type NetName

type NetName interface {
	// Name returns the name of the Transport implementation.
	// the result cannot change between calls.
	Name() NetWorkName
}

type NetService

type NetService interface {
	ServiceName

	ListenAndServe() error

	Close(chan struct{}) error
}

type NetWorkName

type NetWorkName string
const (
	WebSocket NetWorkName = "websocket"
	TCP       NetWorkName = "tcp"
	TCP6      NetWorkName = "tcp6"
	KCP       NetWorkName = "kcp"
)

type Option

type Option func(*Options)

A Option sets options such as credentials, codec and keepalive parameters, etc.

func WithCheckFlags

func WithCheckFlags(CheckFlags uint32) Option

func WithCompressType

func WithCompressType(CompressType bbq.CompressType) Option

func WithConnCallback

func WithConnCallback(ph ConnCallback) Option

func WithConnectionTimeout

func WithConnectionTimeout(connectionTimeout time.Duration) Option

func WithContentType

func WithContentType(ContentType bbq.ContentType) Option

func WithKcpMTU

func WithKcpMTU(mtu int) Option

func WithKcpNodelay

func WithKcpNodelay(nodelay bool, intval, fr, nc int) Option

func WithKcpSetAckNoDelay

func WithKcpSetAckNoDelay(KcpSetAckNoDelay bool) Option

func WithKcpSetStreamMode

func WithKcpSetStreamMode(KcpSetStreamMode bool) Option

func WithKcpSetWriteDelay

func WithKcpSetWriteDelay(KcpSetWriteDelay bool) Option

func WithMaxSendPacketSize

func WithMaxSendPacketSize(maxSendPacketSize int) Option

func WithNetNoDelay

func WithNetNoDelay(NetNoDelay bool) Option

func WithNetwork

func WithNetwork(network NetWorkName, address string) Option

func WithPacketHandler

func WithPacketHandler(ph PacketHandler) Option

func WithReadBufferSize

func WithReadBufferSize(readBufferSize int) Option

func WithRequestTimeout

func WithRequestTimeout(requestTimeout time.Duration) Option

func WithTcpKeepAlive

func WithTcpKeepAlive(NetKeepAlive bool) Option

func WithTls

func WithTls(CACertFile, TLSCertFile, TLSKeyFile string) Option

func WithWriteBufferSize

func WithWriteBufferSize(writeBufferSize int) Option

type Options

type Options struct {
	CACertFile  string // ca证书
	TLSCertFile string // server证书
	TLSKeyFile  string // server秘钥

	CompressType bbq.CompressType //压缩类型
	ContentType  bbq.ContentType  //协议编码类型
	CheckFlags   uint32

	RequestTimeout   time.Duration
	MaxCloseWaitTime time.Duration

	NetNoDelay bool

	// for tcp
	NetKeepAlive bool

	// for kcp
	KcpSetStreamMode bool
	KcpSetWriteDelay bool

	KcpSetAckNoDelay bool

	// for kcp nodelay
	KcpInternalUpdateTimerInterval int
	KcpEnableFastResend            int
	KcpDisableCongestionControl    int

	// for kcp mtu
	KcpMTU int

	PacketHandler PacketHandler
	ConnCallback  ConnCallback
	// contains filtered or unexported fields
}

type Packet

type Packet struct {
	Src *Conn // not nil indicates this is request packet

	// header: 只能在packet的生命周期内使用
	Header *bbq.Header
	// contains filtered or unexported fields
}

NOTE 如果需要通过chan 或者其他方式给其他协程使用,一定要retain,release NOTE 如果需要通过chan 或者其他方式给其他协程使用,一定要retain,release NOTE 如果需要通过chan 或者其他方式给其他协程使用,一定要retain,release Packet is a packet for sending data

func NewPacket

func NewPacket() *Packet

NewPacket 一定要记得 pkt.Release()

func (*Packet) Data

func (p *Packet) Data() []byte

func (*Packet) GetPacketCap

func (p *Packet) GetPacketCap() uint32

PacketCap returns the current packetBody capacity

func (*Packet) PacketBody

func (p *Packet) PacketBody() []byte

PacketBody returns the total packetBody of packet

func (*Packet) Release

func (p *Packet) Release()

Release releases the packet to packet pool

func (*Packet) Retain

func (p *Packet) Retain()

想持有pkt,需要自行Retain/Release

func (*Packet) Serialize

func (p *Packet) Serialize() []byte

func (*Packet) String

func (p *Packet) String() string

func (*Packet) WriteBody

func (p *Packet) WriteBody(b []byte) error

WriteBytes appends slice of bytes to the end of packetBody

type PacketHandler

type PacketHandler interface {
	// 同步接口,不会异步调用,想要异步处理需要内部保证安全
	HandlePacket(pkt *Packet) error
}

type PacketReadWriter

type PacketReadWriter struct {
	Config Config
	// contains filtered or unexported fields
}

PacketReadWriter is a connection that send and receive data packets upon a network stream connection

func NewPacketReadWriter

func NewPacketReadWriter(conn *Conn) *PacketReadWriter

NewPacketReadWriter creates a packet connection based on network connection

func NewPacketReadWriterWithConfig

func NewPacketReadWriterWithConfig(conn *Conn, cfg *Config) *PacketReadWriter

func (*PacketReadWriter) ReadPacket

func (pc *PacketReadWriter) ReadPacket() (*Packet, error)

recv receives the next packet

type PacketType

type PacketType uint8
const (
	PacketRPC PacketType = 0x0
	PacketSys PacketType = 0x1
)

func (PacketType) String

func (t PacketType) String() string

type ServiceName

type ServiceName interface {
	// Name returns the name of the Transport implementation.
	// the result cannot change between calls.
	Name() string
}

type TCPListener

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

func NewTCPListener

func NewTCPListener(net NetWorkName, opts *Options) *TCPListener

func (*TCPListener) Accept

func (tl *TCPListener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

func (*TCPListener) Addr

func (tl *TCPListener) Addr() net.Addr

Addr returns the listener's network address.

func (*TCPListener) Close

func (tl *TCPListener) Close() error

Close closes the listener. Any blocked Accept operations will be unblocked and return errors.

func (*TCPListener) Listen

func (tl *TCPListener) Listen(network NetWorkName, address string) (net.Listener, error)

func (*TCPListener) Name

func (tl *TCPListener) Name() NetWorkName

type WebSocketService

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

func (*WebSocketService) Close

func (ws *WebSocketService) Close(chan struct{}) error

func (*WebSocketService) ListenAndServe

func (ws *WebSocketService) ListenAndServe(network NetWorkName, address string, opts *Options) error

func (*WebSocketService) Name

func (ws *WebSocketService) Name() NetWorkName

Jump to

Keyboard shortcuts

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