netutil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2021 License: MIT Imports: 15 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// max length of content: 1G
	MaxContentLength = 1 << 30
)

Variables

View Source
var (
	// content length greater than MaxContentLength
	ErrContentLengthOverflow = errors.New("content length overflow")
)

Functions

func IsNetworkError

func IsNetworkError(err error) bool

IsNetworkError returns whether the error is a network error or an EOF

func KeepAliveTCPConn

func KeepAliveTCPConn(conn net.Conn, d time.Duration)

KeepAliveTCPConn sets conn's keepalive duration

func ListenAndServeTCP

func ListenAndServeTCP(addr string, keepalive time.Duration, handler ConnHandler, async bool, certs ...tls.Certificate) error

ListenAndServeTCP wraps TCPServer's ListenAndServeTCP

Types

type ConnHandler

type ConnHandler func(ip string, conn net.Conn)

ConnHandler handles net.Conn

type Option

type Option func(*option)

Option represents options of NewSession

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout specify read timeout of session

type Session

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

Session wraps network session

func NewSession

func NewSession(conn net.Conn, handler SessionEventHandler, options ...Option) *Session

NewSession creates a session

func (*Session) Close

func (s *Session) Close() error

Close closes the session

func (*Session) Conn

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

Conn returns the underlying connection

func (*Session) IsClosed

func (s *Session) IsClosed() bool

IsClosed returns whether the session is closed

func (*Session) Serve

func (s *Session) Serve() bool

Serve runs the read/write loops, it will block until the session closed

func (*Session) Write

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

Write implements io.Writer Write method, this IS NOT thread-safe.

type SessionEventHandler

type SessionEventHandler interface {
	OnReady()                        // ready to read/write
	OnClose(err error)               // session closed, err maybe nil
	OnMessage(body proto.Body) error // received a message
}

SessionEventHandler handles session events

type TCPKeepAliveListener

type TCPKeepAliveListener struct {
	*net.TCPListener
	// contains filtered or unexported fields
}

TCPKeepAliveListener wraps TCPListener with a keepalive duration

func NewTCPKeepAliveListener

func NewTCPKeepAliveListener(ln *net.TCPListener, d time.Duration) *TCPKeepAliveListener

NewTCPKeepAliveListener creates a TCPKeepAliveListener

func (TCPKeepAliveListener) Accept

func (ln TCPKeepAliveListener) Accept() (c net.Conn, err error)

Accept implements net.Listener Accept method

type TCPServer

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

TCPServer represents a tcp server

func NewTCPServer

func NewTCPServer(addr string, handler ConnHandler) *TCPServer

NewTCPServer creates a tcp server

func (*TCPServer) ListenAndServe

func (server *TCPServer) ListenAndServe(async bool, keepalive time.Duration, certs ...tls.Certificate) error

ListenAndServe starts the tcp server

func (*TCPServer) Shutdown

func (server *TCPServer) Shutdown() error

Shutdown shutdowns the tcp server

Jump to

Keyboard shortcuts

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