/// <summary> /// TBD /// </summary> /// <param name="localAddress">TBD</param> /// <param name="recipient">TBD</param> /// <param name="serializedMessage">TBD</param> /// <param name="senderOption">TBD</param> /// <param name="seqOption">TBD</param> /// <param name="ackOption">TBD</param> /// <returns>TBD</returns> public override ByteString ConstructMessage(Address localAddress, IActorRef recipient, SerializedMessage serializedMessage, IActorRef senderOption = null, SeqNo seqOption = null, Ack ackOption = null) { var ackAndEnvelope = new AckAndEnvelopeContainer(); var envelope = new RemoteEnvelope() { Recipient = SerializeActorRef(recipient.Path.Address, recipient) }; if (senderOption != null && senderOption.Path != null) { envelope.Sender = SerializeActorRef(localAddress, senderOption); } if (seqOption != null) { envelope.Seq = (ulong)seqOption.RawValue; } else { envelope.Seq = SeqUndefined; } if (ackOption != null) { ackAndEnvelope.Ack = AckBuilder(ackOption); } envelope.Message = serializedMessage; ackAndEnvelope.Envelope = envelope; return(ackAndEnvelope.ToByteString()); }
/// <summary> /// 获取采番值 /// </summary> /// <param name="key">采番键值</param> /// <returns></returns> public SeqRule GetKeyValues(string key) { SeqRule ack = new SeqRule(); string sSql = "SELECT * FROM SeqNo WHERE SeqKey='" + key + "'"; DataSet ds = DbHelperMySQL.Query(sSql); SeqNo model = new SeqNo(); if (ds.Tables.Count > 0) { model = DataRowToModel(ds.Tables[0].Rows[0]); ack.date_type = model.DateType; ack.pre_fix = model.PreFix; //清零 ClearZero(model); if (model.NowNumber < model.MaxNumber) { model.NowNumber += model.IncreNumber; int length = (model.MaxNumber.ToString()).Length - (model.NowNumber.ToString()).Length; for (int i = 0; i < length; i++) { ack.key_values += "0"; } ack.key_values = ack.key_values + model.NowNumber.ToString(); EditSeqNo(model); } } return(ack); }
public override string ToString() { var sb = new StringBuilder("Batch("); if ((Process != null)) { sb.Append(", Process: "); Process.ToString(sb); } if ((Spans != null)) { sb.Append(", Spans: "); Spans.ToString(sb); } if (__isset.seqNo) { sb.Append(", SeqNo: "); SeqNo.ToString(sb); } if ((Stats != null) && __isset.stats) { sb.Append(", Stats: "); Stats.ToString(sb); } sb.Append(')'); return(sb.ToString()); }
private bool AcceptIncomingPacket(SeqNo seq) { if (_isFirstIncomingPacketAck) { _incomingPacketAck = seq - 1; _incomingPacketAckBuffer.ClearAll(); _isFirstIncomingPacketAck = false; } var diff = seq.CompareTo(_incomingPacketAck); if (diff == 0) { // Already received packet return(false); } else if (diff > 0) { _incomingPacketAck = seq; if (diff < _incomingPacketAckBuffer.Capacity) { // New packet _incomingPacketAckBuffer.LeftShift(diff); } else { // Early packet but still ok _incomingPacketAckBuffer.ClearAll(); } _incomingPacketAckBuffer.Set(0); return(true); } else { diff *= -1; if (diff < _incomingPacketAckBuffer.Capacity) { if (_incomingPacketAckBuffer[diff]) { // Already received packet return(false); } else { // New packet _incomingPacketAckBuffer.Set(diff); return(true); } } else { // Late packet return(false); } } }
public Message(IInternalActorRef recipient, Address recipientAddress, SerializedMessage serializedMessage, IActorRef senderOptional = null, SeqNo seq = null) { Seq = seq; SenderOptional = senderOptional; SerializedMessage = serializedMessage; RecipientAddress = recipientAddress; Recipient = recipient; }
/// <summary> /// Gets tracked messages for packet seq. /// </summary> /// <returns>May return <c>null</c>.</returns> /// <remarks>Do not modify returned list.</remarks> public List <ReliableMessage>?Get(SeqNo packetSeq) { var index = GetIndex(packetSeq); return(_packetSeqBuffer[index] == packetSeq ? _messageBuffer[index] : null); }
public override byte[] ToBytes() { var objectBytes = MessageData.ToBytes(); return(TLUtils.Combine( MessageId.ToBytes(), SeqNo.ToBytes(), BitConverter.GetBytes(objectBytes.Length), objectBytes)); }
/// <summary> /// Tracks messages. /// </summary> public void Track(SeqNo packetSeq, List <ReliableMessage> messageSeqs) { var index = GetIndex(packetSeq); _packetSeqBuffer[index] = packetSeq; var messages = _messageBuffer[index]; messages.Clear(); messages.AddRange(messageSeqs); }
public void SeqNo_must_handle_overflow() { var s1 = new SeqNo(long.MaxValue - 1); var s2 = new SeqNo(long.MaxValue); var s3 = new SeqNo(long.MinValue); var s4 = new SeqNo(long.MinValue + 1); Assert.True(s1.Inc() == s2); Assert.True(s2.Inc() == s3); Assert.True(s3.Inc() == s4); }
public void SeqNo_must_correctly_handle_large_gaps() { var smin = new SeqNo(long.MinValue); var smin2 = new SeqNo(long.MinValue + 1); var s0 = new SeqNo(0); Assert.True(s0 < smin); Assert.False(s0 > smin); Assert.True(smin2 < s0); Assert.False(smin2 > s0); }
/// <summary> /// TBD /// </summary> /// <param name="raw">TBD</param> /// <param name="provider">TBD</param> /// <param name="localAddress">TBD</param> /// <returns>TBD</returns> public override AckAndMessage DecodeMessage(ByteString raw, IRemoteActorRefProvider provider, Address localAddress) { var ackAndEnvelope = AckAndEnvelopeContainer.Parser.ParseFrom(raw); Ack ackOption = null; if (ackAndEnvelope.Ack != null) { ackOption = new Ack(new SeqNo((long)ackAndEnvelope.Ack.CumulativeAck), ackAndEnvelope.Ack.Nacks.Select(x => new SeqNo((long)x))); } Message messageOption = null; if (ackAndEnvelope.Envelope != null) { var envelopeContainer = ackAndEnvelope.Envelope; if (envelopeContainer != null) { var recipient = provider.ResolveActorRefWithLocalAddress(envelopeContainer.Recipient.Path, localAddress); Address recipientAddress; if (AddressCache != null) { recipientAddress = AddressCache.Cache.GetOrCompute(envelopeContainer.Recipient.Path); } else { ActorPath.TryParseAddress(envelopeContainer.Recipient.Path, out recipientAddress); } var serializedMessage = envelopeContainer.Message; IActorRef senderOption = null; if (envelopeContainer.Sender != null) { senderOption = provider.ResolveActorRefWithLocalAddress(envelopeContainer.Sender.Path, localAddress); } SeqNo seqOption = null; if (envelopeContainer.Seq != SeqUndefined) { unchecked { seqOption = new SeqNo((long)envelopeContainer.Seq); //proto takes a ulong } } messageOption = new Message(recipient, recipientAddress, serializedMessage, senderOption, seqOption); } } return(new AckAndMessage(ackOption, messageOption)); }
/// <summary> /// Returns this packet info as string. /// </summary> /// <returns>Returns packet info.</returns> public override string ToString() { StringBuilder retVal = new StringBuilder(); retVal.Append("----- RTP Packet\r\n"); retVal.Append("Version: " + Version.ToString() + "\r\n"); retVal.Append("IsMaker: " + IsMarker.ToString() + "\r\n"); retVal.Append("PayloadType: " + m_PayloadType.ToString() + "\r\n"); retVal.Append("SeqNo: " + SeqNo.ToString() + "\r\n"); retVal.Append("Timestamp: " + m_Timestamp.ToString() + "\r\n"); retVal.Append("SSRC: " + m_SSRC.ToString() + "\r\n"); retVal.Append("Data: " + m_Data.Length + " bytes.\r\n"); return(retVal.ToString()); }
/// <summary> /// 更新 /// </summary> /// <param name="model"></param> public void EditSeqNo(SeqNo model) { string sSql = "UPDATE SeqNo SET "; sSql += "ZeroType='" + model.ZeroType + "',"; sSql += "NowNumber=" + model.NowNumber + ","; sSql += "IncreNumber=" + model.IncreNumber + ","; sSql += "MaxNumber=" + model.MaxNumber + ","; sSql += "UpDateTime='" + DateTime.Now.ToString() + "',"; sSql += "PreFix='" + model.PreFix + "',"; sSql += "Memo='" + model.Memo + "'"; sSql += " WHERE SeqKey='" + model.SeqKey + "'"; DbHelperMySQL.ExecuteSql(sSql); }
private void AcknowledgeOutgoingPackets(SeqNo ack, BitVector acks) { lock (_outgoingMessageQueue) { var currentAck = ack; foreach (var bit in acks.AsBits()) { if (bit) { AcknowledgeOutgoingPacket(currentAck); } currentAck--; } } }
private void AcknowledgeOutgoingPacket(SeqNo ack) { lock (_outgoingMessageQueue) { var messages = _outgoingMessageTracker.Get(ack); if (messages != null) { Logger.LogTrace($"Acked packet: PO-{ack} => {string.Join(", ", messages.Select(x => $"MO-{x.Seq}"))}"); foreach (var message in messages) { _outgoingMessageQueue.Remove(message.Seq); } } _outgoingMessageTracker.Clear(ack); } }
public void SeqNo_must_correctly_handle_wrapping_over() { var s1 = new SeqNo(long.MaxValue - 1); var s2 = new SeqNo(long.MaxValue); var s3 = new SeqNo(long.MinValue); var s4 = new SeqNo(long.MinValue + 1); Assert.True(s1 < s2); Assert.False(s1 > s2); Assert.True(s2 < s3); Assert.False(s2 > s3); Assert.True(s3 < s4); Assert.False(s3 > s4); }
private bool AcceptIncomingPacket(SeqNo seq) { lock (_incomingPacketSeqLock) { if (_incomingPacketSeq < seq) { // New packet _incomingPacketSeq = seq; return(true); } else { // Late packet return(false); } } }
/// <summary> /// 得到一个对象实体 /// </summary> public SeqNo DataRowToModel(DataRow row) { SeqNo model = new SeqNo(); if (row != null) { if (row["SeqKey"] != null) { model.SeqKey = row["SeqKey"].ToString(); } if (row["ZeroType"] != null) { model.ZeroType = row["ZeroType"].ToString(); } if (row["DateType"] != null) { model.DateType = int.Parse(row["DateType"].ToString()); } if (row["NowNumber"] != null) { model.NowNumber = int.Parse(row["NowNumber"].ToString()); } if (row["IncreNumber"] != null) { model.IncreNumber = int.Parse(row["IncreNumber"].ToString()); } if (row["MaxNumber"] != null) { model.MaxNumber = int.Parse(row["MaxNumber"].ToString()); } if (row["UpDateTime"] != null && row["UpDateTime"].ToString() != "") { model.UpDateTime = DateTime.Parse(row["UpDateTime"].ToString()); } if (row["PreFix"] != null) { model.PreFix = row["PreFix"].ToString(); } if (row["Memo"] != null) { model.Memo = row["Memo"].ToString(); } } return(model); }
public override int GetHashCode() { int hashcode = 157; unchecked { hashcode = (hashcode * 397) + Process.GetHashCode(); hashcode = (hashcode * 397) + TCollections.GetHashCode(Spans); if (__isset.seqNo) { hashcode = (hashcode * 397) + SeqNo.GetHashCode(); } if (__isset.stats) { hashcode = (hashcode * 397) + Stats.GetHashCode(); } } return(hashcode); }
/// <summary> /// 采番清0 /// </summary> /// <param name="model"></param> public void ClearZero(SeqNo model) { switch (model.ZeroType) { case "DAY": if (model.UpDateTime.ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd")) { model.NowNumber = 0; } else { model.NowNumber++; } break; case "MONTH": if (model.UpDateTime.ToString("yyyy-MM") != DateTime.Now.ToString("yyyy-MM")) { model.NowNumber = 0; } else { model.NowNumber++; } break; case "YEAR": if (model.UpDateTime.Year != DateTime.Now.Year) { model.NowNumber = 0; } else { model.NowNumber++; } break; case "NEVER": default: model.NowNumber++; break; } EditSeqNo(model); }
public void SeqNo_must_implement_simple_ordering() { var sm1 = new SeqNo(-1); var s0 = new SeqNo(0); var s1 = new SeqNo(1); var s2 = new SeqNo(2); var s0b = new SeqNo(0); Assert.True(sm1 < s0); Assert.False(sm1 > s0); Assert.True(s0 < s1); Assert.False(s0 > s1); Assert.True(s1 < s2); Assert.False(s1 > s2); Assert.True(s0b == s0); }
static void Main(string[] args) { SeqNo.InitRedis(); Task.Run(() => { for (int i = 0; i < 50; i++) { Console.WriteLine($"Thread1:SeqNo:{SeqNo.GetSeqNoByRedisLock()}"); } }); Task.Run(() => { for (int i = 0; i < 50; i++) { Console.WriteLine($"Thread2:SeqNo:{SeqNo.GetSeqNoByRedisLock()}"); } }); Console.ReadLine(); }
public string Get(string tmpId) { string newSeqNo = null; try { SeqNo seqNo = new SeqNo(); newSeqNo = seqNo.GetSubSeqNo(tmpId); } catch (Exception e) { string logMessage = string.Format($"获取序号出错!TmpId=[{tmpId}]\n {e.StackTrace}"); log.Error(logMessage); var responseMessge = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(logMessage), ReasonPhrase = "Get SEQ_NO ERROR" }; throw new HttpResponseException(responseMessge); } return(newSeqNo); }
public override ByteString ConstructMessage(Address localAddress, IActorRef recipient, SerializedMessage serializedMessage, IActorRef senderOption = null, SeqNo seqOption = null, Ack ackOption = null) { var ackAndEnvelopeBuilder = AckAndEnvelopeContainer.CreateBuilder(); var envelopeBuilder = RemoteEnvelope.CreateBuilder().SetRecipient(SerializeActorRef(recipient.Path.Address, recipient)); if (senderOption != null && senderOption.Path != null) { envelopeBuilder = envelopeBuilder.SetSender(SerializeActorRef(localAddress, senderOption)); } if (seqOption != null) { envelopeBuilder = envelopeBuilder.SetSeq((ulong)seqOption.RawValue); } if (ackOption != null) { ackAndEnvelopeBuilder = ackAndEnvelopeBuilder.SetAck(AckBuilder(ackOption)); } envelopeBuilder = envelopeBuilder.SetMessage(serializedMessage); ackAndEnvelopeBuilder = ackAndEnvelopeBuilder.SetEnvelope(envelopeBuilder); return(ackAndEnvelopeBuilder.Build().ToByteString()); }
public override int GetHashCode() { int hash = 1; if (TigerNo != 0) { hash ^= TigerNo.GetHashCode(); } if (SeqNo != 0) { hash ^= SeqNo.GetHashCode(); } if (BetGold != 0L) { hash ^= BetGold.GetHashCode(); } hash ^= lines_.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
public override int GetHashCode() { int hash = 1; if (TigerNo != 0) { hash ^= TigerNo.GetHashCode(); } if (SeqNo != 0) { hash ^= SeqNo.GetHashCode(); } hash ^= pos_.GetHashCode(); hash ^= bonus_.GetHashCode(); if (current_ != null) { hash ^= Current.GetHashCode(); } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
private void Reset() { _resendBuffer = new AckedSendBuffer<EndpointManager.Send>(_settings.SysMsgBufferSize); ScheduleAutoResend(); _lastCumulativeAck = new SeqNo(-1); _seqCounter = 0L; _pendingAcks = new List<Ack>(); }
protected override void OnReceive(object message) { message.Match() .With<EndpointWriter.FlushAndStop>(flush => { //Trying to serve untilour last breath ResendAll(); _writer.Tell(EndpointWriter.FlushAndStop.Instance); Context.Become(FlushWait); }) .With<EndpointManager.Send>(HandleSend) .With<Ack>(ack => { if (!UidConfirmed) _pendingAcks.Add(ack); else { try { _resendBuffer = _resendBuffer.Acknowledge(ack); } catch (Exception ex) { throw new InvalidAssociationException( string.Format( "Error encountered while processing system message acknowledgement {0} {1}", _resendBuffer, ack), ex); } if (_lastCumulativeAck < ack.CumulativeAck) { _lastCumulativeAck = ack.CumulativeAck; // Cumulative ack is progressing, we might not need to resend non-acked messages yet. // If this progression stops, the timer will eventually kick in, since scheduleAutoResend // does not cancel existing timers (see the "else" case). RescheduleAutoResend(); } else { ScheduleAutoResend(); } ResendNacked(); } }) .With<AttemptSysMsgRedelivery>(sysmsg => { if (UidConfirmed) ResendAll(); }) .With<Terminated>(terminated => { _currentHandle = null; Context.Parent.Tell(new EndpointWriter.StoppedReading(Self)); if (_resendBuffer.NonAcked.Count > 0 || _resendBuffer.Nacked.Count > 0) Context.System.Scheduler.ScheduleTellOnce(_settings.SysResendTimeout, Self, new AttemptSysMsgRedelivery(), Self); Context.Become(Idle); }) .With<GotUid>(g => { Context.Parent.Tell(g); //New system that has the same address as the old - need to start from fresh state UidConfirmed = true; if (Uid.HasValue && Uid.Value != g.Uid) Reset(); else UnstashAcks(); Uid = _refuseUid; }) .With<EndpointWriter.StopReading>(stopped => { _writer.Forward(stopped); //forward the request }); }
public Send(object message, RemoteActorRef recipient, IActorRef senderOption = null, SeqNo seqOpt = null) { Recipient = recipient; SenderOption = senderOption; Message = message; _seq = seqOpt; }
public Send Copy(SeqNo opt) { return new Send(Message, Recipient, SenderOption, opt); }
public override int GetHashCode() => SeqNo.GetHashCode() ^ Identifier?.GetHashCode() ?? -1; // if 'Identifier' is null, then the RHS = '0' (and the HashCode is effectively based on SeqNo).
public void WriteSeqNo(SeqNo seq) { WriteUShort(seq.Value); }
public Sequenced(SeqNo seq, string body) { Body = body; Seq = seq; }
public abstract ByteString ConstructMessage(Address localAddress, IActorRef recipient, SerializedMessage serializedMessage, IActorRef senderOption = null, SeqNo seqOption = null, Ack ackOption = null);
public override ByteString ConstructMessage(Address localAddress, IActorRef recipient, SerializedMessage serializedMessage, IActorRef senderOption = null, SeqNo seqOption = null, Ack ackOption = null) { var ackAndEnvelopeBuilder = AckAndEnvelopeContainer.CreateBuilder(); var envelopeBuilder = RemoteEnvelope.CreateBuilder().SetRecipient(SerializeActorRef(recipient.Path.Address, recipient)); if (senderOption != null && senderOption.Path != null) { envelopeBuilder = envelopeBuilder.SetSender(SerializeActorRef(localAddress, senderOption)); } if (seqOption != null) { envelopeBuilder = envelopeBuilder.SetSeq((ulong)seqOption.RawValue); } if (ackOption != null) { ackAndEnvelopeBuilder = ackAndEnvelopeBuilder.SetAck(AckBuilder(ackOption)); } envelopeBuilder = envelopeBuilder.SetMessage(serializedMessage); ackAndEnvelopeBuilder = ackAndEnvelopeBuilder.SetEnvelope(envelopeBuilder); return ackAndEnvelopeBuilder.Build().ToByteString(); }
public override AckAndMessage DecodeMessage(ByteString raw, RemoteActorRefProvider provider, Address localAddress) { var ackAndEnvelope = AckAndEnvelopeContainer.ParseFrom(raw); Ack ackOption = null; if (ackAndEnvelope.HasAck) { ackOption = new Ack(new SeqNo((long)ackAndEnvelope.Ack.CumulativeAck), ackAndEnvelope.Ack.NacksList.Select(x => new SeqNo((long)x))); } Message messageOption = null; if (ackAndEnvelope.HasEnvelope) { var envelopeContainer = ackAndEnvelope.Envelope; if (envelopeContainer != null) { var recipient = provider.ResolveActorRefWithLocalAddress(envelopeContainer.Recipient.Path, localAddress); Address recipientAddress; ActorPath.TryParseAddress(envelopeContainer.Recipient.Path, out recipientAddress); var serializedMessage = envelopeContainer.Message; IActorRef senderOption = null; if (envelopeContainer.HasSender) { senderOption = provider.ResolveActorRefWithLocalAddress(envelopeContainer.Sender.Path, localAddress); } SeqNo seqOption = null; if (envelopeContainer.HasSeq) { unchecked { seqOption = new SeqNo((long)envelopeContainer.Seq); //proto takes a ulong } } messageOption = new Message(recipient, recipientAddress, serializedMessage, senderOption, seqOption); } } return new AckAndMessage(ackOption, messageOption); }