public void Serialize(ReusableMemoryStream stream, CompressionCodec compressionCodec, Tuple <ISerializer, ISerializer> serializers) { var crcPos = stream.Position; stream.Write(Basics.MinusOne32, 0, 4); // crc placeholder var bodyPos = stream.Position; stream.WriteByte(0); // magic byte stream.WriteByte((byte)compressionCodec); // attributes if (SerializedKeyValue != null) { stream.Write(SerializedKeyValue.GetBuffer(), 0, (int)SerializedKeyValue.Length); } else { DoSerializeKeyValue(stream, serializers); } // update crc var crc = Crc32.Compute(stream, bodyPos, stream.Position - bodyPos); var curPos = stream.Position; stream.Position = crcPos; BigEndianConverter.Write(stream, (int)crc); stream.Position = curPos; }
/// <summary> /// The serialize event. /// </summary> /// <param name="eventData">The event data.</param> /// <returns>The serialized event.</returns> /// <exception cref="T:System.IO.InvalidDataException"> ///A value can not be serialized. ///</exception> ///<exception cref="T:System.ArrayTypeMismatchException"> /// A collection with different types can not be serialized. ///</exception> public byte[] SerializeEventData(EventData eventData) { using (ReusableMemoryStream stream = new ReusableMemoryStream()) { stream.WriteByte(0xf3); stream.WriteByte(4); SerializeEventData(stream, eventData); return(stream.ToArray()); } }
/// <summary> /// Serializes an <see cref="T:Photon.SocketServer.OperationResponse"/>. /// </summary> /// <param name="operationResponse">The operation response to serialize.</param> /// <param name="messageType"> /// The message type. /// Should be eiter <see /// cref="F:Photon.SocketServer.Rpc.Protocols.RtsMessageType.OperationResponse"/> or <see /// cref="F:Photon.SocketServer.Rpc.Protocols.RtsMessageType.InternalOperationResponse"/>. /// </param> /// <returns>A serialized operation response.</returns> private byte[] SerializeOperationResponse(OperationResponse operationResponse, RtsMessageType messageType) { using (ReusableMemoryStream stream = new ReusableMemoryStream()) { stream.WriteByte(0xf3); stream.WriteByte((byte)messageType); SerializeOperationResponse(stream, operationResponse); return(stream.ToArray()); } }
/// <summary> /// Serializes an operation request. /// </summary> /// <param name="operationRequest">The operation request.</param> /// <returns>a serialized operation request message</returns> public byte[] SerializeOperationRequest(OperationRequest operationRequest) { using (ReusableMemoryStream stream = new ReusableMemoryStream()) { stream.WriteByte(0xf3); stream.WriteByte(2); SerializeOperationRequest(stream, operationRequest); return(stream.ToArray()); } }
/// <summary> /// The serialize event. /// </summary> /// <param name="eventData">The event data.</param> /// <returns> The serialized event</returns> public byte[] SerializeEventData(EventData eventData) { using (ReusableMemoryStream stream = new ReusableMemoryStream()) { BigEndianBinaryWriter binaryWriter = new BigEndianBinaryWriter(stream); stream.Position = this.headerSize; stream.WriteByte(eventData.Code); if ((eventData.Parameters == null) || (eventData.Parameters.Count == 0)) { binaryWriter.WriteInt16(0); } else { binaryWriter.WriteInt16((short)eventData.Parameters.Count); Amf3Writer writer2 = new Amf3Writer(binaryWriter); foreach (KeyValuePair <byte, object> pair in eventData.Parameters) { writer2.WriteInteger((long)((ulong)pair.Key)); writer2.Write(pair.Value); } } stream.Position = 0L; this.headerWriter.WriteHeader(stream, RtsMessageType.Event, false); return(stream.ToArray()); } }
public ReusableMemoryStream Serialize(ReusableMemoryStream target, long baseTimestamp, long offsetDelta) { var timestampDelta = Timestamp - baseTimestamp; VarIntConverter.Write(target, SizeOfBodyInBytes(offsetDelta, timestampDelta)); // Record attributes are always null. target.WriteByte(0x00); VarIntConverter.Write(target, timestampDelta); VarIntConverter.Write(target, offsetDelta); Basics.WriteObject(target, Key, KeySerializer); Basics.WriteObject(target, Value, ValueSerializer); if (Headers == null) { target.Write(Basics.ZeroVarInt, 0, Basics.ZeroVarInt.Length); } else { VarIntConverter.Write(target, Headers.Count); foreach (KafkaRecordHeader header in Headers) { SerializeHeader(target, header); } } return(target); }
public async Task TestNoAckFlow() { const string data = "The cuiqk brwon fox jumps over the zaly god."; var server = new FakeServer(SimulationMode.Success); var connection = new Connection(server.EndPoint, Connection.DefaultSocketFactory, BPool, RPool, 16, 16); const int correlationId = 379821; const byte ack = 1; var buffer = new ReusableMemoryStream(null); BigEndianConverter.Write(buffer, 4 + 4 + 1 + data.Length - 4); BigEndianConverter.Write(buffer, correlationId); buffer.WriteByte(ack); var s = Encoding.UTF8.GetBytes(data); buffer.Write(s, 0, s.Length); var bdata = buffer.ToArray(); var p = new TaskCompletionSource <Response>(); connection.Response += (con, cor, b) => p.SetResult(new Response { Connection = con, CorrelationId = cor, Data = b }); await connection.ConnectAsync(); buffer.Position = 8; buffer.WriteByte(0); await connection.SendAsync(correlationId, buffer, false); buffer = new ReusableMemoryStream(null); buffer.Write(bdata, 0, bdata.Length); buffer.Position = 8; buffer.WriteByte(1); await connection.SendAsync(correlationId, buffer, true); var r = await p.Task; server.Stop(); Assert.AreSame(connection, r.Connection); Assert.AreEqual(correlationId, r.CorrelationId); Assert.AreEqual(data, Encoding.UTF8.GetString(r.Data.ToArray())); }
public void Serialize(ReusableMemoryStream stream, CompressionCodec compressionCodec, Tuple <ISerializer, ISerializer> serializers, MessageVersion msgVersion) { var crcPos = stream.Position; stream.Write(Basics.MinusOne32, 0, 4); // crc placeholder var bodyPos = stream.Position; // V0 message format if (msgVersion == MessageVersion.V0) { stream.WriteByte(0); // magic byte stream.WriteByte((byte)compressionCodec); // attributes } else // V1 message format { stream.WriteByte(1); // magic byte stream.WriteByte((byte)compressionCodec); // attributes BigEndianConverter.Write(stream, TimeStamp); } if (SerializedKeyValue != null) { if (SerializedKeyValue.Length < MinimumValidSizeForSerializedKeyValue) { HandleInvalidSerializedKeyValue(stream); } else { stream.Write(SerializedKeyValue.GetBuffer(), 0, (int)SerializedKeyValue.Length); } } else { DoSerializeKeyValue(stream, serializers); } // update crc var crc = Crc32.Compute(stream, bodyPos, stream.Position - bodyPos); var curPos = stream.Position; stream.Position = crcPos; BigEndianConverter.Write(stream, (int)crc); stream.Position = curPos; }
public void Test004_DeserializeString() { using (var serialized = new ReusableMemoryStream(null)) { // Non null string BigEndianConverter.Write(serialized, (short)Value.Length); serialized.Write(Value, 0, Value.Length); serialized.Position = 0; Assert.AreEqual(TheValue, Basics.DeserializeString(serialized)); // Null string serialized.SetLength(0); serialized.WriteByte(0xFF); serialized.WriteByte(0xFF); serialized.Position = 0; Assert.IsNull(Basics.DeserializeString(serialized)); } }
/// <summary> /// Encrypts an event. /// </summary> /// <param name="eventData"> The event data.</param> /// <param name="cryptoProvider">The crypto provider.</param> /// <returns>the encrypted event.</returns> /// <exception cref="T:System.ArgumentNullException"> /// cryptoProvider is null. /// </exception> public byte[] SerializeEventDataEncrypted(IEventData eventData, ICryptoProvider cryptoProvider) { byte[] buffer; if (cryptoProvider == null) { throw new ArgumentNullException("cryptoProvider"); } using (ReusableMemoryStream stream = new ReusableMemoryStream()) { SerializeEventData(stream, eventData); byte[] data = stream.ToArray(); buffer = cryptoProvider.Encrypt(data, 0, data.Length); } using (ReusableMemoryStream stream2 = new ReusableMemoryStream()) { stream2.WriteByte(0xf3); stream2.WriteByte(0x84); stream2.Write(buffer, 0, buffer.Length); return(stream2.ToArray()); } }
/// <summary> /// Serializes an <see cref="T:Photon.SocketServer.OperationResponse"/>. /// The operation response data will be encrypted using the specified <see cref="T:Photon.SocketServer.Security.ICryptoProvider"/>. /// </summary> /// <param name="operationResponse"> The response.</param> /// <param name="cryptoProvider">An <see cref="T:Photon.SocketServer.Security.ICryptoProvider"/> instance used to encrypt the operation response.</param> /// <returns>The serialized operation response.</returns> public byte[] SerializeOperationResponseEncrypted(OperationResponse operationResponse, ICryptoProvider cryptoProvider) { byte[] buffer; if (cryptoProvider == null) { throw new ArgumentNullException("cryptoProvider"); } using (ReusableMemoryStream stream = new ReusableMemoryStream()) { SerializeOperationResponse(stream, operationResponse); byte[] data = stream.ToArray(); buffer = cryptoProvider.Encrypt(data); } using (ReusableMemoryStream stream2 = new ReusableMemoryStream()) { stream2.WriteByte(0xf3); stream2.WriteByte(0x83); stream2.Write(buffer, 0, buffer.Length); return(stream2.ToArray()); } }
public void SerializeBody(ReusableMemoryStream stream, object extra, Basics.ApiVersion version) { stream.Write(Basics.MinusOne32, 0, 4); // ReplicaId, non clients that are not a broker must use -1 BigEndianConverter.Write(stream, MaxWaitTime); BigEndianConverter.Write(stream, MinBytes); if (version >= Basics.ApiVersion.V3) { BigEndianConverter.Write(stream, MaxBytes); } if (version >= Basics.ApiVersion.V4) { stream.WriteByte((byte)IsolationLevel); } Basics.WriteArray(stream, TopicsData, extra, version); }
public async Task TestCorrelationError() { const string data = "The cuiqk brwon fox jumps over the zaly god."; var server = new FakeServer(SimulationMode.CorrelationIdError); var connection = new Connection(server.EndPoint, Connection.DefaultSocketFactory, BPool, RPool, 1024, 1024); const int correlationId = 379821; const byte ack = 1; var buffer = new ReusableMemoryStream(null); BigEndianConverter.Write(buffer, 4 + 4 + 1 + data.Length - 4); BigEndianConverter.Write(buffer, correlationId); buffer.WriteByte(ack); var s = Encoding.UTF8.GetBytes(data); buffer.Write(s, 0, s.Length); var p = new TaskCompletionSource <Error>(); connection.ReceiveError += (c, e) => p.TrySetResult(new Error { Connection = c, Exception = e }); await connection.ConnectAsync(); await connection.SendAsync(correlationId, buffer, true); var r = await p.Task; server.Stop(); connection.Dispose(); Assert.AreSame(connection, r.Connection); Assert.IsInstanceOf <CorrelationException>(r.Exception); var exception = r.Exception as CorrelationException; Assert.AreEqual(correlationId, exception.Expected); }
+ 4; // batchLength public void Serialize(ReusableMemoryStream target) { uint crc = 0; int batchLength = -1, lastOffsetDelta = -1; long firstTimestamp = -1L, maxTimestamp = -1L; BigEndianConverter.Write(target, BaseOffset); var batchLengthPosition = target.Position; BigEndianConverter.Write(target, batchLength); // placeholder for batchLength: int32 // The value of batchLength is the number of bytes required to read after the // batchLength field, we retain the current buffer position to compute it later. var afterBatchLengthPosition = target.Position; BigEndianConverter.Write(target, PartitionLeaderEpoch); // Current magic value is 2 target.WriteByte(2); var crcPosition = target.Position; BigEndianConverter.Write(target, (int)crc); // placeholder for CRC: int32 var afterCrcPosition = target.Position; short attributes = 0x0; if (CompressionCodec != CompressionCodec.None) { attributes |= (short)((short)CompressionCodec & CompressionCodecMask); } if (IsTransactional) { attributes |= TransactionalFlagMask; } if (IsControl) { attributes |= ControlFlagMask; } if (TimestampType == TimestampType.LogAppendTime) { attributes |= TimestampTypeMask; } BigEndianConverter.Write(target, attributes); var lastOffsetDeltaPosition = target.Position; BigEndianConverter.Write(target, lastOffsetDelta); // placeholder for LastOffsetDelta: int32 var firstTimestampPosition = target.Position; BigEndianConverter.Write(target, firstTimestamp); // placeholder for FirstTimestamp: int64 var maxTimestampPosition = target.Position; BigEndianConverter.Write(target, maxTimestamp); // placeholder for MaxTimestamp: int64 BigEndianConverter.Write(target, ProducerId); BigEndianConverter.Write(target, ProducerEpoch); BigEndianConverter.Write(target, BaseSequence); // BaseSequence: int32 var recordsCountPosition = target.Position; BigEndianConverter.Write(target, lastOffsetDelta); // RecordsCount: int32 (lastOffsetDelta, firstTimestamp, maxTimestamp) = CompressionCodec == CompressionCodec.None ? SerializeRecords(target) : SerializeRecordsWithCompression(target, CompressionCodec); var endPosition = target.Position; batchLength = (int)(target.Position - afterBatchLengthPosition); target.Position = batchLengthPosition; BigEndianConverter.Write(target, batchLength); target.Position = lastOffsetDeltaPosition; BigEndianConverter.Write(target, lastOffsetDelta); target.Position = firstTimestampPosition; BigEndianConverter.Write(target, firstTimestamp); target.Position = maxTimestampPosition; BigEndianConverter.Write(target, maxTimestamp); target.Position = recordsCountPosition; BigEndianConverter.Write(target, lastOffsetDelta + 1); // actual records count // Crc is computed on the object from right after the CRC to the end of the buffer crc = Crc32.ComputeCastagnoli(target, afterCrcPosition, endPosition - afterCrcPosition); target.Position = crcPosition; BigEndianConverter.Write(target, (int)crc); target.Position = endPosition; }