/// <summary> /// Adds the specified element to this queue, waiting if necessary for /// another thread to receive it. /// </summary> /// <param name="e"></param> /// <exception cref="ArgumentNullException" /> /// <exception cref="ThreadInterruptedException" /> public override void Put(T e) { if (e == null) { throw new ArgumentNullException("e"); } ReentrantLock qlock = _qlock; for (; ;) { Node node; bool mustWait; //if (Thread.Interrupted) throw new InterruptedException(); qlock.Lock(); try { node = _waitingConsumers.Dequeue(); mustWait = (node == null); if (mustWait) { node = _waitingProducers.Enqueue(e); } } finally { qlock.Unlock(); } if (mustWait) { try { node.WaitForTake(); return; } catch (ThreadInterruptedException) { UnlinkCancelledProducer(node); throw; } } if (node.SetItem(e)) { return; } // else consumer cancelled, so retry } }
/** * Retrieves and removes the head of this queue, waiting if necessary * for another thread to insert it. * * @return the head of this queue * @throws InterruptedException {@inheritDoc} */ public override T Take() { ReentrantLock qlock = _qlock; for (; ;) { Node node; bool mustWait; //if (Thread.interrupted()) throw new InterruptedException(); qlock.Lock(); try { node = _waitingProducers.Dequeue(); mustWait = (node == null); if (mustWait) { node = _waitingConsumers.Enqueue(default(T)); } } finally { qlock.Unlock(); } if (mustWait) { try { Object x = node.WaitForPut(); return((T)x); } catch (ThreadInterruptedException) { UnlinkCancelledConsumer(node); throw; } } else { T x; if (node.GetItem(out x)) { return(x); } // else cancelled, so retry } } }
public BackgroundUploadManager(ConnectionsManager connectionsManager, CacheManager cacheManager, BufferManager bufferManager) { _connectionsManager = connectionsManager; _cacheManager = cacheManager; _bufferManager = bufferManager; _settings = new Settings(this.ThisLock); _connectionsManager.UploadedEvent += (object sender, IEnumerable <Key> keys) => { foreach (var key in keys) { _uploadedKeys.Enqueue(key); } }; _cacheManager.RemoveKeyEvent += (object sender, IEnumerable <Key> keys) => { foreach (var key in keys) { _uploadedKeys.Enqueue(key); } }; _uploadedThread = new Thread(() => { try { for (; ;) { var key = _uploadedKeys.Dequeue(); lock (this.ThisLock) { foreach (var item in _settings.BackgroundUploadItems.ToArray()) { if (item.UploadKeys.Remove(key)) { item.UploadedKeys.Add(key); if (item.State == BackgroundUploadState.Uploading) { if (item.UploadKeys.Count == 0) { item.State = BackgroundUploadState.Completed; } } } } } } } catch (Exception) { } }); _uploadedThread.Priority = ThreadPriority.BelowNormal; _uploadedThread.Name = "BackgroundUploadManager_UploadedThread"; _uploadedThread.Start(); }
private void WatchThread() { for (; ;) { ArraySegment <byte> buffer = new ArraySegment <byte>(); try { buffer = _queue.Dequeue(); if (buffer.Array == null) { return; } _stream.Write(buffer.Array, buffer.Offset, buffer.Count); } catch (Exception) { return; } finally { if (buffer.Array != null) { _bufferManager.ReturnBuffer(buffer.Array); } } } }
public void Dequeue_WithResult_CompletesWithResult() { var queue = new WaitQueue<int>(); var task = queue.Enqueue(item0, none); Test.Async(async () => { var result0 = await queue.Dequeue(none); Assert.AreEqual<int>(item0, result0); }); }
public void Dequeue_WaitWithEnqCompletes_CompletesTask() { var queue = new WaitQueue<int>(); var task0 = queue.Dequeue(none); Assert.IsFalse(task0.IsCompleted); var task1 = queue.Enqueue(item0, none); task1.WaitEx(); task0.WaitEx(); Assert.IsTrue(task1.IsCompleted); Assert.IsTrue(task0.IsCompleted); Assert.AreEqual<int>(item0, task0.ResultEx()); }
public void Dequeue_WithTwoItems_OnlyCompletesFirstItem() { Test.Async(async () => { var queue = new WaitQueue<int>(); var task1 = queue.Enqueue(item0, none); var task2 = queue.Enqueue(item1, none); await task1; var deq0 = await queue.Dequeue(none); Assert.AreEqual<int>(item0, deq0); await task2; Assert.AreEqual<int>(item1, await queue.Peek(none)); }); }
/// <summary> /// Resume the next waiting <see cref="DesTask"/> specifying the /// activator and activation data. /// </summary> /// <param name="activator">The activator.</param> /// <param name="data">The activation data.</param> protected virtual void ResumeNext(object activator, object data) { if (BlockCount > 0) { bool canceled; do { DesTask desTask = WaitQueue.Dequeue(); canceled = desTask.Canceled; if (!canceled) { ResumeTask(desTask, activator, data); } } while (canceled && BlockCount > 0); } }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); List<ConnectionManager> connectionManagers = new List<ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List<Node> nodes = new List<Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment<byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullSeedsRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsRequestEvent += (object sender, PullSeedsRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushSeedsRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullSeedsEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsEvent += (object sender, PullSeedsEventArgs e) => { queue.Enqueue(e); }; List<Seed> seeds = new List<Seed>(); for (int j = 0; j < 32; j++) { var seed = new Seed(); seed.Name = "aaaa.zip"; seed.Keywords.AddRange(new KeywordCollection { "bbbb", "cccc", "dddd", }); seed.CreationTime = DateTime.Now; seed.Length = 10000; seed.Comment = "eeee"; seed.Rank = 1; seed.Key = new Key(new byte[32], HashAlgorithm.Sha256); seed.CompressionAlgorithm = CompressionAlgorithm.Xz; seed.CryptoAlgorithm = CryptoAlgorithm.Aes256; seed.CryptoKey = new byte[32 + 32]; seeds.Add(seed); } senderConnection.PushSeeds(seeds); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(seeds, item.Seeds), "ConnectionManager #6"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
public BackgroundDownloadManager(ConnectionsManager connectionsManager, CacheManager cacheManager, BufferManager bufferManager) { _connectionsManager = connectionsManager; _cacheManager = cacheManager; _bufferManager = bufferManager; _settings = new Settings(this.ThisLock); _cacheManager.SetKeyEvent += (object sender, IEnumerable <Key> keys) => { foreach (var key in keys) { _setKeys.Enqueue(key); } }; _cacheManager.RemoveKeyEvent += (object sender, IEnumerable <Key> keys) => { foreach (var key in keys) { _removeKeys.Enqueue(key); } }; _setThread = new Thread(() => { try { for (; ;) { var key = _setKeys.Dequeue(); lock (this.ThisLock) { _existManager.Set(key, true); } } } catch (Exception) { } }); _setThread.Priority = ThreadPriority.BelowNormal; _setThread.Name = "BackgroundDownloadManager_SetThread"; _setThread.Start(); _removeThread = new Thread(() => { try { for (; ;) { var key = _removeKeys.Dequeue(); lock (this.ThisLock) { _existManager.Set(key, false); } } } catch (Exception) { } }); _removeThread.Priority = ThreadPriority.BelowNormal; _removeThread.Name = "BackgroundDownloadManager_RemoveThread"; _removeThread.Start(); _connectionsManager.GetLockSignaturesEvent = (object sender) => { return(this.SearchSignatures); }; }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); List<ConnectionManager> connectionManagers = new List<ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List<Node> nodes = new List<Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment<byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasRequestEvent += (object sender, PullBroadcastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushBroadcastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasEvent += (object sender, PullBroadcastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List<ProfileMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var metadata = new ProfileMetadata(DateTime.UtcNow, key, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushBroadcastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.ProfileMetadatas), "ConnectionManager #6.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasRequestEvent += (object sender, PullUnicastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushUnicastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #7.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasEvent += (object sender, PullUnicastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List<SignatureMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new SignatureMessageMetadata(digitalSignature.ToString(), DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushUnicastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.SignatureMessageMetadatas), "ConnectionManager #8.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasRequestEvent += (object sender, PullMulticastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var wikis = new WikiCollection(); var chats = new ChatCollection(); for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); wikis.Add(new Wiki(RandomString.GetValue(256), id)); } for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); chats.Add(new Chat(RandomString.GetValue(256), id)); } senderConnection.PushMulticastMetadatasRequest(wikis, chats); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(wikis, item.Wikis), "ConnectionManager #9.1"); Assert.IsTrue(CollectionUtilities.Equals(chats, item.Chats), "ConnectionManager #9.2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasEvent += (object sender, PullMulticastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List<WikiDocumentMetadata>(); var metadatas2 = new List<ChatTopicMetadata>(); var metadatas3 = new List<ChatMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Wiki("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new WikiDocumentMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatTopicMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas2.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatMessageMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas3.Add(metadata); } senderConnection.PushMulticastMetadatas(metadatas1, metadatas2, metadatas3); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.WikiDocumentMetadatas), "ConnectionManager #10.1"); Assert.IsTrue(CollectionUtilities.Equals(metadatas2, item.ChatTopicMetadatas), "ConnectionManager #10.2"); Assert.IsTrue(CollectionUtilities.Equals(metadatas3, item.ChatMessageMetadatas), "ConnectionManager #10.3"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); List<ConnectionManager> connectionManagers = new List<ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "tcp:localhost:9000", "tcp:localhost:9001", "tcp:localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "tcp:localhost:9000", "tcp:localhost:9001", "tcp:localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); var serverTask = Task.Run(() => serverConnectionManager.Connect()); var clientTask = Task.Run(() => clientConnectionManager.Connect()); Task.WaitAll(serverTask, clientTask); Assert.IsTrue(CollectionUtils.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtils.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List<Node> nodes = new List<Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(HashAlgorithm.Sha256, id); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(HashAlgorithm.Sha256, id); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(HashAlgorithm.Sha256, Sha256.ComputeHash(buffer)); senderConnection.PushBlock(key, new ArraySegment<byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtils.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasRequestEvent += (object sender, PullBroadcastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushBroadcastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(signatures, item.Signatures), "ConnectionManager #5.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasEvent += (object sender, PullBroadcastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var metadatas1 = new List<BroadcastMetadata>(); for (int j = 0; j < 4; j++) { var key = new Key(HashAlgorithm.Sha256, new byte[32]); var metadata = new Metadata(1, key, CompressionAlgorithm.Xz, CryptoAlgorithm.Aes256, new byte[32 + 32]); var broadcastMetadata = new BroadcastMetadata("Type", DateTime.UtcNow, metadata, digitalSignature); metadatas1.Add(broadcastMetadata); } senderConnection.PushBroadcastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(metadatas1, item.BroadcastMetadatas), "ConnectionManager #6.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasRequestEvent += (object sender, PullUnicastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushUnicastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(signatures, item.Signatures), "ConnectionManager #7.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasEvent += (object sender, PullUnicastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var metadatas1 = new List<UnicastMetadata>(); for (int j = 0; j < 4; j++) { var key = new Key(HashAlgorithm.Sha256, new byte[32]); var metadata = new Metadata(1, key, CompressionAlgorithm.Xz, CryptoAlgorithm.Aes256, new byte[32 + 32]); var unicastMetadata = new UnicastMetadata("Type", digitalSignature.ToString(), DateTime.UtcNow, metadata, digitalSignature); metadatas1.Add(unicastMetadata); } senderConnection.PushUnicastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(metadatas1, item.UnicastMetadatas), "ConnectionManager #8.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasRequestEvent += (object sender, PullMulticastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var tags = new TagCollection(); for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); tags.Add(new Tag(RandomString.GetValue(256), id)); } senderConnection.PushMulticastMetadatasRequest(tags); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(tags, item.Tags), "ConnectionManager #9.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasEvent += (object sender, PullMulticastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var metadatas1 = new List<MulticastMetadata>(); for (int j = 0; j < 4; j++) { var key = new Key(HashAlgorithm.Sha256, new byte[32]); var metadata = new Metadata(1, key, CompressionAlgorithm.Xz, CryptoAlgorithm.Aes256, new byte[32 + 32]); var tag = new Tag("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var multicastMetadata = new MulticastMetadata("Type", tag, DateTime.UtcNow, metadata, miner, digitalSignature); metadatas1.Add(multicastMetadata); } senderConnection.PushMulticastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(metadatas1, item.MulticastMetadatas), "ConnectionManager #10.1"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
// // Tries to add immediatelly a data item to the queue. // public override bool TryAdd(T di) { // // If the queue is full, return failure immediatelly. // if (count == length) { return(false); } // // The queue seems non-full; so, acquire the queue's lock. // qlock.Enter(); // // When the queue's buffer has free slots, it can't have threads // blocked by the add operation; so, if there are waiters, they // were blocked by the take operation. // if (count < length) { if (!waitQueue.IsEmpty) { // // Try to deliver the data item directly to a waiting thread. // do { WaitNode w = waitQueue.Dequeue(); StParker pk = w.parker; if (pk.TryLock()) { // // Release the queue's lock, pass the data item through // the wait node, unpark the waiter thread and return // success. // qlock.Exit(); w.channel = di; pk.Unpark(w.waitKey); return(true); } } while (!waitQueue.IsEmpty); } // // There is at least a free slot on the queue; So, copy the // data item to the queue's buffer, unlock the queue and // return success. // items[tail] = di; if (++tail == length) { tail = 0; } count++; qlock.Exit(); return(true); } // // The queue's buffer is full, so return false. // qlock.Exit(); return(false); }
public void Test_WaitQueue() { // アイテムが無い場合、Dequeueは待機される。 { WaitQueue<string> queue = new WaitQueue<string>(); var task1 = Task.Run(() => { Assert.AreEqual(queue.WaitDequeue(TimeSpan.Zero), false); Assert.Throws<TimeoutException>(() => { queue.Dequeue(TimeSpan.Zero); }); Assert.AreEqual(queue.Dequeue(), "Test"); }); var task2 = Task.Run(() => { Thread.Sleep(1000 * 3); queue.Enqueue("Test"); }); Task.WaitAll(task1, task2); } // キャパシティを超えるとEnqueueが待機状態になるため、4つ目のアイテムがタイムアウトの例外を投げる。 { WaitQueue<string> queue = new WaitQueue<string>(3); queue.Enqueue("1", TimeSpan.Zero); queue.Enqueue("2", TimeSpan.Zero); queue.Enqueue("3", TimeSpan.Zero); Assert.Throws<TimeoutException>(() => { queue.Enqueue("4", TimeSpan.Zero); }); } // キャパシティを超えるとEnqueueが待機され、Dequeueによって項目が減るとEnqueueが再開される。 { WaitQueue<string> queue = new WaitQueue<string>(3); queue.Enqueue("1", TimeSpan.Zero); queue.Enqueue("2", TimeSpan.Zero); queue.Enqueue("3", TimeSpan.Zero); var task1 = Task.Run(() => { Assert.AreEqual(queue.WaitEnqueue(TimeSpan.Zero), false); Thread.Sleep(1000 * 3); Assert.DoesNotThrow(() => { queue.Enqueue("4", TimeSpan.Zero); }); }); var task2 = Task.Run(() => { Thread.Sleep(1000 * 1); queue.Dequeue(); }); Task.WaitAll(task1, task2); } }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); List <ConnectionManager> connectionManagers = new List <ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List <Node> nodes = new List <Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List <Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List <Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment <byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullSeedsRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsRequestEvent += (object sender, PullSeedsRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushSeedsRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullSeedsEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsEvent += (object sender, PullSeedsEventArgs e) => { queue.Enqueue(e); }; List <Seed> seeds = new List <Seed>(); for (int j = 0; j < 32; j++) { var seed = new Seed(); seed.Name = "aaaa.zip"; seed.Keywords.AddRange(new KeywordCollection { "bbbb", "cccc", "dddd", }); seed.CreationTime = DateTime.Now; seed.Length = 10000; seed.Comment = "eeee"; seed.Rank = 1; seed.Key = new Key(new byte[32], HashAlgorithm.Sha256); seed.CompressionAlgorithm = CompressionAlgorithm.Xz; seed.CryptoAlgorithm = CryptoAlgorithm.Aes256; seed.CryptoKey = new byte[32 + 32]; seeds.Add(seed); } senderConnection.PushSeeds(seeds); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(seeds, item.Seeds), "ConnectionManager #6"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); List <ConnectionManager> connectionManagers = new List <ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List <Node> nodes = new List <Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List <Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List <Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment <byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBroadcastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasRequestEvent += (object sender, PullBroadcastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushBroadcastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBroadcastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasEvent += (object sender, PullBroadcastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List <ProfileMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var metadata = new ProfileMetadata(DateTime.UtcNow, key, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushBroadcastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.ProfileMetadatas), "ConnectionManager #6.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullUnicastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasRequestEvent += (object sender, PullUnicastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushUnicastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #7.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullUnicastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasEvent += (object sender, PullUnicastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List <SignatureMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new SignatureMessageMetadata(digitalSignature.ToString(), DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushUnicastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.SignatureMessageMetadatas), "ConnectionManager #8.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullMulticastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasRequestEvent += (object sender, PullMulticastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var wikis = new WikiCollection(); var chats = new ChatCollection(); for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); wikis.Add(new Wiki(RandomString.GetValue(256), id)); } for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); chats.Add(new Chat(RandomString.GetValue(256), id)); } senderConnection.PushMulticastMetadatasRequest(wikis, chats); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(wikis, item.Wikis), "ConnectionManager #9.1"); Assert.IsTrue(CollectionUtilities.Equals(chats, item.Chats), "ConnectionManager #9.2"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullMulticastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasEvent += (object sender, PullMulticastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List <WikiDocumentMetadata>(); var metadatas2 = new List <ChatTopicMetadata>(); var metadatas3 = new List <ChatMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Wiki("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new WikiDocumentMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatTopicMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas2.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatMessageMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas3.Add(metadata); } senderConnection.PushMulticastMetadatas(metadatas1, metadatas2, metadatas3); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.WikiDocumentMetadatas), "ConnectionManager #10.1"); Assert.IsTrue(CollectionUtilities.Equals(metadatas2, item.ChatTopicMetadatas), "ConnectionManager #10.2"); Assert.IsTrue(CollectionUtilities.Equals(metadatas3, item.ChatMessageMetadatas), "ConnectionManager #10.3"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
public void Test_WaitQueue() { System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("oo"); // アイテムが無い場合、Dequeueは待機される。 { WaitQueue <string> queue = new WaitQueue <string>(); var task1 = Task.Factory.StartNew(() => { Assert.AreEqual(queue.WaitDequeue(TimeSpan.Zero), false); Assert.Throws <TimeoutException>(() => { queue.Dequeue(TimeSpan.Zero); }); Assert.AreEqual(queue.Dequeue(), "Test"); }); var task2 = Task.Factory.StartNew(() => { Thread.Sleep(1000 * 3); queue.Enqueue("Test"); }); Task.WaitAll(task1, task2); } // キャパシティを超えるとEnqueueが待機状態になるため、4つ目のアイテムがタイムアウトの例外を投げる。 { WaitQueue <string> queue = new WaitQueue <string>(3); queue.Enqueue("1", TimeSpan.Zero); queue.Enqueue("2", TimeSpan.Zero); queue.Enqueue("3", TimeSpan.Zero); Assert.Throws <TimeoutException>(() => { queue.Enqueue("4", TimeSpan.Zero); }); } // キャパシティを超えるとEnqueueが待機され、Dequeueによって項目が減るとEnqueueが再開される。 { WaitQueue <string> queue = new WaitQueue <string>(3); queue.Enqueue("1", TimeSpan.Zero); queue.Enqueue("2", TimeSpan.Zero); queue.Enqueue("3", TimeSpan.Zero); var task1 = Task.Factory.StartNew(() => { Assert.AreEqual(queue.WaitEnqueue(TimeSpan.Zero), false); Thread.Sleep(1000 * 3); Assert.DoesNotThrow(() => { queue.Enqueue("4", TimeSpan.Zero); }); }); var task2 = Task.Factory.StartNew(() => { Thread.Sleep(1000 * 1); queue.Dequeue(); }); Task.WaitAll(task1, task2); } }
public UploadManager(ConnectionsManager connectionsManager, CacheManager cacheManager, BufferManager bufferManager) { _connectionsManager = connectionsManager; _cacheManager = cacheManager; _bufferManager = bufferManager; _settings = new Settings(this.ThisLock); _threadCount = Math.Max(1, Math.Min(System.Environment.ProcessorCount, 32) / 2); _connectionsManager.UploadedEvent += (object sender, IEnumerable <Key> keys) => { foreach (var key in keys) { _uploadedKeys.Enqueue(key); } }; _cacheManager.RemoveKeyEvent += (object sender, IEnumerable <Key> keys) => { foreach (var key in keys) { _uploadedKeys.Enqueue(key); } }; _cacheManager.RemoveShareEvent += (object sender, string path) => { _removeSharePaths.Enqueue(path); }; _uploadedThread = new Thread(() => { try { for (; ;) { var key = _uploadedKeys.Dequeue(); while (_removeSharePaths.Count > 0) { Thread.Sleep(1000); } lock (this.ThisLock) { foreach (var item in _settings.UploadItems) { if (item.UploadKeys.Remove(key)) { item.UploadedKeys.Add(key); if (item.State == UploadState.Uploading) { if (item.UploadKeys.Count == 0) { item.State = UploadState.Completed; _settings.UploadedSeeds.Add(item.Seed.Clone()); } } } } } } } catch (Exception) { } }); _uploadedThread.Priority = ThreadPriority.BelowNormal; _uploadedThread.Name = "UploadManager_UploadedThread"; _uploadedThread.Start(); _removeShareThread = new Thread(() => { try { for (; ;) { var path = _removeSharePaths.Dequeue(); lock (this.ThisLock) { List <int> ids = null; if (_shareLink.TryGetValue(path, out ids)) { foreach (var id in ids.ToArray()) { this.Remove(id); } } } } } catch (Exception) { } }); _removeShareThread.Priority = ThreadPriority.BelowNormal; _removeShareThread.Name = "UploadManager_RemoveShareThread"; _removeShareThread.Start(); }
public void Cancelled_Dequeue() { var queue = new WaitQueue<int>(); var cts = new CancellationTokenSource(); var task0 = queue.Dequeue(cts.Token); Task.Run(async () => { // wait until dequeue request gets on waitlist while (queue.WaitCount == 0) await Task.Delay(10); Assert.AreEqual<int>(1, queue.WaitCount); }).GetAwaiter().GetResult(); cts.Cancel(); AssertEx.ThrowsExceptionAsync<OperationCanceledException>(task0).GetAwaiter().GetResult(); var isEmpty = queue.IsEmpty(none).GetAwaiter().GetResult(); Assert.IsTrue(isEmpty); }
public void Cancelled_BeforeDequeue_Faults() { var queue = new WaitQueue<int>(); var cts = new CancellationTokenSource(); cts.Cancel(); var task = queue.Dequeue(cts.Token); AssertEx.ThrowsExceptionAsync<OperationCanceledException>(task).GetAwaiter().GetResult(); Assert.IsTrue(task.IsCanceled); Assert.IsTrue(queue.IsEmpty(none).ResultEx()); }
public void Dequeue_WithoutResult_CompletesWithDefaultResult() { var queue = new WaitQueue<int>(); var task0 = queue.Enqueue(item0, none); var result = queue.Dequeue(none).ResultEx(); task0.Wait(); Assert.AreEqual<int>(item0, result); }
public override int Read(byte[] buffer, int offset, int count) { if (_disposed) { throw new ObjectDisposedException(this.GetType().FullName); } if (offset < 0 || buffer.Length < offset) { throw new ArgumentOutOfRangeException("offset"); } if (count < 0 || (buffer.Length - offset) < count) { throw new ArgumentOutOfRangeException("count"); } try { int readSumLength = 0; for (; ;) { if (_current == null) { _current = _queue.Dequeue(); } if (_current.Value.Array == null) { return(0); } var subCount = Math.Min(count, _current.Value.Count); readSumLength += subCount; Unsafe.Copy(_current.Value.Array, _current.Value.Offset, buffer, offset, subCount); offset += subCount; count -= subCount; if (subCount < _current.Value.Count) { _current = new ArraySegment <byte>(_current.Value.Array, _current.Value.Offset + subCount, _current.Value.Count - subCount); } else { _bufferManager.ReturnBuffer(_current.Value.Array); _current = null; } if (count == 0) { _position += readSumLength; return(readSumLength); } } } catch (Exception e) { throw new StopIoException("QueueStream Read", e); } }
public void Dequeue_EndEnqueue_IsComplete() { var queue = new WaitQueue<int>(); var task1 = queue.Enqueue(item0, none); var task2 = queue.Enqueue(item1, none); Test.Async(async () => { await task1; await task2; Assert.IsFalse(await queue.IsEmpty(none)); Assert.IsFalse(await queue.IsComplete(none)); Assert.IsFalse(await queue.IsEnded(none)); await queue.EndEnqueue(none); Assert.IsTrue(await queue.IsEnded(none)); Assert.IsFalse(await queue.IsEmpty(none)); Assert.IsFalse(await queue.IsComplete(none)); while (queue.Count > 0) await queue.Dequeue(none); Assert.IsTrue(await queue.IsComplete(none)); Assert.IsTrue(await queue.IsEmpty(none)); Assert.IsTrue(await queue.IsEnded(none)); }); Assert.IsTrue(task1.IsCompleted); Assert.IsTrue(task2.IsCompleted); }