Esempio n. 1
0
        /// <summary>
        /// Gets the name of the network peer by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>Network Peer implementation or null</returns>
        public INetworkPeerRemote GetNetworkPeerByName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                ThrowHelper.ThrowArgumentNullException("name");
            }

            name = name.ToLower().Trim();

            INetworkPeerRemote result = null;

            lock (mNetworkPeers)
            {
                foreach (INetworkPeerRemote peer in mNetworkPeers)
                {
                    if (peer.HostName.ToLower().Equals(name))
                    {
                        result = peer;
                        break;
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the network peer by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>The value or null</returns>
        public INetworkPeerRemote GetNetworkPeerById(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                ThrowHelper.ThrowArgumentNullException("id");
            }

            id = id.ToLower().Trim();

            INetworkPeerRemote result = null;

            lock (mNetworkPeers)
            {
                foreach (INetworkPeerRemote peer in mNetworkPeers)
                {
                    if (peer.Id.ToLower().Equals(id))
                    {
                        result = peer;
                        break;
                    }
                }
            }

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the name of the network peer by. Search in global list.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>The Network Peer Remote</returns>
        public static INetworkPeerRemote GetNetworkPeerByName(string name)
        {
            NetworkManager.Instance.DoInitializationCheck();
            if (string.IsNullOrEmpty(name))
            {
                ThrowHelper.ThrowArgumentNullException("name");
            }

            INetworkPeerRemote result = null;

            foreach (INetworkPeerRemote peer in KnownNetworkPeers)
            {
                if (peer.HostName.Equals(name))
                {
                    if (result == null)
                    {
                        result = peer;
                        if (peer.NetworkContext.Name.Equals(NetworkManager.Instance.InternalCurrentNetworkContext.Name))
                        {
                            break;
                        }
                    }
                    else if (peer.NetworkContext.Name.Equals(NetworkManager.Instance.InternalCurrentNetworkContext.Name))
                    {
                        result = peer;
                        break;
                    }
                }
            }

            return(result);
        }
Esempio n. 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NetworkPeerContextEventArgs"/> class.
 /// </summary>
 /// <param name="networkPeer">The network peer.</param>
 public NetworkPeerContextEventArgs(INetworkPeerRemote networkPeer)
 {
     if (networkPeer == null)
     {
         ThrowHelper.ThrowArgumentNullException("networkPeer");
     }
     this.mNetworkPeer = networkPeer;
 }
Esempio n. 5
0
 private void SetNodeFont(TreeNode node, INetworkPeerRemote peer)
 {
     if (peer.Distance == 0)
     {
         node.ForeColor = Color.Red;
     }
     else if (peer.Distance == 1)
     {
         node.ForeColor = Color.Green;
     }
     else
     {
         node.ForeColor = Color.Blue;
     }
 }
Esempio n. 6
0
        private void btDisconnectActive_Click(object sender, EventArgs e)
        {
            string   id      = tcDomains.SelectedTab.Text;
            Wrapper  wrapper = mWrapper[id];
            TreeView tv      = mTreeViewsById[id];

            if (tv.SelectedNode != null && tv.SelectedNode.Parent != null)
            {
                INetworkPeerRemote peer = (INetworkPeerRemote)tv.SelectedNode.Tag;
                INetworkConnection c    = peer.ActiveNetworkConnection;
                if (c != null)
                {
                    c.Close();
                }
            }
        }
Esempio n. 7
0
 private void TreeView_AfterSelect(object sender, TreeViewEventArgs e)
 {
     if (e.Node.Parent != null)
     {
         string             id      = tcDomains.SelectedTab.Text;
         Wrapper            wrapper = mWrapper[id];
         INetworkPeerRemote peer    = (INetworkPeerRemote)e.Node.Tag;
         ListView           lv      = mListViewNetworkConnections[id];
         lv.Items.Clear();
         foreach (INetworkConnection c in peer.NetworkConnections)
         {
             lv.Items.Add(new ListViewItem(c.Id.ToString())
             {
                 Tag = c
             });
         }
     }
 }
Esempio n. 8
0
 private void Callback(IAsyncResult result)
 {
     try
     {
         UdpBroadcastMessage message = null;
         IPEndPoint          ep      = mBroadcastEp;
         using (MemoryStream ms = new MemoryStream(mUdpClient.EndReceive(result, ref ep)))
         {
             ms.Position = 0;
             message     = mMessageFormatter.Read(ms);
             ms.SetLength(0);
         }
         if (LOGGER.IsInfoEnabled)
         {
             LOGGER.Info(string.Format("BROADCAST_SERVER, a broadcast message arrived from '{0}'.", message.SenderId));
         }
         if (NetworkManager.Instance.InternalLocalhost.Id.Equals(message.SenderId))
         {
             if (LOGGER.IsInfoEnabled)
             {
                 LOGGER.Info("BROADCAST_SERVER, this broadcast message arrived from me.");
             }
         }
         else
         {
             if (NetworkManager.Instance.NetworkContextRuleManager.CheckSeparation(NetworkManager.Instance.InternalLocalhost.NetworkContext.Name,
                                                                                   message.NetworkContextName))
             {
                 // láthatom ezt a context-et, megpróbálok rácsatlakozni
                 INetworkPeerRemote peer = NetworkPeerContext.GetNetworkPeerById(message.SenderId);
                 if (peer == null || (peer != null && peer.Distance != 1))
                 {
                     // nincs közvetlen kapcsolatom ilyen peer-el
                     mThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ConnectionTask), message);
                 }
                 else
                 {
                     if (LOGGER.IsInfoEnabled)
                     {
                         LOGGER.Info(string.Format("BROADCAST_SERVER, this is a known peer with direct network connection. No need to establish a new. PeerId: '{0}'.", peer.Id));
                     }
                 }
             }
             else
             {
                 if (LOGGER.IsInfoEnabled)
                 {
                     LOGGER.Info(string.Format("BROADCAST_SERVER, network context separation is active between '{0}' and '{1}'.", NetworkManager.Instance.InternalLocalhost.NetworkContext.Name, message.NetworkContextName));
                 }
             }
         }
     }
     catch (Exception ex)
     {
         if (LOGGER.IsErrorEnabled)
         {
             LOGGER.Error(string.Format("BROADCAST_SERVER, failed to receive a broadcast message. Reason: {0}", ex.Message));
         }
     }
     finally
     {
         BeginReceive();
     }
 }
Esempio n. 9
0
        private static void ConnectionTask(object state)
        {
            UdpBroadcastMessage message = (UdpBroadcastMessage)state;

            if (LOGGER.IsDebugEnabled)
            {
                LOGGER.Debug(string.Format("BROADCAST_SERVER, processing message of sender, id: {0}", message.SenderId));
            }
            if (message.TCPServers != null && message.TCPServers.Length > 0)
            {
                foreach (AddressEndPoint ep in message.TCPServers)
                {
                    if (ep.AddressFamily == AddressFamily.InterNetwork || (ep.AddressFamily == AddressFamily.InterNetworkV6 && NetworkManager.Instance.InternalConfiguration.Settings.EnableIPV6))
                    {
                        try
                        {
                            if (LOGGER.IsInfoEnabled)
                            {
                                LOGGER.Info(string.Format("BROADCAST_SERVER, connecting to [{0}] with address [{1}] and port {2}.", message.SenderId, ep.Host, ep.Port));
                            }
                            Synapse.NetworkStream stream = NetworkManager.Instance.InternalNetworkManager.Connect(ep);
                            stream.SendBufferSize    = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketSendBufferSize;
                            stream.ReceiveBufferSize = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketReceiveBufferSize;
                            stream.NoDelay           = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelNoDelay;
                            NetworkPeerRemote remotePeer = NetworkManager.Instance.ProcessConnection(stream, NetworkManager.Instance.InternalConfiguration.Settings.DefaultConnectionTimeoutInMS, true, !NetworkManager.Instance.InternalConfiguration.Settings.EnableMultipleConnectionWithNetworkPeers);
                            if (NetworkManager.Instance.IsShutdown)
                            {
                                stream.Close();
                                break;
                            }
                            if (remotePeer != null && remotePeer.Id.Equals(message.SenderId))
                            {
                                // létrejött a kapcsolat, tovább nem próbálkozunk
                                if (LOGGER.IsInfoEnabled)
                                {
                                    LOGGER.Info(string.Format("BROADCAST_SERVER, successfully connected to '{0}' on TCP server.", message.SenderId));
                                }
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            if (LOGGER.IsErrorEnabled)
                            {
                                LOGGER.Error(string.Format("BROADCAST_SERVER, failed to connect to [{0}] with address [{1}] and port {2}. Reason: {3}", message.SenderId, ep.Host, ep.Port, ex.Message));
                            }
                        }
                    }
                    else
                    {
                        if (LOGGER.IsInfoEnabled)
                        {
                            LOGGER.Info(string.Format("BROADCAST_SERVER, connecting to [{0}] with address [{1}] not allowed. IPV6 protocol disabled.", message.SenderId, ep.Host));
                        }
                    }
                }
            }
            INetworkPeerRemote peer = NetworkPeerContext.GetNetworkPeerById(message.SenderId);

            if (peer == null || (peer != null && peer.Distance != 1))
            {
                if (message.NATGateways != null && message.NATGateways.Length > 0)
                {
                    foreach (AddressEndPoint ep in message.NATGateways)
                    {
                        if (ep.AddressFamily == AddressFamily.InterNetwork || (ep.AddressFamily == AddressFamily.InterNetworkV6 && NetworkManager.Instance.InternalConfiguration.Settings.EnableIPV6))
                        {
                            try
                            {
                                if (LOGGER.IsInfoEnabled)
                                {
                                    LOGGER.Info(string.Format("BROADCAST_SERVER, connecting to [{0}] with NAT address [{1}] and port {2}.", message.SenderId, ep.Host, ep.Port));
                                }
                                Synapse.NetworkStream stream = NetworkManager.Instance.InternalNetworkManager.Connect(ep);
                                stream.SendBufferSize    = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketSendBufferSize;
                                stream.ReceiveBufferSize = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketReceiveBufferSize;
                                stream.NoDelay           = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelNoDelay;
                                NetworkPeerRemote remotePeer = NetworkManager.Instance.ProcessConnection(stream, NetworkManager.Instance.InternalConfiguration.Settings.DefaultConnectionTimeoutInMS, true, !NetworkManager.Instance.InternalConfiguration.Settings.EnableMultipleConnectionWithNetworkPeers);
                                if (NetworkManager.Instance.IsShutdown)
                                {
                                    stream.Close();
                                    break;
                                }
                                if (remotePeer != null && remotePeer.Id.Equals(message.SenderId))
                                {
                                    // létrejött a kapcsolat, tovább nem próbálkozunk
                                    if (LOGGER.IsInfoEnabled)
                                    {
                                        LOGGER.Info(string.Format("BROADCAST_SERVER, successfully connected to '{0}' on NAT address.", message.SenderId));
                                    }
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                if (LOGGER.IsErrorEnabled)
                                {
                                    LOGGER.Error(string.Format("BROADCAST_SERVER, failed to connect to [{0}] with address [{1}] and port {2}. Reason: {3}", message.SenderId, ep.Host, ep.Port, ex.Message));
                                }
                            }
                        }
                        else
                        {
                            if (LOGGER.IsInfoEnabled)
                            {
                                LOGGER.Info(string.Format("BROADCAST_SERVER, connecting to [{0}] with address [{1}] not allowed. IPV6 protocol disabled.", message.SenderId, ep.Host));
                            }
                        }
                    }
                }
            }
        }