tcp

package
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2019 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxBytes = 1 << 20 // 1 MB

Variables

View Source
var (
	// ServerContextKey is a context key. It can be used in TCP
	// handlers with context.WithValue to access the server that
	// started the handler. The associated value will be of
	// type *Server.
	ServerContextKey = &contextKey{"tcp-server"}
	// ClientContextKey is a context key. It can be used in TCP
	// handlers with context.WithValue to access the client that
	// started the handler. The associated value will be of
	// type *Client.
	ClientContextKey = &contextKey{"tcp-client"}

	// LocalAddrContextKey is a context key. It can be used in
	// TCP handlers with context.WithValue to access the local
	// address the connection arrived on.
	// The associated value will be of type net.Addr.
	LocalAddrContextKey = &contextKey{"local-addr"}
)
View Source
var DefaultServeMux = &defaultServeMux

DefaultServeMux is the default ServeMux used by Serve.

View Source
var ErrAbortHandler = errors.New("net/tcp: abort onMsgHandle")

ErrAbortHandler is a sentinel panic value to abort a handler. While any panic from OnHandshake aborts the response to the client, panicking with ErrAbortHandler also suppresses logging of a stack trace to the server's error log.

View Source
var ErrClientClosed = errors.New("tcp: Client closed")
View Source
var ErrNotFound = errors.New("tcp: Server not found")
View Source
var ErrServerClosed = errors.New("tcp: Server closed")

ErrServerClosed is returned by the Server's Serve and ListenAndServe methods after a call to Shutdown or Close.

View Source
var ErrUnImplement = errors.New("UnImplement Method")
View Source
var NopOnCloseHandler = nopSC
View Source
var NopOnErrorHandler = nopSC
View Source
var NopOnHTTPResponseHandler = nopSC
View Source
var NopOnMsgHandleHandler = nopSC
View Source
var NopOnMsgReadHandler = nopSC
View Source
var NopOnOpenHandler = nopSC

Functions

func ListenAndServe

func ListenAndServe(addr string, readMsg OnMsgReadHandler, handleMsg OnMsgHandleHandler) error

Types

type Client

type Client struct {
	*Server
}

func NewClient

func NewClient(h Handler) *Client

func NewClientFunc

func NewClientFunc(onOpenHandler OnOpenHandler,
	onMsgReadHandler OnMsgReadHandler,
	onMsgHandleHandler OnMsgHandleHandler,
	onCloseHandler OnCloseHandler,
	onErrorHandler OnErrorHandler) *Client

func (*Client) DialAndServe

func (cli *Client) DialAndServe(network, address string) error

func (*Client) ListenAndServe deprecated

func (cli *Client) ListenAndServe() error

Deprecated: use DialAndServe instead.

type ConnState

type ConnState int

A ConnState represents the state of a client connection to a server. It's used by the optional Server.ConnState hook.

const (
	// StateNew represents a new connection that is expected to
	// send a request immediately. Connections begin at this
	// state and then transition to either StateActive or
	// StateClosed.
	StateNew ConnState = iota

	// StateActive represents a connection that has read 1 or more
	// bytes of a request. The Server.ConnState hook for
	// StateActive fires before the request has entered a handler
	// and doesn't fire again until the request has been
	// handled. After the request is handled, the state
	// transitions to StateClosed, StateHijacked, or StateIdle.
	// For HTTP/2, StateActive fires on the transition from zero
	// to one active request, and only transitions away once all
	// active requests are complete. That means that ConnState
	// cannot be used to do per-request work; ConnState only notes
	// the overall state of the connection.
	StateActive

	// StateIdle represents a connection that has finished
	// handling a request and is in the keep-alive state, waiting
	// for a new request. Connections transition from StateIdle
	// to either StateActive or StateClosed.
	StateIdle

	// StateHijacked represents a hijacked connection.
	// This is a terminal state. It does not transition to StateClosed.
	StateHijacked

	// StateClosed represents a closed connection.
	// This is a terminal state. Hijacked connections do not
	// transition to StateClosed.
	StateClosed
)

func (ConnState) String

func (c ConnState) String() string

type NopClient

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

func (*NopClient) OnClose

func (srv *NopClient) OnClose(w io.Writer, r io.Reader) error

func (*NopClient) OnError

func (srv *NopClient) OnError(w io.Writer, r io.Reader, err error) error

func (*NopClient) OnMsgHandle

func (srv *NopClient) OnMsgHandle(w io.Writer, msg interface{}) error

func (*NopClient) OnMsgRead

func (srv *NopClient) OnMsgRead(r io.Reader) (msg interface{}, err error)

func (*NopClient) OnOpen

func (srv *NopClient) OnOpen(conn net.Conn) error

type NopServer

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

func (*NopServer) OnClose

func (srv *NopServer) OnClose(w io.Writer, r io.Reader) error

func (*NopServer) OnError

func (srv *NopServer) OnError(w io.Writer, r io.Reader, err error) error

func (*NopServer) OnMsgHandle

func (srv *NopServer) OnMsgHandle(w io.Writer, msg interface{}) error

func (*NopServer) OnMsgRead

func (srv *NopServer) OnMsgRead(r io.Reader) (msg interface{}, err error)

func (*NopServer) OnOpen

func (srv *NopServer) OnOpen(conn net.Conn) error

type NotFound

type NotFound struct {
	Handler
	NopServer
}

NotFoundHandler returns a simple request handler that replies to each request with a “404 page not found” reply.

func (*NotFound) HandleMsg

func (notfound *NotFound) HandleMsg(b *bufio.Writer, msg interface{}) error

func (*NotFound) OnClose

func (srv *NotFound) OnClose(w io.Writer, r io.Reader) error

func (*NotFound) OnError

func (srv *NotFound) OnError(w io.Writer, r io.Reader, err error) error

func (*NotFound) OnMsgHandle

func (srv *NotFound) OnMsgHandle(w io.Writer, msg interface{}) error

func (*NotFound) OnMsgRead

func (srv *NotFound) OnMsgRead(r io.Reader) (msg interface{}, err error)

func (*NotFound) OnOpen

func (srv *NotFound) OnOpen(conn net.Conn) error

func (*NotFound) ReadMsg

func (notfound *NotFound) ReadMsg(b *bufio.Reader) (msg interface{}, err error)

type OnCloseHandler

type OnCloseHandler interface {
	OnClose(w io.Writer, r io.Reader) error
}

type OnCloseHandlerFunc

type OnCloseHandlerFunc func(w io.Writer, r io.Reader) error

func (OnCloseHandlerFunc) OnClose

func (f OnCloseHandlerFunc) OnClose(w io.Writer, r io.Reader) error

type OnErrorHandler

type OnErrorHandler interface {
	OnError(w io.Writer, r io.Reader, err error) error
}

type OnErrorHandlerFunc

type OnErrorHandlerFunc func(w io.Writer, r io.Reader, err error) error

func (OnErrorHandlerFunc) OnError

func (f OnErrorHandlerFunc) OnError(w io.Writer, r io.Reader, err error) error

type OnMsgHandleHandler

type OnMsgHandleHandler interface {
	OnMsgHandle(b io.Writer, msg interface{}) error
}

type OnMsgHandleHandlerFunc

type OnMsgHandleHandlerFunc func(w io.Writer, msg interface{}) error

func (OnMsgHandleHandlerFunc) OnMsgHandle

func (f OnMsgHandleHandlerFunc) OnMsgHandle(w io.Writer, msg interface{}) error

type OnMsgReadHandler

type OnMsgReadHandler interface {
	OnMsgRead(b io.Reader) (msg interface{}, err error)
}

type OnMsgReadHandlerFunc

type OnMsgReadHandlerFunc func(r io.Reader) (msg interface{}, err error)

func (OnMsgReadHandlerFunc) OnMsgRead

func (f OnMsgReadHandlerFunc) OnMsgRead(r io.Reader) (msg interface{}, err error)

type OnOpenHandler

type OnOpenHandler interface {
	OnOpen(conn net.Conn) error
}

type OnOpenHandlerFunc

type OnOpenHandlerFunc func(conn net.Conn) error

func (OnOpenHandlerFunc) OnOpen

func (f OnOpenHandlerFunc) OnOpen(conn net.Conn) error

type ServeMux

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

func NewServeMux

func NewServeMux() *ServeMux

NewServeMux allocates and returns a new ServeMux.

func (*ServeMux) Handle

func (mux *ServeMux) Handle(handler Handler)

func (*ServeMux) OnClose

func (mux *ServeMux) OnClose(w io.Writer, r io.Reader) error

func (*ServeMux) OnError

func (mux *ServeMux) OnError(w io.Writer, r io.Reader, err error) error

func (*ServeMux) OnMsgHandle

func (mux *ServeMux) OnMsgHandle(w io.Writer, msg interface{}) error

func (*ServeMux) OnMsgRead

func (mux *ServeMux) OnMsgRead(r io.Reader) (req interface{}, err error)

func (*ServeMux) OnOpen

func (mux *ServeMux) OnOpen(conn net.Conn) error

type Server

type Server struct {
	Addr string // TCP address to listen on, ":tcp" if empty

	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	IdleTimeout  time.Duration
	MaxBytes     int

	ErrorLog *log.Logger

	// ConnState specifies an optional callback function that is
	// called when a client connection changes state. See the
	// ConnState type and associated constants for details.
	ConnState func(net.Conn, ConnState)
	// contains filtered or unexported fields
}

func NewServer

func NewServer(h Handler) *Server

func NewServerFunc

func NewServerFunc(
	onOpen OnOpenHandler,
	onMsgRead OnMsgReadHandler,
	onMsgHandle OnMsgHandleHandler,
	onClose OnCloseHandler,
	onError OnErrorHandler) *Server

func (*Server) CheckError

func (srv *Server) CheckError(w io.Writer, r io.Reader, err error) error

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

func (*Server) RegisterOnShutdown

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

func (*Server) Serve

func (srv *Server) Serve(l net.Listener) error

func (*Server) Shutdown

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

type TCPConn

type TCPConn struct {
	*bufio.ReadWriter
	// contains filtered or unexported fields
}

func NewTCPConn

func NewTCPConn(rw *bufio.ReadWriter) *TCPConn

Jump to

Keyboard shortcuts

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