File size: 3,553 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package sendit

import (
	"bufio"
	"errors"
	"io"
	"sync"
)

// Stream is a ReadWriteCloser that can have it's read/write pipe replaced safely while actively sending data.
type Stream struct {
	readCh    chan io.ReadCloser
	readEOFCh chan io.ReadCloser
	writeCh   chan io.WriteCloser
	closeCh   chan struct{}
	mx        sync.Mutex

	readyCh chan struct{}

	isClosed bool
	isReady  bool
}

// NewStream initializes a new stream. SetPipe must be called before data can be transferred.
func NewStream() *Stream {
	s := &Stream{
		readCh:    make(chan io.ReadCloser, 1),
		readEOFCh: make(chan io.ReadCloser, 1),
		writeCh:   make(chan io.WriteCloser, 1),
		closeCh:   make(chan struct{}),
		readyCh:   make(chan struct{}),
	}
	return s
}

// Ready will return a channel indicating when the stream is ready for reading and writing.
func (s *Stream) Ready() <-chan struct{} { return s.readyCh }

func (s *Stream) Write(p []byte) (int, error) {
	var w io.WriteCloser
	select {
	case w = <-s.writeCh:
	case <-s.closeCh:
		return 0, io.ErrClosedPipe
	}

	n, err := w.Write(p)
	s.writeCh <- w
	return n, err
}

func (s *Stream) Read(p []byte) (int, error) {
	var r io.ReadCloser
	select {
	case r = <-s.readCh:
	case <-s.closeCh:
		return 0, io.EOF
	}

	n, err := r.Read(p)
	if err == io.EOF {
		s.readEOFCh <- r
		if n == 0 {
			return s.Read(p)
		}
	} else {
		s.readCh <- r
	}
	return n, err
}

// Close will shutdown the stream, and close the underlying Writer.
func (s *Stream) Close() (err error) {
	s.mx.Lock()
	defer s.mx.Unlock()
	if s.isClosed {
		return nil
	}

	s.isClosed = true
	if !s.isReady {
		close(s.closeCh)
		return nil
	}

	select {
	case wc := <-s.writeCh:
		err = wc.Close()
	case <-s.closeCh:
		return io.ErrClosedPipe
	}
	select {
	case r := <-s.readCh:
		r.Close()
	case r := <-s.readEOFCh:
		r.Close()
	}
	close(s.closeCh)

	return err
}

// SetPipe will swap the io.ReadCloser and WriteCloser with the underlying Stream safely.
func (s *Stream) SetPipe(r io.ReadCloser, wc io.WriteCloser) error {
	s.mx.Lock()
	defer s.mx.Unlock()

	if s.isClosed {
		return io.ErrClosedPipe
	}

	_, err := io.WriteString(wc, "SYN\n")
	if err != nil {
		return err
	}
	var bufClose struct {
		*bufio.Reader
		io.Closer
	}
	bufClose.Reader = bufio.NewReader(r)
	bufClose.Closer = r
	str, err := bufClose.ReadString('\n')
	if err != nil {
		return err
	}
	if str != "SYN\n" {
		return errors.New("expected SYN")
	}

	// Now that Read calls will safely transition to the next pipe, we can give the other end the OK
	// to terminate the first.
	_, err = io.WriteString(wc, "SYNACK\n")
	if err != nil {
		return err
	}
	str, err = bufClose.ReadString('\n')
	if err != nil {
		return err
	}
	if str != "SYNACK\n" {
		return errors.New("expected SYNACK")
	}

	if !s.isReady {
		s.writeCh <- wc
		s.readCh <- bufClose
		s.isReady = true
		close(s.readyCh)
		return nil
	}

	closeOld := func(c io.Closer) bool {
		err = c.Close()
		if err != nil {
			wc.Close()
			r.Close()
			s.isClosed = true
			close(s.closeCh)
			return false
		}
		return true
	}

	// Now we've established the new pipe, we just need to swap and close the read/writers.
	select {
	case oldWriter := <-s.writeCh:
		s.writeCh <- wc
		if !closeOld(oldWriter) {
			return err
		}
		oldReader := <-s.readEOFCh
		s.readCh <- bufClose
		if !closeOld(oldReader) {
			return err
		}
	case oldReader := <-s.readEOFCh:
		s.readCh <- bufClose
		if !closeOld(oldReader) {
			return err
		}
		oldWriter := <-s.writeCh
		s.writeCh <- wc
		if !closeOld(oldWriter) {
			return err
		}
	}

	return nil
}