| 
									
										
										
										
											2015-11-01 21:15:08 +01:00
										 |  |  | package mocks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:01:28 +01:00
										 |  |  | 	"github.com/v2ray/v2ray-core/app/dispatcher" | 
					
						
							| 
									
										
										
										
											2016-01-29 13:39:55 +00:00
										 |  |  | 	v2io "github.com/v2ray/v2ray-core/common/io" | 
					
						
							| 
									
										
										
										
											2015-11-01 21:15:08 +01:00
										 |  |  | 	v2net "github.com/v2ray/v2ray-core/common/net" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type InboundConnectionHandler struct { | 
					
						
							| 
									
										
										
										
											2016-01-31 17:01:28 +01:00
										 |  |  | 	port             v2net.Port | 
					
						
							|  |  |  | 	PacketDispatcher dispatcher.PacketDispatcher | 
					
						
							|  |  |  | 	ConnInput        io.Reader | 
					
						
							|  |  |  | 	ConnOutput       io.Writer | 
					
						
							| 
									
										
										
										
											2015-11-01 21:15:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 20:44:01 +00:00
										 |  |  | func (this *InboundConnectionHandler) Listen(port v2net.Port) error { | 
					
						
							| 
									
										
										
										
											2016-01-19 23:41:40 +01:00
										 |  |  | 	this.port = port | 
					
						
							| 
									
										
										
										
											2015-11-01 21:15:08 +01:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 23:41:40 +01:00
										 |  |  | func (this *InboundConnectionHandler) Port() v2net.Port { | 
					
						
							|  |  |  | 	return this.port | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 23:30:37 +01:00
										 |  |  | func (this *InboundConnectionHandler) Close() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 00:13:26 +02:00
										 |  |  | func (this *InboundConnectionHandler) Communicate(destination v2net.Destination) error { | 
					
						
							|  |  |  | 	ray := this.PacketDispatcher.DispatchToOutbound(destination) | 
					
						
							| 
									
										
										
										
											2015-11-01 21:15:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	input := ray.InboundInput() | 
					
						
							|  |  |  | 	output := ray.InboundOutput() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	readFinish := &sync.Mutex{} | 
					
						
							|  |  |  | 	writeFinish := &sync.Mutex{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	readFinish.Lock() | 
					
						
							|  |  |  | 	writeFinish.Lock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2016-04-18 19:01:24 +02:00
										 |  |  | 		v2reader := v2io.NewAdaptiveReader(this.ConnInput) | 
					
						
							|  |  |  | 		defer v2reader.Release() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		v2io.Pipe(v2reader, input) | 
					
						
							| 
									
										
										
										
											2016-04-18 18:44:10 +02:00
										 |  |  | 		input.Close() | 
					
						
							| 
									
										
										
										
											2015-11-01 21:15:08 +01:00
										 |  |  | 		readFinish.Unlock() | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2016-04-18 19:01:24 +02:00
										 |  |  | 		v2writer := v2io.NewAdaptiveWriter(this.ConnOutput) | 
					
						
							|  |  |  | 		defer v2writer.Release() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		v2io.Pipe(output, v2writer) | 
					
						
							| 
									
										
										
										
											2016-04-18 18:44:10 +02:00
										 |  |  | 		output.Release() | 
					
						
							| 
									
										
										
										
											2015-11-01 21:15:08 +01:00
										 |  |  | 		writeFinish.Unlock() | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	readFinish.Lock() | 
					
						
							|  |  |  | 	writeFinish.Lock() | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |