상속: INodeConnection, IMeshworkOperation
예제 #1
0
파일: Core.cs 프로젝트: tabrath/meshwork
        internal static void ConnectTo(ITransport transport, TransportCallback connectCallback)
        {
            if (transport == null)
            {
                throw new ArgumentNullException("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.
                LocalNodeConnection connection = new LocalNodeConnection(transport);
                transport.Operation = connection;
                transport.Network.AddConnection(connection);
                transportManager.Add(transport, delegate(ITransport bleh)
                {
                    connection.Start();
                    if (connectCallback != null)
                    {
                        connectCallback(transport);
                    }
                });
            }
            else
            {
                transportManager.Add(transport, connectCallback);
            }
        }
예제 #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)
            {
                InvalidNetworkNameError error = new InvalidNetworkNameError(c.NetworkName, network.NetworkName);
                network.SendCriticalError(messageFrom, error);
                throw error.ToException();
            }

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

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

            node.NickName = c.NickName;

            if (isReply == false)
            {
                connection.ConnectionState = ConnectionState.Authenticating;
                network.SendAuthReply(connection, messageFrom);
            }
            else
            {
                connection.SendReady();
            }
        }
예제 #3
0
        private void OnConnectionReady(LocalNodeConnection connection)
        {
            TrustedNodeInfo tnode = connection.NodeRemote.GetTrustedNode();

            lock (nodeList) {
                if (nodeList.Contains(tnode))
                {
                    nodeList.Remove(tnode);
                    nodeList.Sort(new NodeSuccessComparer());
                }
            }
        }
예제 #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();
        }
예제 #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;
                Core.Settings.SyncNetworkInfoAndSave();

                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.
                Message message = network.MessageBuilder.CreateHelloMessage();
                message.To = Network.BroadcastNodeID;
                network.SendBroadcast(message);
            }
            else
            {
                // XXX: Do we need this?
                if (connection.ReadySent == false)
                {
                    connection.SendReady();
                }
            }
        }
예제 #6
0
        private void LocalConnectionError(LocalNodeConnection c, Exception error)
        {
            try {
                // TODO: !!!!
                //(c.Properties ["ListItem"] as ConnectionListItem).ErrorText = ex.Message;

                UpdateConnectionList ();
            } catch (Exception ex) {
                LoggingService.LogError(ex);
                Gui.ShowErrorDialog (ex.ToString(), Gui.MainWindow.Window);
            }
        }
예제 #7
0
 private void network_ConnectingTo(Network network, LocalNodeConnection connection)
 {
     AddConnectionEventHandlers(connection);
 }
예제 #8
0
 private void ConnectionReady(LocalNodeConnection sender)
 {
     UpdateConnectionList();
 }
예제 #9
0
 private void Connection_PongReceived(LocalNodeConnection c)
 {
     try {
         UpdateConnectionList();
     } catch (Exception ex) {
         LoggingService.LogError(ex);
         Gui.ShowErrorDialog (ex.ToString(), Gui.MainWindow.Window);
     }
 }
예제 #10
0
 private void AddConnectionEventHandlers(LocalNodeConnection c)
 {
     c.ConnectionConnected   += (LocalNodeConnectionEventHandler)DispatchService.GuiDispatch(new LocalNodeConnectionEventHandler(ConnectionConnected));
     c.ConnectionClosed      += (LocalNodeConnectionEventHandler)DispatchService.GuiDispatch(new LocalNodeConnectionEventHandler(ConnectionClosed));
     c.PongReceived          += (LocalNodeConnectionEventHandler)DispatchService.GuiDispatch(new LocalNodeConnectionEventHandler(Connection_PongReceived));
     c.ConnectionReady       += (LocalNodeConnectionEventHandler)DispatchService.GuiDispatch(new LocalNodeConnectionEventHandler(ConnectionReady));
     c.ConnectionInfoChanged += (LocalNodeConnectionEventHandler)DispatchService.GuiDispatch(new LocalNodeConnectionEventHandler(ConnectionInfoChanged));
     c.ConnectionError       += (LocalNodeConnectionErrorEventHandler)DispatchService.GuiDispatch(new LocalNodeConnectionErrorEventHandler(LocalConnectionError));
 }
예제 #11
0
 private void ConnectionInfoChanged(LocalNodeConnection connection)
 {
     UpdateConnectionList();
 }
예제 #12
0
 public ReceivedKeyEventArgs(LocalNodeConnection connection, KeyInfo keyInfo)
 {
     this.connection = connection;
     this.keyInfo = keyInfo;
 }
예제 #13
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) {
                InvalidNetworkNameError error = new InvalidNetworkNameError(c.NetworkName, network.NetworkName);
                network.SendCriticalError (messageFrom, error);
                throw error.ToException();
            }

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

            // Update node details:
            Node node = network.Nodes[messageFrom.NodeID];
            node.NickName = c.NickName;

            if (isReply == false) {
                connection.ConnectionState = ConnectionState.Authenticating;
                network.SendAuthReply (connection, messageFrom);
            } else {
                connection.SendReady ();
            }
        }
예제 #14
0
 private void network_NewIncomingConnection(Network network, LocalNodeConnection c)
 {
     try {
         AddConnectionEventHandlers(c);
     } catch (Exception ex) {
         LoggingService.LogError(ex);
         Gui.ShowErrorDialog (ex.ToString(), Gui.MainWindow.Window);
     }
 }
예제 #15
0
 public MessageInfo(Message message, LocalNodeConnection connection)
 {
     this.Message = message;
     this.Connection = connection;
 }
예제 #16
0
 internal void ForwardMessage(Message m, LocalNodeConnection connection)
 {
     if (m.To != LocalNode.NodeID) {
         if (m.To == Network.BroadcastNodeID) {
             this.SendBroadcast(m, connection.NodeRemote);
         } else {
             this.SendRoutedMessage(m);
             return;
         }
     } else {
         throw new InvalidOperationException();
     }
 }
예제 #17
0
 internal bool RaiseReceivedKey(LocalNodeConnection connection, KeyInfo key)
 {
     return OnReceivedKey(new ReceivedKeyEventArgs(connection, key));
 }
예제 #18
0
 private void NewConnection(Network network, LocalNodeConnection connection)
 {
     connection.ConnectionReady  += connectionReadyHandler;
     connection.ConnectionClosed += connectionClosedHandler;
 }
예제 #19
0
 public MessageInfo(Message message, LocalNodeConnection connection)
 {
     this.Message    = message;
     this.Connection = connection;
 }
예제 #20
0
 public ReceivedKeyEventArgs(LocalNodeConnection connection, KeyInfo keyInfo)
     : base()
 {
     Connection = connection;
     Key        = keyInfo;
 }
예제 #21
0
파일: Core.cs 프로젝트: codebutler/meshwork
        internal static void ConnectTo(ITransport transport, TransportCallback connectCallback)
        {
            if (transport == null) {
                throw new ArgumentNullException("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.
                LocalNodeConnection connection = new LocalNodeConnection(transport);
                transport.Operation = connection;
                transport.Network.AddConnection(connection);
                transportManager.Add(transport, delegate (ITransport bleh) {
                    connection.Start();
                    if (connectCallback != null) {
                        connectCallback(transport);
                    }
                });
            } else {
                transportManager.Add (transport, connectCallback);
            }
        }
예제 #22
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;
                Core.Settings.SyncNetworkInfoAndSave();

                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.
                Message message = network.MessageBuilder.CreateHelloMessage ();
                message.To = Network.BroadcastNodeID;
                network.SendBroadcast (message);

            } else {
                // XXX: Do we need this?
                if (connection.ReadySent == false) {
                    connection.SendReady();
                }
            }
        }