2025-01-19 11:09:37 -07:00
|
|
|
package hopp
|
|
|
|
|
|
|
|
import "io"
|
|
|
|
import "net"
|
|
|
|
import "context"
|
|
|
|
import "git.tebibyte.media/sashakoshka/hopp/tape"
|
|
|
|
|
|
|
|
// B implements METADAPT-B over a multiplexed stream-oriented transport such as
|
|
|
|
// QUIC.
|
|
|
|
type b struct {
|
|
|
|
underlying MultiConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// AdaptB returns a connection implementing METADAPT-B over a singular stream-
|
|
|
|
// oriented transport such as TCP or UNIX domain stream sockets.
|
|
|
|
func AdaptB(underlying MultiConn) Conn {
|
|
|
|
return &b {
|
|
|
|
underlying: underlying,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *b) Close() error {
|
|
|
|
return this.underlying.Close()
|
|
|
|
}
|
|
|
|
|
2025-01-22 14:57:06 -07:00
|
|
|
func (this *b) LocalAddr() net.Addr {
|
|
|
|
return this.underlying.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *b) RemoteAddr() net.Addr {
|
|
|
|
return this.underlying.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
2025-01-19 11:09:37 -07:00
|
|
|
func (this *b) OpenTrans() (Trans, error) {
|
|
|
|
stream, err := this.underlying.OpenStream()
|
|
|
|
if err != nil { return nil, err }
|
|
|
|
return transB { underlying: stream }, nil
|
|
|
|
}
|
|
|
|
|
2025-01-19 17:27:15 -07:00
|
|
|
func (this *b) AcceptTrans() (Trans, error) {
|
|
|
|
stream, err := this.underlying.AcceptStream(context.Background())
|
2025-01-19 11:09:37 -07:00
|
|
|
if err != nil { return nil, err }
|
|
|
|
return transB { underlying: stream }, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type transB struct {
|
|
|
|
underlying Stream
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trans transB) Close() error {
|
|
|
|
return trans.underlying.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trans transB) ID() int64 {
|
|
|
|
return trans.underlying.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trans transB) Send(method uint16, data []byte) error {
|
2025-01-19 13:54:13 -07:00
|
|
|
return encodeMessageB(trans.underlying, method, data)
|
2025-01-19 11:09:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (trans transB) Receive() (uint16, []byte, error) {
|
2025-01-19 13:54:13 -07:00
|
|
|
return decodeMessageB(trans.underlying)
|
2025-01-19 11:09:37 -07:00
|
|
|
}
|
|
|
|
|
2025-01-22 14:57:06 -07:00
|
|
|
// MultiConn represens a multiplexed stream-oriented transport for use in
|
|
|
|
// [AdaptB].
|
2025-01-19 11:09:37 -07:00
|
|
|
type MultiConn interface {
|
|
|
|
// See documentation for [net.Conn].
|
|
|
|
io.Closer
|
|
|
|
LocalAddr() net.Addr
|
|
|
|
RemoteAddr() net.Addr
|
|
|
|
// AcceptStream accepts the next incoming stream from the other party.
|
|
|
|
AcceptStream(context.Context) (Stream, error)
|
|
|
|
// OpenStream opens a new stream.
|
|
|
|
OpenStream() (Stream, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream represents a single stream returned by a [MultiConn].
|
|
|
|
type Stream interface {
|
|
|
|
// See documentation for [net.Conn].
|
|
|
|
io.ReadWriteCloser
|
|
|
|
// ID returns the stream ID
|
|
|
|
ID() int64
|
|
|
|
}
|
2025-01-19 13:54:13 -07:00
|
|
|
|
|
|
|
func encodeMessageB(writer io.Writer, method uint16, data []byte) error {
|
|
|
|
buffer := make([]byte, 4 + len(data))
|
|
|
|
tape.EncodeI16(buffer[:2], method)
|
|
|
|
length, ok := tape.U16CastSafe(len(data))
|
|
|
|
if !ok { return ErrPayloadTooLarge }
|
2025-01-19 14:36:01 -07:00
|
|
|
tape.EncodeI16(buffer[2:4], length)
|
2025-01-19 13:54:13 -07:00
|
|
|
copy(buffer[4:], data)
|
|
|
|
_, err := writer.Write(buffer)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeMessageB(reader io.Reader) (uint16, []byte, error) {
|
|
|
|
headerBuffer := [4]byte { }
|
|
|
|
_, err := io.ReadFull(reader, headerBuffer[:])
|
|
|
|
if err != nil { return 0, nil, err }
|
|
|
|
method, err := tape.DecodeI16[uint16](headerBuffer[:2])
|
|
|
|
if err != nil { return 0, nil, err }
|
|
|
|
length, err := tape.DecodeI16[uint16](headerBuffer[2:4])
|
|
|
|
if err != nil { return 0, nil, err }
|
|
|
|
payloadBuffer := make([]byte, int(length))
|
|
|
|
_, err = io.ReadFull(reader, payloadBuffer)
|
|
|
|
if err != nil { return 0, nil, err }
|
|
|
|
return method, payloadBuffer, nil
|
|
|
|
}
|