TcpClient.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. #if !NETFX_CORE || UNITY_EDITOR
  2. // TcpClient.cs
  3. //
  4. // Author:
  5. // Phillip Pearson (pp@myelin.co.nz)
  6. // Gonzalo Paniagua Javier (gonzalo@novell.com)
  7. // Sridhar Kulkarni (sridharkulkarni@gmail.com)
  8. // Marek Safar (marek.safar@gmail.com)
  9. //
  10. // Copyright (C) 2001, Phillip Pearson http://www.myelin.co.nz
  11. // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
  12. // Copyright 2011 Xamarin Inc.
  13. //
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.Collections.Generic;
  36. using System.IO;
  37. using System.Net;
  38. using System.Net.Sockets;
  39. namespace BestHTTP.PlatformSupport.TcpClient.General
  40. {
  41. // This is a little modified TcpClient class from the Mono src tree.
  42. public class TcpClient : IDisposable
  43. {
  44. enum Properties : uint
  45. {
  46. LingerState = 1,
  47. NoDelay = 2,
  48. ReceiveBufferSize = 4,
  49. ReceiveTimeout = 8,
  50. SendBufferSize = 16,
  51. SendTimeout = 32
  52. }
  53. // private data
  54. NetworkStream stream;
  55. bool active;
  56. Socket client;
  57. bool disposed;
  58. Properties values;
  59. int recv_timeout, send_timeout;
  60. int recv_buffer_size, send_buffer_size;
  61. LingerOption linger_state;
  62. bool no_delay;
  63. private void Init(AddressFamily family)
  64. {
  65. active = false;
  66. if (client != null)
  67. {
  68. client.Close();
  69. client = null;
  70. }
  71. client = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
  72. }
  73. public TcpClient()
  74. {
  75. Init(AddressFamily.InterNetwork);
  76. //client.Bind(new IPEndPoint(IPAddress.Any, 0));
  77. ConnectTimeout = TimeSpan.FromSeconds(2);
  78. }
  79. public TcpClient(AddressFamily family)
  80. {
  81. if (family != AddressFamily.InterNetwork &&
  82. family != AddressFamily.InterNetworkV6)
  83. {
  84. throw new ArgumentException("Family must be InterNetwork or InterNetworkV6", "family");
  85. }
  86. Init(family);
  87. /*IPAddress any = IPAddress.Any;
  88. if (family == AddressFamily.InterNetworkV6)
  89. any = IPAddress.IPv6Any;
  90. client.Bind(new IPEndPoint(any, 0));*/
  91. ConnectTimeout = TimeSpan.FromSeconds(2);
  92. }
  93. public TcpClient(IPEndPoint localEP)
  94. {
  95. Init(localEP.AddressFamily);
  96. //client.Bind(localEP);
  97. ConnectTimeout = TimeSpan.FromSeconds(2);
  98. }
  99. public TcpClient(string hostname, int port)
  100. {
  101. ConnectTimeout = TimeSpan.FromSeconds(2);
  102. Connect(hostname, port);
  103. }
  104. protected bool Active
  105. {
  106. get { return active; }
  107. set { active = value; }
  108. }
  109. public Socket Client
  110. {
  111. get { return client; }
  112. set
  113. {
  114. client = value;
  115. stream = null;
  116. }
  117. }
  118. public int Available
  119. {
  120. get { return client.Available; }
  121. }
  122. public bool Connected
  123. {
  124. get { return client.Connected; }
  125. }
  126. public bool IsConnected()
  127. {
  128. try
  129. {
  130. return !(Client.Poll(1, SelectMode.SelectRead) && Client.Available == 0);
  131. }
  132. catch (Exception) { return false; }
  133. }
  134. public bool ExclusiveAddressUse
  135. {
  136. get
  137. {
  138. return (client.ExclusiveAddressUse);
  139. }
  140. set
  141. {
  142. client.ExclusiveAddressUse = value;
  143. }
  144. }
  145. internal void SetTcpClient(Socket s)
  146. {
  147. Client = s;
  148. }
  149. public LingerOption LingerState
  150. {
  151. get
  152. {
  153. if ((values & Properties.LingerState) != 0)
  154. return linger_state;
  155. return (LingerOption)client.GetSocketOption(SocketOptionLevel.Socket,
  156. SocketOptionName.Linger);
  157. }
  158. set
  159. {
  160. if (!client.Connected)
  161. {
  162. linger_state = value;
  163. values |= Properties.LingerState;
  164. return;
  165. }
  166. client.SetSocketOption(
  167. SocketOptionLevel.Socket,
  168. SocketOptionName.Linger, value);
  169. }
  170. }
  171. public bool NoDelay
  172. {
  173. get
  174. {
  175. if ((values & Properties.NoDelay) != 0)
  176. return no_delay;
  177. return (bool)client.GetSocketOption(
  178. SocketOptionLevel.Tcp,
  179. SocketOptionName.NoDelay);
  180. }
  181. set
  182. {
  183. if (!client.Connected)
  184. {
  185. no_delay = value;
  186. values |= Properties.NoDelay;
  187. return;
  188. }
  189. client.SetSocketOption(
  190. SocketOptionLevel.Tcp,
  191. SocketOptionName.NoDelay, value ? 1 : 0);
  192. }
  193. }
  194. public int ReceiveBufferSize
  195. {
  196. get
  197. {
  198. if ((values & Properties.ReceiveBufferSize) != 0)
  199. return recv_buffer_size;
  200. return (int)client.GetSocketOption(
  201. SocketOptionLevel.Socket,
  202. SocketOptionName.ReceiveBuffer);
  203. }
  204. set
  205. {
  206. if (!client.Connected)
  207. {
  208. recv_buffer_size = value;
  209. values |= Properties.ReceiveBufferSize;
  210. return;
  211. }
  212. client.SetSocketOption(
  213. SocketOptionLevel.Socket,
  214. SocketOptionName.ReceiveBuffer, value);
  215. }
  216. }
  217. public int ReceiveTimeout
  218. {
  219. get
  220. {
  221. if ((values & Properties.ReceiveTimeout) != 0)
  222. return recv_timeout;
  223. return (int)client.GetSocketOption(
  224. SocketOptionLevel.Socket,
  225. SocketOptionName.ReceiveTimeout);
  226. }
  227. set
  228. {
  229. if (!client.Connected)
  230. {
  231. recv_timeout = value;
  232. values |= Properties.ReceiveTimeout;
  233. return;
  234. }
  235. client.SetSocketOption(
  236. SocketOptionLevel.Socket,
  237. SocketOptionName.ReceiveTimeout, value);
  238. }
  239. }
  240. public int SendBufferSize
  241. {
  242. get
  243. {
  244. if ((values & Properties.SendBufferSize) != 0)
  245. return send_buffer_size;
  246. return (int)client.GetSocketOption(
  247. SocketOptionLevel.Socket,
  248. SocketOptionName.SendBuffer);
  249. }
  250. set
  251. {
  252. if (!client.Connected)
  253. {
  254. send_buffer_size = value;
  255. values |= Properties.SendBufferSize;
  256. return;
  257. }
  258. client.SetSocketOption(
  259. SocketOptionLevel.Socket,
  260. SocketOptionName.SendBuffer, value);
  261. }
  262. }
  263. public int SendTimeout
  264. {
  265. get
  266. {
  267. if ((values & Properties.SendTimeout) != 0)
  268. return send_timeout;
  269. return (int)client.GetSocketOption(
  270. SocketOptionLevel.Socket,
  271. SocketOptionName.SendTimeout);
  272. }
  273. set
  274. {
  275. if (!client.Connected)
  276. {
  277. send_timeout = value;
  278. values |= Properties.SendTimeout;
  279. return;
  280. }
  281. client.SetSocketOption(
  282. SocketOptionLevel.Socket,
  283. SocketOptionName.SendTimeout, value);
  284. }
  285. }
  286. public TimeSpan ConnectTimeout { get; set; }
  287. // methods
  288. public void Close()
  289. {
  290. ((IDisposable)this).Dispose();
  291. }
  292. public void Connect(IPEndPoint remoteEP)
  293. {
  294. try
  295. {
  296. if (ConnectTimeout > TimeSpan.Zero)
  297. {
  298. // Third version, works in WebPlayer
  299. System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
  300. IAsyncResult result = client.BeginConnect(remoteEP, (res) => mre.Set(), null);
  301. active = mre.WaitOne(ConnectTimeout);
  302. if (active)
  303. client.EndConnect(result);
  304. else
  305. {
  306. try
  307. {
  308. client.Disconnect(true);
  309. }
  310. catch
  311. { }
  312. throw new TimeoutException("Connection timed out!");
  313. }
  314. // Second version with timeout, in WebPlayer can't connect:
  315. // Attempt to access a private/protected method failed. at System.Security.SecurityManager.ThrowException (System.Exception ex) [0x00000] in <filename unknown>:0
  316. /*IAsyncResult result = client.BeginConnect(remoteEP, null, null);
  317. Active = result.AsyncWaitHandle.WaitOne(ConnectTimeout, true);
  318. if (active)
  319. {
  320. client.EndConnect(result);
  321. }
  322. else
  323. {
  324. client.Close();
  325. //throw new SocketException(10060);
  326. throw new TimeoutException("Connection timed out!");
  327. }*/
  328. }
  329. else
  330. {
  331. // First(old) version, no timeout
  332. client.Connect(remoteEP);
  333. active = true;
  334. }
  335. }
  336. finally
  337. {
  338. CheckDisposed();
  339. }
  340. }
  341. public void Connect(IPAddress address, int port)
  342. {
  343. Connect(new IPEndPoint(address, port));
  344. }
  345. void SetOptions()
  346. {
  347. Properties props = values;
  348. values = 0;
  349. if ((props & Properties.LingerState) != 0)
  350. LingerState = linger_state;
  351. if ((props & Properties.NoDelay) != 0)
  352. NoDelay = no_delay;
  353. if ((props & Properties.ReceiveBufferSize) != 0)
  354. ReceiveBufferSize = recv_buffer_size;
  355. if ((props & Properties.ReceiveTimeout) != 0)
  356. ReceiveTimeout = recv_timeout;
  357. if ((props & Properties.SendBufferSize) != 0)
  358. SendBufferSize = send_buffer_size;
  359. if ((props & Properties.SendTimeout) != 0)
  360. SendTimeout = send_timeout;
  361. }
  362. public void Connect(string hostname, int port)
  363. {
  364. if (ConnectTimeout > TimeSpan.Zero)
  365. {
  366. // https://forum.unity3d.com/threads/best-http-released.200006/page-37#post-3150972
  367. System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
  368. IAsyncResult result = Dns.BeginGetHostAddresses(hostname, (res) => mre.Set(), null);
  369. bool success = mre.WaitOne(ConnectTimeout);
  370. if (success)
  371. {
  372. IPAddress[] addresses = Dns.EndGetHostAddresses(result);
  373. Connect(addresses, port, null);
  374. }
  375. else
  376. {
  377. throw new TimeoutException("DNS resolve timed out!");
  378. }
  379. }
  380. else
  381. {
  382. IPAddress[] addresses = Dns.GetHostAddresses(hostname);
  383. Connect(addresses, port, null);
  384. }
  385. }
  386. public void Connect(IPAddress[] ipAddresses, int port, HTTPRequest request)
  387. {
  388. CheckDisposed();
  389. if (ipAddresses == null)
  390. {
  391. throw new ArgumentNullException("ipAddresses");
  392. }
  393. List<IPAddress> addresses = new List<IPAddress>(ipAddresses);
  394. addresses.Sort((a, b) => a.AddressFamily - b.AddressFamily);
  395. for (int i = 0; i < addresses.Count; i++)
  396. {
  397. try
  398. {
  399. IPAddress address = addresses[i];
  400. if (address.Equals(IPAddress.Any) ||
  401. address.Equals(IPAddress.IPv6Any))
  402. {
  403. throw new SocketException((int)SocketError.AddressNotAvailable);
  404. }
  405. Init(address.AddressFamily);
  406. if (address.AddressFamily == AddressFamily.InterNetwork)
  407. {
  408. //client.Bind(new IPEndPoint(IPAddress.Any, 0));
  409. }
  410. else if (address.AddressFamily == AddressFamily.InterNetworkV6)
  411. {
  412. //client.Bind(new IPEndPoint(IPAddress.IPv6Any, 0));
  413. }
  414. else
  415. {
  416. throw new NotSupportedException("This method is only valid for sockets in the InterNetwork and InterNetworkV6 families");
  417. }
  418. if (request != null && request.IsCancellationRequested)
  419. throw new Exception("IsCancellationRequested");
  420. HTTPManager.Logger.Verbose("TcpClient", string.Format("Trying to connect to {0}:{1}", address.ToString(), port.ToString()), request.Context);
  421. Connect(new IPEndPoint(address, port));
  422. if (values != 0)
  423. {
  424. SetOptions();
  425. }
  426. try
  427. {
  428. // Enable Keep-Alive packets
  429. client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  430. /*
  431. TCP_KEEPIDLE 4 // Start keeplives after this period
  432. TCP_KEEPINTVL 5 // Interval between keepalives
  433. TCP_KEEPCNT 6 // Number of keepalives before death
  434. */
  435. //client.SetSocketOption(SocketOptionLevel.Tcp, (SocketOptionName)4, 30);
  436. //client.SetSocketOption(SocketOptionLevel.Tcp, (SocketOptionName)5, 10);
  437. }
  438. catch { }
  439. #if UNITY_WINDOWS || UNITY_EDITOR
  440. // Set the keep-alive time and interval on windows
  441. // https://msdn.microsoft.com/en-us/library/windows/desktop/dd877220%28v=vs.85%29.aspx
  442. // https://msdn.microsoft.com/en-us/library/windows/desktop/ee470551%28v=vs.85%29.aspx
  443. try
  444. {
  445. //SetKeepAlive(true, 30000, 1000);
  446. }
  447. catch{ }
  448. #endif
  449. HTTPManager.Logger.Information("TcpClient", string.Format("Connected to {0}:{1}", address.ToString(), port.ToString()), request.Context);
  450. break;
  451. }
  452. catch (Exception e)
  453. {
  454. /* Reinitialise the socket so
  455. * other properties still work
  456. * (see no-arg constructor)
  457. */
  458. Init(AddressFamily.InterNetwork);
  459. /* This is the last known
  460. * address, so re-throw the
  461. * exception
  462. */
  463. if (i == addresses.Count - 1)
  464. {
  465. throw e;
  466. }
  467. }
  468. }
  469. }
  470. public void EndConnect(IAsyncResult asyncResult)
  471. {
  472. client.EndConnect(asyncResult);
  473. }
  474. public IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback requestCallback, object state)
  475. {
  476. return client.BeginConnect(address, port, requestCallback, state);
  477. }
  478. public IAsyncResult BeginConnect(IPAddress[] addresses, int port, AsyncCallback requestCallback, object state)
  479. {
  480. return client.BeginConnect(addresses, port, requestCallback, state);
  481. }
  482. public IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCallback, object state)
  483. {
  484. return client.BeginConnect(host, port, requestCallback, state);
  485. }
  486. void IDisposable.Dispose()
  487. {
  488. Dispose(true);
  489. GC.SuppressFinalize(this);
  490. }
  491. protected virtual void Dispose(bool disposing)
  492. {
  493. if (disposed)
  494. return;
  495. disposed = true;
  496. if (disposing)
  497. {
  498. // release managed resources
  499. NetworkStream s = stream;
  500. stream = null;
  501. if (s != null)
  502. {
  503. // This closes the socket as well, as the NetworkStream
  504. // owns the socket.
  505. s.Close();
  506. active = false;
  507. s = null;
  508. }
  509. else if (client != null)
  510. {
  511. client.Close();
  512. client = null;
  513. }
  514. }
  515. }
  516. ~TcpClient()
  517. {
  518. Dispose(false);
  519. }
  520. public Stream GetStream()
  521. {
  522. try
  523. {
  524. if (stream == null)
  525. stream = new NetworkStream(client, true);
  526. return stream;
  527. }
  528. finally { CheckDisposed(); }
  529. }
  530. private void CheckDisposed()
  531. {
  532. if (disposed)
  533. throw new ObjectDisposedException(GetType().FullName);
  534. }
  535. #if UNITY_WINDOWS || UNITY_EDITOR
  536. public void SetKeepAlive(bool on, uint keepAliveTime, uint keepAliveInterval)
  537. {
  538. int size = System.Runtime.InteropServices.Marshal.SizeOf(new uint());
  539. var inOptionValues = new byte[size * 3];
  540. BitConverter.GetBytes((uint)(on ? 1 : 0)).CopyTo(inOptionValues, 0);
  541. BitConverter.GetBytes((uint)keepAliveTime).CopyTo(inOptionValues, size);
  542. BitConverter.GetBytes((uint)keepAliveInterval).CopyTo(inOptionValues, size * 2);
  543. //client.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
  544. int dwBytesRet = 0;
  545. WSAIoctl(client.Handle, /*SIO_KEEPALIVE_VALS*/ System.Net.Sockets.IOControlCode.KeepAliveValues, inOptionValues, inOptionValues.Length, /*NULL*/IntPtr.Zero, 0, ref dwBytesRet, /*NULL*/IntPtr.Zero, /*NULL*/IntPtr.Zero);
  546. }
  547. [System.Runtime.InteropServices.DllImport("Ws2_32.dll")]
  548. public static extern int WSAIoctl(
  549. /* Socket, Mode */ IntPtr s, System.Net.Sockets.IOControlCode dwIoControlCode,
  550. /* Optional Or IntPtr.Zero, 0 */ byte[] lpvInBuffer, int cbInBuffer,
  551. /* Optional Or IntPtr.Zero, 0 */ IntPtr lpvOutBuffer, int cbOutBuffer,
  552. /* reference to receive Size */ ref int lpcbBytesReturned,
  553. /* IntPtr.Zero, IntPtr.Zero */ IntPtr lpOverlapped, IntPtr lpCompletionRoutine);
  554. #endif
  555. }
  556. }
  557. #endif