Пример #1
0
        private void UninitializeCall()
        {
            //Set the flag to end the Send and Receive threads.
            bStop        = true;
            IsCallActive = false;

            window.ButtonSetAsync(true, false, false);
            if (IsCaller)
            {
                CallRepo.createCall(Globals.currentUserId, ReceiverID, CallDate.ToString("yyyy-MM-dd hh:mm:ss"), 1);
            }
            IsCaller = false;
            Thread.Sleep(500);
            //Reinitialize crypto properties after the call
            MyCurrentPass   = AES_Crypto.CreatePass();
            MyCurrentSalt   = AES_Crypto.CreateSalt();
            CallCurrentPass = null;
            CallCurrentSalt = null;
            UserRepo.updateCrypto(MyCurrentPass, MyCurrentSalt);
        }
Пример #2
0
        /*
         * Commands are received asynchronously. OnReceive is the handler for them.
         */
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                EndPoint receivedFromEP = new IPEndPoint(IPAddress.Any, 0);

                //Get the IP from where we got a message.
                clientSocket.EndReceiveFrom(ar, ref receivedFromEP);

                //Convert the bytes received into an object of type Data.
                Data msgReceived = new Data(byteData);

                //Act according to the received message.
                switch (msgReceived.cmdCommand)
                {
                //We have an incoming call.
                case Command.Invite:
                {
                    if (IsCallActive == false)
                    {
                        //We have no active call.

                        //Ask the user to accept the call or not.
                        if (MessageBox.Show("Call coming from " + msgReceived.strName + ".\r\n\r\nAccept it?",
                                            "VoiceChat", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes)
                        {
                            //Get someones crypto information
                            var cryptoTuple = UserRepo.fetchUsersCrypto(msgReceived.strName);
                            CallCurrentPass = cryptoTuple.Item1;
                            CallCurrentSalt = cryptoTuple.Item2;

                            SendMessage(Command.OK, receivedFromEP);
                            otherPartyEP = receivedFromEP;
                            otherPartyIP = (IPEndPoint)receivedFromEP;
                            InitializeCall();
                        }
                        else
                        {
                            //The call is declined. Send a busy response.
                            SendMessage(Command.Busy, receivedFromEP);
                        }
                    }
                    else
                    {
                        //We already have an existing call. Send a busy response.
                        SendMessage(Command.Busy, receivedFromEP);
                    }
                    break;
                }

                //OK is received in response to an Invite.
                case Command.OK:
                {
                    //Start a call.
                    InitializeCall();
                    MessageBox.Show("Person has answered. Call started.", "Call handler", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                    break;
                }

                //Remote party is busy.
                case Command.Busy:
                {
                    MessageBox.Show("User busy.", "VoiceChat", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
                    CallRepo.createCall(Globals.currentUserId, ReceiverID, CallDate.ToString("yyyy-MM-dd hh:mm:ss"), 5);
                    IsCaller = false;
                    window.ButtonSetAsync(true, false, false);

                    //Reinitialize crypto properties after the call
                    MyCurrentPass   = AES_Crypto.CreatePass();
                    MyCurrentSalt   = AES_Crypto.CreateSalt();
                    CallCurrentPass = null;
                    CallCurrentSalt = null;
                    UserRepo.updateCrypto(MyCurrentPass, MyCurrentSalt);

                    break;
                }

                case Command.Bye:
                {
                    //Check if the Bye command has indeed come from the user/IP with which we have
                    //a call established. This is used to prevent other users from sending a Bye, which
                    //would otherwise end the call.
                    if (receivedFromEP.Equals(otherPartyEP) == true)
                    {
                        //End the call.
                        UninitializeCall();
                    }
                    MessageBox.Show("Person has disconnected", "Call handler", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                    break;
                }
                }

                byteData = new byte[1024];
                //Get ready to receive more commands.
                clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref receivedFromEP, new AsyncCallback(OnReceive), null);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "VoiceChat-OnReceive ()", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
        }