예제 #1
0
        public static async Task WriteStringAsync(this HttpListenerContext context, string value, Encoding encoding = null)
        {
            var buffer = value == null ? new byte[0] : (encoding ?? Encoding.UTF8).GetBytes(value);

            try
            {
                await
                context.Response.WriteDataAsync(buffer, 0, buffer.Length)
                .WithTimeout(HttpServerSettings.ReadWriteTimeout)                                 //NOTE: HttpResponseStream is not cancellable with CancellationToken :(
                .ConfigureAwait(false);
            }
            catch (Exception e)
            {
                context.AbortConnection();
                throw new HttpConnectionClosed(e);
            }
        }
예제 #2
0
 public static async Task WriteStringAsync(this HttpListenerContext context, string value, Encoding encoding = null)
 {
     using (var buffer = await ResponseBuffersPool.AcquireAsync())
     {
         /* NOTE: In theory here we need to check that there is enough space in buffer to store encoded string. Something like that: encoding.GetMaxByteCount(value.Length) <= buffer.Length
          * If not - use Encoder.Convert to encode string by chunks. Omit this stuff for simplicity :) */
         var length = (encoding ?? Encoding.UTF8).GetBytes(value, 0, value.Length, buffer.Item, 0);
         try
         {
             await
             context.Response.WriteDataAsync(buffer.Item, 0, length)
             .WithTimeout(HttpServerSettings.ReadWriteTimeout)                                     //NOTE: HttpResponseStream is not cancellable with CancellationToken :(
             .ConfigureAwait(false);
         }
         catch (Exception e)
         {
             context.AbortConnection();
             throw new HttpConnectionClosed(e);
         }
     }
 }