public static ProducerRequest ReadFrom(ByteBuffer buffer) { var versionId = buffer.GetShort(); var correlationId = buffer.GetInt(); var clientId = ApiUtils.ReadShortString(buffer); var requiredAcks = buffer.GetShort(); var ackTimeoutMs = buffer.GetInt(); // built the topic structure var topicCount = buffer.GetInt(); var partitionDataPairs = Enumerable.Range(1, topicCount).SelectMany(_ => { // process topic var topic = ApiUtils.ReadShortString(buffer); var partitionCount = buffer.GetInt(); return Enumerable.Range(1, partitionCount).Select(__ => { var partition = buffer.GetInt(); var messagesSetSize = buffer.GetInt(); var messageSetBuffer = new byte[messagesSetSize]; buffer.Get(messageSetBuffer, 0, messagesSetSize); return Tuple.Create( new TopicAndPartition(topic, partition), new ByteBufferMessageSet(ByteBuffer.Wrap(messageSetBuffer))); }); }); return new ProducerRequest(versionId, correlationId, clientId, requiredAcks, ackTimeoutMs, partitionDataPairs.ToDictionary(k => k.Item1, v => v.Item2)); }
public static FetchResponsePartitionData ReadFrom(ByteBuffer buffer) { var error = buffer.GetShort(); var hw = buffer.GetLong(); var messageSetSize = buffer.GetInt(); var messageSetBuffer = buffer.Slice(); messageSetBuffer.Limit(messageSetSize); buffer.Position = buffer.Position + messageSetSize; return new FetchResponsePartitionData(error, hw, new ByteBufferMessageSet(messageSetBuffer)); }
/// <summary> /// Read size prefixed string where the size is stored as a 2 byte short. /// </summary> /// <param name="buffer">The buffer to read from</param> /// <returns></returns> public static string ReadShortString(ByteBuffer buffer) { var size = buffer.GetShort(); if (size < 0) { return null; } var bytes = new byte[size]; buffer.Read(bytes, 0, bytes.Length); return Encoding.UTF8.GetString(bytes); }
public static TopicMetadataRequest ReadFrom(ByteBuffer buffer) { var versonId = buffer.GetShort(); var correlationID = buffer.GetInt(); var clientId = ApiUtils.ReadShortString(buffer); var numTopic = ApiUtils.ReadIntInRange(buffer, "number of topics", Tuple.Create(0, int.MaxValue)); var topics = new List<string>(numTopic); for (var i = 0; i < numTopic; i++) { topics.Add(ApiUtils.ReadShortString(buffer)); } return new TopicMetadataRequest(versonId, correlationID, clientId, topics); }
public static OffsetResponse ReadFrom(ByteBuffer buffer) { var correlationId = buffer.GetInt(); var numTopics = buffer.GetInt(); var pairs = Enumerable.Range(1, numTopics).SelectMany(_ => { var topic = ApiUtils.ReadShortString(buffer); var numPartitions = buffer.GetInt(); return Enumerable.Range(1, numPartitions).Select(__ => { var partiton = buffer.GetInt(); var error = buffer.GetShort(); var numOffsets = buffer.GetInt(); var offsets = Enumerable.Range(1, numOffsets).Select(o => buffer.GetLong()).ToList(); return new KeyValuePair<TopicAndPartition, PartitionOffsetsResponse>( new TopicAndPartition(topic, partiton), new PartitionOffsetsResponse(error, offsets)); }); }); return new OffsetResponse(correlationId, pairs.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); }
public static ProducerResponse ReadFrom(ByteBuffer buffer) { var correlationId = buffer.GetInt(); var topicCount = buffer.GetInt(); var statusPairs = Enumerable.Range(0, topicCount).SelectMany( _ => { var topic = ApiUtils.ReadShortString(buffer); var partitionCount = buffer.GetInt(); return Enumerable.Range(0, partitionCount).Select( __ => { var partition = buffer.GetInt(); var error = buffer.GetShort(); var offset = buffer.GetLong(); return new KeyValuePair<TopicAndPartition, ProducerResponseStatus>( new TopicAndPartition(topic, partition), new ProducerResponseStatus(error, offset)); }); }); return new ProducerResponse(statusPairs.ToDictionary(x => x.Key, x => x.Value), correlationId); }
public static OffsetRequest ReadFrom(ByteBuffer buffer) { var versionId = buffer.GetShort(); var correlationId = buffer.GetInt(); var clientId = ApiUtils.ReadShortString(buffer); var replicaId = buffer.GetInt(); var topicCount = buffer.GetInt(); var pairs = Enumerable.Range(1, topicCount).SelectMany( _ => { var topic = ApiUtils.ReadShortString(buffer); var partitionCount = buffer.GetInt(); return Enumerable.Range(1, partitionCount).Select(__ => { var partitionId = buffer.GetInt(); var time = buffer.GetLong(); var maxNumOffsets = buffer.GetInt(); return Tuple.Create( new TopicAndPartition(topic, partitionId), new PartitionOffsetRequestInfo(time, maxNumOffsets)); }); }); return new OffsetRequest(pairs.ToDictionary(x => x.Item1, x => x.Item2), versionId: versionId, clientId: clientId, correlationId:correlationId, replicaId:replicaId); }
public static FetchRequest ReadFrom(ByteBuffer buffer) { var versionId = buffer.GetShort(); var correlationId = buffer.GetInt(); var clientId = ApiUtils.ReadShortString(buffer); var replicaId = buffer.GetInt(); var maxWait = buffer.GetInt(); var minBytes = buffer.GetInt(); var topicCount = buffer.GetInt(); var pairs = Enumerable.Range(1, topicCount).SelectMany(_ => { var topic = ApiUtils.ReadShortString(buffer); var partitionCount = buffer.GetInt(); return Enumerable.Range(1, partitionCount).Select(__ => { var partitionId = buffer.GetInt(); var offset = buffer.GetLong(); var fetchSize = buffer.GetInt(); return Tuple.Create( new TopicAndPartition(topic, partitionId), new PartitionFetchInfo(offset, fetchSize)); }); }); return new FetchRequest(versionId, correlationId, clientId, replicaId, maxWait, minBytes, pairs.ToDictionary(x => x.Item1, x => x.Item2)); }
/// <summary> /// Read an short out of the ByteBuffer from the current position and check that it falls within the given /// range. If not, throw KafkaException. /// </summary> /// <param name="buffer"></param> /// <param name="name"></param> /// <param name="range"></param> /// <returns></returns> public static short ReadShortInRange(ByteBuffer buffer, string name, Tuple<short, short> range) { var value = buffer.GetShort(); if (value < range.Item1 || value > range.Item2) { throw new KafkaException(string.Format("{0} has value {1} which is not in the range {2}", name, value, range)); } return value; }