github.com/cloudwego/hertz@v0.9.3/pkg/network/connection.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package network
    18  
    19  import (
    20  	"context"
    21  	"crypto/tls"
    22  	"fmt"
    23  	"io"
    24  	"net"
    25  	"time"
    26  )
    27  
    28  // Reader is for buffered Reader
    29  type Reader interface {
    30  	// Peek returns the next n bytes without advancing the reader.
    31  	Peek(n int) ([]byte, error)
    32  
    33  	// Skip discards the next n bytes.
    34  	Skip(n int) error
    35  
    36  	// Release the memory space occupied by all read slices. This method needs to be executed actively to
    37  	// recycle the memory after confirming that the previously read data is no longer in use.
    38  	// After invoking Release, the slices obtained by the method such as Peek will
    39  	// become an invalid address and cannot be used anymore.
    40  	Release() error
    41  
    42  	// Len returns the total length of the readable data in the reader.
    43  	Len() int
    44  
    45  	// ReadByte is used to read one byte with advancing the read pointer.
    46  	ReadByte() (byte, error)
    47  
    48  	// ReadBinary is used to read next n byte with copy, and the read pointer will be advanced.
    49  	ReadBinary(n int) (p []byte, err error)
    50  }
    51  
    52  type Writer interface {
    53  	// Malloc will provide a n bytes buffer to send data.
    54  	Malloc(n int) (buf []byte, err error)
    55  
    56  	// WriteBinary will use the user buffer to flush.
    57  	// NOTE: Before flush successfully, the buffer b should be valid.
    58  	WriteBinary(b []byte) (n int, err error)
    59  
    60  	// Flush will send data to the peer end.
    61  	Flush() error
    62  }
    63  
    64  type ReadWriter interface {
    65  	Reader
    66  	Writer
    67  }
    68  
    69  type Conn interface {
    70  	net.Conn
    71  	Reader
    72  	Writer
    73  
    74  	// SetReadTimeout should work for every Read process
    75  	SetReadTimeout(t time.Duration) error
    76  	SetWriteTimeout(t time.Duration) error
    77  }
    78  
    79  type ConnTLSer interface {
    80  	Handshake() error
    81  	ConnectionState() tls.ConnectionState
    82  }
    83  
    84  type HandleSpecificError interface {
    85  	HandleSpecificError(err error, rip string) (needIgnore bool)
    86  }
    87  
    88  type ErrorNormalization interface {
    89  	ToHertzError(err error) error
    90  }
    91  
    92  type DialFunc func(addr string) (Conn, error)
    93  
    94  /****************** Stream-based connection *******************/
    95  
    96  // StreamConn is interface for stream-based connection abstraction.
    97  type StreamConn interface {
    98  	GetRawConnection() interface{}
    99  	// HandshakeComplete blocks until the handshake completes (or fails).
   100  	HandshakeComplete() context.Context
   101  	// GetVersion returns the version of the protocol used by the connection.
   102  	GetVersion() uint32
   103  	// CloseWithError closes the connection with an error.
   104  	// The error string will be sent to the peer.
   105  	CloseWithError(err ApplicationError, errMsg string) error
   106  	// LocalAddr returns the local address.
   107  	LocalAddr() net.Addr
   108  	// RemoteAddr returns the address of the peer.
   109  	RemoteAddr() net.Addr
   110  	// The context is cancelled when the connection is closed.
   111  	Context() context.Context
   112  	// Streamer is the interface for stream operations.
   113  	Streamer
   114  }
   115  
   116  type Streamer interface {
   117  	// AcceptStream returns the next stream opened by the peer, blocking until one is available.
   118  	// If the connection was closed due to a timeout, the error satisfies
   119  	// the net.Error interface, and Timeout() will be true.
   120  	AcceptStream(context.Context) (Stream, error)
   121  	// AcceptUniStream returns the next unidirectional stream opened by the peer, blocking until one is available.
   122  	// If the connection was closed due to a timeout, the error satisfies
   123  	// the net.Error interface, and Timeout() will be true.
   124  	AcceptUniStream(context.Context) (ReceiveStream, error)
   125  	// OpenStream opens a new bidirectional QUIC stream.
   126  	// There is no signaling to the peer about new streams:
   127  	// The peer can only accept the stream after data has been sent on the stream.
   128  	// If the error is non-nil, it satisfies the net.Error interface.
   129  	// When reaching the peer's stream limit, err.Temporary() will be true.
   130  	// If the connection was closed due to a timeout, Timeout() will be true.
   131  	OpenStream() (Stream, error)
   132  	// OpenStreamSync opens a new bidirectional QUIC stream.
   133  	// It blocks until a new stream can be opened.
   134  	// If the error is non-nil, it satisfies the net.Error interface.
   135  	// If the connection was closed due to a timeout, Timeout() will be true.
   136  	OpenStreamSync(context.Context) (Stream, error)
   137  	// OpenUniStream opens a new outgoing unidirectional QUIC stream.
   138  	// If the error is non-nil, it satisfies the net.Error interface.
   139  	// When reaching the peer's stream limit, Temporary() will be true.
   140  	// If the connection was closed due to a timeout, Timeout() will be true.
   141  	OpenUniStream() (SendStream, error)
   142  	// OpenUniStreamSync opens a new outgoing unidirectional QUIC stream.
   143  	// It blocks until a new stream can be opened.
   144  	// If the error is non-nil, it satisfies the net.Error interface.
   145  	// If the connection was closed due to a timeout, Timeout() will be true.
   146  	OpenUniStreamSync(context.Context) (SendStream, error)
   147  }
   148  
   149  type Stream interface {
   150  	ReceiveStream
   151  	SendStream
   152  }
   153  
   154  // ReceiveStream is the interface for receiving data on a stream.
   155  type ReceiveStream interface {
   156  	StreamID() int64
   157  	io.Reader
   158  
   159  	// CancelRead aborts receiving on this stream.
   160  	// It will ask the peer to stop transmitting stream data.
   161  	// Read will unblock immediately, and future Read calls will fail.
   162  	// When called multiple times or after reading the io.EOF it is a no-op.
   163  	CancelRead(err ApplicationError)
   164  
   165  	// SetReadDeadline sets the deadline for future Read calls and
   166  	// any currently-blocked Read call.
   167  	// A zero value for t means Read will not time out.
   168  	SetReadDeadline(t time.Time) error
   169  }
   170  
   171  // SendStream is the interface for sending data on a stream.
   172  type SendStream interface {
   173  	StreamID() int64
   174  	// Writer writes data to the stream.
   175  	// Write can be made to time out and return a net.Error with Timeout() == true
   176  	// after a fixed time limit; see SetDeadline and SetWriteDeadline.
   177  	// If the stream was canceled by the peer, the error implements the StreamError
   178  	// interface, and Canceled() == true.
   179  	// If the connection was closed due to a timeout, the error satisfies
   180  	// the net.Error interface, and Timeout() will be true.
   181  	io.Writer
   182  	// CancelWrite aborts sending on this stream.
   183  	// Data already written, but not yet delivered to the peer is not guaranteed to be delivered reliably.
   184  	// Write will unblock immediately, and future calls to Write will fail.
   185  	// When called multiple times or after closing the stream it is a no-op.
   186  	CancelWrite(err ApplicationError)
   187  	// Closer closes the write-direction of the stream.
   188  	// Future calls to Write are not permitted after calling Close.
   189  	// It must not be called concurrently with Write.
   190  	// It must not be called after calling CancelWrite.
   191  	io.Closer
   192  
   193  	// The Context is canceled as soon as the write-side of the stream is closed.
   194  	// This happens when Close() or CancelWrite() is called, or when the peer
   195  	// cancels the read-side of their stream.
   196  	Context() context.Context
   197  	// SetWriteDeadline sets the deadline for future Write calls
   198  	// and any currently-blocked Write call.
   199  	// Even if write times out, it may return n > 0, indicating that
   200  	// some data was successfully written.
   201  	// A zero value for t means Write will not time out.
   202  	SetWriteDeadline(t time.Time) error
   203  }
   204  
   205  type ApplicationError interface {
   206  	ErrCode() uint64
   207  	fmt.Stringer
   208  }