netutil

package
v0.2.18 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2022 License: MIT Imports: 20 Imported by: 2

Documentation

Index

Constants

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

Variables

View Source
var (
	ErrNotHandshaked  = errors.New("hello command required")
	ErrInvalidCommand = errors.New("invalid command")
)

Functions

func IP added in v0.0.18

func IP(req *http.Request) string

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, certs ...tls.Certificate) error

ListenAndServeTCP listen and serve a tcp address

Types

type Command added in v0.0.18

type Command interface {
	Name() string     // Name of command
	NArg() int        // Number of arguments
	Arg(i int) string // Arg returns ith argument
}

Command represents textproto command

type CommandHandler added in v0.0.18

type CommandHandler interface {
	Commands() []string
	OnCommand(Command) error
}

CommandHandler handles textproto command

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(err error)

Close closes the session

func (*Session) Conn

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

Conn returns the underlying connection

func (*Session) ContentType added in v0.0.18

func (s *Session) ContentType() proto.ContentType

ContentType returns type of content

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 {
	OnOpen()                                // ready to read/write
	OnClose(err error)                      // session closed, err maybe nil
	OnHandshake(proto.ContentType) error    // session handshaked
	OnMessage(proto.Type, 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 ListenTCP added in v0.0.18

func ListenTCP(addr string, handler ConnHandler, keepalive time.Duration, certs ...tls.Certificate) (*TCPServer, net.Listener, error)

ListenTCP creates a tcp server

func (*TCPServer) Serve added in v0.0.18

func (server *TCPServer) Serve(listener net.Listener) error

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