Exemplo n.º 1
0
 private void UpdateClientConnections()
 {
     using (_logger.BeginScope("Updating client connections"))
     {
         foreach (var key in ServerIPs.Changes)
         {
             if (!ServerIPs.TryGetValue(key, out var endpoint))
             {
                 if (_clientConnections.TryRemove(key, out var conn))
                 {
                     conn.Dispose();
                 }
             }
             else if (!_clientConnections.TryGetValue(key, out var conn))
             {
                 _clientConnections[key] = new TcpClientConnection(this, endpoint, _logger);
             }
             else if (conn.TargetEndPoint != endpoint)
             {
                 conn.Dispose();
                 _clientConnections[key] = new TcpClientConnection(this, endpoint, _logger);
             }
         }
     }
 }
Exemplo n.º 2
0
        public void TryGetValueTest()
        {
            var objectA = new object();
            var dict    = new ShieldedDict <string, object>(new KeyValuePair <string, object>[] {
                new KeyValuePair <string, object>("key a", objectA),
            });

            object x;

            Assert.IsTrue(dict.TryGetValue("key a", out x));
            Assert.AreEqual(objectA, x);

            object y = null;

            Assert.IsTrue(Shield.InTransaction(() => dict.TryGetValue("key a", out y)));
            Assert.AreEqual(objectA, y);

            Assert.IsFalse(dict.TryGetValue("not me", out y));
            Assert.IsFalse(Shield.InTransaction(() => dict.TryGetValue("not me", out y)));
        }
Exemplo n.º 3
0
        private static void ReadItemsShielded(ShieldedDict<long, object> ld, int iterations)
        {
            var sp = Stopwatch.StartNew();

            for (int i = 0; i < iterations; i++)
            {
                object value;
                ld.TryGetValue(i, out value);
            }

            Console.WriteLine(sp.Elapsed + " Reading values Shielded dictionary");
        }
Exemplo n.º 4
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="ownId">The ID of this server.</param>
 /// <param name="serverIPs">The dictionary with server IDs and IP endpoints, including this server. Do not make later
 /// changes to any of the IPEndPoint objects!</param>
 public TcpTransport(string ownId, IDictionary <string, IPEndPoint> serverIPs)
 {
     OwnId = ownId ?? throw new ArgumentNullException(nameof(ownId));
     if (serverIPs == null)
     {
         throw new ArgumentNullException(nameof(serverIPs));
     }
     LocalEndpoint = serverIPs[ownId];
     (ServerIPs, _clientConnections) = Shield.InTransaction(() =>
     {
         var ips     = new ShieldedDict <string, IPEndPoint>(serverIPs.Where(kvp => !StringComparer.InvariantCultureIgnoreCase.Equals(kvp.Key, ownId)));
         var clients = new ConcurrentDictionary <string, TcpClientConnection>(
             ips.Select(kvp => new KeyValuePair <string, TcpClientConnection>(kvp.Key, new TcpClientConnection(this, kvp.Value))));
         Shield.PreCommit(() => ips.TryGetValue("any", out var _) || true,
                          () => Shield.SyncSideEffect(UpdateClientConnections));
         return(ips, clients);
     });
 }
Exemplo n.º 5
0
        public void TryGetValueTest()
        {
            var objectA = new object();
            var dict = new ShieldedDict<string, object>(new KeyValuePair<string, object>[] {
                new KeyValuePair<string, object>("key a", objectA),
            });

            object x;
            Assert.IsTrue(dict.TryGetValue("key a", out x));
            Assert.AreEqual(objectA, x);

            object y = null;
            Assert.IsTrue(Shield.InTransaction(() => dict.TryGetValue("key a", out y)));
            Assert.AreEqual(objectA, y);

            Assert.IsFalse(dict.TryGetValue("not me", out y));
            Assert.IsFalse(Shield.InTransaction(() => dict.TryGetValue("not me", out y)));
        }