2019-03-22 17:09:37 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
2019-05-09 14:53:04 +01:00
|
|
|
"net"
|
2019-03-22 17:09:37 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-05-09 14:53:04 +01:00
|
|
|
type timeoutConn struct {
|
|
|
|
conn net.Conn
|
2019-04-25 09:17:26 +01:00
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:53:04 +01:00
|
|
|
func (tc *timeoutConn) Read(b []byte) (n int, err error) {
|
|
|
|
// deadline needs to be set before each read operation
|
|
|
|
err = tc.SetReadDeadline(time.Now().Add(tc.timeout))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return tc.conn.Read(b)
|
2019-04-25 09:17:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:53:04 +01:00
|
|
|
func (tc *timeoutConn) Write(b []byte) (n int, err error) {
|
|
|
|
// deadline needs to be set before each write operation
|
|
|
|
err = tc.SetWriteDeadline(time.Now().Add(tc.timeout))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return tc.conn.Write(b)
|
2019-04-25 09:17:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:53:04 +01:00
|
|
|
func (tc *timeoutConn) Close() error {
|
|
|
|
return tc.conn.Close()
|
2019-04-25 09:17:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:53:04 +01:00
|
|
|
func (tc *timeoutConn) LocalAddr() net.Addr {
|
|
|
|
return tc.conn.LocalAddr()
|
2019-04-25 09:17:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:53:04 +01:00
|
|
|
func (tc *timeoutConn) RemoteAddr() net.Addr {
|
|
|
|
return tc.conn.RemoteAddr()
|
2019-04-25 09:17:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:53:04 +01:00
|
|
|
func (tc *timeoutConn) SetDeadline(t time.Time) error {
|
|
|
|
return tc.conn.SetDeadline(t)
|
2019-04-25 09:17:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:53:04 +01:00
|
|
|
func (tc *timeoutConn) SetReadDeadline(t time.Time) error {
|
|
|
|
return tc.conn.SetReadDeadline(t)
|
2019-04-25 09:17:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:53:04 +01:00
|
|
|
func (tc *timeoutConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return tc.conn.SetWriteDeadline(t)
|
2019-04-25 09:17:26 +01:00
|
|
|
}
|