1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-09 17:45:03 +09:00
any-sync/net/transport/transport.go
2023-05-31 20:24:07 +02:00

44 lines
1.3 KiB
Go

//go:generate mockgen -destination mock_transport/mock_transport.go github.com/anyproto/any-sync/net/transport Transport,MultiConn
package transport
import (
"context"
"errors"
"net"
"time"
)
var (
ErrConnClosed = errors.New("connection closed")
)
// Transport is a common interface for a network transport
type Transport interface {
// SetAccepter sets accepter that will be called for new connections
// this method should be called before app start
SetAccepter(accepter Accepter)
// Dial creates a new connection by given address
Dial(ctx context.Context, addr string) (mc MultiConn, err error)
}
// MultiConn is an object of multiplexing connection containing handshake info
type MultiConn interface {
// Context returns the connection context that contains handshake details
Context() context.Context
// Accept accepts new sub connections
Accept() (conn net.Conn, err error)
// Open opens new sub connection
Open(ctx context.Context) (conn net.Conn, err error)
// LastUsage returns the time of the last connection activity
LastUsage() time.Time
// Addr returns remote peer address
Addr() string
// IsClosed returns true when connection is closed
IsClosed() bool
// Close closes the connection and all sub connections
Close() error
}
type Accepter interface {
Accept(mc MultiConn) (err error)
}