Skip to content

Commit c468763

Browse files
committed
post rebase fixes
1 parent eb3bdbc commit c468763

24 files changed

Lines changed: 194 additions & 195 deletions

examples/Telnet.Server/TelnetServerHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Telnet.Server
66
using System;
77
using System.Net;
88
using System.Threading.Tasks;
9+
using DotNetty.Codecs;
910
using DotNetty.Common.Concurrency;
1011
using DotNetty.Transport.Channels;
1112

@@ -17,7 +18,7 @@ public override void ChannelActive(IChannelHandlerContext contex)
1718
contex.WriteAndFlushAsync(string.Format("It is {0} now !\r\n", DateTime.Now));
1819
}
1920

20-
protected override async void ChannelRead0(IChannelHandlerContext contex, string msg)
21+
protected override void ChannelRead0(IChannelHandlerContext context, string msg)
2122
{
2223
// Generate and write a response.
2324
string response;
@@ -36,11 +37,10 @@ protected override async void ChannelRead0(IChannelHandlerContext contex, string
3637
response = "Did you say '" + msg + "'?\r\n";
3738
}
3839

39-
ValueTask waitClose = contex.WriteAndFlushAsync(response);
40+
Task waitClose = context.WriteAndFlushAsync(response);
4041
if (close)
4142
{
42-
await waitClose;
43-
contex.CloseAsync();
43+
waitClose.CloseOnComplete(context);
4444
}
4545
}
4646

src/DotNetty.Codecs.Http/Cors/CorsHandler.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void SetExposeHeaders(IHttpResponse response)
167167

168168
void SetMaxAge(IHttpResponse response) => response.Headers.Set(HttpHeaderNames.AccessControlMaxAge, this.config.MaxAge);
169169

170-
public override Task WriteAsync(IChannelHandlerContext context, object message)
170+
public override ValueTask WriteAsync(IChannelHandlerContext context, object message)
171171
{
172172
if (this.config.IsCorsSupportEnabled && message is IHttpResponse response)
173173
{
@@ -177,7 +177,7 @@ public override Task WriteAsync(IChannelHandlerContext context, object message)
177177
this.SetExposeHeaders(response);
178178
}
179179
}
180-
return context.WriteAndFlushAsync(message);
180+
return context.WriteAndFlushAsync(message, true);
181181
}
182182

183183
static void Forbidden(IChannelHandlerContext ctx, IHttpRequest request)
@@ -197,15 +197,8 @@ static void Respond(IChannelHandlerContext ctx, IHttpRequest request, IHttpRespo
197197
Task task = ctx.WriteAndFlushAsync(response);
198198
if (!keepAlive)
199199
{
200-
task.ContinueWith(CloseOnComplete, ctx,
201-
TaskContinuationOptions.ExecuteSynchronously);
200+
task.CloseOnComplete(ctx);
202201
}
203202
}
204-
205-
static void CloseOnComplete(Task task, object state)
206-
{
207-
var ctx = (IChannelHandlerContext)state;
208-
ctx.CloseAsync();
209-
}
210203
}
211204
}

src/DotNetty.Codecs.Http/DotNetty.Codecs.Http.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<Compile Include="..\shared\SharedAssemblyInfo.cs" Link="Properties\SharedAssemblyInfo.cs" />
3030
</ItemGroup>
3131
<ItemGroup>
32-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.4.0" />
32+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.0-preview2-26406-04" />
3333
</ItemGroup>
3434
<ItemGroup>
3535
<ProjectReference Include="..\DotNetty.Common\DotNetty.Common.csproj" />

src/DotNetty.Codecs.Http/HttpClientUpgradeHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public HttpClientUpgradeHandler(ISourceCodec sourceCodec, IUpgradeCodec upgradeC
7272
this.upgradeCodec = upgradeCodec;
7373
}
7474

75-
public override Task WriteAsync(IChannelHandlerContext context, object message)
75+
public override ValueTask WriteAsync(IChannelHandlerContext context, object message)
7676
{
7777
if (!(message is IHttpRequest))
7878
{
@@ -81,14 +81,14 @@ public override Task WriteAsync(IChannelHandlerContext context, object message)
8181

8282
if (this.upgradeRequested)
8383
{
84-
return TaskEx.FromException(new InvalidOperationException("Attempting to write HTTP request with upgrade in progress"));
84+
return new ValueTask(TaskEx.FromException(new InvalidOperationException("Attempting to write HTTP request with upgrade in progress")));
8585
}
8686

8787
this.upgradeRequested = true;
8888
this.SetUpgradeRequestHeaders(context, (IHttpRequest)message);
8989

9090
// Continue writing the request.
91-
Task task = context.WriteAsync(message);
91+
ValueTask task = context.WriteAsync(message);
9292

9393
// Notify that the upgrade request was issued.
9494
context.FireUserEventTriggered(UpgradeEvent.UpgradeIssued);

src/DotNetty.Codecs.Http/HttpServerExpectContinueHandler.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,15 @@ public override void ChannelRead(IChannelHandlerContext context, object message)
3939
// the expectation failed so we refuse the request.
4040
IHttpResponse rejection = this.RejectResponse(req);
4141
ReferenceCountUtil.Release(message);
42-
context.WriteAndFlushAsync(rejection)
43-
.ContinueWith(CloseOnFailure, context, TaskContinuationOptions.ExecuteSynchronously);
42+
context.WriteAndFlushAsync(rejection).CloseOnFailure(context);
4443
return;
4544
}
4645

47-
context.WriteAndFlushAsync(accept)
48-
.ContinueWith(CloseOnFailure, context, TaskContinuationOptions.ExecuteSynchronously);
46+
context.WriteAndFlushAsync(accept).CloseOnFailure(context);
4947
req.Headers.Remove(HttpHeaderNames.Expect);
5048
}
5149
base.ChannelRead(context, message);
5250
}
5351
}
54-
55-
static Task CloseOnFailure(Task task, object state)
56-
{
57-
if (task.IsFaulted)
58-
{
59-
var context = (IChannelHandlerContext)state;
60-
return context.CloseAsync();
61-
}
62-
return TaskEx.Completed;
63-
}
6452
}
6553
}

src/DotNetty.Codecs.Http/HttpServerKeepAliveHandler.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override void ChannelRead(IChannelHandlerContext context, object message)
3131
base.ChannelRead(context, message);
3232
}
3333

34-
public override Task WriteAsync(IChannelHandlerContext context, object message)
34+
public override ValueTask WriteAsync(IChannelHandlerContext context, object message)
3535
{
3636
// modify message on way out to add headers if needed
3737
if (message is IHttpResponse response)
@@ -52,18 +52,11 @@ public override Task WriteAsync(IChannelHandlerContext context, object message)
5252
}
5353
if (message is ILastHttpContent && !this.ShouldKeepAlive())
5454
{
55-
return base.WriteAsync(context, message)
56-
.ContinueWith(CloseOnComplete, context, TaskContinuationOptions.ExecuteSynchronously);
55+
return new ValueTask(base.WriteAsync(context, message).CloseOnComplete(context));
5756
}
5857
return base.WriteAsync(context, message);
5958
}
6059

61-
static Task CloseOnComplete(Task task, object state)
62-
{
63-
var context = (IChannelHandlerContext)state;
64-
return context.CloseAsync();
65-
}
66-
6760
void TrackResponse(IHttpResponse response)
6861
{
6962
if (!IsInformational(response))

src/DotNetty.Codecs/Compression/JZlibEncoder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ Task FinishEncode(IChannelHandlerContext context)
240240
this.z.next_out = null;
241241
}
242242

243-
return context.WriteAndFlushAsync(footer)
244-
.ContinueWith(_ => context.CloseAsync());
243+
return context.WriteAndFlushAsync(footer).CloseOnComplete(context);
245244
}
246245

247246
public override void HandlerAdded(IChannelHandlerContext context) => this.ctx = context;

src/DotNetty.Codecs/MessageAggregator.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,10 @@ protected internal override void Decode(IChannelHandlerContext context, TMessage
130130
bool closeAfterWrite = this.CloseAfterContinueResponse(continueResponse);
131131
this.handlingOversizedMessage = this.IgnoreContentAfterContinueResponse(continueResponse);
132132

133-
Task task = context
134-
.WriteAndFlushAsync(continueResponse)
135-
.ContinueWith(ContinueResponseWriteAction, context, TaskContinuationOptions.ExecuteSynchronously);
133+
WriteContinueResponse(context, continueResponse, closeAfterWrite);
136134

137135
if (closeAfterWrite)
138136
{
139-
task.ContinueWith(CloseAfterWriteAction, context, TaskContinuationOptions.ExecuteSynchronously);
140137
return;
141138
}
142139

@@ -245,19 +242,21 @@ protected internal override void Decode(IChannelHandlerContext context, TMessage
245242
throw new MessageAggregationException("Unknown aggregation state.");
246243
}
247244
}
248-
249-
static void CloseAfterWriteAction(Task task, object state)
245+
246+
static async void WriteContinueResponse(IChannelHandlerContext ctx, object message, bool closeAfterWrite)
250247
{
251-
var ctx = (IChannelHandlerContext)state;
252-
ctx.Channel.CloseAsync();
253-
}
254-
255-
static void ContinueResponseWriteAction(Task task, object state)
256-
{
257-
if (task.IsFaulted)
248+
try
249+
{
250+
await ctx.WriteAndFlushAsync(message);
251+
}
252+
catch (Exception ex)
253+
{
254+
ctx.FireExceptionCaught(ex);
255+
}
256+
257+
if (closeAfterWrite)
258258
{
259-
var ctx = (IChannelHandlerContext)state;
260-
ctx.FireExceptionCaught(task.Exception);
259+
ctx.Channel.CloseAsync();
261260
}
262261
}
263262

src/DotNetty.Codecs/MessageToMessageCodec.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected MessageToMessageCodec()
5050
public sealed override void ChannelRead(IChannelHandlerContext context, object message) =>
5151
this.decoder.ChannelRead(context, message);
5252

53-
public sealed override Task WriteAsync(IChannelHandlerContext context, object message) =>
53+
public sealed override ValueTask WriteAsync(IChannelHandlerContext context, object message) =>
5454
this.encoder.WriteAsync(context, message);
5555

5656
public virtual bool AcceptInboundMessage(object msg) => msg is TInbound;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace DotNetty.Codecs
5+
{
6+
using System;
7+
using System.Threading.Tasks;
8+
using DotNetty.Common.Utilities;
9+
using DotNetty.Transport.Channels;
10+
11+
public static class TaskExtensions
12+
{
13+
public static async Task CloseOnComplete(this ValueTask task, IChannelHandlerContext ctx)
14+
{
15+
try
16+
{
17+
await task;
18+
}
19+
finally
20+
{
21+
await ctx.CloseAsync();
22+
}
23+
}
24+
25+
static readonly Func<Task, object, Task> CloseOnCompleteContinuation = Close;
26+
static readonly Func<Task, object, Task> CloseOnFailureContinuation = CloseOnFailure;
27+
28+
public static Task CloseOnComplete(this Task task, IChannelHandlerContext ctx)
29+
=> task.ContinueWith(CloseOnCompleteContinuation, ctx, TaskContinuationOptions.ExecuteSynchronously);
30+
31+
public static Task CloseOnComplete(this Task task, IChannel channel)
32+
=> task.ContinueWith(CloseOnCompleteContinuation, channel, TaskContinuationOptions.ExecuteSynchronously);
33+
34+
public static Task CloseOnFailure(this Task task, IChannelHandlerContext ctx)
35+
=> task.ContinueWith(CloseOnFailureContinuation, ctx, TaskContinuationOptions.ExecuteSynchronously);
36+
37+
static Task Close(Task task, object state)
38+
{
39+
switch (state)
40+
{
41+
case IChannelHandlerContext ctx:
42+
return ctx.CloseAsync();
43+
case IChannel ch:
44+
return ch.CloseAsync();
45+
default:
46+
throw new InvalidOperationException("must never get here");
47+
}
48+
}
49+
50+
static Task CloseOnFailure(Task task, object state)
51+
{
52+
if (task.IsFaulted)
53+
{
54+
return Close(task, state);
55+
}
56+
return TaskEx.Completed;
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)