private async Task GetFriends(APITranslatorClient friendConnection, HashSet <string> missingFriends) { var foundFriends = await friendConnection.GetAddresses(missingFriends.ToList()); var tasks = new List <Task <APITranslatorClient?> >(); foreach (var missingFriend in missingFriends) { if (!foundFriends.ContainsKey(missingFriend)) { continue; } var address = foundFriends[missingFriend]; tasks.Add(AddIfValid(new Address { IP = address.IP, Port = address.Port, UserID = missingFriend }, true)); } await Task.WhenAll(tasks); foreach (var task in tasks) { var client = task.Result; if (client is null) { continue; } missingFriends.Remove(client.ServerID); client.Dispose(); } }
public TranslationTester() { localGateway = new APITranslationServer(server, new IPEndPoint(IPAddress.Loopback, 12000), api); running = localGateway.Run(); var args = new APITranslatorClient.ConnectionArgs { Address = IPAddress.Loopback, Port = 12000, Self = client, ServerID = server.ID }; remoteGateway = APITranslatorClient.CreateAndConnect(args).Result; }
public async void OnConnect(string folder, IPEndPoint endpoint) { var file = Path.Combine(folder, "client.identity"); PrivateIdentity identity; if (!Env.CheckFile(file)) { if (!Env.Confirm($"No identity was found at {file}. Do you want to create a new one?")) { return; } identity = PrivateIdentity.Create(); var hex = Transcoder.HexFromBytes(PrivateIdentity.Export(identity)); await Env.WriteFile(file, hex); } else { var hex = Transcoder.BytesFromHex(await Env.ReadFile(file)); var result = PrivateIdentity.Import(hex); if (result.IsError) { Env.Alert("Could not load identity!!"); return; } identity = result.Value; } APITranslatorClient nodeConnection; try { nodeConnection = await APITranslatorClient.CreateAndConnect(new APITranslatorClient.ConnectionArgs { Self = identity, ServerID = identity.ID, Address = endpoint.Address, Port = endpoint.Port }); } catch { Env.Alert("Unable to connect to your node!"); return; } Resources = new ResourceManager(identity, nodeConnection, OnDisconnect); VM = new ConnectedViewModel(OnDisconnect); }
private async Task <APITranslatorClient?> AddIfValid(Address address, bool inform = false) { APITranslatorClient messenger; try { messenger = await APITranslatorClient.CreateAndConnect(new APITranslatorClient.ConnectionArgs { Address = IPAddress.Parse(address.IP), Port = address.Port, ServerID = address.UserID, Self = self }); } catch (Exception e) { Debug.WriteLine(e); return(null); } if (inform) { try { await messenger.InformAddress(port); } catch (Exception e) { Debug.WriteLine(e); } } await conn.InsertOrReplaceAsync(address); return(messenger); }