Exemplo n.º 1
0
        private void CallToLogin(string login)
        {
            StopBackgroundWorker();
            ReadFriendDataRequest tmp = MainModel.RestWebApiRequest.ReadFriendData(MainModel.UserData.Login, MainModel.UserData.Password, login);

            //AudioManager.BeginCall("192.168.43.24");
            if (tmp.read_data)
            {
                string avatar     = tmp.avatar;
                string address_ip = tmp.address_ip;// "192.168.43.24";
                string call_id    = MainModel.RestWebApiRequest.CreateCall(MainModel.UserData.Login, MainModel.UserData.Password, login);

                if (call_id != "0")
                {
                    CallWindow callWindow = new CallWindow(MainModel.UserData.Login, MainModel.UserData.Password, avatar, login, address_ip, call_id, "create", udpClient);
                    if (!callWindow.cvm.CallModel.IsNotInLocalNewtwork)
                    {
                        callWindow.ShowDialog();
                    }
                }
                else
                {
                    MessageBox.Show("Nieudało się wywołać połączenia.");
                }
            }
            else
            {
                MessageBox.Show("Użytkownik o podanym loginie prawdopodobnie nie istnieje.");
            }

            UpdateCallList();
            StartBackgroundWorker();
        }
Exemplo n.º 2
0
        public void ReceiveCall(string login, string avatar, string address_ip, string call_id)
        {
            StopBackgroundWorker();
            CallWindow loginnWindow = new CallWindow(MainModel.UserData.Login, MainModel.UserData.Password, avatar, login, address_ip, call_id, "select", udpClient);

            loginnWindow.ShowDialog();
            UpdateCallList();
            StartBackgroundWorker();
        }
Exemplo n.º 3
0
        public CallViewModel(string login, string password, string friendAvatar, string friendLogin, string address_ip, string call_id, string state, CallWindow window, UdpClient udp)
        {
            callWindow = window;
            udpClient  = udp;
            CallModel  = new CallModel(login, password, friendAvatar, friendLogin, address_ip, call_id, state);

            CallModel.Message = "message";
            CallModel.Chat.Add("test");
            CallModel.Chat.Add("test2");
            CallModel.Chat.Add("test3");

            sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            if (state == "create")
            {
                MY_PORT     = SENDER_PORT;
                FRIEND_PORT = RECEIVED_PORT;
            }
            else
            {
                MY_PORT     = RECEIVED_PORT;
                FRIEND_PORT = SENDER_PORT;
            }

            MY_ADDRESS_IP = new IPHelpfulFunktions().GetLocalIPAddress();

            try
            {
                epLocal = new IPEndPoint(IPAddress.Parse(MY_ADDRESS_IP), Convert.ToInt32(MY_PORT));
                sck.Bind(epLocal);

                epRemote = new IPEndPoint(IPAddress.Parse(CallModel.FriendAddressIP), Convert.ToInt32(FRIEND_PORT));
                sck.Connect(epRemote);

                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
            }
            catch (Exception exp)
            {
                //MessageBox.Show(exp.ToString());
                MessageBox.Show(friendLogin + " prawdopodobnie nie jest w sieci lokalnej i połączenie z niem nie jest możliwe.");
                CallModel.IsNotInLocalNewtwork = true;
            }
        }
Exemplo n.º 4
0
        public void Start()
        {
            try
            {
                Global.Busy = true;
                Global.dir  = "->";
                // Create a TcpClient.
                IPAddress  ip      = IPAddress.Parse(callIPAddress);
                IPEndPoint address = new IPEndPoint(ip, 16000);
                client = new TcpClient(callIPAddress, 16000);
                sp.Play();
                myPort = RandomPort();

                // Get a client stream for reading and writing.
                NetworkStream stream = client.GetStream();

                //  Send the message to the connected TcpServer.
                sentPSSP = new PSSP(PSSP.Type.CALL, Global.GetLocalIPAddress(), myPort);
                Byte[] data = sentPSSP.ToBytes();
                stream.Write(data, 0, data.Length);

                // Buffer to store the response bytes
                data = new Byte[1024];
                // String to store the response ASCII representation.
                String responseData = String.Empty;

                // Read the first batch of the TcpServer response bytes.
                int bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);

                recvPSSP = new PSSP(data);
                sp.Stop();
                //  Checking if other user has answered or rejected our call
                if (recvPSSP.getOP() == "ACK-")
                {
                    MainWindow.instance.Invoke((System.Windows.Forms.MethodInvoker) delegate { MainWindow.instance.DisableTimer(); MainWindow.instance.LockUI('l'); });
                    CallWindow callWindow = new CallWindow(recvPSSP.getIP(), recvPSSP.getPORT(), sentPSSP.getIP(), myPort, client);
                    callWindow.ShowDialog();
                }
                else
                {
                    MainWindow.instance.Invoke((System.Windows.Forms.MethodInvoker) delegate { MainWindow.instance.UnlockUI(); });
                    Global.Busy = false;
                    Global.AddtoHistory(new HistoryRecord(new Contact("Me", Global.GetLocalIPAddress()).ToString(), "->", new Contact(Global.GetContactName(callIPAddress), callIPAddress).ToString(), DateTime.Now, "00:00:00", "Missed"));
                    if (recvPSSP.getOP() == "BUSY")
                    {
                        MessageBox.Show(string.Format("{0} is busy!", Global.GetContactName(callIPAddress)));
                    }
                    if (recvPSSP.getOP() == "NACK")
                    {
                        MessageBox.Show(string.Format("{0} rejected the call!", Global.GetContactName(callIPAddress)));
                    }
                }
                stream.Close();
                client.Close();
            }
            catch (SocketException se)
            {
                Global.Busy = false;
                if (MainWindow.locked)
                {
                    MessageBox.Show(string.Format("{0} is unavailable!", Global.GetContactName(callIPAddress)));
                }
                MainWindow.instance.Invoke((System.Windows.Forms.MethodInvoker) delegate { MainWindow.instance.DisableTimer(); MainWindow.instance.UnlockUI(); });
            }
            catch (Exception e)
            {
                Global.Busy = false;
                Console.WriteLine(e);
            }
        }
Exemplo n.º 5
0
        static void ClientHandler(object obj)
        {
            //var param = client as object;
            object[] args = obj as object[];

            TcpClient client = (TcpClient)args[0];
            bool      busy   = (bool)args[1];

            byte[] buffer = new byte[34];

            PSSP pssp = new PSSP();

            bool dialogshown = false;

            try
            {
                while (true)
                {
                    int l = client.GetStream().Read(buffer, 0, buffer.Length);
                    Console.WriteLine("Serwer otrzymal: " + new ASCIIEncoding().GetString(buffer).Substring(0, l));

                    PSSP receive_pssp = new PSSP(buffer);

                    if (!busy)
                    {
                        Global.Busy = true;

                        if (receive_pssp.getOP() == "CALL" && !dialogshown)
                        {
                            Global.dir  = "<-";
                            Global.Busy = true;
                            MainWindow.instance.Invoke((MethodInvoker) delegate { MainWindow.instance.LockUI('l'); });
                            string ip = receive_pssp.getIP();
                            Global.remoteAddress = ip;
                            IncomingCallMessageBox incomingCallMessageBox = new IncomingCallMessageBox(ip, client);
                            incomingCallMessageBox.ShowDialog();
                            dialogshown = true;

                            //komunikat o połączeniu
                            // jeśli Yes to wyślij ACK-
                            // połącz udp

                            if (Global.CallAnswered == Global.AnswerType.YES)
                            {
                                int port = receive_pssp.getPORT();
                                myPortNumber = RandomPort(receive_pssp.getPORT());
                                pssp         = new PSSP(PSSP.Type.ACK, Global.GetLocalIPAddress(), myPortNumber);
                                client.GetStream().Write(pssp.ToBytes(), 0, pssp.ToBytes().Length);
                                Console.WriteLine("Serwer wyslal: " + pssp.ToString());
                                CallWindow callWindow = new CallWindow(receive_pssp.getIP(), port, pssp.getIP(), pssp.getPORT(), client);
                                callWindow.ShowDialog();
                                // w oknie rozmowy czeka na END
                                break;
                            }
                            // jeśli No to wyślij NACK
                            if (Global.CallAnswered == Global.AnswerType.NO)
                            {
                                pssp = new PSSP(PSSP.Type.NACK);
                                client.GetStream().Write(pssp.ToBytes(), 0, 34);
                                Global.Busy = false;
                                MainWindow.instance.UnlockUI();
                                client.Close();
                                break;
                            }
                            if (Global.CallAnswered == Global.AnswerType.NOINFO)
                            {
                                Global.Busy = false;
                                client.Close();
                                break;
                            }
                        }
                        if (receive_pssp.getOP() == "END-")
                        {
                            // zakończ UDP
                            // wykonuje się w oknie rozmowy
                            Global.Busy = false;
                            client.Close();
                            break;
                        }
                        Array.Clear(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        pssp = new PSSP(PSSP.Type.BUSY);
                        client.GetStream().Write(pssp.ToBytes(), 0, 33);
                        Console.WriteLine("Wysłano BUSY do {0}", client.Client.RemoteEndPoint.ToString());
                        Global.AddtoHistory(new HistoryRecord(new Contact("Me", Global.GetLocalIPAddress()).ToString(), "<-", new Contact(Global.GetContactName(receive_pssp.getIP()), receive_pssp.getIP()).ToString(), DateTime.Now, "00:00:00", "Missed"));
                        client.Close();
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                client.Close();
                Global.Busy = false;
                Console.WriteLine("Zamknieto klienta");
            }
        }