/// <summary>
        /// Event called to remove the disconnected session from the list of active connections.
        /// </summary>
        /// <param name="sender">Sender of the disconnection event.</param>
        /// <param name="e">Session events.</param>
        private void RemoveClientEvent(object sender, SessionClosedEventArgs <TSession, TConfig> e)
        {
            TSession sessionOut;

            ConnectedSessions.TryRemove(e.Session.Id, out sessionOut);
            e.Session.Closed -= RemoveClientEvent;
        }
        /// <summary>
        /// Called by the socket when a new connection has been accepted.
        /// </summary>
        /// <param name="e">Event args for this event.</param>
        private void AcceptCompleted(SocketAsyncEventArgs e)
        {
            if (MainSocket.IsBound == false)
            {
                return;
            }

            e.AcceptSocket.NoDelay = true;

            var session = CreateSession(e.AcceptSocket);


            //((ISetupSocketSession<TConfig>)session).Setup(, AsyncPool, Config, ThreadPool);

            // Add event to remove this session from the active client list.
            session.Closed += RemoveClientEvent;

            // Add this session to the list of connected sessions.
            ConnectedSessions.TryAdd(session.Id, session);

            // Start the session.
            ((ISetupSocketSession)session).Start();

            // Invoke the events.
            OnConnect(session);

            // Accept the next connection request
            StartAccept(e);
        }
示例#3
0
        /// <summary>
        /// Called internally with a session on the server closes.
        /// </summary>
        /// <param name="session">Session which closed.</param>
        /// <param name="reason">Reason the session closed.</param>
        protected override void OnClose(TSession session, SocketCloseReason reason)
        {
            TSession outSession;

            ConnectedSessions.TryRemove(session.Id, out outSession);

            session.IncomingMessage -= OnIncomingMessage;
            session.Dispose();
            base.OnClose(session, reason);
        }
        protected override void OnClose(TSession session, SocketCloseReason reason)
        {
            MainSocket.Close();

            TSession sessOut;

            // If the session is null, the connection timed out while trying to connect.
            if (session != null)
            {
                ConnectedSessions.TryRemove(Session.Id, out sessOut);
            }

            base.OnClose(session, reason);
        }
        /// <summary>
        /// Connects to the specified endpoint.
        /// </summary>
        /// <param name="endPoint">Endpoint to connect to.</param>
        public void Connect(IPEndPoint endPoint)
        {
            if (MainSocket != null && Session?.CurrentState != SocketSession <TSession, TConfig> .State.Closed)
            {
                throw new InvalidOperationException("Client is in the process of connecting.");
            }

            MainSocket = new System.Net.Sockets.Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
            {
                NoDelay = true
            };

            // Set to true if the client connection either timed out or was canceled.
            bool timedOut = false;

            _connectionTimeoutCancellation = new CancellationTokenSource();

            _connectionTimeoutTask = new Task(async() =>
            {
                try
                {
                    await Task.Delay(Config.ConnectionTimeout, _connectionTimeoutCancellation.Token);
                }
                catch
                {
                    return;
                }

                timedOut = true;
                OnClose(null, SocketCloseReason.TimeOut);
                MainSocket.Close();
            });


            var eventArg = new SocketAsyncEventArgs
            {
                RemoteEndPoint = endPoint
            };

            eventArg.Completed += (sender, args) =>
            {
                if (timedOut)
                {
                    return;
                }
                if (args.LastOperation == SocketAsyncOperation.Connect)
                {
                    // Stop the timeout timer.
                    _connectionTimeoutCancellation.Cancel();

                    Session            = CreateSession(MainSocket);
                    Session.Connected += (sndr, e) => OnConnect(Session);

                    ConnectedSessions.TryAdd(Session.Id, Session);

                    ((ISetupSocketSession)Session).Start();
                }
            };


            MainSocket.ConnectAsync(eventArg);

            _connectionTimeoutTask.Start();
        }