File
Blob: spec/transport/transport.go
| 1 | package transport |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/tls" |
| 6 | "crypto/x509" |
| 7 | "net" |
| 8 | "time" |
| 9 | |
| 10 | "go.miragespace.co/specter/spec/protocol" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | ConnectTimeout = time.Second * 10 |
| 15 | RTTMeasureInterval = time.Second * 3 |
| 16 | ) |
| 17 | |
| 18 | type StreamDelegate struct { |
| 19 | net.Conn // The backing bytes stream of the delegation |
| 20 | Certificate *x509.Certificate // The verified peer certificate, if any |
| 21 | Identity *protocol.Node // The identity that the peer claims to be. Implementation may use mTLS to verify |
| 22 | Kind protocol.Stream_Type // Type of the bytes stream of the delegation |
| 23 | } |
| 24 | |
| 25 | type DatagramDelegate struct { |
| 26 | Identity *protocol.Node |
| 27 | Buffer []byte |
| 28 | } |
| 29 | |
| 30 | type ConnectedPeer struct { |
| 31 | Identity *protocol.Node |
| 32 | Addr net.Addr |
| 33 | Version string |
| 34 | Physical PhysicalConn `json:"-"` |
| 35 | } |
| 36 | |
| 37 | type Transport interface { |
| 38 | Identity() *protocol.Node |
| 39 | |
| 40 | DialStream(ctx context.Context, peer *protocol.Node, kind protocol.Stream_Type) (net.Conn, error) |
| 41 | AcceptStream() <-chan *StreamDelegate |
| 42 | |
| 43 | ListConnected() []ConnectedPeer |
| 44 | |
| 45 | SupportDatagram() bool |
| 46 | ReceiveDatagram() <-chan *DatagramDelegate |
| 47 | SendDatagram(*protocol.Node, []byte) error |
| 48 | } |
| 49 | |
| 50 | type ClientTransport interface { |
| 51 | Transport |
| 52 | WithClientCertificate(cert tls.Certificate) error |
| 53 | } |
| 54 | |
| 55 | // PhysicalConn binds stream operations and lifetime to one physical connection. |
| 56 | // Implementations must be comparable so handles can be used as registry keys. |
| 57 | type PhysicalConn interface { |
| 58 | Done() <-chan struct{} |
| 59 | Err() error |
| 60 | OpenStream(kind protocol.Stream_Type) (net.Conn, error) |
| 61 | Close(reason string) error |
| 62 | } |
| 63 | |
| 64 | type PhysicalConnProvider interface { |
| 65 | PhysicalConn() PhysicalConn |
| 66 | } |