/// <summary> /// Non-blocking write of an message to an underlying ring-buffer. /// </summary> /// <param name="msgTypeId"> type of the message encoding. </param> /// <param name="srcBuffer"> containing the encoded binary message. </param> /// <param name="srcIndex"> at which the encoded message begins. </param> /// <param name="length"> of the encoded message in bytes. </param> /// <returns> true if written to the ring-buffer, or false if insufficient space exists. </returns> /// <exception cref="ArgumentException"> if the length is greater than <seealso cref="IRingBuffer.MaxMsgLength()"/> </exception> public bool Write(int msgTypeId, IDirectBuffer srcBuffer, int srcIndex, int length) { RecordDescriptor.CheckTypeId(msgTypeId); CheckMsgLength(length); var isSuccessful = false; var buffer = _buffer; var recordLength = length + RecordDescriptor.HeaderLength; var requiredCapacity = BitUtil.Align(recordLength, RecordDescriptor.Alignment); var recordIndex = ClaimCapacity(buffer, requiredCapacity); if (InsufficientCapacity != recordIndex) { buffer.PutLongOrdered(recordIndex, RecordDescriptor.MakeHeader(-recordLength, msgTypeId)); // TODO JPW original: UnsafeAccess.UNSAFE.storeFence(); Thread.MemoryBarrier(); buffer.PutBytes(RecordDescriptor.EncodedMsgOffset(recordIndex), srcBuffer, srcIndex, length); buffer.PutIntOrdered(RecordDescriptor.LengthOffset(recordIndex), recordLength); isSuccessful = true; } return isSuccessful; }
public ElectionStartEventDecoder Wrap( IDirectBuffer buffer, int offset, int actingBlockLength, int actingVersion) { this._buffer = buffer; this._offset = offset; this._actingBlockLength = actingBlockLength; this._actingVersion = actingVersion; Limit(offset + actingBlockLength); return(this); }
public ConsensusModuleDecoder Wrap( IDirectBuffer buffer, int offset, int actingBlockLength, int actingVersion) { this._buffer = buffer; this._offset = offset; this._actingBlockLength = actingBlockLength; this._actingVersion = actingVersion; Limit(offset + actingBlockLength); return(this); }
public void Wrap(IDirectBuffer buffer) { FreeGcHandle(); _needToFreeGcHandle = false; _pBuffer = (byte *)buffer.BufferPointer.ToPointer(); Capacity = buffer.Capacity; ByteArray = buffer.ByteArray; ByteBuffer = buffer.ByteBuffer; }
public AppendedPositionDecoder Wrap( IDirectBuffer buffer, int offset, int actingBlockLength, int actingVersion) { this._buffer = buffer; this._offset = offset; this._actingBlockLength = actingBlockLength; this._actingVersion = actingVersion; Limit(offset + actingBlockLength); return(this); }
public ClusterActionRequestDecoder Wrap( IDirectBuffer buffer, int offset, int actingBlockLength, int actingVersion) { this._buffer = buffer; this._offset = offset; this._actingBlockLength = actingBlockLength; this._actingVersion = actingVersion; Limit(offset + actingBlockLength); return(this); }
public IngressMessageHeaderDecoder Wrap( IDirectBuffer buffer, int offset, int actingBlockLength, int actingVersion) { this._buffer = buffer; this._offset = offset; this._actingBlockLength = actingBlockLength; this._actingVersion = actingVersion; Limit(offset + actingBlockLength); return(this); }
/// <summary> /// Allocate a counter record and wrap it with a new <seealso cref="AtomicCounter"/> for use. /// <para> /// If the keyBuffer is null then a copy of the key is not attempted. /// /// </para> /// </summary> /// <param name="typeId"> for the counter. </param> /// <param name="keyBuffer"> containing the optional key for the counter. </param> /// <param name="keyOffset"> within the keyBuffer at which the key begins. </param> /// <param name="keyLength"> of the key in the keyBuffer. </param> /// <param name="labelBuffer"> containing the mandatory label for the counter. </param> /// <param name="labelOffset"> within the labelBuffer at which the label begins. </param> /// <param name="labelLength"> of the label in the labelBuffer. </param> /// <returns> the id allocated for the counter. </returns> public virtual AtomicCounter NewCounter( int typeId, IDirectBuffer keyBuffer, int keyOffset, int keyLength, IDirectBuffer labelBuffer, int labelOffset, int labelLength) { return(new AtomicCounter(ValuesBuffer, Allocate(typeId, keyBuffer, keyOffset, keyLength, labelBuffer, labelOffset, labelLength), this)); }
public RecoveryPlanQueryDecoder Wrap( IDirectBuffer buffer, int offset, int actingBlockLength, int actingVersion) { this._buffer = buffer; this._offset = offset; this._actingBlockLength = actingBlockLength; this._actingVersion = actingVersion; Limit(offset + actingBlockLength); return(this); }
public long TryClaim(int length, BufferClaim bufferClaim, IDirectBuffer sessionHeader) { long result = _publication.TryClaim(length, bufferClaim); if (result > 0) { bufferClaim.PutBytes(sessionHeader, 0, AeronCluster.SESSION_HEADER_LENGTH); } return(result); }
public DeleteDetachedSegmentsRequestDecoder Wrap( IDirectBuffer buffer, int offset, int actingBlockLength, int actingVersion) { this._buffer = buffer; this._offset = offset; this._actingBlockLength = actingBlockLength; this._actingVersion = actingVersion; Limit(offset + actingBlockLength); return(this); }
public void Wrap( RecoveryPlanDecoder parentMessage, IDirectBuffer buffer) { this._parentMessage = parentMessage; this._buffer = buffer; _dimensions.Wrap(buffer, parentMessage.Limit()); _blockLength = _dimensions.BlockLength(); _count = _dimensions.NumInGroup(); _index = -1; parentMessage.Limit(parentMessage.Limit() + HEADER_SIZE); }
public void SetUp() { delegateFragmentHandler = A.Fake<FragmentHandler>(); termBuffer = A.Fake<IDirectBuffer>(); adapter = new FragmentAssembler(delegateFragmentHandler); header = A.Fake<Header>(x => x.Wrapping(new Header(INITIAL_TERM_ID, LogBufferDescriptor.TERM_MIN_LENGTH))); header.SetBuffer(termBuffer, 0); A.CallTo(() => termBuffer.GetInt(A<int>._)).Returns(SESSION_ID); }
public void SetUp() { delegateFragmentHandler = A.Fake <IFragmentHandler>(); termBuffer = A.Fake <IDirectBuffer>(); adapter = new FragmentAssembler(delegateFragmentHandler); header = A.Fake <Header>(x => x.Wrapping(new Header(INITIAL_TERM_ID, LogBufferDescriptor.TERM_MIN_LENGTH))); header.SetBuffer(termBuffer, 0); A.CallTo(() => termBuffer.GetInt(A <int> ._)).Returns(SESSION_ID); }
private unsafe void DataHandler(IDirectBuffer buffer, int offset, int length, int publicationSessionId) { if (_clientSessions.ContainsKey(publicationSessionId)) { MessageReceived?.Invoke(publicationSessionId, new ReadOnlySpan <byte>((byte *)buffer.BufferPointer + offset, length)); } else { _log.Warn($"Received message from unknown peer. Publication SessionId: {publicationSessionId}"); } }
public bool TerminateDriver(IDirectBuffer tokenBuffer, int tokenOffset, int tokenLength) { _correlatedMessage.CorrelationId(Aeron.NULL_VALUE); _terminateDriver.TokenBuffer(tokenBuffer, tokenOffset, tokenLength); return(_toDriverCommandBuffer.Write( ControlProtocolEvents.TERMINATE_DRIVER, _buffer, 0, _terminateDriver.Length() )); }
/// <summary> /// Get the timestamp at the beginning of recovery. <see cref="Adaptive.Aeron.Aeron.NULL_VALUE"/> if no snapshot for recovery. /// </summary> /// <param name="counters"> to search within. </param> /// <param name="counterId"> for the active recovery counter. </param> /// <returns> the timestamp if found otherwise <see cref="Adaptive.Aeron.Aeron.NULL_VALUE"/>. </returns> public static long GetTimestamp(CountersReader counters, int counterId) { IDirectBuffer buffer = counters.MetaDataBuffer; if (counters.GetCounterState(counterId) == CountersReader.RECORD_ALLOCATED && counters.GetCounterTypeId(counterId) == RECOVERY_STATE_TYPE_ID) { return(buffer.GetLong(CountersReader.MetaDataOffset(counterId) + CountersReader.KEY_OFFSET + TIMESTAMP_OFFSET)); } return(Aeron.Aeron.NULL_VALUE); }
public void OnFragment(IDirectBuffer buffer, int offset, int length, Header header) { messageHeaderDecoder.Wrap(buffer, offset); int schemaId = messageHeaderDecoder.SchemaId(); if (schemaId != MessageHeaderDecoder.SCHEMA_ID) { throw new ClusterException("expected schemaId=" + MessageHeaderDecoder.SCHEMA_ID + ", actual=" + schemaId); } int templateId = messageHeaderDecoder.TemplateId(); switch (templateId) { case JoinLogDecoder.TEMPLATE_ID: joinLogDecoder.Wrap( buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.BlockLength(), messageHeaderDecoder.Version()); clusteredServiceAgent.OnJoinLog( joinLogDecoder.LeadershipTermId(), joinLogDecoder.LogPosition(), joinLogDecoder.MaxLogPosition(), joinLogDecoder.MemberId(), joinLogDecoder.LogSessionId(), joinLogDecoder.LogStreamId(), joinLogDecoder.LogChannel()); break; case ServiceTerminationPositionDecoder.TEMPLATE_ID: serviceTerminationPositionDecoder.Wrap( buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.BlockLength(), messageHeaderDecoder.Version()); clusteredServiceAgent.OnServiceTerminationPosition(serviceTerminationPositionDecoder.LogPosition()); break; case ElectionStartEventDecoder.TEMPLATE_ID: electionStartEventDecoder.Wrap( buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.BlockLength(), messageHeaderDecoder.Version()); clusteredServiceAgent.OnElectionStartEvent(electionStartEventDecoder.LogPosition()); break; } }
/// <summary> /// The implementation of <seealso cref="IControlledFragmentHandler"/> that reassembles and forwards whole messages. /// </summary> /// <param name="buffer"> containing the data. </param> /// <param name="offset"> at which the data begins. </param> /// <param name="length"> of the data in bytes. </param> /// <param name="header"> representing the meta data for the data. </param> /// <returns> <seealso cref="ControlledFragmentHandlerAction"/> to be taken after processing fragment. </returns> public ControlledFragmentHandlerAction OnFragment(IDirectBuffer buffer, int offset, int length, Header header) { byte flags = header.Flags; var action = ControlledFragmentHandlerAction.CONTINUE; if ((flags & FrameDescriptor.UNFRAGMENTED) == FrameDescriptor.UNFRAGMENTED) { action = _delegate.OnFragment(buffer, offset, length, header); } else { if ((flags & FrameDescriptor.BEGIN_FRAG_FLAG) == FrameDescriptor.BEGIN_FRAG_FLAG) { BufferBuilder builder; if (!_builderBySessionIdMap.TryGetValue(header.SessionId, out builder)) { builder = GetBufferBuilder(header.SessionId); _builderBySessionIdMap[header.SessionId] = builder; } builder.Reset().Append(buffer, offset, length); } else { BufferBuilder builder; if (_builderBySessionIdMap.TryGetValue(header.SessionId, out builder)) { int limit = builder.Limit(); if (limit > 0) { builder.Append(buffer, offset, length); if ((flags & FrameDescriptor.END_FRAG_FLAG) == FrameDescriptor.END_FRAG_FLAG) { int msgLength = builder.Limit(); action = _delegate.OnFragment(builder.Buffer(), 0, msgLength, header); if (ControlledFragmentHandlerAction.ABORT == action) { builder.Limit(limit); } else { builder.Reset(); } } } } } } return(action); }
public static void StoreDefaultFrameHeader(UnsafeBuffer logMetaDataBuffer, IDirectBuffer defaultHeader) { if (defaultHeader.Capacity != DataHeaderFlyweight.HEADER_LENGTH) { ThrowHelper.ThrowArgumentException( $"Default header of {defaultHeader.Capacity:D} not equal to {DataHeaderFlyweight.HEADER_LENGTH:D}"); return; } logMetaDataBuffer.PutInt(LOG_DEFAULT_FRAME_HEADER_LENGTH_OFFSET, DataHeaderFlyweight.HEADER_LENGTH); logMetaDataBuffer.PutBytes(LOG_DEFAULT_FRAME_HEADER_OFFSET, defaultHeader, 0, DataHeaderFlyweight.HEADER_LENGTH); }
public ControlledFragmentHandlerAction OnFragment(IDirectBuffer buffer, int offset, int length, Header header) { messageHeaderDecoder.Wrap(buffer, offset); templateId = messageHeaderDecoder.TemplateId(); switch (templateId) { case SessionEventDecoder.TEMPLATE_ID: sessionEventDecoder.Wrap(buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.BlockLength(), messageHeaderDecoder.Version()); clusterSessionId = sessionEventDecoder.ClusterSessionId(); correlationId = sessionEventDecoder.CorrelationId(); leaderMemberId = sessionEventDecoder.LeaderMemberId(); eventCode = sessionEventDecoder.Code(); detail = sessionEventDecoder.Detail(); break; case NewLeaderEventDecoder.TEMPLATE_ID: newLeaderEventDecoder.Wrap(buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.BlockLength(), messageHeaderDecoder.Version()); clusterSessionId = newLeaderEventDecoder.ClusterSessionId(); break; case SessionHeaderDecoder.TEMPLATE_ID: sessionHeaderDecoder.Wrap(buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.BlockLength(), messageHeaderDecoder.Version()); clusterSessionId = sessionHeaderDecoder.ClusterSessionId(); correlationId = sessionHeaderDecoder.CorrelationId(); break; case ChallengeDecoder.TEMPLATE_ID: challengeDecoder.Wrap(buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.BlockLength(), messageHeaderDecoder.Version()); encodedChallenge = new byte[challengeDecoder.EncodedChallengeLength()]; challengeDecoder.GetEncodedChallenge(encodedChallenge, 0, challengeDecoder.EncodedChallengeLength()); clusterSessionId = challengeDecoder.ClusterSessionId(); correlationId = challengeDecoder.CorrelationId(); break; default: throw new ClusterException("unknown templateId: " + templateId); } pollComplete = true; return(ControlledFragmentHandlerAction.BREAK); }
public void OnFragment(IDirectBuffer buffer, int offset, int length, Header header) { _messageHeaderDecoder.Wrap(buffer, offset); int templateId = _messageHeaderDecoder.TemplateId(); switch (templateId) { case SessionEventDecoder.TEMPLATE_ID: _sessionEventDecoder.Wrap( buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, _messageHeaderDecoder.BlockLength(), _messageHeaderDecoder.Version()); _listener.SessionEvent( _sessionEventDecoder.CorrelationId(), _sessionEventDecoder.ClusterSessionId(), _sessionEventDecoder.Code(), _sessionEventDecoder.Detail()); break; case NewLeaderEventDecoder.TEMPLATE_ID: _newLeaderEventDecoder.Wrap(buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, _messageHeaderDecoder.BlockLength(), _messageHeaderDecoder.Version()); _listener.NewLeader(_newLeaderEventDecoder.LastCorrelationId(), _newLeaderEventDecoder.ClusterSessionId(), _newLeaderEventDecoder.LastMessageTimestamp(), _newLeaderEventDecoder.LeadershipTimestamp(), _newLeaderEventDecoder.LeadershipTermId(), _newLeaderEventDecoder.LeaderMemberId(), _newLeaderEventDecoder.MemberEndpoints()); break; case SessionHeaderDecoder.TEMPLATE_ID: _sessionHeaderDecoder.Wrap( buffer, offset + MessageHeaderDecoder.ENCODED_LENGTH, _messageHeaderDecoder.BlockLength(), _messageHeaderDecoder.Version()); _listener.OnMessage( _sessionHeaderDecoder.CorrelationId(), _sessionHeaderDecoder.ClusterSessionId(), _sessionHeaderDecoder.Timestamp(), buffer, offset + SESSION_HEADER_LENGTH, length - SESSION_HEADER_LENGTH, header); break; case ChallengeDecoder.TEMPLATE_ID: break; default: throw new InvalidOperationException("unknown templateId: " + templateId); } }
/// <summary> /// Get the <seealso cref="Image.SourceIdentity()"/> for the recording. /// </summary> /// <param name="counters"> to search within. </param> /// <param name="counterId"> for the active recording. </param> /// <returns> <seealso cref="Image.SourceIdentity()"/> for the recording or null if not found. </returns> public static string GetSourceIdentity(CountersReader counters, int counterId) { IDirectBuffer buffer = counters.MetaDataBuffer; if (counters.GetCounterState(counterId) == RECORD_ALLOCATED && counters.GetCounterTypeId(counterId) == RECORDING_POSITION_TYPE_ID) { int recordOffset = MetaDataOffset(counterId); return(buffer.GetStringAscii(recordOffset + KEY_OFFSET + SOURCE_IDENTITY_LENGTH_OFFSET)); } return(null); }
public long AddCounter(int typeId, IDirectBuffer keyBuffer, int keyOffset, int keyLength, IDirectBuffer labelBuffer, int labelOffset, int labelLength) { long correlationId = _toDriverCommandBuffer.NextCorrelationId(); _counterMessage.TypeId(typeId).KeyBuffer(keyBuffer, keyOffset, keyLength).LabelBuffer(labelBuffer, labelOffset, labelLength).CorrelationId(correlationId); if (!_toDriverCommandBuffer.Write(ControlProtocolEvents.ADD_COUNTER, _buffer, 0, _counterMessage.Length())) { throw new AeronException("Could not write add counter command"); } return(correlationId); }
/// <summary> /// Non-blocking publish of a partial buffer containing a message to a cluster. /// </summary> /// <param name="correlationId"> to be used to identify the message to the cluster. </param> /// <param name="buffer"> containing message. </param> /// <param name="offset"> offset in the buffer at which the encoded message begins. </param> /// <param name="length"> in bytes of the encoded message. </param> /// <returns> the same as <seealso cref="Publication#offer(DirectBuffer, int, int)"/> when in <seealso cref="Cluster.Role#LEADER"/> /// otherwise 1. </returns> public long Offer(long correlationId, IDirectBuffer buffer, int offset, int length) { if (_cluster.Role() != ClusterRole.Leader) { return(1); } _sessionHeaderEncoder.CorrelationId(correlationId); _sessionHeaderEncoder.Timestamp(_cluster.TimeMs()); _messageBuffer.Reset(buffer, offset, length); return(_responsePublication.Offer(_vectors)); }
/// <summary> /// Is the counter still active and log still recording? /// </summary> /// <param name="counters"> to search within. </param> /// <param name="counterId"> to search for. </param> /// <returns> true if the counter is still active otherwise false. </returns> public static bool IsActive(CountersReader counters, int counterId) { IDirectBuffer buffer = counters.MetaDataBuffer; if (counters.GetCounterState(counterId) == CountersReader.RECORD_ALLOCATED) { int recordOffset = CountersReader.MetaDataOffset(counterId); return(buffer.GetInt(recordOffset + CountersReader.TYPE_ID_OFFSET) == COMMIT_POSITION_TYPE_ID); } return(false); }
/// <summary> /// The implementation of <seealso cref="FragmentHandler"/> that reassembles and forwards whole messages. /// </summary> /// <param name="buffer"> containing the data. </param> /// <param name="offset"> at which the data begins. </param> /// <param name="length"> of the data in bytes. </param> /// <param name="header"> representing the meta data for the data. </param> public void OnFragment(IDirectBuffer buffer, int offset, int length, Header header) { byte flags = header.Flags; if ((flags & FrameDescriptor.UNFRAGMENTED) == FrameDescriptor.UNFRAGMENTED) { _delegate.OnFragment(buffer, offset, length, header); } else { HandleFragment(buffer, offset, length, header, flags); } }
private void CompleteMessageReceived(IDirectBuffer buffer, int offset, int length, Header header) { var data = new byte[length]; buffer.GetBytes(offset, data); // // var d = Util.Deserialize<TestMessage>(data); // Console.WriteLine($"Received message ({d}) to stream {testStreamId:D} from session {header.SessionId:x} term id {header.TermId:x} term offset {header.TermOffset:D} ({length:D}@{offset:D})"); _subject.OnNext(data); // Received the intended message, time to exit the program //running.Set(false); }
/// <summary> /// Get the recording id for a given counter id. /// </summary> /// <param name="countersReader"> to search within. </param> /// <param name="counterId"> for the active recording. </param> /// <returns> the counter id if found otherwise <seealso cref="NULL_RECORDING_ID"/>. </returns> public static long GetRecordingId(CountersReader countersReader, int counterId) { IDirectBuffer buffer = countersReader.MetaDataBuffer; if (countersReader.GetCounterState(counterId) == RECORD_ALLOCATED && countersReader.GetCounterTypeId(counterId) == RECORDING_POSITION_TYPE_ID) { return(buffer.GetLong(MetaDataOffset(counterId) + KEY_OFFSET + RECORDING_ID_OFFSET)); } return(NULL_RECORDING_ID); }
/// <summary> /// The implementation of <seealso cref="IControlledFragmentHandler"/> that reassembles and forwards whole messages. /// </summary> /// <param name="buffer"> containing the data. </param> /// <param name="offset"> at which the data begins. </param> /// <param name="length"> of the data in bytes. </param> /// <param name="header"> representing the meta data for the data. </param> public ControlledFragmentHandlerAction OnFragment(IDirectBuffer buffer, int offset, int length, Header header) { byte flags = header.Flags; var action = ControlledFragmentHandlerAction.CONTINUE; if ((flags & FrameDescriptor.UNFRAGMENTED) == FrameDescriptor.UNFRAGMENTED) { action = _delegate.OnFragment(buffer, offset, length, header); } else { if ((flags & FrameDescriptor.BEGIN_FRAG_FLAG) == FrameDescriptor.BEGIN_FRAG_FLAG) { BufferBuilder builder; if (!_builderBySessionIdMap.TryGetValue(header.SessionId, out builder)) { builder = _builderFunc(header.SessionId); _builderBySessionIdMap[header.SessionId] = builder; } builder.Reset().Append(buffer, offset, length); } else { BufferBuilder builder; if (_builderBySessionIdMap.TryGetValue(header.SessionId, out builder)) { int limit = builder.Limit(); builder.Append(buffer, offset, length); if ((flags & FrameDescriptor.END_FRAG_FLAG) == FrameDescriptor.END_FRAG_FLAG) { int msgLength = builder.Limit(); action = _delegate.OnFragment(builder.Buffer(), 0, msgLength, header); if (ControlledFragmentHandlerAction.ABORT == action) { builder.Limit(limit); } else { builder.Reset(); } } } } } return action; }
/// <summary> /// Is the recording counter still active. /// </summary> /// <param name="countersReader"> to search within. </param> /// <param name="counterId"> to search for. </param> /// <param name="recordingId"> to confirm it is still the same value. </param> /// <returns> true if the counter is still active otherwise false. </returns> public static bool IsActive(CountersReader countersReader, int counterId, long recordingId) { IDirectBuffer buffer = countersReader.MetaDataBuffer; if (countersReader.GetCounterState(counterId) == CountersReader.RECORD_ALLOCATED) { int recordOffset = CountersReader.MetaDataOffset(counterId); return(buffer.GetInt(recordOffset + CountersReader.TYPE_ID_OFFSET) == RECORDING_POSITION_TYPE_ID && buffer.GetLong(recordOffset + CountersReader.KEY_OFFSET + RECORDING_ID_OFFSET) == recordingId); } return(false); }
private static ulong ReadUInt64(IDirectBuffer db, int index, int length) { ulong ret = 0; for (int pos = 0; pos < length; pos++) { byte b = (byte)(db.ReadAsciiDigit(index + pos)); if (b > 0) { ret += b * (ULongPower(10, (short)(length - pos - 1))); } } return(ret); }
public static void PingHandler(Publication pongPublication, IDirectBuffer buffer, int offset, int length) { if (pongPublication.Offer(buffer, offset, length) > 0L) { return; } PingHandlerIdleStrategy.Reset(); while (pongPublication.Offer(buffer, offset, length) < 0L) { PingHandlerIdleStrategy.Idle(); } }
private static void PingHandler(Publication pongPublication, IDirectBuffer buffer, int offset, int length) { if (pongPublication.Offer(buffer, offset, length) > 0L) { return; } PingHandlerIdleStrategy.Reset(); while (pongPublication.Offer(buffer, offset, length) < 0L) { PingHandlerIdleStrategy.Idle(); } }
/// <summary> /// The implementation of <seealso cref="FragmentHandler"/> that reassembles and forwards whole messages. /// </summary> /// <param name="buffer"> containing the data. </param> /// <param name="offset"> at which the data begins. </param> /// <param name="length"> of the data in bytes. </param> /// <param name="header"> representing the meta data for the data. </param> public void OnFragment(IDirectBuffer buffer, int offset, int length, Header header) { byte flags = header.Flags; if ((flags & FrameDescriptor.UNFRAGMENTED) == FrameDescriptor.UNFRAGMENTED) { _delegate(buffer, offset, length, header); } else { if ((flags & FrameDescriptor.BEGIN_FRAG_FLAG) == FrameDescriptor.BEGIN_FRAG_FLAG) { BufferBuilder builder; if (!_builderBySessionIdMap.TryGetValue(header.SessionId, out builder)) { builder = _builderFunc(header.SessionId); _builderBySessionIdMap[header.SessionId] = builder; } builder.Reset().Append(buffer, offset, length); } else { BufferBuilder builder; _builderBySessionIdMap.TryGetValue(header.SessionId, out builder); if (null != builder && builder.Limit() != 0) { builder.Append(buffer, offset, length); if ((flags & FrameDescriptor.END_FRAG_FLAG) == FrameDescriptor.END_FRAG_FLAG) { int msgLength = builder.Limit(); _delegate(builder.Buffer(), 0, msgLength, header); builder.Reset(); } } } } }
public static UnsafeBuffer CreateToDriverBuffer(MappedByteBuffer buffer, IDirectBuffer metaDataBuffer) { return new UnsafeBuffer(buffer.Pointer, END_OF_METADATA_OFFSET, metaDataBuffer.GetInt(ToDriverBufferLengthOffset(0))); }
/// <summary> /// Non-blocking publish of a buffer containing a message. /// </summary> /// <param name="buffer"> containing message. </param> /// <returns> The new stream position, otherwise <seealso cref="NOT_CONNECTED"/>, <seealso cref="BACK_PRESSURED"/>, /// <seealso cref="ADMIN_ACTION"/> or <seealso cref="CLOSED"/>. </returns> public long Offer(IDirectBuffer buffer) { return Offer(buffer, 0, buffer.Capacity); }
static void PrintMessage(IDirectBuffer buffer, int offset, int length, Header header) { var message = buffer.GetStringWithoutLengthUtf8(offset, length); Console.WriteLine($"Received message ({message}) to stream {header.StreamId:D} from session {header.SessionId:x} term id {header.TermId:x} term offset {header.TermOffset:D} ({length:D}@{offset:D})"); }
public void Wrap(IDirectBuffer buffer) { FreeGcHandle(); _needToFreeGcHandle = false; _pBuffer = (byte*) buffer.BufferPointer.ToPointer(); Capacity = buffer.Capacity; }
/// <summary> /// Append a fragmented message to the the term buffer. /// The message will be split up into fragments of MTU length minus header. /// </summary> /// <param name="header"> for writing the default header. </param> /// <param name="srcBuffer"> containing the message. </param> /// <param name="srcOffset"> at which the message begins. </param> /// <param name="length"> of the message in the source buffer. </param> /// <param name="maxPayloadLength"> that the message will be fragmented into. </param> /// /// <param name="reservedValueSupplier"><see cref="ReservedValueSupplier"/> for the frame</param> /// <returns> the resulting offset of the term after the append on success otherwise <seealso cref="#TRIPPED"/> or <seealso cref="#FAILED"/> /// packed with the termId if a padding record was inserted at the end. </returns> public long AppendFragmentedMessage(HeaderWriter header, IDirectBuffer srcBuffer, int srcOffset, int length, int maxPayloadLength, ReservedValueSupplier reservedValueSupplier) { int numMaxPayloads = length/maxPayloadLength; int remainingPayload = length%maxPayloadLength; int lastFrameLength = remainingPayload > 0 ? BitUtil.Align(remainingPayload + DataHeaderFlyweight.HEADER_LENGTH, FrameDescriptor.FRAME_ALIGNMENT) : 0; int requiredLength = (numMaxPayloads*(maxPayloadLength + DataHeaderFlyweight.HEADER_LENGTH)) + lastFrameLength; long rawTail = GetAndAddRawTail(requiredLength); int termId = TermId(rawTail); long termOffset = rawTail & 0xFFFFFFFFL; IAtomicBuffer termBuffer = _termBuffer; int termLength = termBuffer.Capacity; long resultingOffset = termOffset + requiredLength; if (resultingOffset > termLength) { resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId); } else { int offset = (int) termOffset; byte flags = FrameDescriptor.BEGIN_FRAG_FLAG; int remaining = length; do { int bytesToWrite = Math.Min(remaining, maxPayloadLength); int frameLength = bytesToWrite + DataHeaderFlyweight.HEADER_LENGTH; int alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT); header.Write(termBuffer, offset, frameLength, termId); termBuffer.PutBytes(offset + DataHeaderFlyweight.HEADER_LENGTH, srcBuffer, srcOffset + (length - remaining), bytesToWrite); if (remaining <= maxPayloadLength) { flags |= FrameDescriptor.END_FRAG_FLAG; } FrameDescriptor.FrameFlags(termBuffer, offset, flags); if (null != reservedValueSupplier) { long reservedValue = reservedValueSupplier(termBuffer, offset, frameLength); termBuffer.PutLong(offset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue); } FrameDescriptor.FrameLengthOrdered(termBuffer, offset, frameLength); flags = 0; offset += alignedLength; remaining -= bytesToWrite; } while (remaining > 0); } return resultingOffset; }
public HeaderWriter(IDirectBuffer defaultHeader) { _versionFlagsType = (long)defaultHeader.GetInt(HeaderFlyweight.VERSION_FIELD_OFFSET) << 32; _sessionId = (long)defaultHeader.GetInt(DataHeaderFlyweight.SESSION_ID_FIELD_OFFSET) << 32; _streamId = defaultHeader.GetInt(DataHeaderFlyweight.STREAM_ID_FIELD_OFFSET) & 0xFFFFFFFFL; }
/// <summary> /// Attach a view to an existing <seealso cref="IDirectBuffer"/> /// </summary> /// <param name="buffer"> to which the view is attached. </param> public UnsafeBuffer(IDirectBuffer buffer) { Wrap(buffer); }
/// <summary> /// Attach a view to an existing <seealso cref="IDirectBuffer"/> /// </summary> /// <param name="buffer"> to which the view is attached. </param> /// <param name="offset"> within the buffer to begin. </param> /// <param name="length"> of the buffer to be included. </param> public UnsafeBuffer(IDirectBuffer buffer, int offset, int length) { Wrap(buffer, offset, length); }
public static UnsafeBuffer CreateErrorLogBuffer(MappedByteBuffer buffer, IDirectBuffer metaDataBuffer) { var offset = END_OF_METADATA_OFFSET + metaDataBuffer.GetInt(ToDriverBufferLengthOffset(0)) + metaDataBuffer.GetInt(ToClientsBufferLengthOffset(0)) + metaDataBuffer.GetInt(CountersMetaDataBufferLengthOffset(0)) + metaDataBuffer.GetInt(CountersValuesBufferLengthOffset(0)); return new UnsafeBuffer(buffer.Pointer, offset, metaDataBuffer.GetInt(ErrorLogBufferLengthOffset(0))); }
/// <summary> /// Non-blocking publish of a partial buffer containing a message. /// </summary> /// <param name="buffer"> containing message. </param> /// <param name="offset"> offset in the buffer at which the encoded message begins. </param> /// <param name="length"> in bytes of the encoded message. </param> /// <returns> The new stream position, otherwise a negative error value <seealso cref="NOT_CONNECTED"/>, <seealso cref="BACK_PRESSURED"/>, /// <seealso cref="ADMIN_ACTION"/> or <seealso cref="CLOSED"/>. </returns> public long Offer(IDirectBuffer buffer, int offset, int length, ReservedValueSupplier reservedValueSupplier = null) { var newPosition = CLOSED; if (!_isClosed) { var limit = _positionLimit.Volatile; var partitionIndex = LogBufferDescriptor.ActivePartitionIndex(_logMetaDataBuffer); var termAppender = _termAppenders[partitionIndex]; var rawTail = termAppender.RawTailVolatile(); var termOffset = rawTail & 0xFFFFFFFFL; var position = LogBufferDescriptor.ComputeTermBeginPosition(LogBufferDescriptor.TermId(rawTail), _positionBitsToShift, InitialTermId) + termOffset; if (position < limit) { long result; if (length <= _maxPayloadLength) { result = termAppender.AppendUnfragmentedMessage(_headerWriter, buffer, offset, length, reservedValueSupplier); } else { CheckForMaxMessageLength(length); result = termAppender.AppendFragmentedMessage(_headerWriter, buffer, offset, length, _maxPayloadLength, reservedValueSupplier); } newPosition = NewPosition(partitionIndex, (int) termOffset, position, result); } else if (_clientConductor.IsPublicationConnected(LogBufferDescriptor.TimeOfLastStatusMessage(_logMetaDataBuffer))) { newPosition = BACK_PRESSURED; } else { newPosition = NOT_CONNECTED; } } return newPosition; }
public static long ClientLivenessTimeout(IDirectBuffer metaDataBuffer) { return metaDataBuffer.GetLong(ClientLivenessTimeoutOffset(0)); }
public void PutBytes(int index, IDirectBuffer srcBuffer, int srcIndex, int length) { BoundsCheck0(index, length); srcBuffer.BoundsCheck(srcIndex, length); void* destination = _pBuffer + index; void* source = (byte*) srcBuffer.BufferPointer.ToPointer() + srcIndex; ByteUtil.MemoryCopy(destination, source, (uint) length); }
public void Wrap(IDirectBuffer buffer, int offset, int length) { #if SHOULD_BOUNDS_CHECK int bufferCapacity = buffer.Capacity; if (offset != 0 && (offset < 0 || offset > bufferCapacity - 1)) { throw new ArgumentException("offset=" + offset + " not valid for buffer.capacity()=" + bufferCapacity); } if (length < 0 || length > bufferCapacity - offset) { throw new ArgumentException("offset=" + offset + " length=" + length + " not valid for buffer.capacity()=" + bufferCapacity); } #endif FreeGcHandle(); _needToFreeGcHandle = false; _pBuffer = (byte*) buffer.BufferPointer.ToPointer() + offset; Capacity = length; }
private static ulong ReadUInt64(IDirectBuffer db, int index, int length) { ulong ret = 0; for (int pos = 0; pos < length; pos++) { byte b = (byte)(db.ReadAsciiDigit(index + pos)); if (b > 0) { ret += b * (ULongPower(10, (short)(length - pos - 1))); } } return ret; }
private static void PongHandler(IDirectBuffer buffer, int offset, int length, Header header) { var pingTimestamp = buffer.GetLong(offset); var rttNs = Stopwatch.GetTimestamp() - pingTimestamp; var b = rttNs*1000*1000*1000d/Stopwatch.Frequency; Histogram.RecordValue((long) b); }
private static long ReadHHMMSSXXXXXXAsUtcTicks(DateTime date, IDirectBuffer db, int index) { // TODO method ReadAsciiDigit var hh = (db.ReadAsciiDigit(index)) * 10 + db.ReadAsciiDigit(index + 1); var mm = (db.ReadAsciiDigit(index + 2)) * 10 + db.ReadAsciiDigit(index + 3); var ss = (db.ReadAsciiDigit(index + 4)) * 10 + db.ReadAsciiDigit(index + 5); var micros = (db.ReadAsciiDigit(index + 6)) * 100000 + (db.ReadAsciiDigit(index + 7)) * 10000 + (db.ReadAsciiDigit(index + 8)) * 1000 + (db.ReadAsciiDigit(index + 9)) * 100 + (db.ReadAsciiDigit(index + 10)) * 10 + (db.ReadAsciiDigit(index + 11)); var ticks = date.Date.Ticks // hours + hh * TimeSpan.TicksPerHour // minutes + mm * TimeSpan.TicksPerMinute // seconds + ss * TimeSpan.TicksPerSecond // micros + micros * 10; var dt = new DateTime(ticks, DateTimeKind.Unspecified); // this is pefromance killer - for the same date delta is always the same, should // calculate utc via ticks by adding pre-calculated delta //dt = dt.ConvertToUtcWithUncpecifiedKind("ny"); return dt.Ticks; }
public void OnFragment(IDirectBuffer buffer, int offset, int length, Header header) { _totalBytes.Add(length); }
/// <summary> /// Store the default frame header to the log meta data buffer. /// </summary> /// <param name="logMetaDataBuffer"> into which the default headers should be stored. </param> /// <param name="defaultHeader"> to be stored. </param> /// <exception cref="ArgumentException"> if the default header is larger than <seealso cref="LOG_DEFAULT_FRAME_HEADER_MAX_LENGTH"/> </exception> public static void StoreDefaultFrameHeader(UnsafeBuffer logMetaDataBuffer, IDirectBuffer defaultHeader) { if (defaultHeader.Capacity != DataHeaderFlyweight.HEADER_LENGTH) { throw new ArgumentException( $"Default header of {defaultHeader.Capacity:D} not equal to {DataHeaderFlyweight.HEADER_LENGTH:D}"); } logMetaDataBuffer.PutInt(LOG_DEFAULT_FRAME_HEADER_LENGTH_OFFSET, DataHeaderFlyweight.HEADER_LENGTH); logMetaDataBuffer.PutBytes(LOG_DEFAULT_FRAME_HEADER_OFFSET, defaultHeader, 0, DataHeaderFlyweight.HEADER_LENGTH); }
/////////////////////////////////////////////////////////////////////////// public int CompareTo(IDirectBuffer that) { var thisCapacity = this.Capacity; var thatCapacity = that.Capacity; var thisPointer = this._pBuffer; var thatPointer = (byte*) that.BufferPointer.ToPointer(); for (int i = 0, length = Math.Min(thisCapacity, thatCapacity); i < length; i++) { var cmp = thisPointer[i] - thatPointer[i]; if (0 != cmp) { return cmp; } } if (thisCapacity != thatCapacity) { return thisCapacity - thatCapacity; } return 0; }
/// <summary> /// Append an unfragmented message to the the term buffer. /// </summary> /// <param name="header"> for writing the default header. </param> /// <param name="srcBuffer"> containing the message. </param> /// <param name="srcOffset"> at which the message begins. </param> /// <param name="length"> of the message in the source buffer. </param> /// <param name="reservedValueSupplier"><see cref="ReservedValueSupplier"/> for the frame</param> /// <returns> the resulting offset of the term after the append on success otherwise <seealso cref="TRIPPED"/> or <seealso cref="FAILED"/> /// packed with the termId if a padding record was inserted at the end. </returns> public virtual long AppendUnfragmentedMessage(HeaderWriter header, IDirectBuffer srcBuffer, int srcOffset, int length, ReservedValueSupplier reservedValueSupplier) { int frameLength = length + DataHeaderFlyweight.HEADER_LENGTH; int alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT); long rawTail = GetAndAddRawTail(alignedLength); long termOffset = rawTail & 0xFFFFFFFFL; IAtomicBuffer termBuffer = _termBuffer; int termLength = termBuffer.Capacity; long resultingOffset = termOffset + alignedLength; if (resultingOffset > termLength) { resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, TermId(rawTail)); } else { int offset = (int) termOffset; header.Write(termBuffer, offset, frameLength, TermId(rawTail)); termBuffer.PutBytes(offset + DataHeaderFlyweight.HEADER_LENGTH, srcBuffer, srcOffset, length); if (null != reservedValueSupplier) { long reservedValue = reservedValueSupplier(termBuffer, offset, frameLength); termBuffer.PutLong(offset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue); } FrameDescriptor.FrameLengthOrdered(termBuffer, offset, frameLength); } return resultingOffset; }