Exemplo n.º 1
0
 protected void ClosePeer(StateObject state)
 {
     try
     {
         Monitor.Enter(_deleteSync);
         ulong handle = state.Handle;
         if (_useIOEvents.ContainsKey(state.Handle))
         {
             _ioEventPool.Push(_useIOEvents[handle]);
             _useIOEvents.Remove(handle);
         }
         state.Init();
         OnDisconnected(handle);
     }
     finally
     {
         Monitor.Exit(_deleteSync);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Hub for the completion of all I/O events on the underlying connection
        /// </summary>
        private void AsyncEventCompleted(object sender, SocketAsyncEventArgs e)
        {
            if (isDisposed)
            {
                return;
            }

            switch (e.SocketError)
            {
            // Was the operation successful?
            case SocketError.Success:
                switch (e.LastOperation)
                {
                case SocketAsyncOperation.Connect:
                    // Set IsConnected to true and dispatch a message to our subscriber
                    IsConnected = true;
                    Messenger.Default.Send <ConnectionMessage>(new ConnectionMessage()
                    {
                        MessageId = ConnectionMessageId.ConnectionConnected, ConnectionName = "Testing", AdditionalInformation = e.RemoteEndPoint.ToString()
                    }, connectionToken);

                    Connected();

                    // Open the flood gates, we're connected!
                    connectingResetEvent.Set();
                    connectingVerifyResetEvent.Set();
                    break;

                case SocketAsyncOperation.Receive:
                    if (e.BytesTransferred == 0)
                    {
                        if (e.LastOperation == SocketAsyncOperation.Connect)
                        {
                            connectionPool.Push(e);
                        }
                        else
                        {
                            transmitPool.Push(e);
                        }

                        // Terminate our connection.
                        IsConnected = false;
                        Messenger.Default.Send <ConnectionMessage>(new ConnectionMessage()
                        {
                            MessageId = ConnectionMessageId.ConnectionError, ConnectionName = "Testing", AdditionalInformation = e.SocketError.ToString()
                        }, connectionToken);

                        // Send our Disconnection message, and try to reconnect
                        Messenger.Default.Send <ConnectionMessage>(new ConnectionMessage()
                        {
                            MessageId = ConnectionMessageId.ConnectionDisconnected, ConnectionName = "Testing", AdditionalInformation = string.Empty
                        }, connectionToken);

                        return;
                    }
                    else
                    {
                        ReceivedData(e);
                    }
                    break;
                }

                // Put our items back on the pool.
                if (e.LastOperation == SocketAsyncOperation.Connect)
                {
                    connectionPool.Push(e);
                }
                else
                {
                    transmitPool.Push(e);
                }

                break;

            // No? Send the error message off, reset the connection, and continue on :)
            default:
                // Put our items back on the pool.
                // NOTE: This is done early here to ensure maximum efficiency when we reconnect.
                if (e.LastOperation == SocketAsyncOperation.Connect)
                {
                    connectionPool.Push(e);
                }
                else
                {
                    transmitPool.Push(e);
                }

                receivingResetEvent.Set();

                // Terminate our connection.
                IsConnected = false;
                Messenger.Default.Send <ConnectionMessage>(new ConnectionMessage()
                {
                    MessageId = ConnectionMessageId.ConnectionError, ConnectionName = "Testing", AdditionalInformation = e.SocketError.ToString()
                }, connectionToken);

                // Send our Disconnection message, and try to reconnect
                Messenger.Default.Send <ConnectionMessage>(new ConnectionMessage()
                {
                    MessageId = ConnectionMessageId.ConnectionDisconnected, ConnectionName = "Testing", AdditionalInformation = string.Empty
                }, connectionToken);

                break;
            }
        }