Documentation ¶
Overview ¶
Package serve2 provides a mechanism to detect and handle multiple protocols on a single net.Conn or net.Listener.
Protocols are defined as handlers that, given the amount of header bytes they require, can check if a header matches the protocol, as well as handle the connection itself afterwards.
The read bytes from the header are provided to the handler by ProxyConn, that simulates the first few reads until the header buffer is empty, at which point it resumes normal operation.
Any protocol where the client writes data that is unique to the protocol to the server immediately after opening the connection can be handled by serve2. Echo and discard do not really fit here, as they would normally put no constaints on the client - instead, serve2's test implementations require that you write ECHO or DISCARD to the connection to trigger those protocol handlers.
Index ¶
Constants ¶
const (
// DefaultBytesToCheck default maximum amount of bytes to check
DefaultBytesToCheck = 128
)
Variables ¶
var (
ErrGreedyHandler = errors.New("remaining handlers too greedy")
)
Errors
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger func(format string, v ...interface{})
Logger is used to provide logging functionality for serve2
type Protocol ¶
type Protocol interface { // Check informs if the bytes match the protocol. If there is not enough // data yet, it should return false and the wanted amount of bytes, allowing // future calls when more data is available. It does not need to return the // same every time, and incrementally checking more and more data is // allowed. Returning false and 0 bytes needed means that the protocol // handler is 100% sure that this is not the proper protocol, and will not // result in any further calls. // Check, when called with nil, nil, must return false, N, where N is the // smallest amount of bytes that makes sense to call Check with. Check(header []byte, hints []interface{}) (ok bool, needed int) // Handle manages the protocol. In case of an encapsulating protocol, Handle // can return a net.Conn which will be thrown through the entire protocol // management show again. Handle(c net.Conn) (net.Conn, error) }
Protocol is the protocol detection and handling interface used by serve2.
type Server ¶
type Server struct { // DefaultProtocol is the protocol fallback if no match is made DefaultProtocol Protocol // Logger is used for logging if set Logger Logger // BytesToCheck is the max amount of bytes to check BytesToCheck int // Protocols is the list of protocols Protocols []Protocol }
Server handles a set of Protocols.
func (*Server) AddHandlers ¶
AddHandlers registers a set of Protocols
func (*Server) HandleConn ¶
HandleConn runs a connection through protocol detection and handling as needed.