|
1 | 1 | package p2p |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bufio" |
5 | 4 | "bytes" |
6 | | - "encoding/binary" |
7 | 5 | "errors" |
8 | 6 | "fmt" |
9 | 7 | "io" |
10 | 8 | "io/ioutil" |
11 | | - "math/big" |
12 | | - "net" |
13 | 9 | "sync" |
14 | 10 | "sync/atomic" |
15 | 11 | "time" |
@@ -138,140 +134,6 @@ func (rw *lockedRW) WriteMsg(msg Msg) error { |
138 | 134 | return rw.wrapped.WriteMsg(msg) |
139 | 135 | } |
140 | 136 |
|
141 | | -// frameRW is a MsgReadWriter that reads and writes devp2p message frames. |
142 | | -// As required by the interface, ReadMsg and WriteMsg can be called from |
143 | | -// multiple goroutines. |
144 | | -type frameRW struct { |
145 | | - net.Conn // make Conn methods available. be careful. |
146 | | - bufconn *bufio.ReadWriter |
147 | | - |
148 | | - // this channel is used to 'lend' bufconn to a caller of ReadMsg |
149 | | - // until the message payload has been consumed. the channel |
150 | | - // receives a value when EOF is reached on the payload, unblocking |
151 | | - // a pending call to ReadMsg. |
152 | | - rsync chan struct{} |
153 | | - |
154 | | - // this mutex guards writes to bufconn. |
155 | | - writeMu sync.Mutex |
156 | | -} |
157 | | - |
158 | | -func newFrameRW(conn net.Conn, timeout time.Duration) *frameRW { |
159 | | - rsync := make(chan struct{}, 1) |
160 | | - rsync <- struct{}{} |
161 | | - return &frameRW{ |
162 | | - Conn: conn, |
163 | | - bufconn: bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn)), |
164 | | - rsync: rsync, |
165 | | - } |
166 | | -} |
167 | | - |
168 | | -var magicToken = []byte{34, 64, 8, 145} |
169 | | - |
170 | | -func (rw *frameRW) WriteMsg(msg Msg) error { |
171 | | - rw.writeMu.Lock() |
172 | | - defer rw.writeMu.Unlock() |
173 | | - rw.SetWriteDeadline(time.Now().Add(msgWriteTimeout)) |
174 | | - if err := writeMsg(rw.bufconn, msg); err != nil { |
175 | | - return err |
176 | | - } |
177 | | - return rw.bufconn.Flush() |
178 | | -} |
179 | | - |
180 | | -func writeMsg(w io.Writer, msg Msg) error { |
181 | | - // TODO: handle case when Size + len(code) + len(listhdr) overflows uint32 |
182 | | - code := ethutil.Encode(uint32(msg.Code)) |
183 | | - listhdr := makeListHeader(msg.Size + uint32(len(code))) |
184 | | - payloadLen := uint32(len(listhdr)) + uint32(len(code)) + msg.Size |
185 | | - |
186 | | - start := make([]byte, 8) |
187 | | - copy(start, magicToken) |
188 | | - binary.BigEndian.PutUint32(start[4:], payloadLen) |
189 | | - |
190 | | - for _, b := range [][]byte{start, listhdr, code} { |
191 | | - if _, err := w.Write(b); err != nil { |
192 | | - return err |
193 | | - } |
194 | | - } |
195 | | - _, err := io.CopyN(w, msg.Payload, int64(msg.Size)) |
196 | | - return err |
197 | | -} |
198 | | - |
199 | | -func makeListHeader(length uint32) []byte { |
200 | | - if length < 56 { |
201 | | - return []byte{byte(length + 0xc0)} |
202 | | - } |
203 | | - enc := big.NewInt(int64(length)).Bytes() |
204 | | - lenb := byte(len(enc)) + 0xf7 |
205 | | - return append([]byte{lenb}, enc...) |
206 | | -} |
207 | | - |
208 | | -func (rw *frameRW) ReadMsg() (msg Msg, err error) { |
209 | | - <-rw.rsync // wait until bufconn is ours |
210 | | - |
211 | | - rw.SetReadDeadline(time.Now().Add(frameReadTimeout)) |
212 | | - |
213 | | - // read magic and payload size |
214 | | - start := make([]byte, 8) |
215 | | - if _, err = io.ReadFull(rw.bufconn, start); err != nil { |
216 | | - return msg, err |
217 | | - } |
218 | | - if !bytes.HasPrefix(start, magicToken) { |
219 | | - return msg, fmt.Errorf("bad magic token %x", start[:4]) |
220 | | - } |
221 | | - size := binary.BigEndian.Uint32(start[4:]) |
222 | | - |
223 | | - // decode start of RLP message to get the message code |
224 | | - posr := &postrack{rw.bufconn, 0} |
225 | | - s := rlp.NewStream(posr) |
226 | | - if _, err := s.List(); err != nil { |
227 | | - return msg, err |
228 | | - } |
229 | | - msg.Code, err = s.Uint() |
230 | | - if err != nil { |
231 | | - return msg, err |
232 | | - } |
233 | | - msg.Size = size - posr.p |
234 | | - |
235 | | - rw.SetReadDeadline(time.Now().Add(payloadReadTimeout)) |
236 | | - |
237 | | - if msg.Size <= wholePayloadSize { |
238 | | - // msg is small, read all of it and move on to the next message. |
239 | | - pbuf := make([]byte, msg.Size) |
240 | | - if _, err := io.ReadFull(rw.bufconn, pbuf); err != nil { |
241 | | - return msg, err |
242 | | - } |
243 | | - rw.rsync <- struct{}{} // bufconn is available again |
244 | | - msg.Payload = bytes.NewReader(pbuf) |
245 | | - } else { |
246 | | - // lend bufconn to the caller until it has |
247 | | - // consumed the payload. eofSignal will send a value |
248 | | - // on rw.rsync when EOF is reached. |
249 | | - pr := &eofSignal{rw.bufconn, msg.Size, rw.rsync} |
250 | | - msg.Payload = pr |
251 | | - } |
252 | | - return msg, nil |
253 | | -} |
254 | | - |
255 | | -// postrack wraps an rlp.ByteReader with a position counter. |
256 | | -type postrack struct { |
257 | | - r rlp.ByteReader |
258 | | - p uint32 |
259 | | -} |
260 | | - |
261 | | -func (r *postrack) Read(buf []byte) (int, error) { |
262 | | - n, err := r.r.Read(buf) |
263 | | - r.p += uint32(n) |
264 | | - return n, err |
265 | | -} |
266 | | - |
267 | | -func (r *postrack) ReadByte() (byte, error) { |
268 | | - b, err := r.r.ReadByte() |
269 | | - if err == nil { |
270 | | - r.p++ |
271 | | - } |
272 | | - return b, err |
273 | | -} |
274 | | - |
275 | 137 | // eofSignal wraps a reader with eof signaling. the eof channel is |
276 | 138 | // closed when the wrapped reader returns an error or when count bytes |
277 | 139 | // have been read. |
|
0 commit comments