-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for web transports
- Loading branch information
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package signalr | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/quic-go/webtransport-go" | ||
"sync" | ||
) | ||
|
||
type webTransportsConnection struct { | ||
ConnectionBase | ||
session *webtransport.Session | ||
|
||
// current stream guarded by mutex | ||
stream webtransport.Stream | ||
sync.Mutex | ||
} | ||
|
||
func newWebTransportsConnection(ctx context.Context, connectionID string, session *webtransport.Session) *webTransportsConnection { | ||
w := &webTransportsConnection{ | ||
session: session, | ||
ConnectionBase: *NewConnectionBase(ctx, connectionID), | ||
} | ||
return w | ||
} | ||
|
||
func (w *webTransportsConnection) Write(p []byte) (n int, err error) { | ||
err = w.syncStream() | ||
if err != nil { | ||
return 0, err | ||
} | ||
n, err = ReadWriteWithContext(w.Context(), | ||
func() (int, error) { | ||
return w.stream.Write(p) | ||
}, | ||
func() {}) | ||
if err != nil { | ||
err = fmt.Errorf("%T: %w", w, err) | ||
_ = w.stream.Close() | ||
} | ||
return n, err | ||
} | ||
|
||
func (w *webTransportsConnection) Read(p []byte) (n int, err error) { | ||
err = w.syncStream() | ||
if err != nil { | ||
return 0, err | ||
} | ||
n, err = ReadWriteWithContext(w.Context(), | ||
func() (int, error) { | ||
return w.stream.Read(p) | ||
}, | ||
func() {}) | ||
if err != nil { | ||
err = fmt.Errorf("%T: %w", w, err) | ||
_ = w.stream.Close() | ||
} | ||
return n, err | ||
} | ||
|
||
func (w *webTransportsConnection) syncStream() (err error) { | ||
w.Lock() | ||
defer w.Unlock() | ||
if w.stream == nil { | ||
w.stream, err = w.session.OpenStream() | ||
} | ||
return | ||
} |