Ejemplo 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);
                }
            }
        }
Ejemplo 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;
        }
Ejemplo n.º 3
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;
                            ShutdownServer(listenSocket);
                        }
                        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.Yield();
                    }while (true);
                }
                catch (Exception error)
                {
                    if (isListen == 0)
                    {
                        return;
                    }
                    Log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
                    Thread.Sleep(1);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取客户端请求
        /// </summary>
        private void getSocketVerify()
        {
            Socket     listenSocket = this.Socket;
            SocketLink head, newSocketLink;

            while (isListen != 0)
            {
                try
                {
NEXT:
                    newSocketLink = new SocketLink();
ACCEPT:
                    newSocketLink.Socket = listenSocket.Accept();
                    if (isListen == 0)
                    {
                        if (this.Socket != null)
                        {
                            this.Socket = null;
                            ShutdownServer(listenSocket);
                        }
                        return;
                    }
                    if (verify(newSocketLink.Socket))
                    {
                        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);
                    }
                    newSocketLink.DisposeSocket();
                    goto ACCEPT;
                }
                catch (Exception error)
                {
                    if (isListen == 0)
                    {
                        return;
                    }
                    Log.Add(AutoCSer.Log.LogType.Error, error);
                    Thread.Sleep(1);
                }
            }
        }