| 
									
										
										
										
											2015-09-24 14:51:19 +02:00
										 |  |  | package net | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2015-09-24 14:51:19 +02:00
										 |  |  | 	"net" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							|  |  |  | 	emptyTime time.Time | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type TimeOutReader struct { | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	timeout    int | 
					
						
							| 
									
										
										
										
											2015-09-24 14:51:19 +02:00
										 |  |  | 	connection net.Conn | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	worker     io.Reader | 
					
						
							| 
									
										
										
										
											2015-09-24 14:51:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 11:38:38 +00:00
										 |  |  | func NewTimeOutReader(timeout int /* seconds */, connection net.Conn) *TimeOutReader { | 
					
						
							| 
									
										
										
										
											2015-10-31 09:39:45 +01:00
										 |  |  | 	reader := &TimeOutReader{ | 
					
						
							| 
									
										
										
										
											2015-09-24 14:51:19 +02:00
										 |  |  | 		connection: connection, | 
					
						
							| 
									
										
										
										
											2016-01-04 22:57:17 +01:00
										 |  |  | 		timeout:    -100, | 
					
						
							| 
									
										
										
										
											2015-09-24 14:51:19 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	reader.SetTimeOut(timeout) | 
					
						
							|  |  |  | 	return reader | 
					
						
							| 
									
										
										
										
											2015-09-24 14:51:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 09:39:45 +01:00
										 |  |  | func (reader *TimeOutReader) Read(p []byte) (int, error) { | 
					
						
							|  |  |  | 	return reader.worker.Read(p) | 
					
						
							| 
									
										
										
										
											2015-09-24 14:51:19 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-10-04 00:21:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (reader *TimeOutReader) GetTimeOut() int { | 
					
						
							|  |  |  | 	return reader.timeout | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (reader *TimeOutReader) SetTimeOut(value int) { | 
					
						
							| 
									
										
										
										
											2016-01-04 22:01:46 +01:00
										 |  |  | 	if value == reader.timeout { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	reader.timeout = value | 
					
						
							|  |  |  | 	if value > 0 { | 
					
						
							|  |  |  | 		reader.worker = &timedReaderWorker{ | 
					
						
							|  |  |  | 			timeout:    value, | 
					
						
							|  |  |  | 			connection: reader.connection, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		reader.worker = &noOpReaderWorker{ | 
					
						
							|  |  |  | 			connection: reader.connection, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-31 09:39:45 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-11 23:51:58 +01:00
										 |  |  | func (reader *TimeOutReader) Release() { | 
					
						
							|  |  |  | 	reader.connection = nil | 
					
						
							|  |  |  | 	reader.worker = nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 09:39:45 +01:00
										 |  |  | type timedReaderWorker struct { | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	timeout    int | 
					
						
							|  |  |  | 	connection net.Conn | 
					
						
							| 
									
										
										
										
											2015-10-31 09:39:45 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (this *timedReaderWorker) Read(p []byte) (int, error) { | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	deadline := time.Duration(this.timeout) * time.Second | 
					
						
							| 
									
										
										
										
											2015-10-31 09:39:45 +01:00
										 |  |  | 	this.connection.SetReadDeadline(time.Now().Add(deadline)) | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	nBytes, err := this.connection.Read(p) | 
					
						
							|  |  |  | 	this.connection.SetReadDeadline(emptyTime) | 
					
						
							|  |  |  | 	return nBytes, err | 
					
						
							| 
									
										
										
										
											2015-10-31 09:39:45 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type noOpReaderWorker struct { | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	connection net.Conn | 
					
						
							| 
									
										
										
										
											2015-10-31 09:39:45 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (this *noOpReaderWorker) Read(p []byte) (int, error) { | 
					
						
							| 
									
										
										
										
											2015-10-31 14:08:13 +01:00
										 |  |  | 	return this.connection.Read(p) | 
					
						
							| 
									
										
										
										
											2015-10-04 00:21:06 +02:00
										 |  |  | } |