2018-07-04 10:51:47 +00:00
|
|
|
package multiplex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-09 07:24:20 +00:00
|
|
|
"errors"
|
2018-07-04 10:51:47 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/mux"
|
|
|
|
|
|
|
|
pool "github.com/libp2p/go-buffer-pool"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// streamID is a convenience type for operating on stream IDs
|
|
|
|
type streamID struct {
|
|
|
|
id uint64
|
|
|
|
initiator bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// header computes the header for the given tag
|
|
|
|
func (id *streamID) header(tag uint64) uint64 {
|
|
|
|
header := id.id<<3 | tag
|
|
|
|
if !id.initiator {
|
|
|
|
header--
|
|
|
|
}
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
|
|
|
type Stream struct {
|
|
|
|
id streamID
|
|
|
|
name string
|
|
|
|
dataIn chan []byte
|
|
|
|
mp *Multiplex
|
|
|
|
|
|
|
|
extra []byte
|
|
|
|
|
|
|
|
// exbuf is for holding the reference to the beginning of the extra slice
|
|
|
|
// for later memory pool freeing
|
|
|
|
exbuf []byte
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
rDeadline, wDeadline pipeDeadline
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
clLock sync.Mutex
|
|
|
|
closedRemote bool
|
|
|
|
|
|
|
|
// Closed when the connection is reset.
|
|
|
|
reset chan struct{}
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
// Closed when the writer is closed (reset will also be closed)
|
|
|
|
closedLocal context.Context
|
|
|
|
doCloseLocal context.CancelFunc
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) Name() string {
|
|
|
|
return s.name
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// tries to preload pending data
|
|
|
|
func (s *Stream) preloadData() {
|
|
|
|
select {
|
|
|
|
case read, ok := <-s.dataIn:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.extra = read
|
|
|
|
s.exbuf = read
|
|
|
|
default:
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func (s *Stream) waitForData() error {
|
2018-07-04 10:51:47 +00:00
|
|
|
select {
|
|
|
|
case <-s.reset:
|
|
|
|
// This is the only place where it's safe to return these.
|
|
|
|
s.returnBuffers()
|
2019-06-09 07:24:20 +00:00
|
|
|
return mux.ErrReset
|
2018-07-04 10:51:47 +00:00
|
|
|
case read, ok := <-s.dataIn:
|
|
|
|
if !ok {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
s.extra = read
|
|
|
|
s.exbuf = read
|
|
|
|
return nil
|
2019-06-09 07:24:20 +00:00
|
|
|
case <-s.rDeadline.wait():
|
|
|
|
return errTimeout
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) returnBuffers() {
|
|
|
|
if s.exbuf != nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
pool.Put(s.exbuf)
|
2018-07-04 10:51:47 +00:00
|
|
|
s.exbuf = nil
|
|
|
|
s.extra = nil
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case read, ok := <-s.dataIn:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if read == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
pool.Put(read)
|
2018-07-04 10:51:47 +00:00
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) Read(b []byte) (int, error) {
|
2019-06-09 07:24:20 +00:00
|
|
|
select {
|
|
|
|
case <-s.reset:
|
|
|
|
return 0, mux.ErrReset
|
|
|
|
default:
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
if s.extra == nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
err := s.waitForData()
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
n := 0
|
|
|
|
for s.extra != nil && n < len(b) {
|
|
|
|
read := copy(b[n:], s.extra)
|
|
|
|
n += read
|
|
|
|
if read < len(s.extra) {
|
|
|
|
s.extra = s.extra[read:]
|
|
|
|
} else {
|
|
|
|
if s.exbuf != nil {
|
|
|
|
pool.Put(s.exbuf)
|
|
|
|
}
|
|
|
|
s.extra = nil
|
|
|
|
s.exbuf = nil
|
|
|
|
s.preloadData()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) Write(b []byte) (int, error) {
|
|
|
|
var written int
|
|
|
|
for written < len(b) {
|
|
|
|
wl := len(b) - written
|
|
|
|
if wl > MaxMessageSize {
|
|
|
|
wl = MaxMessageSize
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := s.write(b[written : written+wl])
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
|
|
|
|
written += n
|
|
|
|
}
|
|
|
|
|
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) write(b []byte) (int, error) {
|
|
|
|
if s.isClosed() {
|
2019-06-09 07:24:20 +00:00
|
|
|
return 0, errors.New("cannot write to closed stream")
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
err := s.mp.sendMsg(s.wDeadline.wait(), s.id.header(messageTag), b)
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
if err == context.Canceled {
|
|
|
|
err = errors.New("cannot write to closed stream")
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) isClosed() bool {
|
2019-06-09 07:24:20 +00:00
|
|
|
return s.closedLocal.Err() != nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) Close() error {
|
2019-06-09 07:24:20 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), ResetStreamTimeout)
|
|
|
|
defer cancel()
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
err := s.mp.sendMsg(ctx.Done(), s.id.header(closeTag), nil)
|
|
|
|
|
|
|
|
if s.isClosed() {
|
2018-07-04 10:51:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
s.clLock.Lock()
|
2018-07-04 10:51:47 +00:00
|
|
|
remote := s.closedRemote
|
|
|
|
s.clLock.Unlock()
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
s.doCloseLocal()
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
if remote {
|
2019-06-09 07:24:20 +00:00
|
|
|
s.cancelDeadlines()
|
2018-07-04 10:51:47 +00:00
|
|
|
s.mp.chLock.Lock()
|
|
|
|
delete(s.mp.channels, s.id)
|
|
|
|
s.mp.chLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
if err != nil && !s.mp.isShutdown() {
|
|
|
|
log.Warningf("Error closing stream: %s; killing connection", err.Error())
|
|
|
|
s.mp.Close()
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) Reset() error {
|
|
|
|
s.clLock.Lock()
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
// Don't reset when fully closed.
|
|
|
|
if s.closedRemote && s.isClosed() {
|
2018-07-04 10:51:47 +00:00
|
|
|
s.clLock.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// Don't reset twice.
|
|
|
|
select {
|
|
|
|
case <-s.reset:
|
|
|
|
s.clLock.Unlock()
|
|
|
|
return nil
|
|
|
|
default:
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
close(s.reset)
|
|
|
|
s.doCloseLocal()
|
2018-07-04 10:51:47 +00:00
|
|
|
s.closedRemote = true
|
2019-06-09 07:24:20 +00:00
|
|
|
s.cancelDeadlines()
|
|
|
|
|
|
|
|
go s.mp.sendResetMsg(s.id.header(resetTag), true)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
s.clLock.Unlock()
|
|
|
|
|
|
|
|
s.mp.chLock.Lock()
|
|
|
|
delete(s.mp.channels, s.id)
|
|
|
|
s.mp.chLock.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func (s *Stream) cancelDeadlines() {
|
|
|
|
s.rDeadline.set(time.Time{})
|
|
|
|
s.wDeadline.set(time.Time{})
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
func (s *Stream) SetDeadline(t time.Time) error {
|
2019-06-09 07:24:20 +00:00
|
|
|
s.clLock.Lock()
|
|
|
|
defer s.clLock.Unlock()
|
|
|
|
|
|
|
|
if s.closedRemote && s.isClosed() {
|
|
|
|
return errStreamClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.closedRemote {
|
|
|
|
s.rDeadline.set(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.isClosed() {
|
|
|
|
s.wDeadline.set(t)
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) SetReadDeadline(t time.Time) error {
|
2019-06-09 07:24:20 +00:00
|
|
|
s.clLock.Lock()
|
|
|
|
defer s.clLock.Unlock()
|
|
|
|
|
|
|
|
if s.closedRemote {
|
|
|
|
return errStreamClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
s.rDeadline.set(t)
|
2018-07-04 10:51:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) SetWriteDeadline(t time.Time) error {
|
2019-06-09 07:24:20 +00:00
|
|
|
s.clLock.Lock()
|
|
|
|
defer s.clLock.Unlock()
|
|
|
|
|
|
|
|
if s.isClosed() {
|
|
|
|
return errStreamClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
s.wDeadline.set(t)
|
2018-07-04 10:51:47 +00:00
|
|
|
return nil
|
|
|
|
}
|