Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package ssh | 5 package ssh |
6 | 6 |
7 import ( | 7 import ( |
8 "crypto" | 8 "crypto" |
9 "crypto/rand" | 9 "crypto/rand" |
10 "errors" | 10 "errors" |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 data: make(chan []byte, 16), | 331 data: make(chan []byte, 16), |
332 dataExt: make(chan []byte, 16), | 332 dataExt: make(chan []byte, 16), |
333 win: make(chan int, 16), | 333 win: make(chan int, 16), |
334 msg: make(chan interface{}, 16), | 334 msg: make(chan interface{}, 16), |
335 } | 335 } |
336 } | 336 } |
337 | 337 |
338 // Close closes the channel. This does not close the underlying connection. | 338 // Close closes the channel. This does not close the underlying connection. |
339 func (c *clientChan) Close() error { | 339 func (c *clientChan) Close() error { |
340 return c.writePacket(marshal(msgChannelClose, channelCloseMsg{ | 340 return c.writePacket(marshal(msgChannelClose, channelCloseMsg{ |
341 PeersId: c.peersId, | 341 PeersId: c.peersId, |
dave_cheney.net
2011/11/24 15:18:27
boy is my face red.
| |
342 })) | 342 })) |
343 } | 343 } |
344 | 344 |
345 // Thread safe channel list. | 345 // Thread safe channel list. |
346 type chanlist struct { | 346 type chanlist struct { |
347 // protects concurrent access to chans | 347 // protects concurrent access to chans |
348 sync.Mutex | 348 sync.Mutex |
349 // chans are indexed by the local id of the channel, clientChan.id. | 349 // chans are indexed by the local id of the channel, clientChan.id. |
350 // The PeersId value of messages received by ClientConn.mainloop is | 350 // The PeersId value of messages received by ClientConn.mainloop is |
351 // used to locate the right local clientChan in this slice. | 351 // used to locate the right local clientChan in this slice. |
(...skipping 25 matching lines...) Expand all Loading... | |
377 | 377 |
378 func (c *chanlist) remove(id uint32) { | 378 func (c *chanlist) remove(id uint32) { |
379 c.Lock() | 379 c.Lock() |
380 defer c.Unlock() | 380 defer c.Unlock() |
381 c.chans[int(id)] = nil | 381 c.chans[int(id)] = nil |
382 } | 382 } |
383 | 383 |
384 // A chanWriter represents the stdin of a remote process. | 384 // A chanWriter represents the stdin of a remote process. |
385 type chanWriter struct { | 385 type chanWriter struct { |
386 win chan int // receives window adjustments | 386 win chan int // receives window adjustments |
387 » id uint32 // this channel's id | 387 » peersId uint32 // the peers id |
dave_cheney.net
2011/11/24 15:18:27
Drop id
| |
388 » peersId uint32 // the server channel's id | |
dave_cheney.net
2011/11/24 15:18:27
the peers id.
| |
389 rwin int // current rwin size | 388 rwin int // current rwin size |
390 packetWriter // for sending channelDataMsg | 389 packetWriter // for sending channelDataMsg |
391 } | 390 } |
392 | 391 |
393 // Write writes data to the remote process's standard input. | 392 // Write writes data to the remote process's standard input. |
394 func (w *chanWriter) Write(data []byte) (n int, err error) { | 393 func (w *chanWriter) Write(data []byte) (n int, err error) { |
395 for { | 394 for { |
396 if w.rwin == 0 { | 395 if w.rwin == 0 { |
397 win, ok := <-w.win | 396 win, ok := <-w.win |
398 if !ok { | 397 if !ok { |
(...skipping 17 matching lines...) Expand all Loading... | |
416 func (w *chanWriter) Close() error { | 415 func (w *chanWriter) Close() error { |
417 return w.writePacket(marshal(msgChannelEOF, channelEOFMsg{w.peersId})) | 416 return w.writePacket(marshal(msgChannelEOF, channelEOFMsg{w.peersId})) |
418 } | 417 } |
419 | 418 |
420 // A chanReader represents stdout or stderr of a remote process. | 419 // A chanReader represents stdout or stderr of a remote process. |
421 type chanReader struct { | 420 type chanReader struct { |
422 // TODO(dfc) a fixed size channel may not be the right data structure. | 421 // TODO(dfc) a fixed size channel may not be the right data structure. |
423 // If writes to this channel block, they will block mainLoop, making | 422 // If writes to this channel block, they will block mainLoop, making |
424 // it unable to receive new messages from the remote side. | 423 // it unable to receive new messages from the remote side. |
425 data chan []byte // receives data from remote | 424 data chan []byte // receives data from remote |
426 » id uint32 // this channel's id | 425 » peersId uint32 // the peers id |
dave_cheney.net
2011/11/24 15:18:27
drop id
| |
427 » peersId uint32 // the server channel's id | |
dave_cheney.net
2011/11/24 15:18:27
see above
| |
428 packetWriter // for sending windowAdjustMsg | 426 packetWriter // for sending windowAdjustMsg |
429 buf []byte | 427 buf []byte |
430 } | 428 } |
431 | 429 |
432 // Read reads data from the remote process's stdout or stderr. | 430 // Read reads data from the remote process's stdout or stderr. |
433 func (r *chanReader) Read(data []byte) (int, error) { | 431 func (r *chanReader) Read(data []byte) (int, error) { |
434 var ok bool | 432 var ok bool |
435 for { | 433 for { |
436 if len(r.buf) > 0 { | 434 if len(r.buf) > 0 { |
437 n := copy(data, r.buf) | 435 n := copy(data, r.buf) |
438 r.buf = r.buf[n:] | 436 r.buf = r.buf[n:] |
439 msg := windowAdjustMsg{ | 437 msg := windowAdjustMsg{ |
440 PeersId: r.peersId, | 438 PeersId: r.peersId, |
441 AdditionalBytes: uint32(n), | 439 AdditionalBytes: uint32(n), |
442 } | 440 } |
443 return n, r.writePacket(marshal(msgChannelWindowAdjust, msg)) | 441 return n, r.writePacket(marshal(msgChannelWindowAdjust, msg)) |
444 } | 442 } |
445 r.buf, ok = <-r.data | 443 r.buf, ok = <-r.data |
446 if !ok { | 444 if !ok { |
447 return 0, io.EOF | 445 return 0, io.EOF |
448 } | 446 } |
449 } | 447 } |
450 panic("unreachable") | 448 panic("unreachable") |
451 } | 449 } |
452 | |
453 func (r *chanReader) Close() error { | |
454 return r.writePacket(marshal(msgChannelEOF, channelEOFMsg{r.peersId})) | |
455 } | |
dave_cheney.net
2011/11/24 15:18:27
nit: you can delete this method if you like. It is
| |
LEFT | RIGHT |