github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/communicator/ssh/connection.go (about)

     1  package ssh
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  )
     7  
     8  // timeoutConn wraps a net.Conn, and sets a deadline for every read
     9  // and write operation.
    10  type timeoutConn struct {
    11  	net.Conn
    12  	ReadTimeout  time.Duration
    13  	WriteTimeout time.Duration
    14  }
    15  
    16  func (c *timeoutConn) Read(b []byte) (int, error) {
    17  	err := c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
    18  	if err != nil {
    19  		return 0, err
    20  	}
    21  	return c.Conn.Read(b)
    22  }
    23  
    24  func (c *timeoutConn) Write(b []byte) (int, error) {
    25  	err := c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout))
    26  	if err != nil {
    27  		return 0, err
    28  	}
    29  	return c.Conn.Write(b)
    30  }