Esempio n. 1
0
        /// <summary>
        /// 获取客户端请求
        /// </summary>
        private void getSocket()
        {
            Socket     listenSocket = this.Socket;
            SocketLink head, newSocketLink;

            while (isListen != 0)
            {
                try
                {
NEXT:
                    newSocketLink        = new SocketLink();
                    newSocketLink.Socket = listenSocket.Accept();
                    if (isListen == 0)
                    {
                        if (this.Socket != null)
                        {
                            this.Socket = null;
#if DotNetStandard
                            AutoCSer.Net.TcpServer.CommandBase.CloseServer(listenSocket);
#else
                            listenSocket.Dispose();
#endif
                        }
                        return;
                    }
                    do
                    {
                        if ((head = socketHead) == null)
                        {
                            newSocketLink.LinkNext = null;
                            if (Interlocked.CompareExchange(ref socketHead, newSocketLink, null) == null)
                            {
                                socketHandle.Set();
                                newSocketLink = null;
                                goto NEXT;
                            }
                        }
                        else
                        {
                            newSocketLink.LinkNext = head;
                            if (Interlocked.CompareExchange(ref socketHead, newSocketLink, head) == head)
                            {
                                newSocketLink = null;
                                goto NEXT;
                            }
                        }
                        AutoCSer.Threading.ThreadYield.YieldOnly();
                    }while (true);
                }
                catch (Exception error)
                {
                    if (isListen == 0)
                    {
                        return;
                    }
                    Log.add(AutoCSer.Log.LogType.Error, error);
                    Thread.Sleep(1);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 套接字处理
        /// </summary>
        internal void OnSocket()
        {
            ServerSocket serverSocket = null;

            while (isListen != 0)
            {
                socketHandle.Wait();
                SocketLink socket = Interlocked.Exchange(ref socketHead, null);
                do
                {
                    try
                    {
                        while (socket != null)
                        {
                            socket = socket.Start(this, ref serverSocket);
                        }
                        break;
                    }
                    catch (Exception error)
                    {
                        Log.add(AutoCSer.Log.LogType.Debug, error);
                    }
                    if (serverSocket != null)
                    {
                        serverSocket.Close();
                        serverSocket = null;
                    }
                    socket = socket.Cancel();
                }while (true);
            }
            socketHead = null;
        }