tcp

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ClientContextKey = &contextKey{"tcp-client"}
)

connext keys

View Source
var DefaultConnHandler = &disconnectHandler{}

DefaultConnHandler is default connection handler

View Source
var (
	DefaultErrorLogFunc = log.Printf
)

default tcp options

View Source
var ErrAbortHandler = errors.New("tnet/tcp: abort Handler")

ErrAbortHandler means abort handler error

View Source
var ErrClientClosed = errors.New("tnet/tcp: Client closed")

ErrClientClosed means client has been closed

View Source
var ErrConnectionRefused = errors.New("tnet/tcp: Connection refused")

ErrConnectionRefused means connection refused

View Source
var ErrServerClosed = errors.New("tnet/tcp: Server closed")

ErrServerClosed means server has been closed

View Source
var (
	LocalAddrContextKey = &contextKey{"local-addr"}
)

context keys

View Source
var (
	ServerContextKey = &contextKey{"tcp-server"}
)

context keys

Functions

func SetKeepAliveCount added in v0.3.0

func SetKeepAliveCount(conn *net.TCPConn, count int) (err error)

SetKeepAliveCount set the TCP_KEEPCNT option

Types

type Client

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

Client over tcp

func NewClient

func NewClient(opts ...ClientOption) *Client

NewClient create a new tcp client

func (*Client) Close

func (cli *Client) Close() error

Close close all connections of client

func (*Client) DialAndServe

func (cli *Client) DialAndServe(ctx context.Context) error

DialAndServe starts client

func (*Client) RegisterOnShutdown

func (cli *Client) RegisterOnShutdown(f func())

RegisterOnShutdown registers OnShutdown funcs

func (*Client) Shutdown

func (cli *Client) Shutdown(ctx context.Context) error

Shutdown shutdowns client graceful

type ClientOption

type ClientOption func(opts *ClientOptions)

ClientOption is option setter for tcp client

func WithClientConnContextFunc

func WithClientConnContextFunc(f func(ctx context.Context, c net.Conn) context.Context) ClientOption

WithClientConnContextFunc sets connection context hook function opt

func WithClientErrorLogFunc

func WithClientErrorLogFunc(errorLogFunc func(fmt string, args ...interface{})) ClientOption

WithClientErrorLogFunc sets error log function opt

func WithClientHandler

func WithClientHandler(h ConnHandler) ClientOption

WithClientHandler sets client handler opt

func WithClientKeepAliveCount added in v0.3.0

func WithClientKeepAliveCount(count int) ClientOption

WithClientKeepAliveCount sets tcp keepalive count opt

func WithClientKeepAlivePeriod added in v0.3.0

func WithClientKeepAlivePeriod(period time.Duration) ClientOption

WithClientKeepAlivePeriod sets tcp keepalive period opt

func WithConnectAddress

func WithConnectAddress(addr string) ClientOption

WithConnectAddress sets connect address opt

type ClientOptions

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

ClientOptions is options of tcp client

type Conn

type Conn interface {
	Reader() io.Reader
	Writer() io.Writer
	BufferReader() *bufio.Reader
	BufferWriter() *bufio.Writer
	AbortPendingRead()
	CancelContext()
}

Conn is tcp connection

type ConnHandler

type ConnHandler interface {
	ServeConn(ctx context.Context, conn Conn)
}

ConnHandler is connection handler

func NewRawTCPConnHandler added in v0.2.0

func NewRawTCPConnHandler(h RawTCPHandler) ConnHandler

NewRawTCPConnHandler creates a new raw tcp connection handler

type ConnState

type ConnState int

ConnState is connection state

const (
	StateNew ConnState = iota
	StateActive
	StateIdle
	StateClosed
)

connection state values

func (ConnState) String

func (c ConnState) String() string

type RawTCPConnHandler added in v0.2.0

type RawTCPConnHandler struct {
	Handler RawTCPHandler
}

RawTCPConnHandler is raw tcp connection handler

func (*RawTCPConnHandler) ServeConn added in v0.2.0

func (ch *RawTCPConnHandler) ServeConn(ctx context.Context, conn Conn)

ServeConn serves new connection

type RawTCPHandler

type RawTCPHandler interface {
	ServeTCP(ctx context.Context, conn Conn)
}

RawTCPHandler is raw tcp handler

type Server

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

Server over tcp

func NewServer

func NewServer(opts ...ServerOption) *Server

NewServer create a new tcp server

func (*Server) Close

func (srv *Server) Close() error

Close close all connections of server

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe starts tcp server

func (*Server) RegisterOnShutdown

func (srv *Server) RegisterOnShutdown(f func())

RegisterOnShutdown registers OnShutdown functions

func (*Server) Shutdown

func (srv *Server) Shutdown(ctx context.Context) error

Shutdown shutdowns client graceful

type ServerOption

type ServerOption func(opts *ServerOptions)

ServerOption is option setter for server

func WithListenAddress

func WithListenAddress(addr string) ServerOption

WithListenAddress sets listen address opt

func WithServerBaseContextFunc

func WithServerBaseContextFunc(f func(net.Listener) context.Context) ServerOption

WithServerBaseContextFunc sets server base context hook funcion opt

func WithServerConnContextFunc

func WithServerConnContextFunc(f func(ctx context.Context, c net.Conn) context.Context) ServerOption

WithServerConnContextFunc sets new connection context hook funcion opt

func WithServerErrorLogFunc

func WithServerErrorLogFunc(errorLogFunc func(fmt string, args ...interface{})) ServerOption

WithServerErrorLogFunc sets error log function opt

func WithServerHandler

func WithServerHandler(h ConnHandler) ServerOption

WithServerHandler sets server handler opt

func WithServerKeepAliveCount added in v0.3.0

func WithServerKeepAliveCount(count int) ServerOption

WithServerKeepAliveCount sets tcp keepalive count opt

func WithServerKeepAlivePeriod added in v0.3.0

func WithServerKeepAlivePeriod(period time.Duration) ServerOption

WithServerKeepAlivePeriod sets tcp keepalive period opt

type ServerOptions

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

ServerOptions is server options

Jump to

Keyboard shortcuts

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