1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-06 20:14:31 -04:00
v2fly/common/net/timed_io.go

31 lines
565 B
Go
Raw Normal View History

2015-09-24 08:51:19 -04:00
package net
import (
"net"
"time"
)
var (
emptyTime time.Time
)
type TimeOutReader struct {
timeout int
connection net.Conn
}
func NewTimeOutReader(timeout int, connection net.Conn) *TimeOutReader {
return &TimeOutReader{
timeout: timeout,
connection: connection,
}
}
func (reader *TimeOutReader) Read(p []byte) (n int, err error) {
deadline := time.Duration(reader.timeout) * time.Second
reader.connection.SetReadDeadline(time.Now().Add(deadline))
n, err = reader.connection.Read(p)
reader.connection.SetReadDeadline(emptyTime)
return
}