using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 常用工具集.Utility.Network
{
    public class TimeOutSocket
    {
        private bool IsConnectionSuccessful = false;
        private Exception socketexception;
        private ManualResetEvent TimeoutObject = new ManualResetEvent(false);
        private string ip;
        private int port;
        private int timeout;
        private TcpClient tcpClient;
        public TimeOutSocket(string hostname, int port, int timeout_milliseconds)
        {
            this.ip = hostname;
            this.port = port;
            this.timeout = timeout_milliseconds;
        }

        public TcpClient Connect()
        {
            TimeoutObject.Reset();
            socketexception = null;

            TcpClient tcpclient = new TcpClient();
            tcpclient.BeginConnect(this.ip, this.port, new AsyncCallback(CallBackMethod), tcpclient);

            if (TimeoutObject.WaitOne(timeout, false))
            {
                if (IsConnectionSuccessful)
                {
                    return tcpclient;
                }
                else
                {
                    throw socketexception;
                }
            }
            else
            {
                tcpclient.Close();
                throw new TimeoutException("TimeOut Exception");
            }
        }

        public void Disconnect()
        {
            tcpClient.Close();
        }
        private void CallBackMethod(IAsyncResult asyncresult)
        {
            try
            {
                IsConnectionSuccessful = false;
                TcpClient tcpclient = asyncresult.AsyncState as TcpClient;

                if (tcpclient.Client != null)
                {
                    tcpclient.EndConnect(asyncresult);
                    IsConnectionSuccessful = true;
                }
            }
            catch (Exception ex)
            {
                IsConnectionSuccessful = false;
                socketexception = ex;
            }
            finally
            {
                TimeoutObject.Set();
            }
        }
    }
}
