private void _resource_OnReceived(object sender, PeerCommandEventArgs e)
        {
            try
            {
                lock (_activeConnections)
                {
                    var command = _serializer.Deserialize(e.Data);
                    if (!string.IsNullOrEmpty(command.UserId))
                    {
                        lock (_responceWaiters)
                        {
                            TaskCompletionSource <Command> taskSource;
                            if (_responceWaiters.TryGetValue(new Tuple <string, CommandName>(command.UserId, command.Name),
                                                             out taskSource))
                            {
                                taskSource.TrySetResult(command);
                            }
                        }
                    }

                    var        user = _userRepository.FirstMatching(UserSpecifications.UserId(command.UserId));
                    RemoteUser remoteUser;
                    if (user != null)
                    {
                        var existedConnection = _activeConnections.FirstOrDefault(i => i.User.Equals(user));
                        if (existedConnection != null)
                        {
                            existedConnection.Peer = e.Peer;
                            existedConnection.Peer.HandleActivity();
                            remoteUser = existedConnection;
                        }
                        else
                        {
                            remoteUser = new RemoteUser(user, e.Peer);
                        }
                        _activeConnections.Add(remoteUser);
                    }
                    else
                    {
                        remoteUser = new RemoteUser(new User(), e.Peer);
                        _activeConnections.Add(remoteUser);
                    }
                    CommandRecieved(this, new RemoteUserCommandEventArgs {
                        Command = command, RemoteUser = remoteUser
                    });
                }
            }
            catch (Exception exc)
            {
                Logger.Exception(exc, "_resource_OnReceived");
            }
        }
Exemple #2
0
        private async void HandleAuthentication(RemoteUser remoteUser, Command command)
        {
            var request = _commandBuilder.GetUnderlyingObject <AuthenticationRequest>(command);
            var result  = new AuthenticationResult();

            var user = _usersRepository.FirstMatching(UserSpecifications.UserId(request.UserId));

            if (user != null)
            {
                result.Result   = AuthenticationResultType.Success;
                user.OsName     = request.OsName;
                user.PushUri    = request.PushUri;
                user.DeviceName = request.DeviceName;
            }
            else
            {
                result.Result = AuthenticationResultType.NotRegistered;
            }
            _commandBuilder.ChangeUnderlyingObject(command, result);
            await remoteUser.Peer.SendCommand(command);
        }