Esempio n. 1
0
        /// <summary>
        /// Send a request of connection to all peers that are contained in the all XML-Lists if them aren't already connected with this client.
        /// Then deletes all XML-Lists.
        /// </summary>
        /// <param name="n">Max number of connections that will be opened. Set 0 to open all possible connections.</param>
        public static void ConnToPeers(int n = 0)
        {
            string[] lists = Directory.GetFiles((Global.TempDirectory + @"List\"), "List_*.xml", SearchOption.AllDirectories);

            foreach (string listPath in lists)
            {
                XmlDocument list = new XmlDocument();

                // load the Xml-List
                while (true)
                {
                    try
                    {
                        list.Load(listPath);
                        break;
                    }
                    catch (IOException)
                    {
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error to load a XML-List from " + listPath + " : " + ex.Message, "Error");
                        throw new IOException("Error to load a XML-List from " + listPath + " : " + ex.Message);
                    }
                }

                XmlNodeList nodes = list.DocumentElement.GetElementsByTagName("Peers");

                // get the single peers from the list
                int i = 0;
                foreach (XmlNode node in nodes)
                {
                    if (i < n)
                    {
                        string IP = node.SelectSingleNode("IP").InnerText;

                        // control if this peer is already connected with this client
                        if (PeersList.GetPeerByIP(IP) == null)
                        {
                            string RequestConnectionMessage = Bouncer.RequestMessage(IP);

                            MessageSender.SendConnectionRequest(RequestConnectionMessage, IP);
                        }

                        if (n != 0)
                        {
                            i++;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                // delete the list
                File.Delete(listPath);
            }
        }