Ejemplo n.º 1
0
        internal void ConnectTo(ITransport transport, TransportCallback connectCallback)
        {
            if (transport == null)
            {
                throw new ArgumentNullException(nameof(transport));
            }

            if (transport.Network == null)
            {
                throw new ArgumentNullException("transport.Network");
            }

            if (transport.ConnectionType == ConnectionType.NodeConnection)
            {
                // XXX: This doesn't belong here. Have LocalNodeConnection set this up
                // and call me with the proper callback.
                var connection = new LocalNodeConnection(transport);
                transport.Operation = connection;
                transport.Network.AddConnection(connection);
                transportManager.Add(transport, delegate
                {
                    connection.Start();
                    connectCallback?.Invoke(transport);
                });
            }
            else
            {
                transportManager.Add(transport, connectCallback);
            }
        }
Ejemplo n.º 2
0
        /*	internal void ProcessPongMessage (Node messageFrom, ulong timestamp)
         *      {*/
        //TODO: Pinging/ponging is a total mess

        /*	if (timestamp == messageFrom.TimeOfLastPing) {
         *              messageFrom.PingTime = DateTime.Now.Subtract(messageFrom.TimeOfLastPing).Milliseconds;
         *              messageFrom.RaisePongReceived(messageFrom.PingTime);
         *              //messageFrom.TimeOfLastPing = null;
         *      } else {
         *              // TODO: Do something?
         *      }*/
        // }

        /*
         * internal void ProcessAuthMessage (LocalNodeConnection connection, Node messageFrom, AuthInfo info)
         * {
         *      ProcessAuthMessage (connection, messageFrom, info, false);
         * }
         */

        internal void ProcessAuthMessage(LocalNodeConnection connection, Node messageFrom, AuthInfo c, bool isReply)
        {
            // Some checks:

            // XXX: Isn't this checked elsewhere?
            if (c.NetworkName != network.NetworkName)
            {
                var error = new InvalidNetworkNameError(c.NetworkName, network.NetworkName);
                network.SendCriticalError(messageFrom, error);
                throw error.ToException();
            }

            if (c.ProtocolVersion != Core.ProtocolVersion)
            {
                var e = new VersionMismatchError(c.ProtocolVersion.ToString());
                network.SendCriticalError(messageFrom, e);
                throw new VersionMismatchError().ToException();
            }

            // Update node details:
            var node = network.Nodes[messageFrom.NodeID];

            node.NickName = c.NickName;

            if (isReply == false)
            {
                connection.ConnectionState = ConnectionState.Authenticating;
                network.SendAuthReply(connection, messageFrom);
            }
            else
            {
                connection.SendReady();
            }
        }
Ejemplo n.º 3
0
        private void OnConnectionReady(LocalNodeConnection connection)
        {
            var tnode = connection.NodeRemote.GetTrustedNode();

            lock (nodeList) {
                if (nodeList.Contains(tnode))
                {
                    nodeList.Remove(tnode);
                    nodeList.Sort(new NodeSuccessComparer());
                }
            }
        }
Ejemplo n.º 4
0
        private void OnConnectionClosed(LocalNodeConnection connection)
        {
            connection.ConnectionReady  -= connectionReadyHandler;
            connection.ConnectionClosed -= connectionClosedHandler;

            if (connection.RemoteNodeInfo != null && IsGoodNode(connection.RemoteNodeInfo))
            {
                nodeList.Add(connection.RemoteNodeInfo);
                nodeList.Sort(new NodeSuccessComparer());
            }

            ConnectIfNeeded();
        }
Ejemplo n.º 5
0
        internal void ProcessReadyMessage(LocalNodeConnection connection, Node messageFrom)
        {
            if (connection.ConnectionState != ConnectionState.Ready)
            {
                connection.NodeRemote.RemotelyUntrusted = false;
                connection.ConnectionState = ConnectionState.Ready;
                connection.RaiseConnectionReady();
                connection.RemoteNodeInfo.LastConnected = DateTime.Now;
                network.Core.Settings.SyncNetworkInfoAndSave(network.Core);

                if (connection.ReadySent == false)
                {
                    connection.SendReady();
                }

                if (messageFrom.FinishedKeyExchange == false &&
                    messageFrom.SentKeyExchange == false)
                {
                    messageFrom.CreateNewSessionKey();
                }

                // The network needs to know about me this new connection,
                // any nodes I know about, memos, etc... so say hi to
                // everyone and let them know everything that I know.
                var message = network.MessageBuilder.CreateHelloMessage();
                message.To = Network.BroadcastNodeID;
                network.SendBroadcast(message);
            }
            else
            {
                // XXX: Do we need this?
                if (connection.ReadySent == false)
                {
                    connection.SendReady();
                }
            }
        }
Ejemplo n.º 6
0
 public MessageInfo(Message message, LocalNodeConnection connection)
 {
     Message    = message;
     Connection = connection;
 }
Ejemplo n.º 7
0
 private void NewConnection(Network network, LocalNodeConnection connection)
 {
     connection.ConnectionReady  += connectionReadyHandler;
     connection.ConnectionClosed += connectionClosedHandler;
 }
Ejemplo n.º 8
0
 public ReceivedKeyEventArgs(LocalNodeConnection connection, KeyInfo keyInfo)
 {
     this.connection = connection;
     this.keyInfo    = keyInfo;
 }