void doRendezvous(MessageResponseDelegate response) { response?.Invoke("Attempting to initiate rendezvous connection."); response?.Invoke("Binding to random socket."); System.Net.Sockets.Socket udp = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp); udp.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)); response?.Invoke("Successfully bound to UDP endpoint " + udp.LocalEndPoint.ToString()); response?.Invoke("Sending UDP punch."); byte[] b = App.serializer.serialize(new Commands.BeginPunchCommand() { myId = App.theCore.id, port = (ushort)((System.Net.IPEndPoint)udp.LocalEndPoint).Port }); App.udpSend(b, actualEndpoint); response?.Invoke("Waiting for UDP response."); rendezvousSemaphore.WaitOne(); response?.Invoke("Received a UDP response! Remote endpoint is " + rendezvousAddress.ToString()); response?.Invoke("Binding UDT to UDP socket."); Udt.Socket s = new Udt.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream); s.ReuseAddress = true; s.Bind(udp); s.Rendezvous = true; response?.Invoke("Performing UDT control rendezvous..."); try { s.Connect(rendezvousAddress); } catch { return; } while (s.State == Udt.SocketState.Connecting) { System.Threading.Thread.Sleep(10); } controlConnection = new UdtOutgoingConnection(s, udp); dataConnection = controlConnection; response?.Invoke("Rendezvous connection successful!"); controlConnection.send(new Model.Commands.GetFileListing("/")); dataConnection.commandReceived += commandReceived; controlConnection.commandReceived += commandReceived; }
private async Task <Message> GenerateResponse(Bill bill, ITelegramBotClient client, bool modifyExistingMessage = false) { MessageResponseDelegate responseDelegate = async(message, replyMarkup) => { if (!modifyExistingMessage) { return(await client.SendTextMessageAsync(bill.ChatId, message, false, false, 0, replyMarkup)); } try { return (await client.EditMessageTextAsync(bill.ChatId, bill.MessageId, message, ParseMode.Default, false, replyMarkup)); } catch (Exception) { return(null); } }; var users = storageManager.GetChatUsers(bill.ChatId); switch (bill.CurrentStatus) { case Bill.Status.SetAmountIntegeral: case Bill.Status.SetAmountFractional: return(await responseDelegate("Amount: " + bill.AmountString, KeyboardMarkupHelpers.CreateDigitInlineKeyboardMarkup(command))); case Bill.Status.SetSharedWith: return(await responseDelegate("Amount: " + bill.Amount + "\nSelected Users:", KeyboardMarkupHelpers.CreateUserSelectionKeyboardMarkup(command, users, bill.SharedWith))); case Bill.Status.Sealed: return(await responseDelegate("Amount: " + bill.AmountString + "\nSelected Users: " + string.Join(", ", users.Where(u => bill.SharedWith.Contains(u.Id)).Select(u => u.LastName + ' ' + u.FirstName)) + "\nTestIssue: \n https://github.com/RoommateX/test/issues/16", null)); default: throw new ArgumentOutOfRangeException(); } }
void doReverseConnection(MessageResponseDelegate response) { response?.Invoke("Attempting to initiate reverse connection."); byte[] b = App.serializer.serialize(new Commands.ConnectToMe() { myId = App.theCore.id }); App.udpSend(b, b.Length, actualEndpoint); System.Threading.Thread t = new System.Threading.Thread(delegate() { System.Threading.Thread.Sleep(15000); if (dataConnection == null || controlConnection == null) { response?.Invoke("Failed to initiate reverse connection. Attempting rendezvous connection..."); doRendezvous(response); } }); t.Name = "Reverse connection timeout thread"; t.IsBackground = true; t.Start(); }
public void createConnection(MessageResponseDelegate response = null) { bool createData = true; if (dataConnection != null) { if (dataConnection.connected) { createData = false; } } bool createControl = true; if (controlConnection != null) { if (controlConnection.connected) { createControl = false; } } if (createControl == false && createData == false) { return; } if (id == App.theCore.id) { response?.Invoke("Loopback peer found, creating loopback connection..."); if (createControl) { controlConnection = new LoopbackOutgoingConnection(); } if (createData) { dataConnection = new LoopbackOutgoingConnection(); } } else { if (isLocal && internalAddress != null) { response?.Invoke("Local peer found."); if (internalAddress.Length > 1) { response?.Invoke("Multiple local addresses found. Trying each..."); } for (int i = 0; i < internalAddress.Length; i++) { actualEndpoint = new System.Net.IPEndPoint(internalAddress[i], localControlPort); bool reverseConnect = false; if (App.settings.getBool("Default to Reverse Connection", false)) { reverseConnect = true; } bool rendezvousConnect = false; if (App.settings.getBool("Always Rendezvous", false) && useUDT) { reverseConnect = false; rendezvousConnect = true; } if (!reverseConnect && !rendezvousConnect) { try { if (createControl && controlConnection == null) { response?.Invoke("Creating TCP connection to " + actualEndpoint.Address.ToString() + ":" + localDataPort.ToString()); controlConnection = new ReliableOutgoingConnection(actualEndpoint.Address, localDataPort); } if (createData && dataConnection == null) { response?.Invoke("Creating TCP connection to " + actualEndpoint.Address.ToString() + ":" + localDataPort.ToString()); dataConnection = new ReliableOutgoingConnection(actualEndpoint.Address, localDataPort); } } catch (Exception e) { SystemLog.addEntry("Failed to connect to " + actualEndpoint.Address.ToString()); reverseConnect = true; } } if (reverseConnect) { doReverseConnection(response); return; } if (rendezvousConnect) { doRendezvous(response); return; } } } else { bool reverseConnect = false; if (App.settings.getBool("Default to Reverse Connection", false)) { reverseConnect = true; } bool rendezvousConnect = false; if (App.settings.getBool("Always Rendezvous", false) && useUDT) { reverseConnect = false; rendezvousConnect = true; } if (!reverseConnect && !rendezvousConnect) { try { if (createControl) { response?.Invoke("Creating TCP connection to " + actualEndpoint.Address.ToString() + ":" + externalDataPort.ToString()); controlConnection = new ReliableOutgoingConnection(actualEndpoint.Address, externalDataPort); } if (createData) { response?.Invoke("Creating TCP connection to " + actualEndpoint.Address.ToString() + ":" + externalDataPort.ToString()); dataConnection = new ReliableOutgoingConnection(actualEndpoint.Address, externalDataPort); } } catch (System.Net.Sockets.SocketException s) { response?.Invoke("Error: " + s.Message + "."); reverseConnect = true; } } if (reverseConnect) { doReverseConnection(response); return; } if (rendezvousConnect) { doRendezvous(response); return; } } } if (createData) { if (dataConnection != null && dataConnection != controlConnection) { dataConnection.commandReceived += commandReceived; } } if (createControl && controlConnection != null) { controlConnection.commandReceived += commandReceived; } System.Threading.Thread.Sleep(500); }