Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for datagrams #99

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions mock_stream_creator_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package webtransport

import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"math/rand"
"net"
Expand Down Expand Up @@ -31,6 +33,11 @@
queue []T
}

type receiveMessageResult struct {
msg []byte
err error
}

func newAcceptQueue[T any]() *acceptQueue[T] {
return &acceptQueue[T]{c: make(chan struct{}, 1)}
}
Expand Down Expand Up @@ -272,6 +279,43 @@
}
}

func (s *Session) ReceiveMessage(ctx context.Context) ([]byte, error) {
resultChannel := make(chan receiveMessageResult)
go func() {
msg, err := s.qconn.ReceiveMessage(ctx)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / interop

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-check / All

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-check / All

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-check / All

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-check / All

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)

Check failure on line 285 in session.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

s.qconn.ReceiveMessage undefined (type http3.StreamCreator has no field or method ReceiveMessage)
resultChannel <- receiveMessageResult{msg: msg, err: err}
}()

select {
case result := <-resultChannel:
if result.err != nil {
return nil, result.err
}

datastream := bytes.NewReader(result.msg)
quarterStreamId, err := quicvarint.Read(datastream)
if err != nil {
return nil, err
}

return result.msg[quicvarint.Len(quarterStreamId):], nil
case <-ctx.Done():
return nil, fmt.Errorf("WebTransport stream closed")
}
}

// SendMessage sends a datagram over a WebTransport session.
// Note that datagrams are unreliable - depending on network conditions, datagrams sent by the server may never be
// received by the client.
func (s *Session) SendMessage(msg []byte) error {
buf := &bytes.Buffer{}

// "Quarter Stream ID" (!) of associated request stream, as per https://datatracker.ietf.org/doc/html/draft-ietf-masque-h3-datagram
buf.Write(quicvarint.Append(nil, uint64(s.requestStr.StreamID()/4)))
buf.Write(msg)
return s.qconn.SendMessage(buf.Bytes())

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / interop

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-check / All

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-check / All

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-check / All

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage) (compile)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-check / All

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage) (compile)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage)

Check failure on line 316 in session.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

s.qconn.SendMessage undefined (type http3.StreamCreator has no field or method SendMessage)
}

func (s *Session) OpenStream() (Stream, error) {
s.closeMx.Lock()
defer s.closeMx.Unlock()
Expand Down
Loading