After the week I've had, I deserve to make a commit like this lmao
This commit is contained in:
93
metadaptb.go
Normal file
93
metadaptb.go
Normal file
@@ -0,0 +1,93 @@
|
||||
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()
|
||||
}
|
||||
|
||||
func (this *b) OpenTrans() (Trans, error) {
|
||||
stream, err := this.underlying.OpenStream()
|
||||
if err != nil { return nil, err }
|
||||
return transB { underlying: stream }, nil
|
||||
}
|
||||
|
||||
func (this *b) AcceptTrans(ctx context.Context) (Trans, error) {
|
||||
stream, err := this.underlying.AcceptStream(ctx)
|
||||
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 {
|
||||
buffer := make([]byte, 4 + len(data))
|
||||
tape.EncodeI16(buffer[:2], method)
|
||||
length, ok := tape.U16CastSafe(len(data))
|
||||
if !ok { return ErrPayloadTooLarge }
|
||||
tape.EncodeI16(data[2:4], length)
|
||||
copy(buffer[4:], data)
|
||||
_, err := trans.underlying.Write(buffer)
|
||||
return err
|
||||
}
|
||||
|
||||
func (trans transB) Receive() (uint16, []byte, error) {
|
||||
headerBuffer := [4]byte { }
|
||||
_, err := io.ReadFull(trans.underlying, 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(trans.underlying, payloadBuffer)
|
||||
if err != nil { return 0, nil, err }
|
||||
return method, payloadBuffer, nil
|
||||
}
|
||||
|
||||
// MultiConn represens a multiplexed stream-oriented transport for use in [B].
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user