/// <summary> /// Make a complete payload from <see cref="FbnsConnectionData"/> using Thrift. /// </summary> /// <returns>Payload</returns> public static async Task <IByteBuffer> BuildPayload(FbnsConnectionData data) { _memoryBufferTransport = new TMemoryBufferTransport(); _thrift = new TCompactProtocol(_memoryBufferTransport); _payloadData = data; var rawPayload = await ToThrift(); // zlib deflate var dataStream = new MemoryStream(512); using (var zlibStream = new ZlibStream(dataStream, CompressionMode.Compress, CompressionLevel.Level9, true)) { await zlibStream.WriteAsync(rawPayload, 0, rawPayload.Length); } var readData = new byte[dataStream.Length]; dataStream.Position = 0; await dataStream.ReadAsync(readData, 0, readData.Length); dataStream.Dispose(); var compressed = Unpooled.CopiedBuffer(readData); return(compressed); }
async Task IrisSub() { var dic = new Dictionary <string, object> { { "seq_id", SeqId.ToString() }, { "sub", new List <string>() }, { "snapshot_at_ms", SnapshotAt.ToUnixTimeMiliSeconds() } }; var json = JsonConvert.SerializeObject(dic); var bytes = Encoding.UTF8.GetBytes(json); var dataStream = new MemoryStream(512); using (var zlibStream = new ZlibStream(dataStream, CompressionMode.Compress, CompressionLevel.Level9, true)) { await zlibStream.WriteAsync(bytes, 0, bytes.Length); } var compressed = dataStream.GetWindowsRuntimeBuffer(0, (int)dataStream.Length); var publishPacket = new PublishPacket(QualityOfService.AtLeastOnce, false, false) { Payload = compressed, PacketId = (ushort)CryptographicBuffer.GenerateRandomNumber(), TopicName = "/ig_sub_iris" }; await FbnsPacketEncoder.EncodePacket(publishPacket, _outboundWriter); }
async Task PubSub() { var user = _instaApi.GetLoggedUser().LoggedInUser; var dic = new Dictionary <string, List <string> > { { "sub", new List <string> { SkyWalker.DirectSubscribe(user?.Pk.ToString()), SkyWalker.LiveSubscribe(user?.Pk.ToString()), } } }; var json = JsonConvert.SerializeObject(dic); var bytes = Encoding.UTF8.GetBytes(json); var dataStream = new MemoryStream(512); using (var zlibStream = new ZlibStream(dataStream, CompressionMode.Compress, CompressionLevel.Level9, true)) { await zlibStream.WriteAsync(bytes, 0, bytes.Length); } var compressed = dataStream.GetWindowsRuntimeBuffer(0, (int)dataStream.Length); var publishPacket = new PublishPacket(QualityOfService.AtLeastOnce, false, false) { Payload = compressed, PacketId = (ushort)CryptographicBuffer.GenerateRandomNumber(), TopicName = "/pubsub" }; await FbnsPacketEncoder.EncodePacket(publishPacket, _outboundWriter); }
async Task RealtimeSub() { var user = _instaApi.GetLoggedUser().LoggedInUser; var dic = new Dictionary <string, List <string> > { { "sub", new List <string> { GraphQLSubscriptions.GetAppPresenceSubscription(), GraphQLSubscriptions.GetZeroProvisionSubscription(_instaApi.GetCurrentDevice().DeviceGuid.ToString()), GraphQLSubscriptions.GetDirectStatusSubscription(), GraphQLSubscriptions.GetDirectTypingSubscription(user?.Pk.ToString()), GraphQLSubscriptions.GetAsyncAdSubscription(user?.Pk.ToString()) } } }; var json = JsonConvert.SerializeObject(dic); var bytes = Encoding.UTF8.GetBytes(json); var dataStream = new MemoryStream(512); using (var zlibStream = new ZlibStream(dataStream, CompressionMode.Compress, CompressionLevel.Level9, true)) { await zlibStream.WriteAsync(bytes, 0, bytes.Length); } var compressed = dataStream.GetWindowsRuntimeBuffer(0, (int)dataStream.Length); var publishPacket = new PublishPacket(QualityOfService.AtLeastOnce, false, false) { Payload = compressed, PacketId = (ushort)CryptographicBuffer.GenerateRandomNumber(), TopicName = "/ig_realtime_sub" }; await FbnsPacketEncoder.EncodePacket(publishPacket, _outboundWriter); }
public static async Task CompressAsync(byte[] byteIn, Stream outStream) { using var stream = new ZlibStream(new MemoryStream(), CompressionMode.Compress, CompressionLevel.BestCompression); await stream.WriteAsync(byteIn, 0, byteIn.Length); await stream.CopyToAsync(outStream); }
public async Task <byte[]> Compress(byte[] responseBody) { using (var ms = new MemoryStream()) { using (var zip = new ZlibStream(ms, CompressionMode.Compress, true)) { await zip.WriteAsync(responseBody, 0, responseBody.Length).ConfigureAwait(false); } return(ms.ToArray()); } }
public override async Task Send(IEnumerable <RpcRequest> requests) { CheckDisposed(); object formatted = FormatRequestMessages(requests); string encoded = Rencode.Encode(formatted); byte[] encodedBytes = encoded.Select(Convert.ToByte).ToArray(); using (var zlib = new ZlibStream(Stream, CompressionMode.Compress, true)) { await zlib.WriteAsync(encodedBytes, 0, encodedBytes.Length); } }
public static async Task <IBuffer> BuildPayload(IInstaApi instaApi) { InstaApi = instaApi; _memoryBufferTransport = new TMemoryBufferTransport(new TConfiguration()); _thrift = new TCompactProtocol(_memoryBufferTransport); _payloadData = new FbnsConnectionData(); // since we don't need payload data, just ignore it var rawPayload = await ToThrift(); var dataStream = new MemoryStream(512); using (var zlibStream = new ZlibStream(dataStream, CompressionMode.Compress, CompressionLevel.Level9, true)) await zlibStream.WriteAsync(rawPayload, 0, rawPayload.Length); var compressed = dataStream.GetWindowsRuntimeBuffer(0, (int)dataStream.Length); return(compressed); }
/// <summary> /// Make a complete payload from <see cref="FbnsConnectionData"/> using Thrift. /// </summary> /// <returns>Payload</returns> public static async Task <IBuffer> BuildPayload(FbnsConnectionData data) { _memoryBufferTransport = new TMemoryBufferTransport(); _thrift = new TCompactProtocol(_memoryBufferTransport); _payloadData = data; var rawPayload = await ToThrift(); // zlib deflate var dataStream = new MemoryStream(512); using (var zlibStream = new ZlibStream(dataStream, CompressionMode.Compress, CompressionLevel.Level9, true)) { await zlibStream.WriteAsync(rawPayload, 0, rawPayload.Length); } var compressed = dataStream.GetWindowsRuntimeBuffer(0, (int)dataStream.Length); return(compressed); }
public static async Task <(MemoryStream output, IMS2SizeHeader size)> GetEncryptionStreamAsync(Stream input, long inputSize, IMultiArray key, IMultiArray iv, bool zlibCompress) { if (zlibCompress) { using var ms = new MemoryStream(); using (var z = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression, true)) { byte[] inputBytes = new byte[inputSize]; int read = await input.ReadAsync(inputBytes, 0, (int)inputSize).ConfigureAwait(false); if (read != inputSize) { throw new EndOfStreamException(); } await z.WriteAsync(inputBytes, 0, (int)inputSize).ConfigureAwait(false); } ms.Position = 0; return(await InternalGetEncryptionStreamAsync(ms, ms.Length, key, iv, inputSize).ConfigureAwait(false)); } return(await InternalGetEncryptionStreamAsync(input, inputSize, key, iv, inputSize).ConfigureAwait(false)); }