2013-12-08 21:20:27 -05:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"sync"
|
2013-12-09 17:24:55 -05:00
|
|
|
"time"
|
2013-12-08 21:20:27 -05:00
|
|
|
)
|
|
|
|
|
2013-12-10 20:31:54 -05:00
|
|
|
// MuxConn is able to multiplex multiple streams on top of any
|
|
|
|
// io.ReadWriteCloser. These streams act like TCP connections (Dial, Accept,
|
|
|
|
// Close, full duplex, etc.).
|
|
|
|
//
|
|
|
|
// The underlying io.ReadWriteCloser is expected to guarantee delivery
|
|
|
|
// and ordering, such as TCP. Congestion control and such aren't implemented
|
|
|
|
// by the streams, so that is also up to the underlying connection.
|
2013-12-08 21:20:27 -05:00
|
|
|
//
|
|
|
|
// MuxConn works using a fairly dumb multiplexing technique of simply
|
2013-12-09 17:29:28 -05:00
|
|
|
// framing every piece of data sent into a prefix + data format. Streams
|
|
|
|
// are established using a subset of the TCP protocol. Only a subset is
|
|
|
|
// necessary since we assume ordering on the underlying RWC.
|
2013-12-08 21:20:27 -05:00
|
|
|
type MuxConn struct {
|
2013-12-20 12:49:44 -05:00
|
|
|
curId uint32
|
|
|
|
rwc io.ReadWriteCloser
|
|
|
|
streamsAccept map[uint32]*Stream
|
|
|
|
streamsDial map[uint32]*Stream
|
|
|
|
muAccept sync.RWMutex
|
|
|
|
muDial sync.RWMutex
|
|
|
|
wlock sync.Mutex
|
|
|
|
doneCh chan struct{}
|
2013-12-08 21:20:27 -05:00
|
|
|
}
|
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
type muxPacketFrom byte
|
2013-12-09 17:24:55 -05:00
|
|
|
type muxPacketType byte
|
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
const (
|
|
|
|
muxPacketFromAccept muxPacketFrom = iota
|
|
|
|
muxPacketFromDial
|
|
|
|
)
|
|
|
|
|
2013-12-09 17:24:55 -05:00
|
|
|
const (
|
|
|
|
muxPacketSyn muxPacketType = iota
|
2013-12-12 02:50:41 -05:00
|
|
|
muxPacketSynAck
|
2013-12-09 17:24:55 -05:00
|
|
|
muxPacketAck
|
|
|
|
muxPacketFin
|
|
|
|
muxPacketData
|
|
|
|
)
|
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
func (f muxPacketFrom) String() string {
|
|
|
|
switch f {
|
|
|
|
case muxPacketFromAccept:
|
|
|
|
return "accept"
|
|
|
|
case muxPacketFromDial:
|
|
|
|
return "dial"
|
|
|
|
default:
|
|
|
|
panic("unknown from type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 20:31:54 -05:00
|
|
|
// Create a new MuxConn around any io.ReadWriteCloser.
|
2013-12-20 12:49:44 -05:00
|
|
|
func NewMuxConn(rwc io.ReadWriteCloser) *MuxConn {
|
2013-12-08 21:20:27 -05:00
|
|
|
m := &MuxConn{
|
2013-12-20 12:49:44 -05:00
|
|
|
rwc: rwc,
|
|
|
|
streamsAccept: make(map[uint32]*Stream),
|
|
|
|
streamsDial: make(map[uint32]*Stream),
|
|
|
|
doneCh: make(chan struct{}),
|
2013-12-08 21:20:27 -05:00
|
|
|
}
|
|
|
|
|
2013-12-12 16:38:44 -05:00
|
|
|
go m.cleaner()
|
2013-12-08 21:20:27 -05:00
|
|
|
go m.loop()
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:30:29 -05:00
|
|
|
// Close closes the underlying io.ReadWriteCloser. This will also close
|
|
|
|
// all streams that are open.
|
|
|
|
func (m *MuxConn) Close() error {
|
2013-12-20 12:55:57 -05:00
|
|
|
m.muAccept.Lock()
|
|
|
|
m.muDial.Lock()
|
|
|
|
defer m.muAccept.Unlock()
|
|
|
|
defer m.muDial.Unlock()
|
2013-12-08 21:30:29 -05:00
|
|
|
|
|
|
|
// Close all the streams
|
2013-12-20 12:49:44 -05:00
|
|
|
for _, w := range m.streamsAccept {
|
|
|
|
w.Close()
|
|
|
|
}
|
|
|
|
for _, w := range m.streamsDial {
|
2013-12-08 21:30:29 -05:00
|
|
|
w.Close()
|
|
|
|
}
|
2013-12-20 12:49:44 -05:00
|
|
|
m.streamsAccept = make(map[uint32]*Stream)
|
|
|
|
m.streamsDial = make(map[uint32]*Stream)
|
2013-12-08 21:30:29 -05:00
|
|
|
|
2013-12-10 20:31:54 -05:00
|
|
|
// Close the actual connection. This will also force the loop
|
|
|
|
// to end since it'll read EOF or closed connection.
|
2013-12-08 21:30:29 -05:00
|
|
|
return m.rwc.Close()
|
|
|
|
}
|
|
|
|
|
2013-12-09 17:24:55 -05:00
|
|
|
// Accept accepts a multiplexed connection with the given ID. This
|
|
|
|
// will block until a request is made to connect.
|
2013-12-09 19:27:13 -05:00
|
|
|
func (m *MuxConn) Accept(id uint32) (io.ReadWriteCloser, error) {
|
2014-01-02 01:19:43 -05:00
|
|
|
//log.Printf("[TRACE] %p: Accept on stream ID: %d", m, id)
|
2013-12-20 12:49:44 -05:00
|
|
|
|
|
|
|
// Get the stream. It is okay if it is already in the list of streams
|
|
|
|
// because we may have prematurely received a syn for it.
|
|
|
|
m.muAccept.Lock()
|
|
|
|
stream, ok := m.streamsAccept[id]
|
|
|
|
if !ok {
|
|
|
|
stream = newStream(muxPacketFromAccept, id, m)
|
|
|
|
m.streamsAccept[id] = stream
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
2013-12-20 12:49:44 -05:00
|
|
|
m.muAccept.Unlock()
|
2013-12-09 17:24:55 -05:00
|
|
|
|
|
|
|
stream.mu.Lock()
|
2013-12-12 02:50:41 -05:00
|
|
|
defer stream.mu.Unlock()
|
2013-12-20 12:49:44 -05:00
|
|
|
|
|
|
|
// If the stream isn't closed, then it is already open somehow
|
2013-12-09 17:24:55 -05:00
|
|
|
if stream.state != streamStateSynRecv && stream.state != streamStateClosed {
|
2013-12-21 11:07:34 -05:00
|
|
|
panic(fmt.Sprintf(
|
|
|
|
"Stream %d already open in bad state: %d", id, stream.state))
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-12 02:50:41 -05:00
|
|
|
if stream.state == streamStateClosed {
|
|
|
|
// Go into the listening state and wait for a syn
|
2013-12-09 17:24:55 -05:00
|
|
|
stream.setState(streamStateListen)
|
2013-12-12 02:50:41 -05:00
|
|
|
if err := stream.waitState(streamStateSynRecv); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2013-12-10 20:01:02 -05:00
|
|
|
|
2013-12-12 02:50:41 -05:00
|
|
|
if stream.state == streamStateSynRecv {
|
|
|
|
// Send a syn-ack
|
2013-12-20 12:49:44 -05:00
|
|
|
if _, err := stream.write(muxPacketSynAck, nil); err != nil {
|
2013-12-12 02:50:41 -05:00
|
|
|
return nil, err
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 02:50:41 -05:00
|
|
|
if err := stream.waitState(streamStateEstablished); err != nil {
|
2013-12-09 17:24:55 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial opens a connection to the remote end using the given stream ID.
|
|
|
|
// An Accept on the remote end will only work with if the IDs match.
|
2013-12-09 19:27:13 -05:00
|
|
|
func (m *MuxConn) Dial(id uint32) (io.ReadWriteCloser, error) {
|
2014-01-02 01:19:43 -05:00
|
|
|
//log.Printf("[TRACE] %p: Dial on stream ID: %d", m, id)
|
2014-01-02 00:53:57 -05:00
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
m.muDial.Lock()
|
|
|
|
|
|
|
|
// If we have any streams with this ID, then it is a failure. The
|
|
|
|
// reaper should clear out old streams once in awhile.
|
|
|
|
if stream, ok := m.streamsDial[id]; ok {
|
|
|
|
m.muDial.Unlock()
|
2013-12-21 11:07:34 -05:00
|
|
|
panic(fmt.Sprintf(
|
|
|
|
"Stream %d already open for dial. State: %d",
|
|
|
|
id, stream.state))
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
// Create the new stream and put it in our list. We can then
|
|
|
|
// unlock because dialing will no longer be allowed on that ID.
|
|
|
|
stream := newStream(muxPacketFromDial, id, m)
|
|
|
|
m.streamsDial[id] = stream
|
|
|
|
|
|
|
|
// Don't let anyone else mess with this stream
|
2013-12-09 17:24:55 -05:00
|
|
|
stream.mu.Lock()
|
2013-12-12 02:50:41 -05:00
|
|
|
defer stream.mu.Unlock()
|
2013-12-20 12:49:44 -05:00
|
|
|
|
|
|
|
m.muDial.Unlock()
|
2013-12-09 17:24:55 -05:00
|
|
|
|
|
|
|
// Open a connection
|
2013-12-20 12:49:44 -05:00
|
|
|
if _, err := stream.write(muxPacketSyn, nil); err != nil {
|
2013-12-09 17:24:55 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-20 12:49:44 -05:00
|
|
|
|
|
|
|
// It is safe to set the state after the write above because
|
|
|
|
// we hold the stream lock.
|
2013-12-09 17:24:55 -05:00
|
|
|
stream.setState(streamStateSynSent)
|
2013-12-10 20:01:02 -05:00
|
|
|
|
2013-12-12 02:50:41 -05:00
|
|
|
if err := stream.waitState(streamStateEstablished); err != nil {
|
|
|
|
return nil, err
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
2013-12-12 02:50:41 -05:00
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
stream.write(muxPacketAck, nil)
|
2013-12-12 02:50:41 -05:00
|
|
|
return stream, nil
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
// NextId returns the next available listen stream ID that isn't currently
|
2013-12-09 19:27:13 -05:00
|
|
|
// taken.
|
|
|
|
func (m *MuxConn) NextId() uint32 {
|
2013-12-20 12:49:44 -05:00
|
|
|
m.muAccept.Lock()
|
|
|
|
defer m.muAccept.Unlock()
|
2013-12-09 19:27:13 -05:00
|
|
|
|
|
|
|
for {
|
2013-12-31 00:03:10 -05:00
|
|
|
// We never use stream ID 0 because 0 is the zero value of a uint32
|
|
|
|
// and we want to reserve that for "not in use"
|
|
|
|
if m.curId == 0 {
|
|
|
|
m.curId = 1
|
|
|
|
}
|
|
|
|
|
2013-12-10 13:34:35 -05:00
|
|
|
result := m.curId
|
2013-12-20 12:49:44 -05:00
|
|
|
m.curId += 1
|
|
|
|
if _, ok := m.streamsAccept[result]; !ok {
|
2013-12-10 13:34:35 -05:00
|
|
|
return result
|
|
|
|
}
|
2013-12-09 19:27:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
func (m *MuxConn) cleaner() {
|
|
|
|
checks := []struct {
|
2013-12-20 13:02:47 -05:00
|
|
|
Map *map[uint32]*Stream
|
2013-12-20 12:49:44 -05:00
|
|
|
Lock *sync.RWMutex
|
|
|
|
}{
|
2013-12-20 13:02:47 -05:00
|
|
|
{&m.streamsAccept, &m.muAccept},
|
|
|
|
{&m.streamsDial, &m.muDial},
|
2013-12-08 21:20:27 -05:00
|
|
|
}
|
|
|
|
|
2013-12-12 16:38:44 -05:00
|
|
|
for {
|
|
|
|
done := false
|
|
|
|
select {
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
case <-m.doneCh:
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
for _, check := range checks {
|
|
|
|
check.Lock.Lock()
|
2013-12-20 13:02:47 -05:00
|
|
|
for id, s := range *check.Map {
|
2013-12-12 16:38:44 -05:00
|
|
|
s.mu.Lock()
|
2013-12-20 12:49:44 -05:00
|
|
|
|
|
|
|
if done && s.state != streamStateClosed {
|
|
|
|
s.closeWriter()
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.state == streamStateClosed {
|
|
|
|
// Only clean up the streams that have been closed
|
|
|
|
// for a certain amount of time.
|
|
|
|
since := time.Now().UTC().Sub(s.stateUpdated)
|
|
|
|
if since > 2*time.Second {
|
2013-12-20 13:02:47 -05:00
|
|
|
delete(*check.Map, id)
|
2013-12-20 12:49:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 16:38:44 -05:00
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
2013-12-20 12:49:44 -05:00
|
|
|
check.Lock.Unlock()
|
2013-12-12 16:38:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:20:27 -05:00
|
|
|
func (m *MuxConn) loop() {
|
2013-12-10 20:31:54 -05:00
|
|
|
// Force close every stream that we know about when we exit so
|
|
|
|
// that they all read EOF and don't block forever.
|
2013-12-10 19:49:14 -05:00
|
|
|
defer func() {
|
2013-12-12 02:50:41 -05:00
|
|
|
log.Printf("[INFO] Mux connection loop exiting")
|
2013-12-12 16:38:44 -05:00
|
|
|
close(m.doneCh)
|
2013-12-10 19:49:14 -05:00
|
|
|
}()
|
2013-12-08 21:30:29 -05:00
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
var from muxPacketFrom
|
2013-12-09 19:27:13 -05:00
|
|
|
var id uint32
|
2013-12-09 17:24:55 -05:00
|
|
|
var packetType muxPacketType
|
|
|
|
var length int32
|
2013-12-08 21:20:27 -05:00
|
|
|
for {
|
2013-12-20 12:49:44 -05:00
|
|
|
if err := binary.Read(m.rwc, binary.BigEndian, &from); err != nil {
|
|
|
|
log.Printf("[ERR] Error reading stream direction: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2013-12-08 21:20:27 -05:00
|
|
|
if err := binary.Read(m.rwc, binary.BigEndian, &id); err != nil {
|
|
|
|
log.Printf("[ERR] Error reading stream ID: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2013-12-09 17:24:55 -05:00
|
|
|
if err := binary.Read(m.rwc, binary.BigEndian, &packetType); err != nil {
|
|
|
|
log.Printf("[ERR] Error reading packet type: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2013-12-08 21:20:27 -05:00
|
|
|
if err := binary.Read(m.rwc, binary.BigEndian, &length); err != nil {
|
|
|
|
log.Printf("[ERR] Error reading length: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mitchellh): probably would be better to re-use a buffer...
|
|
|
|
data := make([]byte, length)
|
2014-01-02 00:53:36 -05:00
|
|
|
n := 0
|
|
|
|
for n < int(length) {
|
2014-01-02 01:13:06 -05:00
|
|
|
if n2, err := m.rwc.Read(data[n:]); err != nil {
|
2013-12-09 17:24:55 -05:00
|
|
|
log.Printf("[ERR] Error reading data: %s", err)
|
|
|
|
return
|
2014-01-02 00:53:36 -05:00
|
|
|
} else {
|
|
|
|
n += n2
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 12:49:44 -05:00
|
|
|
// Get the proper stream. Note that the map we look into is
|
|
|
|
// opposite the "from" because if the dial side is talking to
|
|
|
|
// us, we need to look into the accept map, and so on.
|
|
|
|
//
|
|
|
|
// Note: we also switch the "from" value so that logging
|
|
|
|
// below is correct.
|
|
|
|
var stream *Stream
|
|
|
|
switch from {
|
|
|
|
case muxPacketFromDial:
|
|
|
|
m.muAccept.Lock()
|
|
|
|
stream = m.streamsAccept[id]
|
|
|
|
m.muAccept.Unlock()
|
|
|
|
|
|
|
|
from = muxPacketFromAccept
|
|
|
|
case muxPacketFromAccept:
|
|
|
|
m.muDial.Lock()
|
|
|
|
stream = m.streamsDial[id]
|
|
|
|
m.muDial.Unlock()
|
|
|
|
|
|
|
|
from = muxPacketFromDial
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown stream direction: %d", from))
|
2013-12-08 21:20:27 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 01:34:18 -05:00
|
|
|
if stream == nil && packetType != muxPacketSyn {
|
|
|
|
log.Printf(
|
|
|
|
"[WARN] %p: Non-existent stream %d (%s) received packer %d",
|
|
|
|
m, id, from, packetType)
|
2014-01-02 00:19:47 -05:00
|
|
|
continue
|
|
|
|
}
|
2013-12-31 00:03:10 -05:00
|
|
|
|
2014-01-02 01:19:43 -05:00
|
|
|
//log.Printf("[TRACE] %p: Stream %d (%s) received packet %d", m, id, from, packetType)
|
2014-01-02 00:19:47 -05:00
|
|
|
switch packetType {
|
|
|
|
case muxPacketSyn:
|
|
|
|
// If the stream is nil, this is the only case where we'll
|
|
|
|
// automatically create the stream struct.
|
|
|
|
if stream == nil {
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
m.muAccept.Lock()
|
|
|
|
stream, ok = m.streamsAccept[id]
|
|
|
|
if !ok {
|
|
|
|
stream = newStream(muxPacketFromAccept, id, m)
|
|
|
|
m.streamsAccept[id] = stream
|
2013-12-20 12:49:44 -05:00
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
m.muAccept.Unlock()
|
2013-12-20 12:49:44 -05:00
|
|
|
}
|
2013-12-31 00:03:10 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
stream.mu.Lock()
|
|
|
|
switch stream.state {
|
|
|
|
case streamStateClosed:
|
|
|
|
fallthrough
|
|
|
|
case streamStateListen:
|
|
|
|
stream.setState(streamStateSynRecv)
|
|
|
|
default:
|
|
|
|
log.Printf("[ERR] Syn received for stream in state: %d", stream.state)
|
2013-12-12 02:50:41 -05:00
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
stream.mu.Unlock()
|
|
|
|
case muxPacketAck:
|
|
|
|
stream.mu.Lock()
|
|
|
|
switch stream.state {
|
|
|
|
case streamStateSynRecv:
|
|
|
|
stream.setState(streamStateEstablished)
|
|
|
|
case streamStateFinWait1:
|
|
|
|
stream.setState(streamStateFinWait2)
|
|
|
|
case streamStateLastAck:
|
|
|
|
stream.closeWriter()
|
|
|
|
fallthrough
|
|
|
|
case streamStateClosing:
|
|
|
|
stream.setState(streamStateClosed)
|
|
|
|
default:
|
|
|
|
log.Printf("[ERR] Ack received for stream in state: %d", stream.state)
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
stream.mu.Unlock()
|
|
|
|
case muxPacketSynAck:
|
|
|
|
stream.mu.Lock()
|
|
|
|
switch stream.state {
|
|
|
|
case streamStateSynSent:
|
|
|
|
stream.setState(streamStateEstablished)
|
|
|
|
default:
|
|
|
|
log.Printf("[ERR] SynAck received for stream in state: %d", stream.state)
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
stream.mu.Unlock()
|
|
|
|
case muxPacketFin:
|
|
|
|
stream.mu.Lock()
|
|
|
|
switch stream.state {
|
|
|
|
case streamStateEstablished:
|
|
|
|
stream.closeWriter()
|
|
|
|
stream.setState(streamStateCloseWait)
|
|
|
|
stream.write(muxPacketAck, nil)
|
|
|
|
case streamStateFinWait2:
|
|
|
|
stream.closeWriter()
|
|
|
|
stream.setState(streamStateClosed)
|
|
|
|
stream.write(muxPacketAck, nil)
|
|
|
|
case streamStateFinWait1:
|
|
|
|
stream.closeWriter()
|
|
|
|
stream.setState(streamStateClosing)
|
|
|
|
stream.write(muxPacketAck, nil)
|
|
|
|
default:
|
|
|
|
log.Printf("[ERR] Fin received for stream %d in state: %d", id, stream.state)
|
2013-12-10 14:40:17 -05:00
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
stream.mu.Unlock()
|
|
|
|
|
|
|
|
case muxPacketData:
|
|
|
|
stream.mu.Lock()
|
|
|
|
switch stream.state {
|
|
|
|
case streamStateFinWait1:
|
|
|
|
fallthrough
|
|
|
|
case streamStateFinWait2:
|
|
|
|
fallthrough
|
|
|
|
case streamStateEstablished:
|
2014-02-21 19:48:17 -05:00
|
|
|
if len(data) > 0 && stream.writeCh != nil {
|
|
|
|
//log.Printf("[TRACE] %p: Stream %d (%s) WRITE-START", m, id, from)
|
|
|
|
stream.writeCh <- data
|
|
|
|
//log.Printf("[TRACE] %p: Stream %d (%s) WRITE-END", m, id, from)
|
2014-01-02 00:19:47 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Printf("[ERR] Data received for stream in state: %d", stream.state)
|
2013-12-09 17:24:55 -05:00
|
|
|
}
|
2014-02-21 19:48:17 -05:00
|
|
|
stream.mu.Unlock()
|
2013-12-08 21:20:27 -05:00
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-08 21:20:27 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
func (m *MuxConn) write(from muxPacketFrom, id uint32, dataType muxPacketType, p []byte) (int, error) {
|
|
|
|
m.wlock.Lock()
|
|
|
|
defer m.wlock.Unlock()
|
2013-12-08 21:20:27 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
if err := binary.Write(m.rwc, binary.BigEndian, from); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := binary.Write(m.rwc, binary.BigEndian, id); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := binary.Write(m.rwc, binary.BigEndian, byte(dataType)); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := binary.Write(m.rwc, binary.BigEndian, int32(len(p))); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-01-02 00:53:36 -05:00
|
|
|
|
|
|
|
// Write all the bytes. If we don't write all the bytes, report an error
|
|
|
|
var err error = nil
|
|
|
|
n := 0
|
|
|
|
for n < len(p) {
|
|
|
|
var n2 int
|
2014-01-02 01:20:03 -05:00
|
|
|
n2, err = m.rwc.Write(p[n:])
|
2014-01-02 00:53:36 -05:00
|
|
|
n += n2
|
|
|
|
if err != nil {
|
2014-01-02 00:59:00 -05:00
|
|
|
log.Printf("[ERR] %p: Stream %d (%s) write error: %s", m, id, from, err)
|
2014-01-02 00:53:36 -05:00
|
|
|
break
|
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
}
|
2014-01-02 00:53:36 -05:00
|
|
|
|
|
|
|
return n, err
|
2014-01-02 00:19:47 -05:00
|
|
|
}
|
2013-12-08 21:20:27 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
// Stream is a single stream of data and implements io.ReadWriteCloser.
|
|
|
|
// A Stream is full-duplex so you can write data as well as read data.
|
|
|
|
type Stream struct {
|
|
|
|
from muxPacketFrom
|
|
|
|
id uint32
|
|
|
|
mux *MuxConn
|
|
|
|
reader io.Reader
|
|
|
|
state streamState
|
|
|
|
stateChange map[chan<- streamState]struct{}
|
|
|
|
stateUpdated time.Time
|
|
|
|
mu sync.Mutex
|
|
|
|
writeCh chan<- []byte
|
|
|
|
}
|
2013-12-08 21:20:27 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
type streamState byte
|
2013-12-09 17:24:55 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
const (
|
|
|
|
streamStateClosed streamState = iota
|
|
|
|
streamStateListen
|
|
|
|
streamStateSynRecv
|
|
|
|
streamStateSynSent
|
|
|
|
streamStateEstablished
|
|
|
|
streamStateFinWait1
|
|
|
|
streamStateFinWait2
|
|
|
|
streamStateCloseWait
|
|
|
|
streamStateClosing
|
|
|
|
streamStateLastAck
|
|
|
|
)
|
2013-12-09 17:24:55 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
func newStream(from muxPacketFrom, id uint32, m *MuxConn) *Stream {
|
|
|
|
// Create the stream object and channel where data will be sent to
|
|
|
|
dataR, dataW := io.Pipe()
|
|
|
|
writeCh := make(chan []byte, 4096)
|
|
|
|
|
|
|
|
// Set the data channel so we can write to it.
|
|
|
|
stream := &Stream{
|
|
|
|
from: from,
|
|
|
|
id: id,
|
|
|
|
mux: m,
|
|
|
|
reader: dataR,
|
|
|
|
writeCh: writeCh,
|
|
|
|
stateChange: make(map[chan<- streamState]struct{}),
|
|
|
|
}
|
|
|
|
stream.setState(streamStateClosed)
|
|
|
|
|
|
|
|
// Start the goroutine that will read from the queue and write
|
|
|
|
// data out.
|
|
|
|
go func() {
|
|
|
|
defer dataW.Close()
|
|
|
|
|
2014-02-21 19:48:17 -05:00
|
|
|
drain := false
|
2014-01-02 00:19:47 -05:00
|
|
|
for {
|
|
|
|
data := <-writeCh
|
|
|
|
if data == nil {
|
|
|
|
// A nil is a tombstone letting us know we're done
|
|
|
|
// accepting data.
|
|
|
|
return
|
2013-12-20 12:49:44 -05:00
|
|
|
}
|
|
|
|
|
2014-02-21 19:48:17 -05:00
|
|
|
if drain {
|
|
|
|
// We're draining, meaning we're just waiting for the
|
|
|
|
// write channel to close.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
if _, err := dataW.Write(data); err != nil {
|
2014-02-21 19:48:17 -05:00
|
|
|
drain = true
|
2013-12-20 12:49:44 -05:00
|
|
|
}
|
2013-12-31 00:03:10 -05:00
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
}()
|
2013-12-08 21:20:27 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
return stream
|
|
|
|
}
|
2013-12-10 14:40:17 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
func (s *Stream) Close() error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2013-12-10 14:40:17 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
if s.state != streamStateEstablished && s.state != streamStateCloseWait {
|
|
|
|
return fmt.Errorf("Stream in bad state: %d", s.state)
|
|
|
|
}
|
2013-12-09 17:24:55 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
if s.state == streamStateEstablished {
|
|
|
|
s.setState(streamStateFinWait1)
|
|
|
|
} else {
|
|
|
|
s.setState(streamStateLastAck)
|
|
|
|
}
|
2013-12-10 14:40:17 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
s.write(muxPacketFin, nil)
|
|
|
|
return nil
|
|
|
|
}
|
2013-12-12 02:50:41 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
func (s *Stream) Read(p []byte) (int, error) {
|
|
|
|
return s.reader.Read(p)
|
|
|
|
}
|
2013-12-12 02:50:41 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
func (s *Stream) Write(p []byte) (int, error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
state := s.state
|
|
|
|
s.mu.Unlock()
|
2013-12-12 02:50:41 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
if state != streamStateEstablished && state != streamStateCloseWait {
|
|
|
|
return 0, fmt.Errorf("Stream %d in bad state to send: %d", s.id, state)
|
|
|
|
}
|
2013-12-20 12:49:44 -05:00
|
|
|
|
2014-01-02 00:19:47 -05:00
|
|
|
return s.write(muxPacketData, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) closeWriter() {
|
2014-02-21 19:48:17 -05:00
|
|
|
if s.writeCh != nil {
|
|
|
|
s.writeCh <- nil
|
|
|
|
s.writeCh = nil
|
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) setState(state streamState) {
|
2014-01-02 01:19:43 -05:00
|
|
|
//log.Printf("[TRACE] %p: Stream %d (%s) went to state %d", s.mux, s.id, s.from, state)
|
2014-01-02 00:19:47 -05:00
|
|
|
s.state = state
|
|
|
|
s.stateUpdated = time.Now().UTC()
|
|
|
|
for ch, _ := range s.stateChange {
|
|
|
|
select {
|
|
|
|
case ch <- state:
|
|
|
|
default:
|
2013-12-31 00:03:10 -05:00
|
|
|
}
|
2014-01-02 00:19:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) waitState(target streamState) error {
|
|
|
|
// Register a state change listener to wait for changes
|
|
|
|
stateCh := make(chan streamState, 10)
|
|
|
|
s.stateChange[stateCh] = struct{}{}
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
s.mu.Lock()
|
|
|
|
delete(s.stateChange, stateCh)
|
|
|
|
}()
|
|
|
|
|
2014-02-21 19:48:17 -05:00
|
|
|
//log.Printf("[TRACE] %p: Stream %d (%s) waiting for state: %d", s.mux, s.id, s.from, target)
|
2014-01-02 00:19:47 -05:00
|
|
|
state := <-stateCh
|
|
|
|
if state == target {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Stream %d went to bad state: %d", s.id, state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) write(dataType muxPacketType, p []byte) (int, error) {
|
|
|
|
return s.mux.write(s.from, s.id, dataType, p)
|
|
|
|
}
|