internal void OnRecived(int connectionID, byte[] bytes) { if (IsHeartbeat(bytes)) { ConsoleColor c = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Server got a heartbeat from {0}", connectionID); Console.ForegroundColor = c; return; } UnPacker unPacker = new UnPacker(bytes); while (unPacker.remaining() > 0) { RecivedEventArgs eventArgs = new RecivedEventArgs(connectionID, this.DeserializeMessage(unPacker)); if (this.Recived != null) this.Recived(this, eventArgs); } }
protected override byte[] processMessage(UnPacker unPacker) { unPacker.UnpackByte(); //Remove id byte from message. return unPacker.UnpackByteArray(unPacker.remaining()); }
private byte[] processRecivedMessage(UnPacker unPacker, int accnum) { if (accnum > this.lastRecivedMessageAckNum) { addMissingMessages(accnum); this.lastRecivedMessageAckNum = accnum; return unPacker.UnpackByteArray(unPacker.remaining()); } else { if (this.missingMessages.Contains(accnum)) { this.missingMessages.Remove(accnum); return unPacker.UnpackByteArray(unPacker.remaining()); } return null; } }
private byte[] processRecivedMessage(UnPacker unPacker, int accnum) { if (this.lastOrderedMessage + 1 == accnum) { this.lastOrderedMessage++; if (this.outOfOrderMessages.Count == 0) return unPacker.UnpackByteArray(unPacker.remaining()); return this.buildOrderedMessages(unPacker); } else if (this.lastOrderedMessage >= accnum) { return null; } else { if (!this.outOfOrderMessages.ContainsKey(accnum)) { Console.WriteLine("Message out of order! {0}", accnum); this.outOfOrderMessages.Add(accnum, unPacker.UnpackByteArray(unPacker.remaining())); } return null; } }
private byte[] buildOrderedMessages(UnPacker unPacker) { List<int> keysToRemove = new List<int>(); Packer packer = new Packer(unPacker.remaining()); packer.Pack(unPacker.UnpackByteArray(unPacker.remaining())); foreach (var item in this.outOfOrderMessages.Keys) { if (this.lastOrderedMessage + 1 == item) { keysToRemove.Add(item); this.lastOrderedMessage++; packer.Pack(this.outOfOrderMessages[item]); } else { break; } } keysToRemove.ForEach((x) => this.outOfOrderMessages.Remove(x)); return packer.GetPackedData(); }
protected override byte[] processMessage(UnPacker unPacker) { unPacker.UnpackByte(); int seqNum = unPacker.UnpackInteger(); if (seqNum < lastRecivedSequenceNumber) { return null; //Discard the message if it is old. } else { lastRecivedSequenceNumber = seqNum; return unPacker.UnpackByteArray(unPacker.remaining()); } }