Skip to content

Commit eb8a232

Browse files
committed
Async migration: Converted lots of stuff to async/await.
Removed locking from Server.cs in favor of concurrent collections. (Probably unsafe! Needs testing and refactoring for data consistency.)
1 parent a404261 commit eb8a232

21 files changed

+911
-926
lines changed

Guncho.Core/Api/Controllers/ProfilesController.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public IHttpActionResult GetProfileByName(string name)
8686
}
8787

8888
[Route("{name}", Name = "PutProfileByName")]
89-
public IHttpActionResult PutProfileByName(string name, [FromBody] ProfileDto newProfile)
89+
public async Task<IHttpActionResult> PutProfileByName(string name, [FromBody] ProfileDto newProfile)
9090
{
9191
var player = playersService.GetPlayerByName(name);
9292

@@ -156,7 +156,7 @@ public IHttpActionResult PutProfileByName(string name, [FromBody] ProfileDto new
156156
return BadRequest(ModelState);
157157
}
158158

159-
var result = playersService.TransactionalUpdate(
159+
var result = await playersService.TransactionalUpdate(
160160
player,
161161
p =>
162162
{
@@ -191,7 +191,7 @@ public IHttpActionResult GetMy()
191191
}
192192

193193
[Route("my")]
194-
public IHttpActionResult PutMy(ProfileDto newProfile)
194+
public Task<IHttpActionResult> PutMy(ProfileDto newProfile)
195195
{
196196
return PutProfileByName(User.Identity.Name, newProfile);
197197
}

Guncho.Core/Api/Controllers/RealmsController.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.ComponentModel.DataAnnotations;
88
using System.Linq;
99
using System.Net.Http;
10+
using System.Threading.Tasks;
1011
using System.Web.Http;
1112
using System.Web.Http.Results;
1213

@@ -147,7 +148,7 @@ public Check(Func<T, bool> checkFunc, string modelKey, string errorMsg)
147148
}
148149

149150
[Route("{realmName}", Name = "PutRealmByName")]
150-
public IHttpActionResult PutRealmByName(string realmName, RealmDto newSettings)
151+
public async Task<IHttpActionResult> PutRealmByName(string realmName, RealmDto newSettings)
151152
{
152153
// TODO: use ETags for concurrency control
153154

@@ -249,7 +250,7 @@ public IHttpActionResult PutRealmByName(string realmName, RealmDto newSettings)
249250
return BadRequest(ModelState);
250251
}
251252

252-
var result = realmsService.TransactionalUpdate(
253+
var result = await realmsService.TransactionalUpdate(
253254
realm,
254255
r =>
255256
{
@@ -320,7 +321,7 @@ private bool CompilerEqualsFactory(CompilerOptionsDto dto, RealmFactory factory)
320321
}
321322

322323
[Route("", Name = "PostNewRealm")]
323-
public IHttpActionResult PostNewRealm(RealmDto newRealm)
324+
public async Task<IHttpActionResult> PostNewRealm(RealmDto newRealm)
324325
{
325326
// verify permission
326327
if (!Request.CheckAccess(GunchoResources.RealmActions.Create, GunchoResources.Realm, newRealm.Name))
@@ -359,7 +360,7 @@ public IHttpActionResult PostNewRealm(RealmDto newRealm)
359360
}
360361

361362
// create the realm
362-
var realm = realmsService.CreateRealm(playersService.GetPlayerByName(User.Identity.Name), newRealm.Name, factory);
363+
var realm = await realmsService.CreateRealm(playersService.GetPlayerByName(User.Identity.Name), newRealm.Name, factory);
363364

364365
if (realm == null)
365366
{
@@ -375,7 +376,7 @@ public IHttpActionResult PostNewRealm(RealmDto newRealm)
375376
}
376377

377378
// invoke the PUT handler to update any other settings
378-
var innerResult = PutRealmByName(newRealm.Name, newRealm) as OkNegotiatedContentResult<RealmDto>;
379+
var innerResult = (await PutRealmByName(newRealm.Name, newRealm)) as OkNegotiatedContentResult<RealmDto>;
379380

380381
if (innerResult != null)
381382
{

Guncho.Core/Api/Hubs/PlayHub.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Guncho.Connections;
22
using Microsoft.AspNet.SignalR;
3+
using Nito.AsyncEx;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -50,7 +51,7 @@ public Task SendCommand(string command)
5051
System.Diagnostics.Debug.WriteLine("SendCommand: id = {0}", new string[] { Context.ConnectionId });
5152
var connection = manager.GetConnectionById(Context.ConnectionId);
5253
connection.EnqueueCommand(command);
53-
return Task.FromResult(0);
54+
return TaskConstants.Completed;
5455
}
5556
}
5657
}

Guncho.Core/Api/Security/GunchoOAuthServerProvider.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text;
77
using System.Threading.Tasks;
88
using System.Security.Claims;
9+
using Nito.AsyncEx;
910

1011
namespace Guncho.Api.Security
1112
{
@@ -21,7 +22,7 @@ public GunchoOAuthServerProvider(UserManager<ApiUser, int> userManager)
2122
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
2223
{
2324
context.Validated();
24-
return Task.FromResult(0);
25+
return TaskConstants.Completed;
2526
}
2627

2728
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
@@ -54,7 +55,7 @@ public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwner
5455
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
5556
{
5657
context.AdditionalResponseParameters.Add("username", context.Identity.Name);
57-
return Task.FromResult(0);
58+
return TaskConstants.Completed;
5859
}
5960
}
6061
}

Guncho.Core/Api/Security/OldTimeyUserStore.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNet.Identity;
2+
using Nito.AsyncEx;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -65,13 +66,13 @@ public Task<ApiUser> FindByNameAsync(string userName)
6566
return Task.FromResult(ToApiUser(player));
6667
}
6768

68-
public Task UpdateAsync(ApiUser user)
69+
public async Task UpdateAsync(ApiUser user)
6970
{
7071
var player = server.GetPlayerById(user.Id);
7172

7273
if (player != null)
7374
{
74-
lock (player)
75+
using (await player.Lock.WriterLockAsync())
7576
{
7677
player.Name = user.UserName;
7778

@@ -80,10 +81,8 @@ public Task UpdateAsync(ApiUser user)
8081
player.PasswordHash = parts[1];
8182
}
8283

83-
server.SavePlayers();
84+
await server.SavePlayers();
8485
}
85-
86-
return Task.FromResult(0);
8786
}
8887

8988
#endregion
@@ -126,7 +125,7 @@ public Task SetPasswordHashAsync(ApiUser user, string passwordHash)
126125
}
127126
else
128127
{
129-
return Task.FromResult(0);
128+
return TaskConstants.Completed;
130129
}
131130
}
132131

0 commit comments

Comments
 (0)