private static void WriteQuery(JsonWriter writer, Spec.Query query) { writer.BeginArray(); writer.WriteNumber((int)query.type); if (query.type == Spec.Query.QueryType.START) { WriteTerm(writer, query.query); writer.BeginObject(); foreach (var opt in query.global_optargs) { writer.WriteMember(opt.key); WriteTerm(writer, opt.val); } writer.EndObject(); } writer.EndArray(); }
public override async Task WriteQueryToStream(Stream stream, ILogger logger, Spec.Query query, CancellationToken cancellationToken) { using (var memoryBuffer = new MemoryStream(1024)) { Serializer.Serialize(memoryBuffer, query); var data = memoryBuffer.ToArray(); var lengthHeader = BitConverter.GetBytes(data.Length); if (!BitConverter.IsLittleEndian) { Array.Reverse(lengthHeader, 0, lengthHeader.Length); } logger.Debug("Writing packet, {0} bytes", data.Length); await stream.WriteAsync(lengthHeader, 0, lengthHeader.Length, cancellationToken); await stream.WriteAsync(data, 0, data.Length, cancellationToken); } }
public override async Task WriteQueryToStream(Stream stream, ILogger logger, Spec.Query query, CancellationToken cancellationToken) { using (var memoryBuffer = new MemoryStream(1024)) using (var streamWriter = new StreamWriter(memoryBuffer, utf8Encoding)) { WriteQuery(new JsonWriter(streamWriter), query); streamWriter.Flush(); var data = memoryBuffer.ToArray(); memoryBuffer.Seek(0, SeekOrigin.Begin); if (logger.InformationEnabled()) { string dataStr = Encoding.UTF8.GetString(data); logger.Information("JSON query: {0}", dataStr); } var tokenHeader = BitConverter.GetBytes(query.token); if (!BitConverter.IsLittleEndian) { Array.Reverse(tokenHeader, 0, tokenHeader.Length); } memoryBuffer.Write(tokenHeader, 0, tokenHeader.Length); var lengthHeader = BitConverter.GetBytes(data.Length); if (!BitConverter.IsLittleEndian) { Array.Reverse(lengthHeader, 0, lengthHeader.Length); } memoryBuffer.Write(lengthHeader, 0, lengthHeader.Length); memoryBuffer.Write(data, 0, data.Length); logger.Debug("Writing packet, {0} bytes", data.Length); data = memoryBuffer.ToArray(); await stream.WriteAsync(data, 0, data.Length, cancellationToken); } }
public abstract Task WriteQueryToStream(Stream stream, ILogger logger, Spec.Query query, CancellationToken cancellationToken);