-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathConnectionLoginHandler.cs
More file actions
119 lines (98 loc) · 4.35 KB
/
Copy pathConnectionLoginHandler.cs
File metadata and controls
119 lines (98 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using Arrowgene.Ddon.Database.Model;
using Arrowgene.Ddon.Server;
using Arrowgene.Ddon.Shared.Entity.PacketStructure;
using Arrowgene.Ddon.Shared.Model;
using Arrowgene.Logging;
namespace Arrowgene.Ddon.GameServer.Handler
{
public class ConnectionLoginHandler : GameRequestPacketHandler<C2SConnectionLoginReq, S2CConnectionLoginRes>
{
private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(ConnectionLoginHandler));
public ConnectionLoginHandler(DdonGameServer server) : base(server)
{
}
public override S2CConnectionLoginRes Handle(GameClient client, C2SConnectionLoginReq request)
{
client.SetChallengeCompleted(true);
Logger.Debug(client,
$"Received SessionKey:{request.SessionKey} for platform:{request.PlatformType}");
S2CConnectionLoginRes res = new S2CConnectionLoginRes();
GameToken token = Database.SelectToken(request.SessionKey);
if (token == null)
{
Logger.Error(client, $"SessionKey:{request.SessionKey} not found");
throw new ResponseErrorException(ErrorCode.ERROR_CODE_COG_ANALIZE_SESSION_KEY);
}
if (!Database.DeleteTokenByAccountId(token.AccountId))
{
Logger.Error(client, $"Failed to delete session key from DB:{request.SessionKey}");
}
Account account = Database.SelectAccountById(token.AccountId);
if (account == null)
{
Logger.Error(client, $"AccountId:{token.AccountId} not found");
throw new ResponseErrorException(ErrorCode.ERROR_CODE_FAIL);
}
else if (account.State == AccountStateType.Banned)
{
throw new ResponseErrorException(ErrorCode.ERROR_CODE_GM_BAN);
}
DateTime now = DateTime.UtcNow;
List<Connection> connections = Database.SelectConnectionsByAccountId(account.Id);
if (connections.Count > 0)
{
foreach (Connection con in connections)
{
if (con.Type == ConnectionType.GameServer)
{
Logger.Error(client, $"game server connection already exists");
throw new ResponseErrorException(ErrorCode.ERROR_CODE_AUTH_DUPLICATE_DATA);
}
}
}
// Order Important,
// account need to be only assigned after
// verification that no connection exists, and before
// registering the connection
client.Account = account;
Connection connection = new Connection
{
ServerId = Server.Id,
AccountId = account.Id,
Type = ConnectionType.GameServer,
Created = now,
CharacterId = token.CharacterId
};
#if DEBUG
if (client.Identity.Contains("127.0.0.1"))
{
client.Account.State = AccountStateType.Admin;
}
#endif
if (!Database.InsertConnection(connection))
{
Logger.Error(client, $"Failed to register game connection");
throw new ResponseErrorException(ErrorCode.ERROR_CODE_NET_NOT_CONNECT_GAME_SERVER);
}
Character character = Server.CharacterManager.SelectCharacter(client, token.CharacterId);
if (character == null)
{
Logger.Error(client, $"CharacterId:{token.CharacterId} not found");
throw new ResponseErrorException(ErrorCode.ERROR_CODE_CHARACTER_DATA_INVALID_CHARACTER_ID);
}
Logger.Info(client, "Logged Into GameServer");
client.Account.LoginTokenCreated = now;
if (!Database.UpdateAccount(client.Account))
{
Logger.Error(client, "Failed to update OneTimeToken");
throw new ResponseErrorException(ErrorCode.ERROR_CODE_AUTH_SESSION_KEY_GENERATE);
}
Server.RpcManager.AnnouncePlayerList();
Logger.Debug(client, $"Updated OneTimeToken:{client.Account.LoginToken}");
res.OneTimeToken = client.Account.LoginToken;
return res;
}
}
}