/// <summary> /// Send all headers to the client /// </summary> /// <param name="response">Response containing call headers.</param> /// <param name="writer">Writer to write everything to</param> public void SerializeResponse(IResponse response, IBufferWriter writer) { WriteString(writer, "{0} {1} {2}\r\n", response.ProtocolVersion, response.StatusCode, response.StatusDescription); var contentType = response.ContentType ?? "text/html"; if (response.ContentEncoding != null) contentType += ";charset=" + response.ContentEncoding.WebName; var length = response.ContentLength == 0 || response.Body != null ? response.ContentLength : response.Body.Length; // go through all property headers. WriteString(writer, "Content-Type: {0}\r\n", contentType); WriteString(writer, "Content-Length: {0}\r\n", length); //writer.WriteLine(response.KeepAlive ? "Connection: Keep-Alive" : "Connection: Close"); if (response.Cookies != null && response.Cookies.Count > 0) { SerializeCookies(response, writer); } foreach (var header in response.Headers) WriteString(writer, "{0}: {1}\r\n", header.Name, header.Value); // header/body delimiter WriteString(writer, "\r\n"); }
/// <summary>Recursive method to print out the full stack trace of inner exceptions.</summary> /// <remarks> /// The method uses reverse recursion. The most inner will start to write first. /// </remarks> /// <param name="e">The exception.</param> /// <param name="writer">The buffer writer to receive the stack trace.</param> private static void DumpStackTrace(Exception e, IBufferWriter writer) { if (e.InnerException != null) { DumpStackTrace(e.InnerException, writer); } writer.Write(e.StackTrace.ToString()); }
/// <summary> /// Serialize a message into something that can be transported over the socket. /// </summary> /// <param name="message">Message to serialize</param> /// <param name="writer">Buffer used to store the message</param> public void Serialize(object message, IBufferWriter writer) { var msg = (IResponse) message; var serializer = new HttpHeaderSerializer(); serializer.SerializeResponse(msg, writer); writer.Copy(msg.Body); }
private void SerializeCookies(IResponse response, IBufferWriter writer) { //Set-Cookie: <name>=<value>[; <name>=<value>][; expires=<date>][; domain=<domain_name>][; path=<some_path>][; secure][; httponly] foreach (var cookie in response.Cookies) { WriteString(writer, "Set-Cookie: {0}={1}", cookie.Name, cookie.Value ?? string.Empty); if (cookie.Expires > DateTime.MinValue) WriteString(writer, ";expires={0}", cookie.Expires.ToString("R")); if (!string.IsNullOrEmpty(cookie.Path)) WriteString(writer, ";path={0}", cookie.Path); WriteString(writer, "\r\n"); } }
/// <summary> /// Serialize a message into something that can be transported over the socket. /// </summary> /// <param name="message">Message to serialize</param> /// <param name="writer">Buffer used to store the message</param> public void Serialize(object message, IBufferWriter writer) { var serializer = new JsonSerializer(); serializer.TypeNameHandling = TypeNameHandling.All; var sb = new StringBuilder(); serializer.Serialize(new JsonTextWriter(new StringWriter(sb)), message); //var str = JsonConvert.SerializeObject(message); var bodyBytes = Encoding.UTF8.GetBytes(sb.ToString()); // version writer.Write(VersionBuffer, 0, VersionBuffer.Length); //length var buffer = BitConverter.GetBytes(bodyBytes.Length); writer.Write(buffer, 0, buffer.Length); //body writer.Write(bodyBytes, 0, bodyBytes.Length); }
/// <summary> /// Initializes a new instance of the <see cref="MessagePackWriter"/> struct. /// </summary> /// <param name="writer">The writer to use.</param> public MessagePackWriter(IBufferWriter <byte> writer) { this.writer = new BufferWriter(writer); this.OldSpec = false; }
/// <summary> /// Writes the <paramref name="response"/> to the <paramref name="output"/>. /// </summary> /// <param name="response">The negotiation response generated in response to a negotiation request.</param> /// <param name="output">Where the <paramref name="response"/> is written to as Json.</param> public static void WriteResponse(NegotiationResponse response, IBufferWriter <byte> output) { var reusableWriter = ReusableUtf8JsonWriter.Get(output); try { var writer = reusableWriter.GetJsonWriter(); writer.WriteStartObject(); // If we already have an error its due to a protocol version incompatibility. // We can just write the error and complete the JSON object and return. if (!string.IsNullOrEmpty(response.Error)) { writer.WriteString(ErrorPropertyNameBytes, response.Error); writer.WriteEndObject(); writer.Flush(); Debug.Assert(writer.CurrentDepth == 0); return; } writer.WriteNumber(NegotiateVersionPropertyNameBytes, response.Version); if (!string.IsNullOrEmpty(response.Url)) { writer.WriteString(UrlPropertyNameBytes, response.Url); } if (!string.IsNullOrEmpty(response.AccessToken)) { writer.WriteString(AccessTokenPropertyNameBytes, response.AccessToken); } if (!string.IsNullOrEmpty(response.ConnectionId)) { writer.WriteString(ConnectionIdPropertyNameBytes, response.ConnectionId); } if (response.Version > 0 && !string.IsNullOrEmpty(response.ConnectionToken)) { writer.WriteString(ConnectionTokenPropertyNameBytes, response.ConnectionToken); } writer.WriteStartArray(AvailableTransportsPropertyNameBytes); if (response.AvailableTransports != null) { var transportCount = response.AvailableTransports.Count; for (var i = 0; i < transportCount; ++i) { var availableTransport = response.AvailableTransports[i]; writer.WriteStartObject(); if (availableTransport.Transport != null) { writer.WriteString(TransportPropertyNameBytes, availableTransport.Transport); } else { // Might be able to remove this after https://github.com/dotnet/corefx/issues/34632 is resolved writer.WriteNull(TransportPropertyNameBytes); } writer.WriteStartArray(TransferFormatsPropertyNameBytes); if (availableTransport.TransferFormats != null) { var formatCount = availableTransport.TransferFormats.Count; for (var j = 0; j < formatCount; ++j) { writer.WriteStringValue(availableTransport.TransferFormats[j]); } } writer.WriteEndArray(); writer.WriteEndObject(); } } writer.WriteEndArray(); writer.WriteEndObject(); writer.Flush(); Debug.Assert(writer.CurrentDepth == 0); } finally { ReusableUtf8JsonWriter.Return(reusableWriter); } }
/// <summary> /// Initializes a new instance of the <see cref="BufferWriterStream"/> class. /// </summary> /// <param name="writer">The writer to write to.</param> internal BufferWriterStream(IBufferWriter <byte> writer) { this.writer = writer ?? throw new ArgumentNullException(nameof(writer)); }
/// <summary> /// Initializes a new instance of the <see cref="BufferTextWriter"/> class. /// </summary> /// <param name="bufferWriter">The buffer writer to write to.</param> /// <param name="encoding">The encoding to use.</param> public BufferTextWriter(IBufferWriter <byte> bufferWriter, Encoding encoding) { this.Initialize(bufferWriter, encoding); }
/// <inheritdoc /> public void WriteMessage(HubMessage message, IBufferWriter <byte> output) { WriteMessageCore(message, output); TextMessageFormatter.WriteRecordSeparator(output); }
public override void Serialize(PongMessage message, int protocolVersion, BitcoinPeerContext peerContext, IBufferWriter <byte> output) { output.WriteULong(message.Nonce); }
/// <summary> /// Writes the serialized representation of a <see cref="HandshakeResponseMessage"/> to the specified writer. /// </summary> /// <param name="responseMessage">The message to write.</param> /// <param name="output">The output writer.</param> public static void WriteResponseMessage(HandshakeResponseMessage responseMessage, IBufferWriter <byte> output) { var writer = new Utf8JsonWriter(output, new JsonWriterState(new JsonWriterOptions() { SkipValidation = true })); writer.WriteStartObject(); if (!string.IsNullOrEmpty(responseMessage.Error)) { writer.WriteString(ErrorPropertyNameBytes, responseMessage.Error); } writer.WriteNumber(MinorVersionPropertyNameBytes, responseMessage.MinorVersion, escape: false); writer.WriteEndObject(); writer.Flush(isFinalBlock: true); TextMessageFormatter.WriteRecordSeparator(output); }
/// <summary> /// ±àÂë /// </summary> /// <param name="writer">BufferдÈëÆ÷</param> /// <param name="pack">×Ö·û´®Êý¾Ý</param> /// <returns></returns> public int Encode(IBufferWriter <byte> writer, string pack) { return(writer.Write(pack, _encoding)); }
public abstract void Encrypt(IBufferWriter <byte> pipeWriter, ReadOnlySequence <byte> plainText, TlsRecordType recordType);
/// <summary> /// Writes the serialized representation of a <see cref="HandshakeRequestMessage"/> to the specified writer. /// </summary> /// <param name="requestMessage">The message to write.</param> /// <param name="output">The output writer.</param> public static void WriteRequestMessage(HandshakeRequestMessage requestMessage, IBufferWriter <byte> output) { var writer = new Utf8JsonWriter(output, new JsonWriterState(new JsonWriterOptions() { SkipValidation = true })); writer.WriteStartObject(); writer.WriteString(ProtocolPropertyNameBytes, requestMessage.Protocol, escape: false); writer.WriteNumber(ProtocolVersionPropertyNameBytes, requestMessage.Version, escape: false); writer.WriteEndObject(); writer.Flush(isFinalBlock: true); TextMessageFormatter.WriteRecordSeparator(output); }
public abstract void Decrypt(IBufferWriter <byte> pipeWriter, ReadOnlySequence <byte> cipherText, TlsRecordType recordType, TlsProtocolVersion tlsVersion);
public void Reset(IBufferWriter <byte> writer) { }
public Utf8JsonWriter(IBufferWriter <byte> writer, JsonWriterOptions options) { }
public override void WriteTo(IBufferWriter <byte> output) => output.Write(_requestBytes);
public BufferWriterStream(IBufferWriter <byte> writer) { writer.AssertNotNull(nameof(writer)); _writer = writer; }
/// <summary> /// Initializes a new instance of the <see cref="MessagePackWriter"/> struct, /// with the same settings as this one, but with its own buffer writer. /// </summary> /// <param name="writer">The writer to use for the new instance.</param> /// <returns>The new writer.</returns> public MessagePackWriter Clone(IBufferWriter <byte> writer) => new MessagePackWriter(writer) { OldSpec = this.OldSpec, };
public static void WriteRequestMessage(HandshakeRequestMessage requestMessage, IBufferWriter <byte> output) { var textWriter = Utf8BufferTextWriter.Get(output); try { using (var writer = JsonUtils.CreateJsonTextWriter(textWriter)) { writer.WriteStartObject(); writer.WritePropertyName(ProtocolPropertyName); writer.WriteValue(requestMessage.Protocol); writer.WritePropertyName(ProtocolVersionPropertyName); writer.WriteValue(requestMessage.Version); writer.WriteEndObject(); writer.Flush(); } } finally { Utf8BufferTextWriter.Return(textWriter); } TextMessageFormatter.WriteRecordSeparator(output); }
/// <seealso cref="Serialize{T}(IBufferWriter{byte}, T, MessagePackSerializerOptions, CancellationToken)"/> public static void Serialize(Type type, IBufferWriter <byte> writer, object obj, MessagePackSerializerOptions options = null, CancellationToken cancellationToken = default) { GetOrAdd(type).Serialize_IBufferWriter_T_Options_CancellationToken.Invoke(writer, obj, options, cancellationToken); }
public static void WriteResponseMessage(HandshakeResponseMessage responseMessage, IBufferWriter <byte> output) { var textWriter = Utf8BufferTextWriter.Get(output); try { using (var writer = JsonUtils.CreateJsonTextWriter(textWriter)) { writer.WriteStartObject(); if (!string.IsNullOrEmpty(responseMessage.Error)) { writer.WritePropertyName(ErrorPropertyName); writer.WriteValue(responseMessage.Error); } writer.WriteEndObject(); writer.Flush(); } } finally { Utf8BufferTextWriter.Return(textWriter); } TextMessageFormatter.WriteRecordSeparator(output); }
private void WriteMessageCore(HubMessage message, IBufferWriter <byte> stream) { var textWriter = Utf8BufferTextWriter.Get(stream); try { using (var writer = JsonUtils.CreateJsonTextWriter(textWriter)) { writer.WriteStartObject(); switch (message) { case InvocationMessage m: WriteMessageType(writer, HubProtocolConstants.InvocationMessageType); WriteHeaders(writer, m); WriteInvocationMessage(m, writer); break; case StreamInvocationMessage m: WriteMessageType(writer, HubProtocolConstants.StreamInvocationMessageType); WriteHeaders(writer, m); WriteStreamInvocationMessage(m, writer); break; case StreamItemMessage m: WriteMessageType(writer, HubProtocolConstants.StreamItemMessageType); WriteHeaders(writer, m); WriteStreamItemMessage(m, writer); break; case CompletionMessage m: WriteMessageType(writer, HubProtocolConstants.CompletionMessageType); WriteHeaders(writer, m); WriteCompletionMessage(m, writer); break; case CancelInvocationMessage m: WriteMessageType(writer, HubProtocolConstants.CancelInvocationMessageType); WriteHeaders(writer, m); WriteCancelInvocationMessage(m, writer); break; case PingMessage _: WriteMessageType(writer, HubProtocolConstants.PingMessageType); break; case CloseMessage m: WriteMessageType(writer, HubProtocolConstants.CloseMessageType); WriteCloseMessage(m, writer); break; default: throw new InvalidOperationException($"Unsupported message type: {message.GetType().FullName}"); } writer.WriteEndObject(); writer.Flush(); } } finally { Utf8BufferTextWriter.Return(textWriter); } }
public static void SerializeTypeless(Type targetType, IBufferWriter <byte> writer, object?value, JsonSerializerOptions options)
private void WriteAssignment(SyncGroupRequest.Assignment message, IBufferWriter <byte> output) { output.WriteString(message.MemberId); output.WriteBytes(message.AssignmentData); }
public void Serialize(IBufferWriter <byte> contentBuffer, JsonRpcMessage message) { throw new NotImplementedException(); }
/// <summary> /// Clears references to the <see cref="IBufferWriter{T}"/> set by a prior call to <see cref="Initialize(IBufferWriter{byte}, Encoding)"/>. /// </summary> public void Reset() { this.bufferWriter = null; }
protected override void SerializeMetric(IBufferWriter <byte> writer, in MetricReading reading)
internal BufferWriterCharAdapter(IBufferWriter <char> writer) { Writer = writer; Memory = default; NextIndex = 0; }
public void WriteMessage(HubMessage message, IBufferWriter <byte> output) { _innerProtocol.WriteMessage(message, output); }
private void WriteString(IBufferWriter writer, string text, params object[] formatters) { var str = string.Format(text, formatters); var buffer = _encoding.GetBytes(str); writer.Write(buffer, 0, buffer.Length); }
public static void Serialize(IBufferWriter <byte> writer, object obj, MessagePackSerializerOptions options = null, CancellationToken cancellationToken = default) => Serialize <object>(writer, obj, options ?? DefaultOptions, cancellationToken);
/// <inheritdoc /> public void WriteMessage(HubMessage message, IBufferWriter <byte> output) => _worker.WriteMessage(message, output);
public void WriteMessage(T message, IBufferWriter <byte> output) { var writer = new BsonWriter(output); _serializer.Write(ref writer, message); }