|
@@ -1,7 +1,7 @@
|
|
|
package net
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
- "io"
|
|
|
|
|
|
|
+ "io"
|
|
|
"net"
|
|
"net"
|
|
|
"time"
|
|
"time"
|
|
|
)
|
|
)
|
|
@@ -11,17 +11,17 @@ var (
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type TimeOutReader struct {
|
|
type TimeOutReader struct {
|
|
|
- timeout int
|
|
|
|
|
|
|
+ timeout int
|
|
|
connection net.Conn
|
|
connection net.Conn
|
|
|
- worker io.Reader
|
|
|
|
|
|
|
+ worker io.Reader
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func NewTimeOutReader(timeout int, connection net.Conn) *TimeOutReader {
|
|
func NewTimeOutReader(timeout int, connection net.Conn) *TimeOutReader {
|
|
|
reader := &TimeOutReader{
|
|
reader := &TimeOutReader{
|
|
|
connection: connection,
|
|
connection: connection,
|
|
|
}
|
|
}
|
|
|
- reader.SetTimeOut(timeout)
|
|
|
|
|
- return reader
|
|
|
|
|
|
|
+ reader.SetTimeOut(timeout)
|
|
|
|
|
+ return reader
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (reader *TimeOutReader) Read(p []byte) (int, error) {
|
|
func (reader *TimeOutReader) Read(p []byte) (int, error) {
|
|
@@ -33,36 +33,36 @@ func (reader *TimeOutReader) GetTimeOut() int {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (reader *TimeOutReader) SetTimeOut(value int) {
|
|
func (reader *TimeOutReader) SetTimeOut(value int) {
|
|
|
- reader.timeout = value
|
|
|
|
|
- if value > 0 {
|
|
|
|
|
- reader.worker = &timedReaderWorker{
|
|
|
|
|
- timeout: value,
|
|
|
|
|
- connection: reader.connection,
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- reader.worker = &noOpReaderWorker{
|
|
|
|
|
- connection: reader.connection,
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ reader.timeout = value
|
|
|
|
|
+ if value > 0 {
|
|
|
|
|
+ reader.worker = &timedReaderWorker{
|
|
|
|
|
+ timeout: value,
|
|
|
|
|
+ connection: reader.connection,
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ reader.worker = &noOpReaderWorker{
|
|
|
|
|
+ connection: reader.connection,
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type timedReaderWorker struct {
|
|
type timedReaderWorker struct {
|
|
|
- timeout int
|
|
|
|
|
- connection net.Conn
|
|
|
|
|
|
|
+ timeout int
|
|
|
|
|
+ connection net.Conn
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (this *timedReaderWorker) Read(p []byte) (int, error) {
|
|
func (this *timedReaderWorker) Read(p []byte) (int, error) {
|
|
|
- deadline := time.Duration(this.timeout) * time.Second
|
|
|
|
|
|
|
+ deadline := time.Duration(this.timeout) * time.Second
|
|
|
this.connection.SetReadDeadline(time.Now().Add(deadline))
|
|
this.connection.SetReadDeadline(time.Now().Add(deadline))
|
|
|
- nBytes, err := this.connection.Read(p)
|
|
|
|
|
- this.connection.SetReadDeadline(emptyTime)
|
|
|
|
|
- return nBytes, err
|
|
|
|
|
|
|
+ nBytes, err := this.connection.Read(p)
|
|
|
|
|
+ this.connection.SetReadDeadline(emptyTime)
|
|
|
|
|
+ return nBytes, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type noOpReaderWorker struct {
|
|
type noOpReaderWorker struct {
|
|
|
- connection net.Conn
|
|
|
|
|
|
|
+ connection net.Conn
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (this *noOpReaderWorker) Read(p []byte) (int, error) {
|
|
func (this *noOpReaderWorker) Read(p []byte) (int, error) {
|
|
|
- return this.connection.Read(p)
|
|
|
|
|
|
|
+ return this.connection.Read(p)
|
|
|
}
|
|
}
|