packer-cn/sdk-internals/communicator/ssh/connection.go

31 lines
582 B
Go

package ssh
import (
"net"
"time"
)
// timeoutConn wraps a net.Conn, and sets a deadline for every read
// and write operation.
type timeoutConn struct {
net.Conn
ReadTimeout time.Duration
WriteTimeout time.Duration
}
func (c *timeoutConn) Read(b []byte) (int, error) {
err := c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
if err != nil {
return 0, err
}
return c.Conn.Read(b)
}
func (c *timeoutConn) Write(b []byte) (int, error) {
err := c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout))
if err != nil {
return 0, err
}
return c.Conn.Write(b)
}