示例#1
0
 private void OnSyncAccept()
 {
     try
     {
         while (true)
         {
             var acceptSocket      = Socket.Accept();
             AcceptSocketInfo item = new AcceptSocketInfo();
             item.Socket = acceptSocket;
             item.Listen = this;
             mAcceptCallBack(item);
         }
     }
     catch (Exception e_)
     {
         Error = e_;
         if (Server.EnableLog(EventArgs.LogType.Error))
         {
             Server.Log(EventArgs.LogType.Error, null, $"{Host}@{Port} accept error {e_.Message}|{e_.StackTrace}!");
         }
         if (Server.EnableLog(EventArgs.LogType.Warring))
         {
             Server.Log(EventArgs.LogType.Error, null, $"{Host}@{Port} accept stoped!");
         }
     }
 }
示例#2
0
 private void OnListenAcceptCallBack(AcceptSocketInfo e)
 {
     if (Options.UseAccessQueue)
     {
         mAcceptDispatcher.Next().Enqueue(e);
     }
     else
     {
         Task.Run(() => AcceptProcess(e));
     }
 }
示例#3
0
        private void ConnectedProcess(AcceptSocketInfo e)
        {
            TcpSession session = new TcpSession();

            session.MaxWaitMessages   = Options.MaxWaitMessages;
            session.Socket            = e.Socket;
            session.Server            = this;
            session.Host              = e.Listen.Host;
            session.Port              = e.Listen.Port;
            session.ReceiveBufferPool = this.ReceiveBufferPool.Next();
            session.SendBufferPool    = this.SendBufferPool.Next();
            session.SSL = e.Listen.SSL;
            session.Initialization(this, null);
            session.SendEventArgs.Completed    += IO_Completed;
            session.ReceiveEventArgs.Completed += IO_Completed;
            session.LittleEndian   = Options.LittleEndian;
            session.RemoteEndPoint = e.Socket.RemoteEndPoint;
            if (this.Packet != null)
            {
                session.Packet           = this.Packet.Clone();
                session.Packet.Completed = OnPacketDecodeCompleted;
            }

            session.ReceiveDispatcher = mReceiveDispatchCenter.Next();
            //if (Options.SendQueueEnabled)
            //{
            //    session.SendDispatcher = mSendDispatchCenter.Next();
            //}
            AddSession(session);
            if (!e.Listen.SSL)
            {
                EventArgs.ConnectedEventArgs cead = new EventArgs.ConnectedEventArgs();
                cead.Server  = this;
                cead.Session = session;
                OnConnected(cead);
                if (EnableLog(LogType.Debug))
                {
                    Log(LogType.Debug, session, "{0} begin receive", session.RemoteEndPoint);
                }
                BeginReceive(session);
            }
            else
            {
                if (EnableLog(LogType.Info))
                {
                    Log(LogType.Info, session, "{0} begin ssl Authenticate", session.RemoteEndPoint);
                }
                session.CreateSSL(SslAuthenticateAsyncCallback, e.Listen, this);
            }
        }
示例#4
0
        private async Task OnAsyncAccept()
        {
            if (Server.EnableLog(EventArgs.LogType.Debug))
            {
                Server.Log(EventArgs.LogType.Debug, null, $"{Host}@{Port} begin accept");
            }
            while (true)
            {
                try
                {
                    while (Server.Status == ServerStatus.Stop)
                    {
                        System.Threading.Thread.Sleep(500);
                    }
                    var socket = await Socket.AcceptAsync();

                    AcceptSocketInfo item = new AcceptSocketInfo();
                    item.Socket = socket;
                    item.Listen = this;
                    mAcceptCallBack(item);
                    mAccetpError = 0;
                }
                catch (Exception e_)
                {
                    if (mIsDisposed)
                    {
                        break;
                    }
                    Error = e_;
                    mAccetpError++;
                    if (Server.EnableLog(EventArgs.LogType.Error))
                    {
                        Server.Log(EventArgs.LogType.Error, null, $"{Host}@{Port} accept error {e_.Message}|{e_.StackTrace}!");
                    }
                    if (mAccetpError >= 10)
                    {
                        if (Server.EnableLog(EventArgs.LogType.Warring))
                        {
                            Server.Log(EventArgs.LogType.Error, null, $"{Host}@{Port} accept stoped!");
                        }
                        Server.Status = ServerStatus.Error;
                        break;
                    }
                }
            }
        }
示例#5
0
 private void OnAcceptCompleted(object sender, SocketAsyncEventArgs e)
 {
     try
     {
         if (e.SocketError == SocketError.Success)
         {
             if (Server.EnableLog(EventArgs.LogType.Debug))
             {
                 Server.Log(EventArgs.LogType.Debug, null, $"{Host}@{Port} accept success from {e.AcceptSocket.RemoteEndPoint}");
             }
             AcceptSocketInfo item = new AcceptSocketInfo();
             item.Socket    = e.AcceptSocket;
             item.Listen    = this;
             e.AcceptSocket = null;
             mAcceptCallBack(item);
             mAccetpError = 0;
         }
         else
         {
             if (Server.EnableLog(EventArgs.LogType.Error))
             {
                 Server.Log(EventArgs.LogType.Error, null, $"{Host}@{Port} accept completed socket error {e.SocketError}!");
             }
         }
     }
     catch (Exception e_)
     {
         if (Server.EnableLog(EventArgs.LogType.Error))
         {
             Server.Log(EventArgs.LogType.Error, null, $"{Host}@{Port} accept completed error {e_.Message}|{e_.StackTrace}!");
         }
     }
     finally
     {
         if (mAsyncAccepts >= 50)
         {
             mAsyncAccepts = 0;
             Task.Run(() => { OnAsyncAccept(); });
         }
         else
         {
             OnAsyncAccept();
         }
     }
 }
示例#6
0
 private void AcceptProcess(AcceptSocketInfo e)
 {
     try
     {
         EventArgs.ConnectingEventArgs cea = new EventArgs.ConnectingEventArgs();
         cea.Server = this;
         cea.Socket = e.Socket;
         EndPoint endPoint = e.Socket.RemoteEndPoint;
         OnConnecting(cea);
         if (cea.Cancel)
         {
             if (EnableLog(LogType.Debug))
             {
                 Log(LogType.Debug, null, $"cancel {endPoint} connect");
             }
             CloseSocket(e.Socket);
         }
         else
         {
             if (e.Socket.Connected)
             {
                 ConnectedProcess(e);
                 if (EnableLog(LogType.Debug))
                 {
                     Log(LogType.Debug, null, $" {endPoint} connected");
                 }
             }
             else
             {
                 if (EnableLog(LogType.Info))
                 {
                     Log(LogType.Info, null, $"Connected process {endPoint} is disconnected");
                 }
             }
         }
     }
     catch (Exception e_)
     {
         if (EnableLog(LogType.Error))
         {
             Error(e_, null, "accept socket process error");
         }
     }
 }
示例#7
0
 private void OnSyncAccept()
 {
     while (true)
     {
         try
         {
             while (Server.Status == ServerStatus.Stop)
             {
                 System.Threading.Thread.Sleep(500);
             }
             var acceptSocket      = Socket.Accept();
             AcceptSocketInfo item = new AcceptSocketInfo();
             item.Socket = acceptSocket;
             item.Listen = this;
             mAcceptCallBack(item);
         }
         catch (Exception e_)
         {
             Error = e_;
             mAccetpError++;
             if (Server.EnableLog(EventArgs.LogType.Error))
             {
                 Server.Log(EventArgs.LogType.Error, null, $"{Host}@{Port} accept error {e_.Message}|{e_.StackTrace}!");
             }
             if (mAccetpError >= 10)
             {
                 if (Server.EnableLog(EventArgs.LogType.Warring))
                 {
                     Server.Log(EventArgs.LogType.Error, null, $"{Host}@{Port} accept stoped!");
                 }
                 Server.Status = ServerStatus.Error;
                 break;
             }
         }
     }
 }
示例#8
0
 private void OnListenAcceptCallBack(AcceptSocketInfo e)
 {
     //mAcceptDispatcher.Enqueue(e);
     Task.Run(() => AcceptProcess(e));
 }
示例#9
0
 private void OnListenAcceptCallBack(AcceptSocketInfo e)
 {
     Task.Run(() => AcceptProcess(e));
 }