TcpClient.cs 21 KB

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