Ejemplo n.º 1
0
        public UdpSocketServer(IAppServer <TAppSession> appServer, IPEndPoint localEndPoint, ICustomProtocol <TCommandInfo> protocol)
            : base(appServer, localEndPoint)
        {
            m_Protocol = protocol;

            if (typeof(TCommandInfo).IsSubclassOf(typeof(UdpCommandInfo)))
            {
                m_SessionKeyFromCommandInfo = true;
                m_UdpCommandInfoReader      = m_Protocol.CreateCommandReader(this.AppServer);
            }
        }
Ejemplo n.º 2
0
        void AceptNewClient(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                var client = e.AcceptSocket;
                m_TcpClientConnected.Set();

                //Get the socket for the accepted client connection and put it into the
                //ReadEventArg object user token
                SocketAsyncEventArgsProxy socketEventArgsProxy;
                if (!m_ReadWritePool.TryPop(out socketEventArgsProxy))
                {
                    AppServer.Logger.LogError("There is no enough buffer block to arrange to new accepted client!");
                    return;
                }

                var session = RegisterSession(client, new AsyncSocketSession <TAppSession, TCommandInfo>(client, m_Protocol.CreateCommandReader(AppServer)));

                if (session != null)
                {
                    session.SocketAsyncProxy = socketEventArgsProxy;
                    session.Closed          += new EventHandler <SocketSessionClosedEventArgs>(session_Closed);
                    session.Start();
                }
                else
                {
                    Async.Run(() => client.SafeCloseClientSocket(AppServer.Logger));
                }
            }
            else
            {
                m_TcpClientConnected.Set();
            }
        }
Ejemplo n.º 3
0
        private void ProcessReceivedData(IPEndPoint remoteEndPoint, byte[] receivedData)
        {
            TAppSession appSession = AppServer.GetAppSessionByIndentityKey(remoteEndPoint.ToString());

            if (appSession == null) //New session
            {
                if (m_LiveConnectionCount >= AppServer.Config.MaxConnectionNumber)
                {
                    AppServer.Logger.LogError(string.Format("Cannot accept a new UDP connection from {0}, the max connection number {1} has been exceed!",
                                                            remoteEndPoint.ToString(), AppServer.Config.MaxConnectionNumber));
                    return;
                }

                var session = RegisterSession(new UdpSocketSession <TAppSession, TCommandInfo>(m_ListenSocket, remoteEndPoint, m_Protocol.CreateCommandReader(AppServer)));

                if (session == null)
                {
                    return;
                }

                Interlocked.Increment(ref m_LiveConnectionCount);
                session.Closed += new EventHandler <SocketSessionClosedEventArgs>(session_Closed);
                session.Start();
                Async.Run(() => session.ProcessData(receivedData), (x) => AppServer.Logger.LogError(x));
            }
            else //Existing session
            {
                var session = appSession.SocketSession as UdpSocketSession <TAppSession, TCommandInfo>;
                Async.Run(() => session.ProcessData(receivedData), (x) => AppServer.Logger.LogError(x));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Starts to listen
        /// </summary>
        private void StartListen()
        {
            m_ListenSocket = new Socket(this.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                m_ListenSocket.Bind(this.EndPoint);
                m_ListenSocket.Listen(100);

                m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
            }
            catch (Exception e)
            {
                AppServer.Logger.LogError(e);
                OnStartupFinished();
                return;
            }

            m_MaxConnectionSemaphore = new Semaphore(AppServer.Config.MaxConnectionNumber, AppServer.Config.MaxConnectionNumber);

            IsRunning = true;

            OnStartupFinished();

            while (!IsStopped)
            {
                Socket client = null;

                try
                {
                    m_MaxConnectionSemaphore.WaitOne();

                    if (IsStopped)
                    {
                        break;
                    }

                    client = m_ListenSocket.Accept();
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
                catch (NullReferenceException)
                {
                    break;
                }
                catch (Exception e)
                {
                    SocketException se = e as SocketException;
                    if (se != null)
                    {
                        //A blocking operation was interrupted by a call to WSACancelBlockingCall
                        //SocketListener has been stopped normally
                        if (se.ErrorCode == 10004 || se.ErrorCode == 10038)
                        {
                            break;
                        }
                    }

                    AppServer.Logger.LogError("Socket Listener stopped unexpectly, Socket Address:" + EndPoint.Address.ToString() + ":" + EndPoint.Port, e);
                    break;
                }

                SyncSocketSession <TAppSession, TCommandInfo> session;

                try
                {
                    session = RegisterSession(client, new SyncSocketSession <TAppSession, TCommandInfo>(client, m_Protocol.CreateCommandReader(AppServer)));
                }
                catch (Exception e)
                {
                    AppServer.Logger.LogError(e);
                    continue;
                }

                if (session == null)
                {
                    Async.Run(() => client.SafeCloseClientSocket(AppServer.Logger));
                    continue;
                }

                session.Closed += new EventHandler <SocketSessionClosedEventArgs>(session_Closed);

                //Start run session asynchronous
                Async.Run(() => session.Start(), TaskCreationOptions.LongRunning, (x) => AppServer.Logger.LogError(session, x));
            }

            IsRunning = false;
        }