gnet

package
v0.0.0-...-8506bae Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 25 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosedListener = errors.New("listener is closed")
)
View Source
var (
	ErrFirstReadTimeout = fmt.Errorf("ErrFirstReadTimeout")
)

Functions

func IsPrivateIP

func IsPrivateIP(ip string) bool

func LocalOutgoingIP

func LocalOutgoingIP() (string, error)

func RandomListen

func RandomListen(ip ...string) (l net.Listener, port string, err error)

func RandomPort

func RandomPort(ip ...string) (port string, err error)

func Read

func Read(c net.Conn, bufSize int) (d string, err error)

func ReadBytes

func ReadBytes(c net.Conn, bufSize int) (d []byte, err error)

func Write

func Write(addr, data string) (err error)

func WriteBytes

func WriteBytes(addr string, data []byte) (err error)

func WriteTo

func WriteTo(writer io.Writer, data string) (n int, err error)

Types

type AESCodec

type AESCodec struct {
	net.Conn
	// contains filtered or unexported fields
}

func NewAESCodec

func NewAESCodec(password string) *AESCodec

func NewAESCodecFromOptions

func NewAESCodecFromOptions(c *AESOptions) *AESCodec

func (*AESCodec) Initialize

func (s *AESCodec) Initialize(ctx Context) (err error)

func (*AESCodec) Read

func (s *AESCodec) Read(p []byte) (n int, err error)

func (*AESCodec) SetConn

func (s *AESCodec) SetConn(c net.Conn) Codec

func (*AESCodec) Write

func (s *AESCodec) Write(p []byte) (n int, err error)

type AESOptions

type AESOptions struct {
	// Password required.
	Password string
	// Salt optionally. A good random salt at least 8 bytes is recommended.
	Salt []byte
	// Type optionally. It must be: aes-128 or aes-192 or aes-256, if empty default is aes-256.
	Type string
	// HashFunc optionally, default is sha256.New
	HashFunc func() hash.Hash
}

type AcceptHandler

type AcceptHandler func(ctx Context, c net.Conn)

type Addr

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

func NewAddr

func NewAddr(addr net.Addr) *Addr

func NewTCPAddr

func NewTCPAddr(address string) *Addr

func (*Addr) Err

func (s *Addr) Err() error

func (*Addr) Host

func (s *Addr) Host() string

func (*Addr) Network

func (s *Addr) Network() string

func (*Addr) Port

func (s *Addr) Port() string

func (*Addr) PortAddr

func (s *Addr) PortAddr(withIP ...string) string

PortAddr returns address string with 'withIP:Addr.Port()', 'withIP' can be a valid ip or domain or empty.

func (*Addr) PortLocalAddr

func (s *Addr) PortLocalAddr() string

PortLocalAddr returns address string with '127.0.0.1:Addr.Port()',

func (*Addr) String

func (s *Addr) String() string

type BufferedConn

type BufferedConn interface {
	net.Conn
	Peek(n int) ([]byte, error)
	ReadByte() (byte, error)
	UnreadByte() error
	Buffered() int
	PeekMax(n int) (d []byte, err error)
}

func NewBufferedConn

func NewBufferedConn(c net.Conn) BufferedConn

func NewBufferedConnSize

func NewBufferedConnSize(c net.Conn, n int) BufferedConn

type CloseHandler

type CloseHandler func(ctx Context)

type Codec

type Codec interface {
	net.Conn
	SetConn(conn net.Conn) Codec
	Initialize(ctx Context) error
}

type CodecFactory

type CodecFactory func(ctx Context) Codec

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

func Dial

func Dial(addr string, timeout time.Duration) (c *Conn, err error)

func NewConn

func NewConn(conn net.Conn, force ...bool) *Conn

func NewContextConn

func NewContextConn(ctx Context, conn net.Conn, force ...bool) *Conn

func (*Conn) AddCodec

func (s *Conn) AddCodec(codec Codec) *Conn

func (*Conn) AddFilter

func (s *Conn) AddFilter(f ConnFilter)

func (*Conn) Close

func (s *Conn) Close() (err error)

func (*Conn) Ctx

func (s *Conn) Ctx() Context

func (*Conn) RawConn

func (s *Conn) RawConn() net.Conn

func (*Conn) Read

func (s *Conn) Read(b []byte) (n int, err error)

func (*Conn) ReadBytes

func (s *Conn) ReadBytes() int64

func (*Conn) ReadTimeout

func (s *Conn) ReadTimeout() time.Duration

func (*Conn) SetAutoCloseOnReadWriteError

func (s *Conn) SetAutoCloseOnReadWriteError(b bool)

func (*Conn) SetReadTimeout

func (s *Conn) SetReadTimeout(readTimeout time.Duration) *Conn

func (*Conn) SetTimeout

func (s *Conn) SetTimeout(readWriteTimeout time.Duration) *Conn

func (*Conn) SetWriteTimeout

func (s *Conn) SetWriteTimeout(writeTimeout time.Duration) *Conn

func (*Conn) Write

func (s *Conn) Write(b []byte) (n int, err error)

func (*Conn) WriteBytes

func (s *Conn) WriteBytes() int64

func (*Conn) WriteTimeout

func (s *Conn) WriteTimeout() time.Duration

type ConnBinder

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

func NewConnBinder

func NewConnBinder(src net.Conn, dst net.Conn) *ConnBinder

func NewContextConnBinder

func NewContextConnBinder(ctx Context, src net.Conn, dst net.Conn) *ConnBinder

func (*ConnBinder) Ctx

func (s *ConnBinder) Ctx() Context

func (*ConnBinder) OnClose

func (s *ConnBinder) OnClose(onClose func()) *ConnBinder

func (*ConnBinder) OnDstClose

func (s *ConnBinder) OnDstClose(onDstClose CloseHandler) *ConnBinder

func (*ConnBinder) OnSrcClose

func (s *ConnBinder) OnSrcClose(onSrcClose CloseHandler) *ConnBinder

func (*ConnBinder) SetAutoClose

func (s *ConnBinder) SetAutoClose(autoClose bool)

func (*ConnBinder) SetReadBufSize

func (s *ConnBinder) SetReadBufSize(readBufSize int) *ConnBinder

func (*ConnBinder) Start

func (s *ConnBinder) Start()

func (*ConnBinder) StartAndWait

func (s *ConnBinder) StartAndWait()

type ConnFilter

type ConnFilter func(ctx Context, c net.Conn) (net.Conn, error)

type Context

type Context interface {
	Clone() Context
	EventConn() *EventConn
	SetEventConn(eventConn *EventConn) Context
	EventListener() *EventListener
	SetEventListener(eventListener *EventListener) Context
	ConnBinder() *ConnBinder
	SetConnBinder(connBinder *ConnBinder) Context
	IsHijacked() bool
	Hijack()
	IsBreak() bool
	Break()
	IsContinue() bool
	Continue()
	ReadBytes() int64
	WriteBytes() int64
	Data(key interface{}) interface{}
	SetData(key, value interface{}) Context
	SetConn(c net.Conn) Context
	Conn() net.Conn
	RawConn() net.Conn
	Listener() *Listener
	SetListener(listener *Listener) Context
	ReadTimeout() time.Duration
	WriteTimeout() time.Duration
	RemoteAddr() net.Addr
	LocalAddr() net.Addr
	IsTLS() bool
}

func NewContext

func NewContext() Context

type DataHandler

type DataHandler func(ctx Context, data []byte)

type ErrorHandler

type ErrorHandler func(ctx Context, err error)

type EventConn

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

func NewContextEventConn

func NewContextEventConn(ctx Context, c net.Conn) *EventConn

func NewEventConn

func NewEventConn(c net.Conn) *EventConn

func (*EventConn) AddCodec

func (s *EventConn) AddCodec(codec Codec) *EventConn

func (*EventConn) AddConnFilter

func (s *EventConn) AddConnFilter(f ConnFilter) *EventConn

func (*EventConn) Close

func (s *EventConn) Close()

func (*EventConn) Ctx

func (s *EventConn) Ctx() Context

func (*EventConn) LocalAddr

func (s *EventConn) LocalAddr() net.Addr

func (*EventConn) OnClose

func (s *EventConn) OnClose(onClose CloseHandler) *EventConn

func (*EventConn) OnData

func (s *EventConn) OnData(onData DataHandler) *EventConn

func (*EventConn) OnReadError

func (s *EventConn) OnReadError(onReadError ErrorHandler) *EventConn

func (*EventConn) OnWriterError

func (s *EventConn) OnWriterError(onWriterError ErrorHandler) *EventConn

func (*EventConn) ReadBytes

func (s *EventConn) ReadBytes() int64

func (*EventConn) ReadTimeout

func (s *EventConn) ReadTimeout() time.Duration

func (*EventConn) RemoteAddr

func (s *EventConn) RemoteAddr() net.Addr

func (*EventConn) SetReadBufferSize

func (s *EventConn) SetReadBufferSize(readBufferSize int) *EventConn

func (*EventConn) SetReadTimeout

func (s *EventConn) SetReadTimeout(readTimeout time.Duration) *EventConn

func (*EventConn) SetTimeout

func (s *EventConn) SetTimeout(readWriteTimeout time.Duration) *EventConn

func (*EventConn) SetWriteTimeout

func (s *EventConn) SetWriteTimeout(writeTimeout time.Duration) *EventConn

func (*EventConn) Start

func (s *EventConn) Start()

func (*EventConn) StartAndWait

func (s *EventConn) StartAndWait()

func (*EventConn) Write

func (s *EventConn) Write(b []byte) (n int, err error)

func (*EventConn) WriteBytes

func (s *EventConn) WriteBytes() int64

func (*EventConn) WriteTimeout

func (s *EventConn) WriteTimeout() time.Duration

type EventListener

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

func ListenEvent

func ListenEvent(addr string) (l *EventListener, err error)

func NewContextEventListener

func NewContextEventListener(ctx Context, l net.Listener) *EventListener

func NewEventListener

func NewEventListener(l net.Listener) *EventListener

func NewEventListenerAddr

func NewEventListenerAddr(addr string) (*EventListener, error)

func (*EventListener) AddCodecFactory

func (s *EventListener) AddCodecFactory(codecFactory CodecFactory) *EventListener

func (*EventListener) AddConnFilter

func (s *EventListener) AddConnFilter(f ConnFilter) *EventListener

func (*EventListener) AddListenerFilter

func (s *EventListener) AddListenerFilter(f ConnFilter) *EventListener

func (*EventListener) Addr

func (s *EventListener) Addr() *Addr

func (*EventListener) Close

func (s *EventListener) Close() *EventListener

func (*EventListener) ConnCount

func (s *EventListener) ConnCount() int64

func (*EventListener) Ctx

func (s *EventListener) Ctx() Context

func (*EventListener) OnAccept

func (s *EventListener) OnAccept(h AcceptHandler) *EventListener

func (*EventListener) OnAcceptError

func (s *EventListener) OnAcceptError(h ErrorHandler) *EventListener

func (*EventListener) OnFistReadTimeout

func (s *EventListener) OnFistReadTimeout(h FirstReadTimeoutHandler) *EventListener

func (*EventListener) SetAutoCloseConn

func (s *EventListener) SetAutoCloseConn(autoCloseConn bool)

SetAutoCloseConn if true, EventListener will close Conn after accept handler return.

func (*EventListener) SetAutoCloseConnOnReadWriteError

func (s *EventListener) SetAutoCloseConnOnReadWriteError(b bool) *EventListener

func (*EventListener) SetFirstReadTimeout

func (s *EventListener) SetFirstReadTimeout(firstReadTimeout time.Duration) *EventListener

func (*EventListener) SetOnConnClose

func (s *EventListener) SetOnConnClose(onConnClose CloseHandler) *EventListener

func (*EventListener) Start

func (s *EventListener) Start() *EventListener

func (*EventListener) StartAndWait

func (s *EventListener) StartAndWait()

type FirstReadTimeoutHandler

type FirstReadTimeoutHandler func(ctx Context, c net.Conn, err error)

type HeartbeatCodec

type HeartbeatCodec struct {
	net.Conn
	sync.Mutex
	sync.Once
	// contains filtered or unexported fields
}

func NewHeartbeatCodec

func NewHeartbeatCodec() *HeartbeatCodec

func (*HeartbeatCodec) Close

func (s *HeartbeatCodec) Close() (err error)

func (*HeartbeatCodec) Initialize

func (s *HeartbeatCodec) Initialize(ctx Context) (err error)

func (*HeartbeatCodec) Interval

func (s *HeartbeatCodec) Interval() time.Duration

func (*HeartbeatCodec) Read

func (s *HeartbeatCodec) Read(b []byte) (n int, err error)

func (*HeartbeatCodec) SetConn

func (s *HeartbeatCodec) SetConn(c net.Conn) Codec

func (*HeartbeatCodec) SetInterval

func (s *HeartbeatCodec) SetInterval(interval time.Duration) *HeartbeatCodec

func (*HeartbeatCodec) SetTimeout

func (s *HeartbeatCodec) SetTimeout(timeout time.Duration) *HeartbeatCodec

func (*HeartbeatCodec) Timeout

func (s *HeartbeatCodec) Timeout() time.Duration

func (*HeartbeatCodec) Write

func (s *HeartbeatCodec) Write(b []byte) (n int, err error)

type Listener

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

func Listen

func Listen(addr string) (l *Listener, err error)

func NewContextListener

func NewContextListener(ctx Context, l net.Listener) *Listener

func NewListener

func NewListener(l net.Listener) *Listener

func NewListenerAddr

func NewListenerAddr(addr string) (*Listener, error)

func (*Listener) Accept

func (s *Listener) Accept() (c net.Conn, err error)

func (*Listener) AddCodecFactory

func (s *Listener) AddCodecFactory(codecFactory CodecFactory) *Listener

func (*Listener) AddConnFilter

func (s *Listener) AddConnFilter(f ConnFilter) *Listener

func (*Listener) AddListenerFilter

func (s *Listener) AddListenerFilter(f ConnFilter) *Listener

func (*Listener) Addr

func (s *Listener) Addr() net.Addr

func (*Listener) Close

func (s *Listener) Close() error

func (*Listener) ConnCount

func (s *Listener) ConnCount() int64

func (*Listener) Ctx

func (s *Listener) Ctx() Context

func (*Listener) NewProtocolListener

func (s *Listener) NewProtocolListener(opt *ProtocolListenerOption) *Listener

func (*Listener) OnFistReadTimeout

func (s *Listener) OnFistReadTimeout(h FirstReadTimeoutHandler)

func (*Listener) SetAutoCloseConnOnReadWriteError

func (s *Listener) SetAutoCloseConnOnReadWriteError(b bool)

func (*Listener) SetFirstReadTimeout

func (s *Listener) SetFirstReadTimeout(firstReadTimeout time.Duration) *Listener

func (*Listener) SetOnConnClose

func (s *Listener) SetOnConnClose(onConnClose CloseHandler) *Listener

type ProtocolChecker

type ProtocolChecker func(listener *Listener, conn BufferedConn) bool

type ProtocolListener

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

func (*ProtocolListener) Accept

func (s *ProtocolListener) Accept() (net.Conn, error)

func (*ProtocolListener) Addr

func (s *ProtocolListener) Addr() net.Addr

func (*ProtocolListener) Close

func (s *ProtocolListener) Close() error

type ProtocolListenerOption

type ProtocolListenerOption struct {
	Name              string
	Checker           ProtocolChecker
	ConnQueueSize     int
	OnQueueOverflow   func(l net.Listener, opt *ProtocolListenerOption, conn BufferedConn)
	OverflowAutoClose bool
}

type RateCodec

type RateCodec struct {
	net.Conn
	// contains filtered or unexported fields
}

func NewRateCodec

func NewRateCodec(bytesPerSecond int64) *RateCodec

func (*RateCodec) Close

func (s *RateCodec) Close() (err error)

func (*RateCodec) Initialize

func (s *RateCodec) Initialize(ctx Context) error

func (*RateCodec) Read

func (s *RateCodec) Read(p []byte) (int, error)

func (*RateCodec) SetConn

func (s *RateCodec) SetConn(c net.Conn) Codec

func (*RateCodec) Write

func (s *RateCodec) Write(p []byte) (int, error)

type TLSClientCodec

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

func NewTLSClientCodec

func NewTLSClientCodec() *TLSClientCodec

func (*TLSClientCodec) AddCertificate

func (s *TLSClientCodec) AddCertificate(certPEMBytes, keyPEMBytes []byte) (err error)

func (*TLSClientCodec) AddServerCa

func (s *TLSClientCodec) AddServerCa(caPEMBytes []byte) *TLSClientCodec

func (*TLSClientCodec) AddToHTTPClient

func (s *TLSClientCodec) AddToHTTPClient(httpClient *http.Client) *TLSClientCodec

func (*TLSClientCodec) Initialize

func (s *TLSClientCodec) Initialize(ctx Context) (err error)

func (*TLSClientCodec) LoadSystemCas

func (s *TLSClientCodec) LoadSystemCas()

func (*TLSClientCodec) PinServerCert

func (s *TLSClientCodec) PinServerCert(serverCertPEMBytes []byte) (err error)

func (*TLSClientCodec) SetConn

func (s *TLSClientCodec) SetConn(c net.Conn) Codec

func (*TLSClientCodec) SetHandshakeTimeout

func (s *TLSClientCodec) SetHandshakeTimeout(handshakeTimeout time.Duration)

func (*TLSClientCodec) SetServerName

func (s *TLSClientCodec) SetServerName(serverName string) *TLSClientCodec

func (*TLSClientCodec) SkipVerify

func (s *TLSClientCodec) SkipVerify(b bool) *TLSClientCodec

func (*TLSClientCodec) SkipVerifyCommonName

func (s *TLSClientCodec) SkipVerifyCommonName(skipVerifyCommonName bool) *TLSClientCodec

type TLSServerCodec

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

func NewTLSServerCodec

func NewTLSServerCodec() *TLSServerCodec

func (*TLSServerCodec) AddCertificate

func (s *TLSServerCodec) AddCertificate(certPEMBytes, keyPEMBytes []byte) (err error)

func (*TLSServerCodec) AddClientCa

func (s *TLSServerCodec) AddClientCa(caPEMBytes []byte) *TLSServerCodec

func (*TLSServerCodec) Initialize

func (s *TLSServerCodec) Initialize(ctx Context) (err error)

func (*TLSServerCodec) LoadSystemCas

func (s *TLSServerCodec) LoadSystemCas()

func (*TLSServerCodec) RequireClientAuth

func (s *TLSServerCodec) RequireClientAuth(b bool) *TLSServerCodec

func (*TLSServerCodec) SetConn

func (s *TLSServerCodec) SetConn(c net.Conn) Codec

func (*TLSServerCodec) SetHandshakeTimeout

func (s *TLSServerCodec) SetHandshakeTimeout(handshakeTimeout time.Duration)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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