Example #1
0
        private async void AcceptIncomingClients()
        {
            try
            {
                while (!_stop.Wait(0))
                {
                    try
                    {
                        var tcpClient = await _tcpListener.AcceptTcpClientAsync();

                        var gossipConnection = new GossipConnection(tcpClient)
                        {
                            LocalNodeId = NodeConfig.NodeId
                        };
                        OnClientConnectionAccepted(this, gossipConnection);

                        var authenticationAwaiter = gossipConnection.RequestAuthentication(Authenticator).GetAwaiter();
                        authenticationAwaiter.OnCompleted(() => ProcessAuthenticationResponse(authenticationAwaiter, gossipConnection));
                    }
                    catch (ObjectDisposedException)
                    {
                        break;
                    }
                    catch (Exception ex)
                    {
                        OnAcceptException(this, ex);
                    }
                }
            }
            finally
            {
                _stopped.Set();
            }
        }
        private GossipConnection OpenNewConnection()
        {
            if (_skipConnectingUntil != DateTime.MinValue &&
                GossipTimestampProvider.CurrentTimestamp < _skipConnectingUntil)
            {
                return(null);
            }

            try
            {
                var task = GossipConnection.ConnectAsync(LocalNodeId, RemoteEndPoint, Authenticator, OnDisconnect);
                if (!task.Wait(ConnectionTimeout) || task.Result == null)
                {
                    Interlocked.Increment(ref _consecutiveConnectionErrors);
                    SkipNewConnectionsForAWhile();
                    OnConnectionFailed(this, new TimeoutException());
                    return(null);
                }
                var connection = task.Result;
                _allConnections[connection] = null;
                OnConnectionSuccess(this, connection);
                return(connection);
            }
            catch (Exception ex)
            {
                Interlocked.Increment(ref _consecutiveConnectionErrors);
                SkipNewConnectionsForAWhile();
                OnConnectionFailed(this, ex);
                return(null);
            }
        }
Example #3
0
        public static GossipConnectionSession FromOpenConnection(GossipConnection connection, Action <GossipConnection> disposeAction = null)
        {
            var session = new GossipConnectionSession(connection);

            session._disposeAction = disposeAction;
            return(session);
        }
Example #4
0
        private async void AcceptIncomingClients()
        {
            try
            {
                while (!_stop.Wait(0))
                {
                    try
                    {
                        var tcpClient = await _tcpListener.AcceptTcpClientAsync();
                        var gossipConnection = new GossipConnection(tcpClient) { LocalNodeId = NodeConfig.NodeId };
                        OnClientConnectionAccepted(this, gossipConnection);

                        var authenticationAwaiter = gossipConnection.RequestAuthentication(Authenticator).GetAwaiter();
                        authenticationAwaiter.OnCompleted(() => ProcessAuthenticationResponse(authenticationAwaiter, gossipConnection));
                    }
                    catch (ObjectDisposedException)
                    {
                        break;
                    }
                    catch (Exception ex)
                    {
                        OnAcceptException(this, ex);
                    }
                }
            }
            finally
            {
                _stopped.Set();
            }
        }
Example #5
0
        public GossipClusterMember Join(GossipConnection connection, IPEndPoint endPoint, IGossipAuthenticator authenticator, params string[] tags)
        {
            var clusterMember = _members.GetOrAdd(connection.RemoteNodeId, id => new GossipClusterMember(NodeConfig.NodeId, connection.RemoteNodeId, endPoint, authenticator, tags));

            clusterMember.AttachOpenConnection(connection);
            return(clusterMember);
        }
 private void CloseSession(GossipConnection connection)
 {
     if (connection == null)
     {
         return;
     }
     _availableConnections.Add(connection);
 }
Example #7
0
        private void ClientConnectionAuthenticationSucceeded(GossipConnection gossipConnection)
        {
            var gossipMember = Cluster.Join(gossipConnection, new IPEndPoint(IPAddress.Any, 0), Authenticator);

            gossipConnection.OnMessageReceived += (gc, msg) => OnMessageReceived(this, gc, msg);
            gossipMember.OnConnectionSuccess   += (m, c) =>
                                                  c.OnMessageReceived += (gc, msg) => OnMessageReceived(this, gc, msg);
        }
Example #8
0
 private static void ProcessSimpleTextMessage(GossipNode node, GossipConnection connection, GossipMessage message)
 {
     var rawMessage = message as RawGossipMessage;
     if (rawMessage == null) return;
     if (rawMessage.MessageType == 1)
     {
         Debug.WriteLine(String.Format("Node received: {0}", node.NodeConfig.NodeId.ToHexStringLower()));
         Debug.WriteLine("Message Size: {0} bytes", rawMessage.Size);
         Debug.WriteLine(String.Format("Message String: {0}", Encoding.UTF8.GetString(rawMessage.Buffer)));
     }
 }
Example #9
0
        private static void ProcessSimpleTextMessage(GossipNode node, GossipConnection connection, GossipMessage message)
        {
            var rawMessage = message as RawGossipMessage;

            if (rawMessage == null)
            {
                return;
            }
            if (rawMessage.MessageType == 1)
            {
                Debug.WriteLine(String.Format("Node received: {0}", node.NodeConfig.NodeId.ToHexStringLower()));
                Debug.WriteLine("Message Size: {0} bytes", rawMessage.Size);
                Debug.WriteLine(String.Format("Message String: {0}", Encoding.UTF8.GetString(rawMessage.Buffer)));
            }
        }
        public static async Task <GossipConnection> ConnectAsync(ulong localNodeId, IPEndPoint remoteEndPoint, IGossipAuthenticator authenticator, Action <GossipConnection> onDisconnected = null)
        {
            var client = new TcpClient();
            await client.ConnectAsync(remoteEndPoint.Address, remoteEndPoint.Port);

            var connection = new GossipConnection(client);

            connection.LocalNodeId = localNodeId;
            if (onDisconnected != null)
            {
                connection.OnDisconnected += onDisconnected;
            }
            await connection.RespondToAuthenticationRequestAsync(authenticator);

            return(connection);
        }
Example #11
0
        private void ProcessInfo(GossipNode node, GossipConnection connection, GossipMessage message)
        {
            var rawMessage = message as RawGossipMessage;
            string msg = Encoding.UTF8.GetString(rawMessage.Buffer);
            if (msg == "")
            {
                Console.WriteLine("First Handshake");

                JoinCluster(node.NodeConfig.BindToEndPoint);
                string senMsg = JsonConvert.SerializeObject(RingInfos);

                Gossip.Cluster.BroadcastMessageAsync(new RawGossipMessage(1, Encoding.UTF8.GetBytes(senMsg))).Wait();
            }
            else
            {
                Console.WriteLine("Second Handshake");

                var ringInfos = JsonConvert.DeserializeObject<List<RingInfo>>(msg);

                RingInfos = ringInfos;
            }
        }
 private GossipConnectionSession(GossipConnection connection)
 {
     Connection = connection;
 }
 public static GossipConnectionSession FromOpenConnection(GossipConnection connection, Action<GossipConnection> disposeAction = null)
 {
     var session = new GossipConnectionSession(connection);
     session._disposeAction = disposeAction;
     return session;
 }
        public void AttachOpenConnection(GossipConnection connection)
        {
            _allConnections.GetOrAdd(connection, c => null);
            _availableConnections.Add(connection);

        }
Example #15
0
 public static async Task<GossipConnection> ConnectAsync(ulong localNodeId, IPEndPoint remoteEndPoint, IGossipAuthenticator authenticator, Action<GossipConnection> onDisconnected = null)
 {
     var client = new TcpClient();
     await client.ConnectAsync(remoteEndPoint.Address, remoteEndPoint.Port);
     var connection = new GossipConnection(client);
     connection.LocalNodeId = localNodeId;
     if (onDisconnected != null) connection.OnDisconnected += onDisconnected;
     await connection.RespondToAuthenticationRequestAsync(authenticator);
     return connection;
 }
 private void CloseSession(GossipConnection connection)
 {
     if (connection == null) return;
     _availableConnections.Add(connection);
 }
 private void OnDisconnect(GossipConnection connection)
 {
     object value;
     _allConnections.TryRemove(connection, out value);
 }
Example #18
0
 private void ProcessAuthenticationResponse(TaskAwaiter<bool?> authenticationAwaiter, GossipConnection gossipConnection)
 {
     bool? result;
     try
     {
         result = authenticationAwaiter.GetResult();
     }
     catch (IOException)
     {
         return;
     }
     if (!result.HasValue) return;
     if (!result.Value)
     {
         OnClientConnectionAuthenticationFailed(this, gossipConnection);
         return;
     }
     ClientConnectionAuthenticationSucceeded(gossipConnection);
     OnClientConnectionAuthenticationSucceeded(this, gossipConnection);
 }
Example #19
0
 private void ClientConnectionAuthenticationSucceeded(GossipConnection gossipConnection)
 {
     var gossipMember = Cluster.Join(gossipConnection, new IPEndPoint(IPAddress.Any, 0), Authenticator);
     gossipConnection.OnMessageReceived += (gc, msg) => OnMessageReceived(this, gc, msg);
     gossipMember.OnConnectionSuccess += (m, c) =>
         c.OnMessageReceived += (gc, msg) => OnMessageReceived(this, gc, msg);
 }
Example #20
0
 public GossipClusterMember Join(GossipConnection connection, IPEndPoint endPoint, IGossipAuthenticator authenticator, params string[] tags)
 {
     var clusterMember = _members.GetOrAdd(connection.RemoteNodeId, id => new GossipClusterMember(NodeConfig.NodeId, connection.RemoteNodeId, endPoint, authenticator, tags));
     clusterMember.AttachOpenConnection(connection);
     return clusterMember;
 }
        private void OnDisconnect(GossipConnection connection)
        {
            object value;

            _allConnections.TryRemove(connection, out value);
        }
Example #22
0
 private GossipConnectionSession(GossipConnection connection)
 {
     Connection = connection;
 }
Example #23
0
        private void ProcessAuthenticationResponse(TaskAwaiter <bool?> authenticationAwaiter, GossipConnection gossipConnection)
        {
            bool?result;

            try
            {
                result = authenticationAwaiter.GetResult();
            }
            catch (IOException)
            {
                return;
            }
            if (!result.HasValue)
            {
                return;
            }
            if (!result.Value)
            {
                OnClientConnectionAuthenticationFailed(this, gossipConnection);
                return;
            }
            ClientConnectionAuthenticationSucceeded(gossipConnection);
            OnClientConnectionAuthenticationSucceeded(this, gossipConnection);
        }
 public void AttachOpenConnection(GossipConnection connection)
 {
     _allConnections.GetOrAdd(connection, c => null);
     _availableConnections.Add(connection);
 }