forked from quic-go/webtransport-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.go
More file actions
78 lines (62 loc) · 1.62 KB
/
stream.go
File metadata and controls
78 lines (62 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package webtransport
import (
"errors"
"fmt"
"io"
"time"
"github.com/lucas-clemente/quic-go"
)
type Stream interface {
io.Reader
io.Writer
io.Closer
CancelRead(ErrorCode)
CancelWrite(ErrorCode)
SetDeadline(time.Time) error
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
}
type stream struct {
str quic.Stream
}
var _ Stream = &stream{}
func (s *stream) maybeConvertStreamError(err error) error {
if err == nil {
return nil
}
var streamErr *quic.StreamError
if errors.As(err, &streamErr) {
errorCode, cerr := httpCodeToWebtransportCode(streamErr.ErrorCode)
if cerr != nil {
return fmt.Errorf("stream reset, but failed to convert stream error %d: %w", streamErr.ErrorCode, cerr)
}
return &StreamError{ErrorCode: errorCode}
}
return err
}
func (s *stream) Read(b []byte) (int, error) {
n, err := s.str.Read(b)
return n, s.maybeConvertStreamError(err)
}
func (s *stream) Write(b []byte) (int, error) {
n, err := s.str.Write(b)
return n, s.maybeConvertStreamError(err)
}
func (s *stream) CancelRead(e ErrorCode) {
s.str.CancelRead(webtransportCodeToHTTPCode(e))
}
func (s *stream) CancelWrite(e ErrorCode) {
s.str.CancelWrite(webtransportCodeToHTTPCode(e))
}
func (s *stream) Close() error {
return s.maybeConvertStreamError(s.str.Close())
}
func (s *stream) SetDeadline(t time.Time) error {
return s.maybeConvertStreamError(s.str.SetDeadline(t))
}
func (s *stream) SetReadDeadline(t time.Time) error {
return s.maybeConvertStreamError(s.str.SetReadDeadline(t))
}
func (s *stream) SetWriteDeadline(t time.Time) error {
return s.maybeConvertStreamError(s.str.SetWriteDeadline(t))
}