net

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelVerbose = slog.Level(-16)
	LevelTrace   = slog.Level(-8)
	LevelNotice  = slog.Level(2)
	LevelHint    = slog.Level(3)
	LevelFatal   = slog.Level(16)
	LevelPanic   = slog.Level(17)
)
View Source
const (
	// Alphabets gets the a to z and A to Z
	Alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)

Variables

View Source
var LevelNames = map[slog.Leveler]string{
	LevelVerbose: "VERBOSE",
	LevelTrace:   "TRACE",
	LevelNotice:  "NOTICE",
	LevelHint:    "HINT",
	LevelFatal:   "FATAL",
	LevelPanic:   "PANIC",
}

Functions

func NewClient

func NewClient(opts ...ClientOpt) *clientS

func NewServer

func NewServer(addr string, opts ...ServerOpt) *serverWrap

Types

type BasicLogger

type BasicLogger interface {
	Printer
}

type CacheWriteable

type CacheWriteable interface {
	// WrChannel is only available without user Handler specified.
	// The default connS.serve will received the data from WrChannel and write
	// to internal connection rawly.
	WrChannel() chan<- []byte
}

type Client

type Client interface {
	api.Addressable

	Close()
	Closed() bool
	NotClosed() bool
	Connected() bool

	Dial(network, addr string) (err error)

	api.Writeable

	io.Reader

	Run(ctx context.Context) // start a go routine to run internal worker loop

}

type ClientOpt

type ClientOpt func(s *clientS)

func WithClientInterceptor

func WithClientInterceptor(ci api.Interceptor) ClientOpt

func WithClientLogger

func WithClientLogger(l Logger) ClientOpt

func WithClientLoggerHandler

func WithClientLoggerHandler(h slog.Handler) ClientOpt

func WithClientSendCacheSize

func WithClientSendCacheSize(size int) ClientOpt

type Closable

type Closable interface {
	Close()
}

type Closers

type Closers []Closable

func (Closers) Close

func (s Closers) Close()

type CorruptDataFinder

type CorruptDataFinder interface {
	OnCorruptData(data []byte, w api.Response, r api.Request) (ate int)
}

CorruptDataFinder try finding bounds of next good diagram and returns ate bytes.

If ate ok, buf[ate:] should be a good diagram. Or returns 0 to declare a wrong state.

type DataProcessor

type DataProcessor interface {
	// Process implements Processor interface to announce that i will process the incoming data in Read().
	Process(data []byte, w api.Response, r api.Request) (nn int, err error)
}

DataProcessor handles data and extract one or more data diagrams.

If must necessary, reading more bytes from r and writing something to w.

type ExtraPrinters

type ExtraPrinters interface {
	Verbose(msg string, args ...any) // only for -tags=verbose
	Print(msg string, args ...any)   // logging always
	Println(args ...any)             // synonym to Print, NOTE first elem of args decoded as msg here

	Trace(msg string, args ...any) // only for state.Env().InTracing()
	Panic(msg string, args ...any) // error and panic
	Fatal(msg string, args ...any) // error and os.Exit(-3)

}

type Handler

type Handler interface {
	Serve(ctx context.Context, w api.Response, r api.Request) (processed bool, err error)
}

Handler is implemented by any value that implements ServeDNS.

type HandlerFunc

type HandlerFunc func(ctx context.Context, w api.Response, r api.Request) (processed bool, err error)

The HandlerFunc type is an adapter to allow the use of ordinary functions as DNS handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.

func (HandlerFunc) Serve

func (f HandlerFunc) Serve(ctx context.Context, w api.Response, r api.Request) (processed bool, err error)

Serve calls f(w, r).

type LogEntry

type LogEntry interface {
	BasicLogger
}

type Logger

type Logger interface {
	LogEntry
}

type OnHotReload

type OnHotReload func(ctx context.Context, ss Server) (err error)

type OnNewResponse

type OnNewResponse interface {
	New() api.Response
}

type OnRestart

type OnRestart func(ctx context.Context, ss Server) (err error)

type OnShutdown

type OnShutdown func(errReason error, ss Server)

type OnTcpServerConnectedWithClient

type OnTcpServerConnectedWithClient func(w api.Response, ss Server)

type OnTcpServerCorruptData

type OnTcpServerCorruptData func(data []byte, w api.Response, r api.Request) (ate int)

type OnTcpServerCreateReadWriter

type OnTcpServerCreateReadWriter func(ss Server, conn api.Response, tsConnected time.Time) (in io.Reader, out io.Writer)

type OnTcpServerDisconnectedWithClient

type OnTcpServerDisconnectedWithClient func(w api.Response, r api.Request, ss Server)

type OnTcpServerListening

type OnTcpServerListening func(ss Server, l net.Listener) // for udp or unixgram, l is nil; for tcp and unix, it points to the listener

type OnTcpServerProcessData

type OnTcpServerProcessData func(data []byte, w api.Response, r api.Request) (nn int, err error)

type Printer

type Printer interface {
	Error(msg string, args ...any) // error
	Warn(msg string, args ...any)  // warning
	Info(msg string, args ...any)  // info. Attr, Attrs in args will be recognized as is
	Debug(msg string, args ...any) // only for state.Env().InDebugging() or IsDebugBuild()

	Log(ctx context.Context, lvl slog.Level, msg string, args ...any)
}

type Runnable

type Runnable interface {
	Run() // a go routine here
}

type Server

type Server interface {
	Listen(ctx context.Context) (err error)
	ListenAndServe(ctx context.Context, handler Handler) (err error) // Start server with block
	ListenAndServeTLS(ctx context.Context, addr, certFile, keyFile string, handler Handler) (err error)

	Start(ctx context.Context) (err error) // Start server without block
	Stop() (err error)

	Restart(ctx context.Context) (err error)
	HotReload(ctx context.Context) (err error)
	Shutdown() (err error) // = Stop
	Close()                // = Stop

	WithOnShutdown(cb OnShutdown) Server // set OnShutdown handler
}

type ServerOpt

type ServerOpt func(s *serverWrap)

func WithNetwork

func WithNetwork(network string) ServerOpt

WithNetwork sets network protocol: tcp, tcp4, tcp6, unix and unixpacket

func WithServerHandler

func WithServerHandler(h Handler) ServerOpt

func WithServerHandlerFunc

func WithServerHandlerFunc(h HandlerFunc) ServerOpt

func WithServerListenConfig

func WithServerListenConfig(c *net.ListenConfig) ServerOpt

WithServerListenConfig gives a user-defined listening config structure

func WithServerLogger

func WithServerLogger(l Logger) ServerOpt

func WithServerLoggerHandler

func WithServerLoggerHandler(h slog.Handler) ServerOpt

func WithServerMaxMessageLength

func WithServerMaxMessageLength(l int) ServerOpt

WithServerMaxMessageLength assumes a message/diagram have the given max-length.

Specifying a proper buffer size is useful for parsing incoming data buffer efficiently.

Too large buffer size takes wasted spaces and too small causes a large message cannot be split from incoming data stream.

Default buffer size is 4096.

serverWrap allocates 2 x bufferSize bytes to internal buffer, and reads 1 x bufferSize bytes into it. After one message extracted from internal buffer, the rest bytes are moved to beginning of buffer, and next reading position following it.

The double bufferSize allows the above algorithm always works properly.

func WithServerOnClientConnected

func WithServerOnClientConnected(cb ...OnTcpServerConnectedWithClient) ServerOpt

func WithServerOnClientDisconnected

func WithServerOnClientDisconnected(cb ...OnTcpServerDisconnectedWithClient) ServerOpt

func WithServerOnCorruptData

func WithServerOnCorruptData(cb OnTcpServerCorruptData) ServerOpt

func WithServerOnCreateReadWriter

func WithServerOnCreateReadWriter(cb OnTcpServerCreateReadWriter) ServerOpt

func WithServerOnListening

func WithServerOnListening(cb ...OnTcpServerListening) ServerOpt

WithServerOnListening sets callback to OnTcpServerListening so that you can do something while the server started ready.

func WithServerOnNewResponse

func WithServerOnNewResponse(nr OnNewResponse) ServerOpt

WithServerOnNewResponse sets a user-defined Response maker.

The default ones does make new connS and run its looper. You may replace it with yours.

func WithServerOnProcessData

func WithServerOnProcessData(cb OnTcpServerProcessData) ServerOpt

func WithServerOnShutdown

func WithServerOnShutdown(cb OnShutdown) ServerOpt

func WithServerQuiet

func WithServerQuiet(b bool) ServerOpt

func WithServerTLSConfig

func WithServerTLSConfig(c *tls.Config) ServerOpt

WithServerTLSConfig enables a tls link

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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