Merge pull request #279 from wutzx/client-conn-wait

Add Wait method to detect underlying SFTP connection closed
diff --git a/client.go b/client.go
index 4f84fb5..ef885d8 100644
--- a/client.go
+++ b/client.go
@@ -122,6 +122,7 @@
 				WriteCloser: wr,
 			},
 			inflight: make(map[uint32]chan<- result),
+			closed:   make(chan struct{}),
 		},
 		maxPacket:             1 << 15,
 		maxConcurrentRequests: 64,
diff --git a/conn.go b/conn.go
index f799715..62e585e 100644
--- a/conn.go
+++ b/conn.go
@@ -36,6 +36,17 @@
 	wg         sync.WaitGroup
 	sync.Mutex                          // protects inflight
 	inflight   map[uint32]chan<- result // outstanding requests
+
+	closed chan struct{}
+	err    error
+}
+
+// Wait blocks until the conn has shut down, and return the error
+// causing the shutdown. It can be called concurrently from multiple
+// goroutines.
+func (c *clientConn) Wait() error {
+	<-c.closed
+	return c.err
 }
 
 // Close closes the SFTP session.
@@ -122,6 +133,8 @@
 	for _, ch := range listeners {
 		ch <- result{err: err}
 	}
+	c.err = err
+	close(c.closed)
 }
 
 type serverConn struct {