forked from quic-go/quic-go
add a Write method for Streams
This commit is contained in:
19
stream.go
19
stream.go
@@ -8,15 +8,18 @@ import (
|
||||
|
||||
// A Stream assembles the data from StreamFrames and provides a super-convenient Read-Interface
|
||||
type Stream struct {
|
||||
Session *Session
|
||||
StreamID protocol.StreamID
|
||||
StreamFrames chan *frames.StreamFrame
|
||||
CurrentFrame *frames.StreamFrame
|
||||
ReadPosInFrame int
|
||||
WriteOffset uint64
|
||||
}
|
||||
|
||||
// NewStream creates a new Stream
|
||||
func NewStream(StreamID protocol.StreamID) *Stream {
|
||||
func NewStream(session *Session, StreamID protocol.StreamID) *Stream {
|
||||
return &Stream{
|
||||
Session: session,
|
||||
StreamID: StreamID,
|
||||
StreamFrames: make(chan *frames.StreamFrame, 8), // ToDo: add config option for this number
|
||||
}
|
||||
@@ -50,6 +53,20 @@ func (s *Stream) Read(p []byte) (int, error) {
|
||||
return bytesRead, nil
|
||||
}
|
||||
|
||||
func (s *Stream) Write(p []byte) (int, error) {
|
||||
frame := &frames.StreamFrame{
|
||||
StreamID: s.StreamID,
|
||||
Offset: s.WriteOffset,
|
||||
Data: p,
|
||||
}
|
||||
err := s.Session.SendFrames([]frames.Frame{frame})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
s.WriteOffset += uint64(len(p))
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// AddStreamFrame adds a new stream frame
|
||||
func (s *Stream) AddStreamFrame(frame *frames.StreamFrame) error {
|
||||
s.StreamFrames <- frame
|
||||
|
||||
Reference in New Issue
Block a user