Ejemplo n.º 1
0
        /// <summary>The callback function invoked when the socket detects any client data was received.</summary>
        /// <param name="asyncResult">The asynchronous result.</param>
        private void OnDataReceived(IAsyncResult asyncResult)
        {
            try
            {
                int iRx;

                if (socket.Connected)
                {
                    // Complete the BeginReceive() asynchronous call by EndReceive() method
                    // which will return the number of characters written to the stream
                    // by the client
                    iRx = socket.EndReceive(asyncResult);

                    // If the number of bytes received is 0 then something fishy is going on, so
                    // we close the socket.
                    if (iRx == 0)
                    {
                        OnConnectionDisconnect();
                    }
                    else
                    {
                        // Raise the Data Received Event. Signals that some data has arrived.
                        DataReceived?.Invoke(this, new ConnectionArgs(this));

                        // Continue the waiting for data on the Socket
                        ListenForData();
                    }
                }
            }
            catch (ObjectDisposedException)
            {
                // If we're shutting down, quietly ignore these exceptions and try to close the connection.
                OnConnectionDisconnect();
            }
            catch (ThreadAbortException)
            {
                // If we're shutting down, quietly ignore these exceptions and try to close the connection.
                OnConnectionDisconnect();
            }
            catch (Exception ex)
            {
                // In order to isolate connection-specific issues, we're going to trap the exception, log
                // the details, and kill that connection.  (Other connections and the game itself should
                // be able to continue through such situations.)
                string ip      = CurrentIPAddress == null ? "[null]" : CurrentIPAddress.ToString();
                string message = $"Exception encountered for connection:{Environment.NewLine}IP: {ip}, ID {ID}:{Environment.NewLine}{ex.ToDeepString()}";
                connectionHost.InformSubscribedSystem(message);

                // If the debugger is attached, we probably want to break now in order to better debug
                // the issue closer to where it occurred; if your debugger broke here you may want to
                // look at the stack trace to see where the exception originated.
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }

                OnConnectionDisconnect();
            }
        }
        private List <AmazonBermudaNode> GetActiveClusterBermudaNodes()
        {
            AmazonEC2 ec2;

            ec2 = AWSClientFactory.CreateAmazonEC2Client();
            var resp      = ec2.DescribeInstances(new DescribeInstancesRequest());
            var addresses = resp.DescribeInstancesResult.Reservation;

            var currentIP             = CurrentIPAddress.ToString();
            var allAmazonBermudaNodes = new List <AmazonBermudaNode>();

            foreach (var a in addresses)
            {
                foreach (var ri in a.RunningInstance.Where(i => !string.IsNullOrWhiteSpace(i.PrivateIpAddress)))
                {
                    var  newNode        = new AmazonBermudaNode();
                    bool hasNodeId      = false;
                    bool hasClusterName = false;
                    foreach (var tag in ri.Tag)
                    {
                        if (tag.Key == BERMUDA_NODE_ID_KEY)
                        {
                            newNode.AmazonInstance    = ri;
                            newNode.IsCurrentInstance = ri.PrivateIpAddress == currentIP;
                            newNode.NodeId            = Int32.Parse(tag.Value);

                            var pi = new PeerInfo();
                            pi.Id            = ri.InstanceId;
                            pi.EndPoint      = new IPEndPoint(IPAddress.Parse(ri.PrivateIpAddress), INTERNAL_PORT);
                            newNode.PeerInfo = pi;

                            hasNodeId = true;
                        }
                        else if (tag.Key == BERMUDA_CLUSTER_NAME_KEY)
                        {
                            newNode.ClusterName = tag.Value;
                            hasClusterName      = true;
                        }
                    }
                    if (hasNodeId && hasClusterName)
                    {
                        allAmazonBermudaNodes.Add(newNode);
                    }
                }
            }

            var activeClusterName         = allAmazonBermudaNodes.Single(abn => abn.IsCurrentInstance).ClusterName;
            var activeClusterBermudaNodes = allAmazonBermudaNodes.Where(abn => abn.ClusterName == activeClusterName);

            return(activeClusterBermudaNodes.ToList());
        }