Beispiel #1
0
        /// <summary>
        /// 从连接池获取一个实例
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="TimeOutSeconds"></param>
        /// <returns></returns>
        public static WlnSocket GetSocket(String host, Int32 port, Int32 TimeOutSeconds = 10)
        {
            var ipaddress = strUtil.IsIP(host) ? System.Net.IPAddress.Parse(host) : new Net.Dns.DnsTool().GetIPAddressDefault(host);

            if (ipaddress.IsIPv4MappedToIPv6)
            {
                ipaddress = ipaddress.MapToIPv4();
            }
            var endpoint = new System.Net.IPEndPoint(ipaddress, port);

            lock (_lock)
            {
                try
                {
beginCheck:
                    foreach (var socket in sockets.OrderBy(a => a.LastUse))
                    {
                        if (socket.Catch || !socket.Connected)
                        {
                            sockets.Remove(socket);
                            try
                            {
                                if (socket.Connected)
                                {
                                    socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                                }
                                socket.IOControl(System.Net.Sockets.IOControlCode.KeepAliveValues, BitConverter.GetBytes(30), null);
                                socket.Close();
                            }
                            catch { }
                            goto beginCheck;
                        }
                        if (!socket.Using && socket.RemoteEndPoint.ToString() == endpoint.ToString() && socket.Connected && socket.LastUse < XCore.NowUnix - 15)
                        {
                            socket.Using   = true;
                            socket.LastUse = XCore.NowUnix;
                            return(socket);
                        }
                    }
                    var newsocket = new WlnSocket(endpoint.AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                    newsocket.Using   = true;
                    newsocket.LastUse = XCore.NowUnix;
                    newsocket.Connect(endpoint);
                    newsocket.SendTimeout    = TimeOutSeconds * 1000; //10s
                    newsocket.ReceiveTimeout = TimeOutSeconds * 1000; //10s
                    sockets.Add(newsocket);
                    return(newsocket);
                }
                catch { return(null); }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 从连接池获取一个实例
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="TimeOutSeconds"></param>
        /// <returns></returns>
        public static WlnSocket GetSocket(String host, Int32 port, Int32 TimeOutSeconds = 10)
        {
            var now      = DateTools.GetUnix();
            var endpoint = new System.Net.IPEndPoint(strUtil.IsIP(host) ? System.Net.IPAddress.Parse(host) : new Net.Dns.DnsTool().GetIPAddressDefault(host), port);

            lock (sockets)
            {
                try
                {
beginCheck:
                    foreach (var socket in sockets.OrderBy(a => a.LastUse))
                    {
                        if (socket.LastUse < now - 30 && socket.Connected)
                        {
                            try { socket.Shutdown(System.Net.Sockets.SocketShutdown.Both); } catch { }
                        }
                        if (socket.RemoteEndPoint.ToString() == endpoint.ToString() && !socket.Using && socket.Connected && socket.LastUse < now - 3)
                        {
                            socket.Using   = true;
                            socket.LastUse = now;
                            return(socket);
                        }
                        if (!socket.Connected)
                        {
                            sockets.Remove(socket);
                            try { socket.IOControl(System.Net.Sockets.IOControlCode.KeepAliveValues, BitConverter.GetBytes(30), null); } catch { }
                            socket.Dispose();
                            goto beginCheck;
                        }
                    }
                    var newsocket = new WlnSocket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                    newsocket.Using   = true;
                    newsocket.LastUse = now;
                    newsocket.Connect(endpoint);
                    newsocket.SendTimeout    = TimeOutSeconds * 1000; //10s
                    newsocket.ReceiveTimeout = TimeOutSeconds * 1000; //10s
                    sockets.Add(newsocket);
                    return(newsocket);
                }
                catch { return(null); }
            }
        }