/// <summary> /// Send a message to a peer still connected. /// </summary> /// <param name="Message">Message in String(UTF16 encode only).</param> /// <param name="PeerIP">IP:Port</param> /// <returns>If return False the sending is failed.</returns> public static void Send(string Message, string PeerIP) { Thread t = new Thread(new ParameterizedThreadStart(delegate { // convert the UTF16 to Byte[] byte[] Message_Byte = new byte[Message.Length * 2]; Message_Byte = ASCIIEncoding.Unicode.GetBytes(Message); PeersList.Peer peer = PeersList.GetPeerByIP(PeerIP); try { NetworkStream stream = peer.Client.GetStream(); stream.Write((byte[])Message_Byte, 0, ((byte[])Message_Byte).Length); } catch { // remove the peer PeersList.RemovePeer(PeerIP); Debug.WriteLine("A message hasn't been sent, probably the peer is offline or disconnected", "Error"); //MessageBox.Show("ERROR: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // DEBUG } })); t.Name = "MessageSender"; t.IsBackground = true; t.Start(); }
/// <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); } }
/// <summary> /// Send a message to a peer still connected ( USE THIS FOR SENDING MESSAGE WITH BINARY PARTS ) /// </summary> /// <param name="IMessage">Message in IMessage interface.</param> /// <param name="PeerIP">IP:Port</param> /// <returns>If return False the sending is failed.</returns> public static void Send(Messages.IMessage IMessage, string PeerIP) { byte[] Message_Byte = IMessage.MessageByte; PeersList.Peer peer = PeersList.GetPeerByIP(PeerIP); try { NetworkStream stream = peer.Client.GetStream(); stream.Write((byte[])Message_Byte, 0, ((byte[])Message_Byte).Length); } catch { // remove the peer PeersList.RemovePeer(PeerIP); Debug.WriteLine("A message hasn't been sent, probably the peer is offline or disconnected", "Error"); //MessageBox.Show("ERROR: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // DEBUG } }