private void HandleVoicePacket(Command command, RemoteUser remoteUser)
        {
            if (remoteUser.IsInCallWith == null)
                return;

            remoteUser.IsInCallWith.Peer.SendCommand(command);
        }
        private void HandleEndCall(Command command, RemoteUser remoteUser)
        {
            if (remoteUser.IsInCallWith == null)
                return;

            var cmd = _commandBuilder.Create(CommandName.EndCall, string.Empty);
            remoteUser.IsInCallWith.Peer.SendCommand(cmd);
        }
        private void HandleVoicePacket(Command command, RemoteUser remoteUser)
        {
            if (remoteUser.IsInCallWith == null)
                return;

            _translationResource.AppendRawData(_commandBuilder.GetUnderlyingObject<byte[]>(command), 
                data => 
                {
                    _commandBuilder.ChangeUnderlyingObject(command, data);
                    remoteUser.IsInCallWith.Peer.SendCommand(command);
                });
        }
 public void SendCommand(Command cmd)
 {
     try
     {
         var data = _serializer.Serialize(cmd);
         _resource.Send(data);
     }
     catch (Exception)
     {
         //Log
     }
 }
        private async void HandleDial(Command command, RemoteUser remoteUser)
        {
            var callerNumber = _commandBuilder.GetUnderlyingObject<string>(command);
            var opponent = _connectionsManager.FindRemoteUserByNumber(callerNumber);

            var dialResult = new DialResult();

            if (opponent == null)
            {
                var opponentUser = _usersRepository.FirstMatching(UserSpecifications.Number(callerNumber));
                if (opponentUser != null)
                {
                    _pushSender.SendVoipPush(opponentUser.PushUri, remoteUser.User.Number, remoteUser.User.Number);
                    var resultCommand = await _connectionsManager.PostWaiter(opponentUser.UserId, CommandName.IncomingCall);
                    var answerType = _commandBuilder.GetUnderlyingObject<AnswerResultType>(resultCommand);
                    if (answerType == AnswerResultType.Answered)
                    {
                        dialResult.Result = DialResultType.Answered;
                        opponent.IsInCallWith = remoteUser;
                        remoteUser.IsInCallWith = opponent;
                    }
                    else
                    {
                        dialResult.Result = DialResultType.Declined;
                    }
                }
                else
                {
                    dialResult.Result = DialResultType.NotFound;
                }
            }
            else
            {
                var incomingCallCommand = _commandBuilder.Create(CommandName.IncomingCall, callerNumber);
                var resultCommand = await opponent.Peer.SendCommandAndWaitAnswer(incomingCallCommand);
                var answerType = _commandBuilder.GetUnderlyingObject<AnswerResultType>(resultCommand);
                if (answerType == AnswerResultType.Answered)
                {
                    dialResult.Result = DialResultType.Answered;
                    opponent.IsInCallWith = remoteUser;
                    remoteUser.IsInCallWith = opponent;
                }
                else
                {
                    dialResult.Result = DialResultType.Declined;
                }
            }
            _commandBuilder.ChangeUnderlyingObject(command, dialResult);
            remoteUser.Peer.SendCommand(command);
        }
 public async Task SendCommand(Command command)
 {
     try
     {
         var message = _serializer.Serialize(command);
         Logger.Trace("Sending data to {0}:{1}:  {2}", HostName, Port, message);
         _dataWriter.WriteString(message);
         await _dataWriter.StoreAsync();
     }
     catch (Exception exc)
     {
         Logger.Exception(exc, "During send data to {0}:{1}", HostName, Port);
         throw;
     }
 }
 public Task<Command> SendCommandAndWaitAnswer(Command command)
 {
     var taskSource = new TaskCompletionSource<Command>();
     try
     {
         lock (_responseWaiters)
         {
             _responseWaiters[command.PacketId] = taskSource;
         }
         SendCommand(command);
     }
     catch (Exception)
     {
         throw;
         //Log
     }
     return taskSource.Task;
 }
 public Task<Command> SendCommandAndGetAnswerAsync(Command cmd)
 {
     var taskSource = new TaskCompletionSource<Command>();
     try
     {
         lock (_responseWaiters)
         {
             var data = _serializer.Serialize(cmd);
             _responseWaiters[cmd.PacketId] = taskSource;
             _resource.Send(data);
         }
     }
     catch (Exception)
     {
         throw;
         //Log
     }
     return taskSource.Task;
 }
        private async void HandleAuthentication(RemoteUser remoteUser, Command command)
        {
            var request = _commandBuilder.GetUnderlyingObject<AuthenticationRequest>(command);
            var result = new AuthenticationResult();

            var user = _usersRepository.GetById(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);
        }
 private void _transportManager_OnCommandRecieved(object sender, CommandEventArgs e)
 {
     switch (e.Command.Name)
     {
         case CommandName.VoicePacket:
             if (IsInCall)
             {
                 var data = _commandBuilder.GetUnderlyingObject<byte[]>(e.Command);
                 _audioDevice.Play(data);
             }
             break;
         case CommandName.EndCall:
             IsInCall = false;
             _audioDevice.StopCapture();
             CallEnded(this, EventArgs.Empty);
             break;
         case CommandName.IncomingCall:
             _incomingCallCommand = e.Command;
             IncomingCall(this, new CallEventsArgs { Number = _commandBuilder.GetUnderlyingObject<string>(e.Command)});
             break;
     }
 }
        private async void HandleRegistration(RemoteUser remoteUser, Command command)
        {
            var request = _commandBuilder.GetUnderlyingObject<RegistrationRequest>(command);
            var result = new RegistrationResult();

            if (string.IsNullOrEmpty(request.Number) ||
                request.Number.Length < 4 ||
                request.Number.Trim(' ', '+').ToCharArray().Any(i => !char.IsDigit(i)))
            {
                result.Result = RegistrationResultType.NotRegistered;
            }
            else
            {
                remoteUser.User.Number = request.Number;
                remoteUser.User.UserId = Guid.NewGuid().ToString();
                _usersRepository.Add(remoteUser.User);
                result.Result = RegistrationResultType.Success;
                result.UserId = remoteUser.User.UserId;
            }
            _commandBuilder.ChangeUnderlyingObject(command, result);
            await remoteUser.Peer.SendCommand(command);
        }
 public string Serialize(Command cmd)
 {
     return _innerSerializer.Serialize(cmd);
 }