private void SendDispatchLoop() { while (true) { if (scheduler.Verbose) { Console.WriteLine("Waiting for the next send to dispatch"); } SendTask sendTask = sendQueue.Receive(); if (scheduler.Verbose) { Console.WriteLine("Dispatching send of message of size {0} to {1}", sendTask.Message.Length, IoScheduler.PublicKeyToString(sendTask.DestinationPublicKey)); } SenderThread senderThread = scheduler.FindSenderForDestinationPublicKey(sendTask.DestinationPublicKey); if (senderThread == null) { senderThread = ClientSenderThread.Create(scheduler, sendTask.DestinationPublicKey); } senderThread.EnqueueSendTask(sendTask); } }
private void Run() { try { if (Connect()) { SendLoop(); } } catch (Exception e) { scheduler.ReportException(e, "sending to " + EndpointDescription()); } scheduler.UnregisterSender(destinationPublicKey, this); // If we crashed in the middle of sending a packet, re-queue it // for sending by another sender thread. if (currentSendTask != null) { scheduler.ResendPacket(currentSendTask); currentSendTask = null; } // If there are packets queued for us to send, re-queue them // for sending by another sender thread. while (sendQueue.TryReceive(out currentSendTask)) { scheduler.ResendPacket(currentSendTask); currentSendTask = null; } }
public void ResendPacket(SendTask sendTask) { if (sendTask.IncrementNumTriesSoFar() < maxSendTries) { sendDispatchThread.EnqueueSendTask(sendTask); } }
protected SenderThread(IoScheduler i_scheduler, byte[] i_destinationPublicKey, SslStream i_stream, X509Certificate2 i_remoteCert) { scheduler = i_scheduler; destinationPublicKey = i_destinationPublicKey; stream = i_stream; remoteCert = i_remoteCert; sendQueue = new BufferBlock <SendTask>(); currentSendTask = null; }
public bool SendPacket(byte[] remotePublicKey, byte[] message) { try { byte[] messageCopy = new byte[message.Length]; Array.Copy(message, messageCopy, message.Length); byte[] remotePublicKeyCopy = new byte[remotePublicKey.Length]; Array.Copy(remotePublicKey, remotePublicKeyCopy, remotePublicKey.Length); SendTask sendTask = new SendTask(remotePublicKeyCopy, messageCopy); if (verbose) { Console.WriteLine("Enqueueing send of a message of size {0} to {1}", message.Length, PublicKeyToString(remotePublicKey)); } sendDispatchThread.EnqueueSendTask(sendTask); return(true); } catch (Exception e) { Console.Error.WriteLine("Unexpected error when trying to send a message. Exception:\n{0}", e); return(false); } }
private void SendLoop() { if (scheduler.Verbose) { Console.WriteLine("Starting send loop with {0}", EndpointDescription()); } while (true) { // Wait for there to be a packet to send. currentSendTask = sendQueue.Receive(); // Send its length as an 8-byte value. UInt64 messageSize = (UInt64)currentSendTask.Message.Length; IoEncoder.WriteUInt64(stream, messageSize); if (scheduler.Verbose) { Console.WriteLine("Sent message size {0} to {1}", messageSize, EndpointDescription()); } // Send its contents. IoEncoder.WriteBytes(stream, currentSendTask.Message, 0, messageSize); if (scheduler.Verbose) { Console.WriteLine("Sent message of size {0} to {1}", messageSize, EndpointDescription()); } // Set the currentSendTask to null so we know we don't have to // resend it if the connection fails. currentSendTask = null; } }
public void EnqueueSendTask(SendTask sendTask) { sendQueue.Post(sendTask); }