public void HandleAckMessage(AckMessage ackMessage) { OutboundMessage outboundMessage = null; if (outboundMessagesBySequence.TryGetValue(ackMessage.AckedSequenceNumber, out outboundMessage)) { outboundMessages.Remove(outboundMessage); outboundMessagesBySequence.Remove(ackMessage.AckedSequenceNumber); outboundMessagePool.Push(outboundMessage); } if (isResetPending && outboundMessages.FirstOrDefault(x => x.NeedsAck) == null) { if (IsVerbose) { NeutrinoConfig.Log(node.Name + " drained all outbound - resetting sequence and sending queued"); } isResetPending = false; nextSequence = 0; foreach (byte[] buffer in pendingResetOutboundMessages) { Enqueue(msgFactory.Read(buffer)); } pendingResetOutboundMessages.Clear(); } }
private void HandleMessageReceived(IPEndPoint receivedFrom, byte[] buffer, int numBytesReceived) { NetworkPeer peer = null; if (peersByEndpoint.TryGetValue(receivedFrom, out peer)) { peer.HandleMessageReceived(buffer, numBytesReceived); } else { if (NeutrinoConfig.LogLevel == NeutrinoLogLevel.Debug) { NeutrinoConfig.Log("Received from potentially new peer at " + receivedFrom); } List <NetworkMessage> initialMessages = new List <NetworkMessage>(msgFactory.Read(buffer, numBytesReceived)); var connectMsg = initialMessages.FirstOrDefault <NetworkMessage>(x => (x is ConnectMessage)); if (connectMsg == null) { NeutrinoConfig.Log("Ignoring peer who didn't send a ConnectMessage with his initial traffic"); } else { var newPeer = NeutrinoConfig.CreatePeer(); newPeer.Init(this, serverSocket, receivedFrom.Address, receivedFrom.Port, ((ConnectMessage)connectMsg).Nickname); peersByEndpoint[(IPEndPoint)receivedFrom] = newPeer; endpointsByPeer[newPeer] = (IPEndPoint)receivedFrom; if (OnPeerConnected != null) { OnPeerConnected(newPeer); } newPeer.HandleMessageReceived(buffer, numBytesReceived); } } }
internal void HandleMessageReceived(byte[] buffer, int numBytesReceived) { previousActivityTimeTicks = Environment.TickCount; foreach (var msg in msgFactory.Read(buffer, numBytesReceived)) { msg.Source = this; AckMessage ackMsg = msg as AckMessage; if (ackMsg != null) { outboundQueue.HandleAckMessage(ackMsg); } else { bool shouldHandle = true; int sequenceNumber = 0; if (msg.IsGuaranteed) { sequenceNumber = msg.SequenceNumber; if (idempotentSequenceNumbers[sequenceNumber] == 1) { shouldHandle = false; } else { idempotentSequenceNumbers[sequenceNumber] = 1; if (sequenceNumber > 0 && ( idempotentSequenceNumbers[sequenceNumber - 1] == 0 || pendingOutOfSequenceMessages.ContainsKey(sequenceNumber - 1))) { pendingOutOfSequenceMessages[sequenceNumber] = msg.Clone(); shouldHandle = false; } } } if (shouldHandle) { ProcessMessage(msg); for (int i = sequenceNumber + 1; i < NeutrinoConfig.MaxPendingGuaranteedMessages; i++) { NetworkMessage pendingMessage = null; if (pendingOutOfSequenceMessages.TryGetValue(i, out pendingMessage)) { pendingOutOfSequenceMessages.Remove(i); ProcessMessage(pendingMessage); } else { break; } } } if (msg.IsGuaranteed) { ackMessage.AckedSequenceNumber = (ushort)sequenceNumber; SendNetworkMessage(ackMessage); } } } }