示例#1
0
        public async Task InformGetAddress()
        {
            var response = await remoteGateway.GetAddresses(new List <string> {
                client.ID
            });

            Assert.IsTrue(response.Count == 0);
            await remoteGateway.InformAddress(-1);

            response = await remoteGateway.GetAddresses(new List <string> {
                client.ID
            });

            Assert.IsTrue(response.ContainsKey(client.ID));
            IPAddress.Parse(response[client.ID].IP);

            var address = new AddressInfo {
                IP = "string here", Port = 8080
            };

            await remoteGateway.AddAddress(new("testkey", address));

            response = await remoteGateway.GetAddresses(new List <string> {
                client.ID, "testkey"
            });

            Assert.IsTrue(response.Count == 2);
            Assert.AreEqual(response["testkey"], address);
        }
示例#2
0
        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();
            }
        }