Skip to content

Commit 810aa8c

Browse files
committed
Allow awaiting LogOn
1 parent 04cfc9f commit 810aa8c

5 files changed

Lines changed: 53 additions & 31 deletions

File tree

SteamKit2/SteamKit2/Steam/Handlers/SteamGameServer/SteamGameServer.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,24 @@ public sealed class StatusDetails
8888
/// <param name="details">The details to use for logging on.</param>
8989
/// <exception cref="System.ArgumentNullException">No logon details were provided.</exception>
9090
/// <exception cref="System.ArgumentException">Username or password are not set within <paramref name="details"/>.</exception>
91-
public void LogOn( LogOnDetails details )
91+
/// <returns>The Job ID of the request. This can be used to find the appropriate <see cref="SteamUser.LoggedOnCallback"/>.</returns>
92+
public AsyncJob<SteamUser.LoggedOnCallback> LogOn( LogOnDetails details )
9293
{
9394
ArgumentNullException.ThrowIfNull( details );
9495

9596
if ( string.IsNullOrEmpty( details.Token ) )
9697
{
9798
throw new ArgumentException( "LogOn requires a game server token to be set in 'details'." );
9899
}
99-
100+
101+
var logon = new ClientMsgProtobuf<CMsgClientLogon>( EMsg.ClientLogonGameServer );
102+
100103
if ( !this.Client.IsConnected )
101104
{
102-
this.Client.PostCallback( new SteamUser.LoggedOnCallback( EResult.NoConnection ) );
103-
return;
105+
this.Client.PostCallback( new SteamUser.LoggedOnCallback( EResult.NoConnection, logon.SourceJobID ) );
106+
return new AsyncJob<SteamUser.LoggedOnCallback>( this.Client, logon.SourceJobID );
104107
}
105108

106-
var logon = new ClientMsgProtobuf<CMsgClientLogon>( EMsg.ClientLogonGameServer );
107-
108109
SteamID gsId = new SteamID( 0, 0, Client.Universe, EAccountType.GameServer );
109110

110111
logon.ProtoHeader.client_sessionid = 0;
@@ -121,6 +122,8 @@ public void LogOn( LogOnDetails details )
121122
logon.Body.game_server_token = details.Token;
122123

123124
this.Client.Send( logon );
125+
126+
return new AsyncJob<SteamUser.LoggedOnCallback>( this.Client, logon.SourceJobID );
124127
}
125128

126129
/// <summary>
@@ -129,16 +132,17 @@ public void LogOn( LogOnDetails details )
129132
/// Results are returned in a <see cref="SteamUser.LoggedOnCallback"/>.
130133
/// </summary>
131134
/// <param name="appId">The AppID served by this game server, or 0 for the default.</param>
132-
public void LogOnAnonymous( uint appId = 0 )
135+
/// <returns>The Job ID of the request. This can be used to find the appropriate <see cref="SteamUser.LoggedOnCallback"/>.</returns>
136+
public AsyncJob<SteamUser.LoggedOnCallback> LogOnAnonymous( uint appId = 0 )
133137
{
138+
var logon = new ClientMsgProtobuf<CMsgClientLogon>( EMsg.ClientLogon );
139+
134140
if ( !this.Client.IsConnected )
135141
{
136-
this.Client.PostCallback( new SteamUser.LoggedOnCallback( EResult.NoConnection ) );
137-
return;
142+
this.Client.PostCallback( new SteamUser.LoggedOnCallback( EResult.NoConnection, logon.SourceJobID ) );
143+
return new AsyncJob<SteamUser.LoggedOnCallback>( this.Client, logon.SourceJobID );
138144
}
139145

140-
var logon = new ClientMsgProtobuf<CMsgClientLogon>( EMsg.ClientLogon );
141-
142146
SteamID gsId = new SteamID( 0, 0, Client.Universe, EAccountType.AnonGameServer );
143147

144148
logon.ProtoHeader.client_sessionid = 0;
@@ -153,6 +157,8 @@ public void LogOnAnonymous( uint appId = 0 )
153157
logon.Body.machine_id = HardwareUtils.GetMachineID( Client.Configuration.MachineInfoProvider );
154158

155159
this.Client.Send( logon );
160+
161+
return new AsyncJob<SteamUser.LoggedOnCallback>( this.Client, logon.SourceJobID );
156162
}
157163

158164
/// <summary>

SteamKit2/SteamKit2/Steam/Handlers/SteamUser/Callbacks.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ internal LoggedOnCallback( IPacketMsg packetMsg )
125125
var logonResp = new ClientMsgProtobuf<CMsgClientLogonResponse>( packetMsg );
126126
var resp = logonResp.Body;
127127

128+
this.JobID = logonResp.TargetJobID;
128129
this.Result = ( EResult )resp.eresult;
129130
this.ExtendedResult = ( EResult )resp.eresult_extended;
130131

@@ -167,6 +168,7 @@ private void HandleNonProtoLogon( IPacketMsg packetMsg )
167168
var logonResp = new ClientMsg<MsgClientLogOnResponse>( packetMsg );
168169
var resp = logonResp.Body;
169170

171+
this.JobID = logonResp.TargetJobID;
170172
this.Result = resp.Result;
171173

172174
this.OutOfGameSecsPerHeartbeat = resp.OutOfGameHeartbeatRateSec;
@@ -180,8 +182,9 @@ private void HandleNonProtoLogon( IPacketMsg packetMsg )
180182
}
181183

182184

183-
internal LoggedOnCallback( EResult result )
185+
internal LoggedOnCallback( EResult result, JobID jobId )
184186
{
187+
this.JobID = jobId;
185188
this.Result = result;
186189
}
187190
}
@@ -203,11 +206,13 @@ internal LoggedOffCallback( IPacketMsg packetMsg )
203206
if ( packetMsg.IsProto )
204207
{
205208
var loggedOff = new ClientMsgProtobuf<CMsgClientLoggedOff>( packetMsg );
209+
this.JobID = loggedOff.TargetJobID;
206210
this.Result = ( EResult )loggedOff.Body.eresult;
207211
}
208212
else
209213
{
210214
var loggedOff = new ClientMsg<MsgClientLoggedOff>( packetMsg );
215+
this.JobID = loggedOff.TargetJobID;
211216
this.Result = loggedOff.Body.Result;
212217
}
213218
}

SteamKit2/SteamKit2/Steam/Handlers/SteamUser/SteamUser.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ public SteamID? SteamID
229229
/// <param name="details">The details to use for logging on.</param>
230230
/// <exception cref="ArgumentNullException">No logon details were provided.</exception>
231231
/// <exception cref="ArgumentException">Username or password are not set within <paramref name="details"/>.</exception>
232-
public void LogOn( LogOnDetails details )
232+
/// <returns>The Job ID of the request. This can be used to find the appropriate <see cref="LoggedOnCallback"/>.</returns>
233+
public AsyncJob<LoggedOnCallback> LogOn( LogOnDetails details )
233234
{
234235
ArgumentNullException.ThrowIfNull( details );
235236

@@ -238,14 +239,14 @@ public void LogOn( LogOnDetails details )
238239
throw new ArgumentException( "LogOn requires a username and password or access token to be set in 'details'." );
239240
}
240241

242+
var logon = new ClientMsgProtobuf<CMsgClientLogon>( EMsg.ClientLogon );
243+
241244
if ( !this.Client.IsConnected )
242245
{
243-
this.Client.PostCallback( new LoggedOnCallback( EResult.NoConnection ) );
244-
return;
246+
this.Client.PostCallback( new LoggedOnCallback( EResult.NoConnection, logon.SourceJobID ) );
247+
return new AsyncJob<LoggedOnCallback>( this.Client, logon.SourceJobID );
245248
}
246249

247-
var logon = new ClientMsgProtobuf<CMsgClientLogon>( EMsg.ClientLogon );
248-
249250
SteamID steamId = new SteamID( details.AccountID, details.AccountInstance, Client.Universe, EAccountType.Individual );
250251

251252
if ( details.LoginID.HasValue )
@@ -316,35 +317,39 @@ public void LogOn( LogOnDetails details )
316317
logon.Body.access_token = details.AccessToken;
317318

318319
this.Client.Send( logon );
320+
321+
return new AsyncJob<LoggedOnCallback>( this.Client, logon.SourceJobID );
319322
}
320323

321324
/// <summary>
322325
/// Logs the client into the Steam3 network as an anonymous user.
323326
/// The client should already have been connected at this point.
324327
/// Results are returned in a <see cref="LoggedOnCallback"/>.
325328
/// </summary>
326-
public void LogOnAnonymous()
329+
/// <returns>The Job ID of the request. This can be used to find the appropriate <see cref="LoggedOnCallback"/>.</returns>
330+
public AsyncJob<LoggedOnCallback> LogOnAnonymous()
327331
{
328-
LogOnAnonymous( new AnonymousLogOnDetails() );
332+
return LogOnAnonymous( new AnonymousLogOnDetails() );
329333
}
330334
/// <summary>
331335
/// Logs the client into the Steam3 network as an anonymous user.
332336
/// The client should already have been connected at this point.
333337
/// Results are returned in a <see cref="LoggedOnCallback"/>.
334338
/// </summary>
335339
/// <param name="details">The details to use for logging on.</param>
336-
public void LogOnAnonymous( AnonymousLogOnDetails details )
340+
/// <returns>The Job ID of the request. This can be used to find the appropriate <see cref="LoggedOnCallback"/>.</returns>
341+
public AsyncJob<LoggedOnCallback> LogOnAnonymous( AnonymousLogOnDetails details )
337342
{
338343
ArgumentNullException.ThrowIfNull( details );
339344

345+
var logon = new ClientMsgProtobuf<CMsgClientLogon>( EMsg.ClientLogon );
346+
340347
if ( !this.Client.IsConnected )
341348
{
342-
this.Client.PostCallback( new LoggedOnCallback( EResult.NoConnection ) );
343-
return;
349+
this.Client.PostCallback( new LoggedOnCallback( EResult.NoConnection, logon.SourceJobID ) );
350+
return new AsyncJob<LoggedOnCallback>( this.Client, logon.SourceJobID );
344351
}
345352

346-
var logon = new ClientMsgProtobuf<CMsgClientLogon>( EMsg.ClientLogon );
347-
348353
SteamID auId = new SteamID( 0, 0, Client.Universe, EAccountType.AnonUser );
349354

350355
logon.ProtoHeader.client_sessionid = 0;
@@ -358,6 +363,8 @@ public void LogOnAnonymous( AnonymousLogOnDetails details )
358363
logon.Body.machine_id = HardwareUtils.GetMachineID( Client.Configuration.MachineInfoProvider );
359364

360365
this.Client.Send( logon );
366+
367+
return new AsyncJob<LoggedOnCallback>( this.Client, logon.SourceJobID );
361368
}
362369

363370
/// <summary>

SteamKit2/Tests/SteamGameServerFacts.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using SteamKit2;
1+
using SteamKit2;
22
using Xunit;
33

44
namespace Tests
@@ -8,7 +8,7 @@ public class SteamGameServerFacts : HandlerTestBase<SteamGameServer>
88
[Fact]
99
public void LogOnPostsLoggedOnCallbackWhenNoConnection()
1010
{
11-
Handler.LogOn(new SteamGameServer.LogOnDetails
11+
var asyncJob = Handler.LogOn(new SteamGameServer.LogOnDetails
1212
{
1313
Token = "SuperSecretToken"
1414
});
@@ -19,19 +19,21 @@ public void LogOnPostsLoggedOnCallbackWhenNoConnection()
1919

2020
var loc = (SteamUser.LoggedOnCallback)callback;
2121
Assert.Equal( EResult.NoConnection, loc.Result );
22+
Assert.Equal( asyncJob.JobID, loc.JobID );
2223
}
2324

2425
[Fact]
2526
public void LogOnAnonymousPostsLoggedOnCallbackWhenNoConnection()
2627
{
27-
Handler.LogOnAnonymous();
28+
var asyncJob = Handler.LogOnAnonymous();
2829

2930
var callback = SteamClient.GetCallback( );
3031
Assert.NotNull( callback );
3132
Assert.IsType<SteamUser.LoggedOnCallback>( callback );
3233

3334
var loc = (SteamUser.LoggedOnCallback)callback;
3435
Assert.Equal( EResult.NoConnection, loc.Result );
36+
Assert.Equal( asyncJob.JobID, loc.JobID );
3537
}
3638
}
3739
}

SteamKit2/Tests/SteamUserFacts.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using SteamKit2;
33
using Xunit;
44

@@ -9,7 +9,7 @@ public class SteamUserFacts : HandlerTestBase<SteamUser>
99
[Fact]
1010
public void LogOnPostsLoggedOnCallbackWhenNoConnection()
1111
{
12-
Handler.LogOn(new SteamUser.LogOnDetails
12+
var asyncJob = Handler.LogOn(new SteamUser.LogOnDetails
1313
{
1414
Username = "iamauser",
1515
Password = "lamepassword"
@@ -21,6 +21,7 @@ public void LogOnPostsLoggedOnCallbackWhenNoConnection()
2121

2222
var loc = (SteamUser.LoggedOnCallback)callback;
2323
Assert.Equal( EResult.NoConnection, loc.Result );
24+
Assert.Equal( asyncJob.JobID, loc.JobID );
2425
}
2526

2627
[Fact]
@@ -82,7 +83,7 @@ public void LogOnDoesNotThrowExceptionIfUserNameAndPasswordProvided()
8283

8384
Assert.Null( ex );
8485
}
85-
86+
8687
[Fact]
8788
public void LogOnDoesNotThrowExceptionIfUserNameAndAccessTokenProvided()
8889
{
@@ -102,14 +103,15 @@ public void LogOnDoesNotThrowExceptionIfUserNameAndAccessTokenProvided()
102103
[Fact]
103104
public void LogOnAnonymousPostsLoggedOnCallbackWhenNoConnection()
104105
{
105-
Handler.LogOnAnonymous();
106+
var asyncJob = Handler.LogOnAnonymous();
106107

107108
var callback = SteamClient.GetCallback( );
108109
Assert.NotNull( callback );
109110
Assert.IsType<SteamUser.LoggedOnCallback>( callback );
110111

111112
var loc = (SteamUser.LoggedOnCallback)callback;
112113
Assert.Equal( EResult.NoConnection, loc.Result );
114+
Assert.Equal( asyncJob.JobID, loc.JobID );
113115
}
114116
}
115117
}

0 commit comments

Comments
 (0)