Skip to content
File

Blob: spec/transport/transport.go

go75 lines
1package transport
2 
3import (
4 "context"
5 "crypto/tls"
6 "crypto/x509"
7 "net"
8 "time"
9 
10 "go.miragespace.co/specter/spec/protocol"
11)
12 
13const (
14 ConnectTimeout = time.Second * 10
15 RTTMeasureInterval = time.Second * 3
16)
17 
18type 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// CloseGracefully preserves completed writes when the backing connection supports it.
26func (s *StreamDelegate) CloseGracefully() error {
27 if conn, ok := s.Conn.(interface{ CloseGracefully() error }); ok {
28 return conn.CloseGracefully()
29 }
30 return s.Conn.Close()
31}
32 
33type DatagramDelegate struct {
34 Identity *protocol.Node
35 Buffer []byte
36}
37 
38type ConnectedPeer struct {
39 Identity *protocol.Node
40 Addr net.Addr
41 Version string
42 Physical PhysicalConn `json:"-"`
43}
44 
45type Transport interface {
46 Identity() *protocol.Node
47 
48 DialStream(ctx context.Context, peer *protocol.Node, kind protocol.Stream_Type) (net.Conn, error)
49 AcceptStream() <-chan *StreamDelegate
50 
51 ListConnected() []ConnectedPeer
52 
53 SupportDatagram() bool
54 ReceiveDatagram() <-chan *DatagramDelegate
55 SendDatagram(*protocol.Node, []byte) error
56}
57 
58type ClientTransport interface {
59 Transport
60 WithClientCertificate(cert tls.Certificate) error
61}
62 
63// PhysicalConn binds stream operations and lifetime to one physical connection.
64// Implementations must be comparable so handles can be used as registry keys.
65type PhysicalConn interface {
66 Done() <-chan struct{}
67 Err() error
68 OpenStream(kind protocol.Stream_Type) (net.Conn, error)
69 Close(reason string) error
70}
71 
72type PhysicalConnProvider interface {
73 PhysicalConn() PhysicalConn
74}