Documentation
¶
Overview ¶
Package quic provides client and server functions for QUIC connections.
The process of creating a basic client and server looks like this:
config := transport.NewConfig() client := quic.NewClient(config) client.SetHandler(handler) err := client.ListenAndServe(address) err = client.Connect(serverAddress) // wait client.Close() server := quic.NewServer(config) server.SetHandler(handler) err := server.ListenAndServe(address)
The handler is where applications interact with QUIC connections:
func (handler) Serve(conn *quic.Conn, events []transport.Event) { for _, e := range events { switch e.Type { case transport.EventConnOpen: case transport.EventConnClosed: } } }
Index ¶
- type AddressVerifier
- type CIDIssuer
- type Client
- func (s *Client) Close() error
- func (s *Client) Connect(addr string) error
- func (s *Client) ListenAndServe(addr string) error
- func (s *Client) LocalAddr() net.Addr
- func (s *Client) Serve() error
- func (s *Client) SetCIDIssuer(cidIss CIDIssuer)
- func (s *Client) SetHandler(v Handler)
- func (s *Client) SetListener(conn net.PacketConn)
- func (s *Client) SetLogger(level int, w io.Writer)
- type Conn
- func (s *Conn) Close() error
- func (s *Conn) CloseWithError(code uint64, reason string) error
- func (s *Conn) ConnectionState() transport.ConnectionState
- func (s *Conn) Datagram() *Datagram
- func (s *Conn) DatagramRead(b []byte) (int, error)
- func (s *Conn) DatagramWrite(b []byte) (int, error)
- func (s *Conn) LocalAddr() net.Addr
- func (s *Conn) NewStream(bidi bool) (uint64, bool)
- func (s *Conn) RemoteAddr() net.Addr
- func (s *Conn) SetUserData(data interface{})
- func (s *Conn) Stream(streamID uint64) (*Stream, error)
- func (s *Conn) StreamClose(streamID uint64) error
- func (s *Conn) StreamCloseRead(streamID uint64, errorCode uint64) error
- func (s *Conn) StreamCloseWrite(streamID uint64, errorCode uint64) error
- func (s *Conn) StreamRead(streamID uint64, b []byte) (int, error)
- func (s *Conn) StreamWrite(streamID uint64, b []byte) (int, error)
- func (s *Conn) StreamWriteTo(streamID uint64, w io.Writer) (int64, error)
- func (s *Conn) UserData() interface{}
- type Datagram
- func (s *Datagram) Close() error
- func (s *Datagram) LocalAddr() net.Addr
- func (s *Datagram) Read(b []byte) (int, error)
- func (s *Datagram) RemoteAddr() net.Addr
- func (s *Datagram) SetDeadline(t time.Time) error
- func (s *Datagram) SetReadDeadline(t time.Time) error
- func (s *Datagram) SetWriteDeadline(t time.Time) error
- func (s *Datagram) Write(b []byte) (int, error)
- type Handler
- type Server
- func (s *Server) Close() error
- func (s *Server) ListenAndServe(addr string) error
- func (s *Server) LocalAddr() net.Addr
- func (s *Server) Serve() error
- func (s *Server) SetAddressVerifier(v AddressVerifier)
- func (s *Server) SetCIDIssuer(cidIss CIDIssuer)
- func (s *Server) SetHandler(v Handler)
- func (s *Server) SetListener(conn net.PacketConn)
- func (s *Server) SetLogger(level int, w io.Writer)
- type Stream
- func (s *Stream) Close() error
- func (s *Stream) CloseRead(errorCode uint64) error
- func (s *Stream) CloseWrite(errorCode uint64) error
- func (s *Stream) LocalAddr() net.Addr
- func (s *Stream) Read(b []byte) (int, error)
- func (s *Stream) RemoteAddr() net.Addr
- func (s *Stream) SetDeadline(t time.Time) error
- func (s *Stream) SetReadDeadline(t time.Time) error
- func (s *Stream) SetWriteDeadline(t time.Time) error
- func (s *Stream) Write(b []byte) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AddressVerifier ¶ added in v0.0.8
type AddressVerifier interface { IsActive(addr net.Addr) bool // NewToken creates a new token from given address, retry source connection ID // and original destination connection ID. NewToken(addr net.Addr, rscid, odcid []byte) []byte // VerifyToken returns odcid when the address and token pair is valid, // empty slice otherwise. VerifyToken(addr net.Addr, dcid, token []byte) []byte }
AddressVerifier generates and validates server retry token. https://www.rfc-editor.org/rfc/rfc9000.html#token-integrity
func NewAddressVerifier ¶ added in v0.0.8
func NewAddressVerifier() AddressVerifier
NewAddressVerifier returns a simple implementation of AddressValidator. It encrypts client original CID into token which is valid for 10 seconds.
type CIDIssuer ¶ added in v0.0.8
type CIDIssuer interface { // NewCID generates a new connection ID. NewCID() ([]byte, error) // CIDLength returns the length of generated connection id which is needed // to decode short-header packets. // Currently, only constant length is supported. CIDLength() int }
CIDIssuer generates connection ID.
func NewServerCIDIssuer ¶ added in v0.0.8
NewServerCIDIssuer returns a new CIDIssuer that creates CID using Plaintext Algorithm. Server ID is encoded in CID using QUIC varint encoding. https://quicwg.org/load-balancers/draft-ietf-quic-load-balancers.html#section-5.1
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a client-side QUIC connection. All setters must only be invoked before calling Serve.
func (*Client) Close ¶ added in v0.0.3
Close closes all current established connections and listening socket.
func (*Client) ListenAndServe ¶ added in v0.0.3
ListenAndServe starts listening on UDP network address addr and serves incoming packets. Unlike Server.ListenAndServe, this function does not block as Serve is invoked in a goroutine.
func (*Client) SetCIDIssuer ¶ added in v0.0.8
func (s *Client) SetCIDIssuer(cidIss CIDIssuer)
SetCIDIssuer sets generator for connection ids. By default, it generates random IDs from Reader in crypto/rand. If transport.Config.TLS.Rand is available, it will use that source instead.
func (*Client) SetHandler ¶ added in v0.0.2
func (s *Client) SetHandler(v Handler)
SetHandler sets QUIC connection callbacks.
func (*Client) SetListener ¶ added in v0.0.4
func (s *Client) SetListener(conn net.PacketConn)
SetListener sets listening socket connection.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a QUIC connection presenting a peer connected to this client or server. Conn is not safe for concurrent use. Its methods would only be called inside the Handler callback.
func (*Conn) Close ¶
Close sets the connection status to closing state. If peer has already initiated closing with an error, this function will return that error, which is either transport.Error or transport.AppError
func (*Conn) CloseWithError ¶ added in v0.0.4
CloseWithError sets the connection to closing state with an application code code and reason sending to peer. The function returns error if peer has already initiated closing.
func (*Conn) ConnectionState ¶ added in v0.0.6
func (s *Conn) ConnectionState() transport.ConnectionState
ConnectionState returns details about the connection.
func (*Conn) Datagram ¶ added in v0.0.4
Datagram returns a Datagram associated with the connection. NOTE: Unlike other Conn.Datagram* functions, the returned Datagram must only be used in a different goroutine (i.e. not in the connection handler). See Datagram struct for details.
func (*Conn) DatagramRead ¶ added in v0.0.6
DatagramRead pulls received datagram directly from the connection buffer. It returns nil when there is no data to read.
func (*Conn) DatagramWrite ¶ added in v0.0.6
DatagramWrite queues data to the connection buffer for sending via datagram.
func (*Conn) NewStream ¶ added in v0.0.4
NewStream creates and returns a new local stream id. If number of streams exceeds peer limits, the function will return false.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address.
func (*Conn) SetUserData ¶ added in v0.0.4
func (s *Conn) SetUserData(data interface{})
SetUserData attaches (or removes) user data to the connection.
func (*Conn) Stream ¶
Stream creates or returns an existing QUIC stream given the ID. NOTE: The returned stream would only be used in a different goroutine. See Stream struct for details.
func (*Conn) StreamClose ¶ added in v0.0.6
StreamClose gracefully closes sending part of the stream.
func (*Conn) StreamCloseRead ¶ added in v0.0.6
StreamCloseRead terminates reading part of the stream.
func (*Conn) StreamCloseWrite ¶ added in v0.0.6
StreamCloseWrite terminates sending part of the stream.
func (*Conn) StreamRead ¶ added in v0.0.6
StreamRead gets data received from the stream buffer.
func (*Conn) StreamWrite ¶ added in v0.0.6
StreamWrite adds data to the stream buffer for sending.
func (*Conn) StreamWriteTo ¶ added in v0.0.8
StreamWriteTo writes stream data to w until there is no more data or when an error occurs.
type Datagram ¶ added in v0.0.6
type Datagram struct {
// contains filtered or unexported fields
}
Datagram provides asynchronous APIs to interact which QUIC datagram. All Datagram functions must be used in a separated goroutine that is different to the connection callback. For example:
func (handler) Serve(conn *quic.Conn, events []transport.Event) { for _, e := range events { switch e.Type { case transport.EventDatagramWritable: go func(datagram *quic.Datagram) { // Working on the datagram. }(conn.Datagram()) } } }
Datagram implements net.Conn.
func (*Datagram) RemoteAddr ¶ added in v0.0.6
RemoteAddr returns the remote network address.
func (*Datagram) SetDeadline ¶ added in v0.0.6
SetDeadline sets the read and write deadlines associated with the stream.
func (*Datagram) SetReadDeadline ¶ added in v0.0.6
SetReadDeadline sets the read deadline associated with the stream.
func (*Datagram) SetWriteDeadline ¶ added in v0.0.6
SetWriteDeadline sets the write deadline associated with the stream.
type Handler ¶
type Handler interface { // Serve handles connection events. // Applications should keep execution time in the callback to a minimum. // If extra works needed, applications should consider using asynchronous APIs, // i.e Stream functions instead of Conn.Stream* functions. // Currently, each connection has its own goroutine for this callback. Serve(conn *Conn, events []transport.Event) }
Handler defines interface to handle QUIC connection states. Multiple goroutines may invoke methods on a Handler simultaneously.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a server-side QUIC connection. All setters must only be invoked before calling Serve.
func (*Server) Close ¶
Close sends Close frame to all connected clients and closes the socket given in Serve. Note: if Close is called before Serve, the socket may not be set so it will not be close. In that case Serve will hang until it gets socket read error.
func (*Server) ListenAndServe ¶ added in v0.0.3
ListenAndServe starts listening on UDP network address addr and serves incoming packets.
func (*Server) Serve ¶
Serve handles incoming requests from a socket connection. XXX: Since net.PacketConn methods can be called simultaneously, users should be able to run Serve in multiple goroutines. For example:
s.SetListen(socket) for i := 1; i < num; i++ { go s.Serve() } s.Serve() // main one blocking
func (*Server) SetAddressVerifier ¶ added in v0.0.8
func (s *Server) SetAddressVerifier(v AddressVerifier)
SetAddressVerifier sets validation for QUIC connections address.
func (*Server) SetCIDIssuer ¶ added in v0.0.8
func (s *Server) SetCIDIssuer(cidIss CIDIssuer)
SetCIDIssuer sets generator for connection ids. By default, it generates random IDs from Reader in crypto/rand. If transport.Config.TLS.Rand is available, it will use that source instead.
func (*Server) SetHandler ¶ added in v0.0.2
func (s *Server) SetHandler(v Handler)
SetHandler sets QUIC connection callbacks.
func (*Server) SetListener ¶ added in v0.0.4
func (s *Server) SetListener(conn net.PacketConn)
SetListener sets listening socket connection.
type Stream ¶ added in v0.0.6
type Stream struct {
// contains filtered or unexported fields
}
Stream provides asynchronous APIs to interact which a QUIC stream. All Stream functions must be used in a separated goroutine that is different to the connection callback. For example:
func (handler) Serve(conn *quic.Conn, events []transport.Event) { for _, e := range events { switch e.Type { case transport.EventStreamOpen: st, err := conn.Stream(e.ID) ... go func(stream *quic.Stream) { // Working on the stream. }(st) } } }
Stream implements net.Conn interface.
func (*Stream) CloseWrite ¶ added in v0.0.6
CloseWrite terminates sending part of the stream.
func (*Stream) Read ¶ added in v0.0.6
Read reads data from the stream. The function is blocked until any stream data is available or timeout.
func (*Stream) RemoteAddr ¶ added in v0.0.6
RemoteAddr returns the remote network address.
func (*Stream) SetDeadline ¶ added in v0.0.6
SetDeadline sets the read and write deadlines associated with the stream.
func (*Stream) SetReadDeadline ¶ added in v0.0.6
SetReadDeadline sets the read deadline associated with the stream.
func (*Stream) SetWriteDeadline ¶ added in v0.0.6
SetWriteDeadline sets the write deadline associated with the stream.