BenVlodgi

delegates bro

Feb 7th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1.     //OLD InitiateSendTransmission
  2.         public bool InitiateSendTransmission(Transmission transmission, Contact contact)
  3.         {
  4.             //lets see if we can actually send it
  5.             //try each IP
  6.             string id = CreateNewID();
  7.             ControlTransferRequest ctr = new ControlTransferRequest(id, CreateNewKeyForRequest(id), LocalIPAddress());
  8.  
  9.             for (int i = 0; i < 3; i++)
  10.             {
  11.                 string ipToTry;
  12.                 if (i == 0)
  13.                     ipToTry = contact.IP_Primary;
  14.                 else
  15.                     ipToTry = contact.IP_Secondary ?? contact.IP_Primary;
  16.  
  17.                 //if (SendMessage(ctr.GetBytes(), ipToTry, CONTROL_PORT) > 0)
  18.                 //return true;
  19.                 byte[] bytesToSend = ctr.GetBytes();
  20.                 int sentbytes = SendMessage(bytesToSend, ipToTry, CONTROL_PORT);
  21.                 //got these, just to compare numbers
  22.                 WriteToLog("Control message:\n# Of Bytes To Send = " + bytesToSend.Length + "\n# of Bytes Sent = " + sentbytes);
  23.  
  24.                 if (sentbytes > 0)
  25.                     return true;
  26.             }
  27.  
  28.             _sendingPublicKeysDictionary.Remove(id);
  29.             return false;
  30.         }
  31.  
  32.  
  33.  
  34.  
  35.     //NEW InitiateSendTransmission
  36.         private bool InitiateSendTransmission(Transmission transmission, Contact contact)
  37.         {
  38.             //lets see if we can actually send it
  39.             //try each IP
  40.             string id = CreateNewID();
  41.             ControlTransferRequest ctr = new ControlTransferRequest(id, CreateNewKeyForRequest(id), LocalIPAddress());
  42.  
  43.  
  44.             if (TryIPs(contact, delegate(string ip)
  45.             {
  46.                 byte[] bytesToSend = ctr.GetBytes();
  47.                 int sentbytes = SendMessage(bytesToSend, ip, CONTROL_PORT);
  48.                 //got these, just to compare numbers
  49.                 WriteToLog("Control message:\n# Of Bytes To Send = " + bytesToSend.Length + "\n# of Bytes Sent = " + sentbytes);
  50.  
  51.                 if (sentbytes > 0)
  52.                     return true;
  53.                 else return false;
  54.             }))
  55.                 return true;
  56.             else
  57.             {
  58.                 _sendingPublicKeysDictionary.Remove(id);
  59.                 return false;
  60.             }
  61.         }
  62.  
  63.         private bool TryIPs(Contact contact, Func<string, bool> doThis)
  64.         {
  65.             for (int i = 0; i < 2; i++)
  66.             {
  67.                 if (i == 0 && contact.IP_Primary != null && !string.IsNullOrEmpty(contact.IP_Primary.Trim()))
  68.                 {
  69.                     if (doThis(contact.IP_Primary)) return true;
  70.                 }
  71.                 else if (contact.IP_Secondary != null && !string.IsNullOrEmpty(contact.IP_Secondary.Trim()))
  72.                     if (doThis(contact.IP_Secondary)) return true;
  73.             }
  74.             return false;
  75.         }
Advertisement
Add Comment
Please, Sign In to add comment