| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  | #if !NETFX_CORE | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | using System; | 
					
						
							| 
									
										
										
										
											2018-09-05 11:22:53 +08:00
										 |  |  |  | using System.Net; | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  | using System.Net.Sockets; | 
					
						
							|  |  |  |  | using System.Threading.Tasks; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Plane.Communication | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     /// <summary> | 
					
						
							|  |  |  |  |     /// 提供作为 TCP 客户端通信的能力。 | 
					
						
							|  |  |  |  |     /// </summary> | 
					
						
							|  |  |  |  |     public class TcpConnection : TcpConnectionBase | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         private string _remoteHostname; | 
					
						
							| 
									
										
										
										
											2023-05-01 13:57:32 +08:00
										 |  |  |  |         const int tcpReceiveBufferSize = 20 * 1024;  //40 * 1024, | 
					
						
							|  |  |  |  |         const int tcpReceiveTimeout = 1200; | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         private int _remotePort; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public TcpConnection(string remoteHostname, int remotePort = 5250) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             _remoteHostname = remoteHostname; | 
					
						
							|  |  |  |  |             _remotePort = remotePort; | 
					
						
							|  |  |  |  |             _client = new TcpClient | 
					
						
							|  |  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-05-01 13:57:32 +08:00
										 |  |  |  |                 ReceiveBufferSize = tcpReceiveBufferSize, | 
					
						
							|  |  |  |  |                 ReceiveTimeout = tcpReceiveTimeout | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  |             }; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public void Open() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             if (!IsOpen) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 try | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     _client.Connect(_remoteHostname, _remotePort); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 catch (SocketException) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     CreateClientAndConnect(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 catch (ObjectDisposedException) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     CreateClientAndConnect(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 _isBroken = false; | 
					
						
							|  |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-08-11 12:14:02 +08:00
										 |  |  |  |              | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  |             _stream = _client.GetStream(); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 11:22:53 +08:00
										 |  |  |  |         public async Task<bool> BindIP(string ip) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             bool bind = false; | 
					
						
							|  |  |  |  |             try | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 IPAddress IPLocal = IPAddress.Parse(ip); | 
					
						
							|  |  |  |  |                 _client.Client.Bind(new IPEndPoint(IPLocal, 0)); | 
					
						
							|  |  |  |  |                 bind = true; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             catch | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 bind = false; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             await Task.Delay(10).ConfigureAwait(false); | 
					
						
							|  |  |  |  |             return bind; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  |         public override async Task OpenAsync() | 
					
						
							|  |  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-03-16 14:13:14 +08:00
										 |  |  |  |             string logstr; | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  |             if (!IsOpen) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 try | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     await _client.ConnectAsync(_remoteHostname, _remotePort).ConfigureAwait(false); | 
					
						
							|  |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-09-05 11:22:53 +08:00
										 |  |  |  |                 //屏蔽掉异常处理 | 
					
						
							|  |  |  |  |                 //CreateClientAndConnectAsync会new一个TcpClient并且重新连接   | 
					
						
							|  |  |  |  |                 //之前设置的TcpClient中Socket Bind会无效,在多网卡工作时会导致断线重连的时间特别长 | 
					
						
							| 
									
										
										
										
											2020-03-16 14:13:14 +08:00
										 |  |  |  |                 catch (SocketException e) | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2020-03-16 14:13:14 +08:00
										 |  |  |  |                     logstr= e.Message; | 
					
						
							| 
									
										
										
										
											2018-09-05 11:22:53 +08:00
										 |  |  |  |                     //await CreateClientAndConnectAsync(); | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  |                 } | 
					
						
							|  |  |  |  |                 catch (ObjectDisposedException) | 
					
						
							|  |  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2018-09-05 11:22:53 +08:00
										 |  |  |  |                     //await CreateClientAndConnectAsync(); | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  |                 } | 
					
						
							|  |  |  |  |                 _isBroken = false; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             _stream = _client.GetStream(); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         private void CreateClientAndConnect() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             _client = new TcpClient(_remoteHostname, _remotePort) | 
					
						
							|  |  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-05-01 13:57:32 +08:00
										 |  |  |  |                 ReceiveBufferSize = tcpReceiveBufferSize, | 
					
						
							|  |  |  |  |                 ReceiveTimeout = tcpReceiveTimeout | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  |             }; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         private async Task CreateClientAndConnectAsync() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             _client = new TcpClient | 
					
						
							|  |  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-05-01 13:57:32 +08:00
										 |  |  |  |                 ReceiveBufferSize = tcpReceiveBufferSize,// 40 * 1024, | 
					
						
							|  |  |  |  |                 ReceiveTimeout = tcpReceiveTimeout// 1200 | 
					
						
							| 
									
										
										
										
											2017-02-27 02:02:19 +08:00
										 |  |  |  |             }; | 
					
						
							|  |  |  |  |             await _client.ConnectAsync(_remoteHostname, _remotePort).ConfigureAwait(false); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #endif |