123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- #if (!NETFX_CORE && !UNITY_WP8) || UNITY_EDITOR
- using System;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- namespace BestHTTP.PlatformSupport.TcpClient.General
- {
-
- public class TcpClient : IDisposable
- {
- enum Properties : uint
- {
- LingerState = 1,
- NoDelay = 2,
- ReceiveBufferSize = 4,
- ReceiveTimeout = 8,
- SendBufferSize = 16,
- SendTimeout = 32
- }
-
- NetworkStream stream;
- bool active;
- Socket client;
- bool disposed;
- Properties values;
- int recv_timeout, send_timeout;
- int recv_buffer_size, send_buffer_size;
- LingerOption linger_state;
- bool no_delay;
- private void Init(AddressFamily family)
- {
- active = false;
- if (client != null)
- {
- client.Close();
- client = null;
- }
- client = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
- }
- public TcpClient()
- {
- Init(AddressFamily.InterNetwork);
-
- ConnectTimeout = TimeSpan.FromSeconds(2);
- }
- public TcpClient(AddressFamily family)
- {
- if (family != AddressFamily.InterNetwork &&
- family != AddressFamily.InterNetworkV6)
- {
- throw new ArgumentException("Family must be InterNetwork or InterNetworkV6", "family");
- }
- Init(family);
-
- ConnectTimeout = TimeSpan.FromSeconds(2);
- }
- public TcpClient(IPEndPoint localEP)
- {
- Init(localEP.AddressFamily);
-
- ConnectTimeout = TimeSpan.FromSeconds(2);
- }
- public TcpClient(string hostname, int port)
- {
- ConnectTimeout = TimeSpan.FromSeconds(2);
- Connect(hostname, port);
- }
- protected bool Active
- {
- get { return active; }
- set { active = value; }
- }
- public Socket Client
- {
- get { return client; }
- set
- {
- client = value;
- stream = null;
- }
- }
- public int Available
- {
- get { return client.Available; }
- }
- public bool Connected
- {
- get { return client.Connected; }
- }
- public bool IsConnected()
- {
- try
- {
- return !(Client.Poll(1, SelectMode.SelectRead) && Client.Available == 0);
- }
- catch (Exception) { return false; }
- }
- public bool ExclusiveAddressUse
- {
- get
- {
- return (client.ExclusiveAddressUse);
- }
- set
- {
- client.ExclusiveAddressUse = value;
- }
- }
- internal void SetTcpClient(Socket s)
- {
- Client = s;
- }
- public LingerOption LingerState
- {
- get
- {
- if ((values & Properties.LingerState) != 0)
- return linger_state;
- return (LingerOption)client.GetSocketOption(SocketOptionLevel.Socket,
- SocketOptionName.Linger);
- }
- set
- {
- if (!client.Connected)
- {
- linger_state = value;
- values |= Properties.LingerState;
- return;
- }
- client.SetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.Linger, value);
- }
- }
- public bool NoDelay
- {
- get
- {
- if ((values & Properties.NoDelay) != 0)
- return no_delay;
- return (bool)client.GetSocketOption(
- SocketOptionLevel.Tcp,
- SocketOptionName.NoDelay);
- }
- set
- {
- if (!client.Connected)
- {
- no_delay = value;
- values |= Properties.NoDelay;
- return;
- }
- client.SetSocketOption(
- SocketOptionLevel.Tcp,
- SocketOptionName.NoDelay, value ? 1 : 0);
- }
- }
- public int ReceiveBufferSize
- {
- get
- {
- if ((values & Properties.ReceiveBufferSize) != 0)
- return recv_buffer_size;
- return (int)client.GetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.ReceiveBuffer);
- }
- set
- {
- if (!client.Connected)
- {
- recv_buffer_size = value;
- values |= Properties.ReceiveBufferSize;
- return;
- }
- client.SetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.ReceiveBuffer, value);
- }
- }
- public int ReceiveTimeout
- {
- get
- {
- if ((values & Properties.ReceiveTimeout) != 0)
- return recv_timeout;
- return (int)client.GetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.ReceiveTimeout);
- }
- set
- {
- if (!client.Connected)
- {
- recv_timeout = value;
- values |= Properties.ReceiveTimeout;
- return;
- }
- client.SetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.ReceiveTimeout, value);
- }
- }
- public int SendBufferSize
- {
- get
- {
- if ((values & Properties.SendBufferSize) != 0)
- return send_buffer_size;
- return (int)client.GetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.SendBuffer);
- }
- set
- {
- if (!client.Connected)
- {
- send_buffer_size = value;
- values |= Properties.SendBufferSize;
- return;
- }
- client.SetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.SendBuffer, value);
- }
- }
- public int SendTimeout
- {
- get
- {
- if ((values & Properties.SendTimeout) != 0)
- return send_timeout;
- return (int)client.GetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.SendTimeout);
- }
- set
- {
- if (!client.Connected)
- {
- send_timeout = value;
- values |= Properties.SendTimeout;
- return;
- }
- client.SetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.SendTimeout, value);
- }
- }
- public TimeSpan ConnectTimeout { get; set; }
-
- public void Close()
- {
- ((IDisposable)this).Dispose();
- }
- public void Connect(IPEndPoint remoteEP)
- {
- try
- {
- if (ConnectTimeout > TimeSpan.Zero)
- {
-
- System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
- IAsyncResult result = client.BeginConnect(remoteEP, (res) => mre.Set(), null);
- active = mre.WaitOne(ConnectTimeout);
- if (active)
- client.EndConnect(result);
- else
- {
- try
- {
- client.Close();
- }
- catch
- { }
- throw new TimeoutException("Connection timed out!");
- }
-
-
-
- }
- else
- {
-
- client.Connect(remoteEP);
- active = true;
- }
- }
- finally
- {
- CheckDisposed();
- }
- }
- public void Connect(IPAddress address, int port)
- {
- Connect(new IPEndPoint(address, port));
- }
- void SetOptions()
- {
- Properties props = values;
- values = 0;
- if ((props & Properties.LingerState) != 0)
- LingerState = linger_state;
- if ((props & Properties.NoDelay) != 0)
- NoDelay = no_delay;
- if ((props & Properties.ReceiveBufferSize) != 0)
- ReceiveBufferSize = recv_buffer_size;
- if ((props & Properties.ReceiveTimeout) != 0)
- ReceiveTimeout = recv_timeout;
- if ((props & Properties.SendBufferSize) != 0)
- SendBufferSize = send_buffer_size;
- if ((props & Properties.SendTimeout) != 0)
- SendTimeout = send_timeout;
- }
- public void Connect(string hostname, int port)
- {
- if (ConnectTimeout > TimeSpan.Zero)
- {
-
- System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
- IAsyncResult result = Dns.BeginGetHostAddresses(hostname, (res) => mre.Set(), null);
- bool success = mre.WaitOne(ConnectTimeout);
- if (success)
- {
- IPAddress[] addresses = Dns.EndGetHostAddresses(result);
- Connect(addresses, port);
- }
- else
- {
- throw new TimeoutException("DNS resolve timed out!");
- }
- }
- else
- {
- IPAddress[] addresses = Dns.GetHostAddresses(hostname);
- Connect(addresses, port);
- }
- }
- public void Connect(IPAddress[] ipAddresses, int port)
- {
- CheckDisposed();
- if (ipAddresses == null)
- {
- throw new ArgumentNullException("ipAddresses");
- }
- for (int i = 0; i < ipAddresses.Length; i++)
- {
- try
- {
- IPAddress address = ipAddresses[i];
- if (address.Equals(IPAddress.Any) ||
- address.Equals(IPAddress.IPv6Any))
- {
- throw new SocketException((int)SocketError.AddressNotAvailable);
- }
- Init(address.AddressFamily);
- if (address.AddressFamily == AddressFamily.InterNetwork)
- {
-
- }
- else if (address.AddressFamily == AddressFamily.InterNetworkV6)
- {
-
- }
- else
- {
- throw new NotSupportedException("This method is only valid for sockets in the InterNetwork and InterNetworkV6 families");
- }
- Connect(new IPEndPoint(address, port));
- if (values != 0)
- {
- SetOptions();
- }
-
- client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
- try
- {
-
-
-
- }
- catch { }
- #if UNITY_WINDOWS || UNITY_EDITOR
-
-
-
- try
- {
-
- }
- catch{ }
- #endif
- HTTPManager.Logger.Information("TcpClient", string.Format("Connected to {0}:{1}", address.ToString(), port.ToString()));
- break;
- }
- catch (Exception e)
- {
-
- Init(AddressFamily.InterNetwork);
-
- if (i == ipAddresses.Length - 1)
- {
- throw e;
- }
- }
- }
- }
- public void EndConnect(IAsyncResult asyncResult)
- {
- client.EndConnect(asyncResult);
- }
- public IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback requestCallback, object state)
- {
- return client.BeginConnect(address, port, requestCallback, state);
- }
- public IAsyncResult BeginConnect(IPAddress[] addresses, int port, AsyncCallback requestCallback, object state)
- {
- return client.BeginConnect(addresses, port, requestCallback, state);
- }
- public IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCallback, object state)
- {
- return client.BeginConnect(host, port, requestCallback, state);
- }
- void IDisposable.Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposed)
- return;
- disposed = true;
- if (disposing)
- {
-
- NetworkStream s = stream;
- stream = null;
- if (s != null)
- {
-
-
- s.Close();
- active = false;
- s = null;
- }
- else if (client != null)
- {
- client.Close();
- client = null;
- }
- }
- }
- ~TcpClient()
- {
- Dispose(false);
- }
- public Stream GetStream()
- {
- try
- {
- if (stream == null)
- stream = new NetworkStream(client, true);
- return stream;
- }
- finally { CheckDisposed(); }
- }
- private void CheckDisposed()
- {
- if (disposed)
- throw new ObjectDisposedException(GetType().FullName);
- }
- #if UNITY_WINDOWS || UNITY_EDITOR
- public void SetKeepAlive(bool on, uint keepAliveTime, uint keepAliveInterval)
- {
- int size = System.Runtime.InteropServices.Marshal.SizeOf(new uint());
- var inOptionValues = new byte[size * 3];
- BitConverter.GetBytes((uint)(on ? 1 : 0)).CopyTo(inOptionValues, 0);
- BitConverter.GetBytes((uint)keepAliveTime).CopyTo(inOptionValues, size);
- BitConverter.GetBytes((uint)keepAliveInterval).CopyTo(inOptionValues, size * 2);
-
- int dwBytesRet = 0;
- WSAIoctl(client.Handle, System.Net.Sockets.IOControlCode.KeepAliveValues, inOptionValues, inOptionValues.Length, IntPtr.Zero, 0, ref dwBytesRet, IntPtr.Zero, IntPtr.Zero);
- }
- [System.Runtime.InteropServices.DllImport("Ws2_32.dll")]
- public static extern int WSAIoctl(
- IntPtr s, System.Net.Sockets.IOControlCode dwIoControlCode,
- byte[] lpvInBuffer, int cbInBuffer,
- IntPtr lpvOutBuffer, int cbOutBuffer,
- ref int lpcbBytesReturned,
- IntPtr lpOverlapped, IntPtr lpCompletionRoutine);
- #endif
- }
- }
- #endif
|