コード例 #1
0
        public void RespondToLogin(JamPacket loginPacket)
        {
            if (loginPacket.Header.DataType != LoginRequest.DATA_TYPE)
            {
                return;
            }

            LoginRequest request = new LoginRequest(loginPacket.Data, Serializer);

            LoginResponse response;

            try
            {
                Account      = AccountFactory.Authenticate(request.Username, request.Password, Server.HashFactory);
                AppSigniture = request.AppSigniture;
                Server.OnClientIdentified(new JamServer.IdentifiedConnectionEventArgs()
                {
                    ServerConnection = this, RemoteEndPoint = Client.Client.RemoteEndPoint, Account = Account
                });

                JamServerConnection existingConnection = Server.GetConnection(Account.AccountID);
                if (existingConnection != null)
                {
                    Server.OnClientConnectedElsewhere(new JamServer.IdentifiedConnectionEventArgs()
                    {
                        ServerConnection = this, RemoteEndPoint = existingConnection.Client.Client.RemoteEndPoint, Account = existingConnection.Account
                    });
                    existingConnection.Dispose();
                }

                JamServerConnection appServiceConnection = Server.GetAppServiceConnection(AppSigniture);
                if (appServiceConnection == null && AppSigniture != Server.AppSigniture)
                {
                    response = new LoginResponse(LoginResponse.LoginResult.AppOffline, null, Guid.Empty, Serializer);
                    Server.OnClientOfflineAppRequest(new JamServer.IdentifiedConnectionEventArgs()
                    {
                        ServerConnection = this, RemoteEndPoint = Client.Client.RemoteEndPoint, Account = Account
                    });
                }
                else
                {
                    Guid appServiceID = Guid.Empty;
                    if (appServiceConnection != null)
                    {
                        appServiceID = appServiceConnection.Account.AccountID;
                    }

                    response = new LoginResponse(LoginResponse.LoginResult.Good, Account, appServiceID, Serializer);
                    Server.AddConnection(this);
                }
            }
            catch (AccountFactory.InvalidUsernameException)
            {
                response = new LoginResponse(LoginResponse.LoginResult.BadUsername, null, Guid.Empty, Serializer);
                Server.OnClientInvalidUsername(new JamServer.ConnectionEventArgs()
                {
                    ServerConnection = this, RemoteEndPoint = Client.Client.RemoteEndPoint
                });
            }
            catch (AccountFactory.InvalidAccessCodeException)
            {
                response = new LoginResponse(LoginResponse.LoginResult.BadPassword, null, Guid.Empty, Serializer);
                Server.OnClientInvalidPassword(new JamServer.ConnectionEventArgs()
                {
                    ServerConnection = this, RemoteEndPoint = Client.Client.RemoteEndPoint
                });
            }
            catch (EntityException)
            {
                Server.Dispose();
                return;
            }

            JamPacket responsePacket = new JamPacket(Guid.Empty, Guid.Empty, LoginResponse.DATA_TYPE, response.GetBytes());

            Send(responsePacket);
        }