2018-07-04 10:51:47 +00:00
|
|
|
package yamux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2021-10-19 13:43:41 +00:00
|
|
|
"math"
|
2018-07-04 10:51:47 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type streamState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
streamInit streamState = iota
|
|
|
|
streamSYNSent
|
|
|
|
streamSYNReceived
|
|
|
|
streamEstablished
|
2021-06-16 20:19:45 +00:00
|
|
|
streamFinished
|
|
|
|
)
|
|
|
|
|
|
|
|
type halfStreamState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
halfOpen halfStreamState = iota
|
|
|
|
halfClosed
|
|
|
|
halfReset
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Stream is used to represent a logical stream
|
|
|
|
// within a session.
|
|
|
|
type Stream struct {
|
|
|
|
sendWindow uint32
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
memorySpan MemoryManager
|
2022-08-19 16:34:07 +00:00
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
id uint32
|
|
|
|
session *Session
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
recvWindow uint32
|
|
|
|
epochStart time.Time
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
state streamState
|
|
|
|
writeState, readState halfStreamState
|
|
|
|
stateLock sync.Mutex
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
recvBuf segmentedBuffer
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
recvNotifyCh chan struct{}
|
|
|
|
sendNotifyCh chan struct{}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
readDeadline, writeDeadline pipeDeadline
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// newStream is used to construct a new stream within a given session for an ID.
|
|
|
|
// It assumes that a memory allocation has been obtained for the initialWindow.
|
2022-11-04 13:57:20 +00:00
|
|
|
func newStream(session *Session, id uint32, state streamState, initialWindow uint32, memorySpan MemoryManager) *Stream {
|
2018-07-04 10:51:47 +00:00
|
|
|
s := &Stream{
|
2019-06-09 07:24:20 +00:00
|
|
|
id: id,
|
|
|
|
session: session,
|
|
|
|
state: state,
|
|
|
|
sendWindow: initialStreamWindow,
|
|
|
|
readDeadline: makePipeDeadline(),
|
|
|
|
writeDeadline: makePipeDeadline(),
|
2022-11-04 13:57:20 +00:00
|
|
|
memorySpan: memorySpan,
|
2021-10-19 13:43:41 +00:00
|
|
|
// Initialize the recvBuf with initialStreamWindow, not config.InitialStreamWindowSize.
|
|
|
|
// The peer isn't allowed to send more data than initialStreamWindow until we've sent
|
|
|
|
// the first window update (which will grant it up to config.InitialStreamWindowSize).
|
2022-04-01 16:16:46 +00:00
|
|
|
recvBuf: newSegmentedBuffer(initialWindow),
|
2021-10-19 13:43:41 +00:00
|
|
|
recvWindow: session.config.InitialStreamWindowSize,
|
|
|
|
epochStart: time.Now(),
|
|
|
|
recvNotifyCh: make(chan struct{}, 1),
|
|
|
|
sendNotifyCh: make(chan struct{}, 1),
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Session returns the associated stream session
|
|
|
|
func (s *Stream) Session() *Session {
|
|
|
|
return s.session
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamID returns the ID of this stream
|
|
|
|
func (s *Stream) StreamID() uint32 {
|
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read is used to read from the stream
|
|
|
|
func (s *Stream) Read(b []byte) (n int, err error) {
|
|
|
|
START:
|
|
|
|
s.stateLock.Lock()
|
2021-06-16 20:19:45 +00:00
|
|
|
state := s.readState
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
|
|
|
|
switch state {
|
|
|
|
case halfOpen:
|
|
|
|
// Open -> read
|
|
|
|
case halfClosed:
|
|
|
|
empty := s.recvBuf.Len() == 0
|
|
|
|
if empty {
|
2018-07-04 10:51:47 +00:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
// Closed, but we have data pending -> read.
|
|
|
|
case halfReset:
|
|
|
|
return 0, ErrStreamReset
|
|
|
|
default:
|
|
|
|
panic("unknown state")
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no data available, block
|
2019-06-09 07:24:20 +00:00
|
|
|
if s.recvBuf.Len() == 0 {
|
2021-10-19 13:43:41 +00:00
|
|
|
select {
|
|
|
|
case <-s.recvNotifyCh:
|
|
|
|
goto START
|
|
|
|
case <-s.readDeadline.wait():
|
|
|
|
return 0, ErrTimeout
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read any bytes
|
|
|
|
n, _ = s.recvBuf.Read(b)
|
|
|
|
|
|
|
|
// Send a window update potentially
|
|
|
|
err = s.sendWindowUpdate()
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write is used to write to the stream
|
2021-10-19 13:43:41 +00:00
|
|
|
func (s *Stream) Write(b []byte) (int, error) {
|
|
|
|
var total int
|
2018-07-04 10:51:47 +00:00
|
|
|
for total < len(b) {
|
|
|
|
n, err := s.write(b[total:])
|
|
|
|
total += n
|
|
|
|
if err != nil {
|
|
|
|
return total, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// write is used to write to the stream, may return on
|
|
|
|
// a short write.
|
|
|
|
func (s *Stream) write(b []byte) (n int, err error) {
|
|
|
|
var flags uint16
|
|
|
|
var max uint32
|
2019-06-09 07:24:20 +00:00
|
|
|
var hdr header
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
START:
|
|
|
|
s.stateLock.Lock()
|
2021-06-16 20:19:45 +00:00
|
|
|
state := s.writeState
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
|
|
|
|
switch state {
|
|
|
|
case halfOpen:
|
|
|
|
// Open for writing -> write
|
|
|
|
case halfClosed:
|
2018-07-04 10:51:47 +00:00
|
|
|
return 0, ErrStreamClosed
|
2021-06-16 20:19:45 +00:00
|
|
|
case halfReset:
|
|
|
|
return 0, ErrStreamReset
|
|
|
|
default:
|
|
|
|
panic("unknown state")
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no data available, block
|
|
|
|
window := atomic.LoadUint32(&s.sendWindow)
|
|
|
|
if window == 0 {
|
2021-10-19 13:43:41 +00:00
|
|
|
select {
|
|
|
|
case <-s.sendNotifyCh:
|
|
|
|
goto START
|
|
|
|
case <-s.writeDeadline.wait():
|
|
|
|
return 0, ErrTimeout
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the flags if any
|
|
|
|
flags = s.sendFlags()
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// Send up to min(message, window
|
|
|
|
max = min(window, s.session.config.MaxMessageSize-headerSize, uint32(len(b)))
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
// Send the header
|
2019-06-09 07:24:20 +00:00
|
|
|
hdr = encode(typeData, flags, s.id, max)
|
|
|
|
if err = s.session.sendMsg(hdr, b[:max], s.writeDeadline.wait()); err != nil {
|
2018-07-04 10:51:47 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce our send window
|
|
|
|
atomic.AddUint32(&s.sendWindow, ^uint32(max-1))
|
|
|
|
|
|
|
|
// Unlock
|
|
|
|
return int(max), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendFlags determines any flags that are appropriate
|
|
|
|
// based on the current stream state
|
|
|
|
func (s *Stream) sendFlags() uint16 {
|
|
|
|
s.stateLock.Lock()
|
|
|
|
defer s.stateLock.Unlock()
|
|
|
|
var flags uint16
|
|
|
|
switch s.state {
|
|
|
|
case streamInit:
|
|
|
|
flags |= flagSYN
|
|
|
|
s.state = streamSYNSent
|
|
|
|
case streamSYNReceived:
|
|
|
|
flags |= flagACK
|
|
|
|
s.state = streamEstablished
|
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendWindowUpdate potentially sends a window update enabling
|
|
|
|
// further writes to take place. Must be invoked with the lock.
|
|
|
|
func (s *Stream) sendWindowUpdate() error {
|
|
|
|
// Determine the flags if any
|
|
|
|
flags := s.sendFlags()
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
// Update the receive window.
|
|
|
|
needed, delta := s.recvBuf.GrowTo(s.recvWindow, flags != 0)
|
2021-06-16 20:19:45 +00:00
|
|
|
if !needed {
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
now := time.Now()
|
|
|
|
if rtt := s.session.getRTT(); flags == 0 && rtt > 0 && now.Sub(s.epochStart) < rtt*4 {
|
|
|
|
var recvWindow uint32
|
|
|
|
if s.recvWindow > math.MaxUint32/2 {
|
|
|
|
recvWindow = min(math.MaxUint32, s.session.config.MaxStreamWindowSize)
|
|
|
|
} else {
|
|
|
|
recvWindow = min(s.recvWindow*2, s.session.config.MaxStreamWindowSize)
|
|
|
|
}
|
|
|
|
if recvWindow > s.recvWindow {
|
2022-04-01 16:16:46 +00:00
|
|
|
grow := recvWindow - s.recvWindow
|
2022-11-04 13:57:20 +00:00
|
|
|
if err := s.memorySpan.ReserveMemory(int(grow), 128); err == nil {
|
2022-04-01 16:16:46 +00:00
|
|
|
s.recvWindow = recvWindow
|
|
|
|
_, delta = s.recvBuf.GrowTo(s.recvWindow, true)
|
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
s.epochStart = now
|
|
|
|
hdr := encode(typeWindowUpdate, flags, s.id, delta)
|
|
|
|
return s.session.sendMsg(hdr, nil, nil)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendClose is used to send a FIN
|
|
|
|
func (s *Stream) sendClose() error {
|
|
|
|
flags := s.sendFlags()
|
|
|
|
flags |= flagFIN
|
2019-06-09 07:24:20 +00:00
|
|
|
hdr := encode(typeWindowUpdate, flags, s.id, 0)
|
|
|
|
return s.session.sendMsg(hdr, nil, nil)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendReset is used to send a RST
|
|
|
|
func (s *Stream) sendReset() error {
|
2019-06-09 07:24:20 +00:00
|
|
|
hdr := encode(typeWindowUpdate, flagRST, s.id, 0)
|
|
|
|
return s.session.sendMsg(hdr, nil, nil)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets the stream (forcibly closes the stream)
|
|
|
|
func (s *Stream) Reset() error {
|
2021-06-16 20:19:45 +00:00
|
|
|
sendReset := false
|
2018-07-04 10:51:47 +00:00
|
|
|
s.stateLock.Lock()
|
|
|
|
switch s.state {
|
2021-06-16 20:19:45 +00:00
|
|
|
case streamFinished:
|
2018-07-04 10:51:47 +00:00
|
|
|
s.stateLock.Unlock()
|
|
|
|
return nil
|
2021-06-16 20:19:45 +00:00
|
|
|
case streamInit:
|
|
|
|
// we haven't sent anything, so we don't need to send a reset.
|
2018-07-04 10:51:47 +00:00
|
|
|
case streamSYNSent, streamSYNReceived, streamEstablished:
|
2021-06-16 20:19:45 +00:00
|
|
|
sendReset = true
|
2018-07-04 10:51:47 +00:00
|
|
|
default:
|
|
|
|
panic("unhandled state")
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// at least one direction is open, we need to reset.
|
|
|
|
|
|
|
|
// If we've already sent/received an EOF, no need to reset that side.
|
|
|
|
if s.writeState == halfOpen {
|
|
|
|
s.writeState = halfReset
|
|
|
|
}
|
|
|
|
if s.readState == halfOpen {
|
|
|
|
s.readState = halfReset
|
|
|
|
}
|
|
|
|
s.state = streamFinished
|
2018-07-04 10:51:47 +00:00
|
|
|
s.notifyWaiting()
|
2021-06-16 20:19:45 +00:00
|
|
|
s.stateLock.Unlock()
|
|
|
|
if sendReset {
|
|
|
|
_ = s.sendReset()
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
s.cleanup()
|
2021-06-16 20:19:45 +00:00
|
|
|
return nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// CloseWrite is used to close the stream for writing.
|
|
|
|
func (s *Stream) CloseWrite() error {
|
2018-07-04 10:51:47 +00:00
|
|
|
s.stateLock.Lock()
|
2021-06-16 20:19:45 +00:00
|
|
|
switch s.writeState {
|
|
|
|
case halfOpen:
|
|
|
|
// Open for writing -> close write
|
|
|
|
case halfClosed:
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
return nil
|
|
|
|
case halfReset:
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
return ErrStreamReset
|
|
|
|
default:
|
|
|
|
panic("invalid state")
|
|
|
|
}
|
|
|
|
s.writeState = halfClosed
|
|
|
|
cleanup := s.readState != halfOpen
|
|
|
|
if cleanup {
|
|
|
|
s.state = streamFinished
|
|
|
|
}
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
s.notifyWaiting()
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
err := s.sendClose()
|
|
|
|
if cleanup {
|
|
|
|
// we're fully closed, might as well be nice to the user and
|
|
|
|
// free everything early.
|
|
|
|
s.cleanup()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// CloseRead is used to close the stream for writing.
|
|
|
|
func (s *Stream) CloseRead() error {
|
|
|
|
cleanup := false
|
|
|
|
s.stateLock.Lock()
|
|
|
|
switch s.readState {
|
|
|
|
case halfOpen:
|
|
|
|
// Open for reading -> close read
|
|
|
|
case halfClosed, halfReset:
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
return nil
|
2018-07-04 10:51:47 +00:00
|
|
|
default:
|
2021-06-16 20:19:45 +00:00
|
|
|
panic("invalid state")
|
|
|
|
}
|
|
|
|
s.readState = halfReset
|
|
|
|
cleanup = s.writeState != halfOpen
|
|
|
|
if cleanup {
|
|
|
|
s.state = streamFinished
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
s.notifyWaiting()
|
2021-06-16 20:19:45 +00:00
|
|
|
if cleanup {
|
|
|
|
// we're fully closed, might as well be nice to the user and
|
|
|
|
// free everything early.
|
2019-06-09 07:24:20 +00:00
|
|
|
s.cleanup()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to close the stream.
|
|
|
|
func (s *Stream) Close() error {
|
|
|
|
_ = s.CloseRead() // can't fail.
|
|
|
|
return s.CloseWrite()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// forceClose is used for when the session is exiting
|
|
|
|
func (s *Stream) forceClose() {
|
|
|
|
s.stateLock.Lock()
|
2021-06-16 20:19:45 +00:00
|
|
|
if s.readState == halfOpen {
|
|
|
|
s.readState = halfReset
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
if s.writeState == halfOpen {
|
|
|
|
s.writeState = halfReset
|
|
|
|
}
|
|
|
|
s.state = streamFinished
|
2018-07-04 10:51:47 +00:00
|
|
|
s.notifyWaiting()
|
2021-06-16 20:19:45 +00:00
|
|
|
s.stateLock.Unlock()
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
s.readDeadline.set(time.Time{})
|
2021-06-16 20:19:45 +00:00
|
|
|
s.writeDeadline.set(time.Time{})
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called when fully closed to release any system resources.
|
|
|
|
func (s *Stream) cleanup() {
|
|
|
|
s.session.closeStream(s.id)
|
|
|
|
s.readDeadline.set(time.Time{})
|
2021-06-16 20:19:45 +00:00
|
|
|
s.writeDeadline.set(time.Time{})
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// processFlags is used to update the state of the stream
|
|
|
|
// based on set flags, if any. Lock must be held
|
2021-10-19 13:43:41 +00:00
|
|
|
func (s *Stream) processFlags(flags uint16) {
|
2018-07-04 10:51:47 +00:00
|
|
|
// Close the stream without holding the state lock
|
2022-11-04 13:57:20 +00:00
|
|
|
var closeStream bool
|
2018-07-04 10:51:47 +00:00
|
|
|
defer func() {
|
|
|
|
if closeStream {
|
2019-06-09 07:24:20 +00:00
|
|
|
s.cleanup()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if flags&flagACK == flagACK {
|
2022-11-04 13:57:20 +00:00
|
|
|
s.stateLock.Lock()
|
2018-07-04 10:51:47 +00:00
|
|
|
if s.state == streamSYNSent {
|
|
|
|
s.state = streamEstablished
|
|
|
|
}
|
2022-11-04 13:57:20 +00:00
|
|
|
s.stateLock.Unlock()
|
2018-07-04 10:51:47 +00:00
|
|
|
s.session.establishStream(s.id)
|
|
|
|
}
|
|
|
|
if flags&flagFIN == flagFIN {
|
2022-11-04 13:57:20 +00:00
|
|
|
var notify bool
|
|
|
|
s.stateLock.Lock()
|
2021-06-16 20:19:45 +00:00
|
|
|
if s.readState == halfOpen {
|
|
|
|
s.readState = halfClosed
|
|
|
|
if s.writeState != halfOpen {
|
|
|
|
// We're now fully closed.
|
|
|
|
closeStream = true
|
|
|
|
s.state = streamFinished
|
|
|
|
}
|
2022-11-04 13:57:20 +00:00
|
|
|
notify = true
|
|
|
|
}
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
if notify {
|
2018-07-04 10:51:47 +00:00
|
|
|
s.notifyWaiting()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if flags&flagRST == flagRST {
|
2022-11-04 13:57:20 +00:00
|
|
|
s.stateLock.Lock()
|
2021-06-16 20:19:45 +00:00
|
|
|
if s.readState == halfOpen {
|
|
|
|
s.readState = halfReset
|
|
|
|
}
|
|
|
|
if s.writeState == halfOpen {
|
|
|
|
s.writeState = halfReset
|
|
|
|
}
|
|
|
|
s.state = streamFinished
|
2022-11-04 13:57:20 +00:00
|
|
|
s.stateLock.Unlock()
|
2018-07-04 10:51:47 +00:00
|
|
|
closeStream = true
|
|
|
|
s.notifyWaiting()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notifyWaiting notifies all the waiting channels
|
|
|
|
func (s *Stream) notifyWaiting() {
|
|
|
|
asyncNotify(s.recvNotifyCh)
|
|
|
|
asyncNotify(s.sendNotifyCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// incrSendWindow updates the size of our send window
|
2021-10-19 13:43:41 +00:00
|
|
|
func (s *Stream) incrSendWindow(hdr header, flags uint16) {
|
|
|
|
s.processFlags(flags)
|
2018-07-04 10:51:47 +00:00
|
|
|
// Increase window, unblock a sender
|
|
|
|
atomic.AddUint32(&s.sendWindow, hdr.Length())
|
|
|
|
asyncNotify(s.sendNotifyCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// readData is used to handle a data frame
|
|
|
|
func (s *Stream) readData(hdr header, flags uint16, conn io.Reader) error {
|
2021-10-19 13:43:41 +00:00
|
|
|
s.processFlags(flags)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
// Check that our recv window is not exceeded
|
|
|
|
length := hdr.Length()
|
|
|
|
if length == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// Copy into buffer
|
2021-10-19 13:43:41 +00:00
|
|
|
if err := s.recvBuf.Append(conn, length); err != nil {
|
|
|
|
s.session.logger.Printf("[ERR] yamux: Failed to read stream data on stream %d: %v", s.id, err)
|
2018-07-04 10:51:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
// Unblock the reader
|
2018-07-04 10:51:47 +00:00
|
|
|
asyncNotify(s.recvNotifyCh)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDeadline sets the read and write deadlines
|
|
|
|
func (s *Stream) SetDeadline(t time.Time) error {
|
|
|
|
if err := s.SetReadDeadline(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.SetWriteDeadline(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadDeadline sets the deadline for future Read calls.
|
|
|
|
func (s *Stream) SetReadDeadline(t time.Time) error {
|
2019-06-09 07:24:20 +00:00
|
|
|
s.stateLock.Lock()
|
|
|
|
defer s.stateLock.Unlock()
|
2021-06-16 20:19:45 +00:00
|
|
|
if s.readState == halfOpen {
|
|
|
|
s.readDeadline.set(t)
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteDeadline sets the deadline for future Write calls
|
|
|
|
func (s *Stream) SetWriteDeadline(t time.Time) error {
|
2019-06-09 07:24:20 +00:00
|
|
|
s.stateLock.Lock()
|
|
|
|
defer s.stateLock.Unlock()
|
2021-06-16 20:19:45 +00:00
|
|
|
if s.writeState == halfOpen {
|
|
|
|
s.writeDeadline.set(t)
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
return nil
|
|
|
|
}
|