Example #1
0
 static void tcp_recv(TcpClient cli, CallWindow cw)
 {
     try
     {
         byte[] buffer = new byte[1024];
         PSSP   pssp;
         while (true)
         {
             cli.GetStream().Read(buffer, 0, 34);
             pssp = new PSSP(buffer);
             if (pssp.getOP() == "END-")
             {
                 waveIn.StopRecording();
                 waveOut.Stop();
                 recieve_thread.Abort();
                 Global.Busy = false;
                 MainWindow.instance.Invoke((MethodInvoker) delegate { MainWindow.instance.UnlockUI(); });
                 cw.Invoke((MethodInvoker) delegate { cw.Close(); });
                 Global.AddtoHistory(new HistoryRecord(new Contact("Me", Global.GetLocalIPAddress()).ToString(), Global.dir, new Contact(Global.GetContactName(Global.remoteAddress), Global.remoteAddress).ToString(), DateTime.Now, calltime, "Answered"));
                 break;
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Example #2
0
 static void tcp_recv(TcpClient cli, IncomingCallMessageBox icmb)
 {
     try
     {
         byte[] buffer = new byte[1024];
         PSSP   pssp;
         while (true)
         {
             cli.GetStream().Read(buffer, 0, 34);
             pssp = new PSSP(buffer);
             if (Global.CallAnswered == Global.AnswerType.NO || Global.CallAnswered == Global.AnswerType.YES)
             {
                 break;
             }
             if (pssp.getOP() == "END-")
             {
                 icmb.sp.Stop();
                 Global.AddtoHistory(new HistoryRecord(new Contact("Me", Global.GetLocalIPAddress()).ToString(), Global.dir, new Contact(Global.GetContactName(Global.remoteAddress), Global.remoteAddress).ToString(), DateTime.Now, "00:00:00", "Missed"));
                 MainWindow.instance.Invoke((MethodInvoker) delegate { MainWindow.instance.UnlockUI(); });
                 icmb.Invoke((MethodInvoker) delegate { icmb.Close(); });
                 Global.Busy = false;
                 MessageBox.Show(string.Format("Missed call from {0}", Global.GetContactName(Global.remoteAddress)));
                 break;
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Example #3
0
        public void End()
        {
            PSSP pssp = new PSSP(PSSP.Type.END);

            tcpcli.GetStream().Write(pssp.ToBytes(), 0, pssp.ToBytes().Length);
            tcpcli.GetStream().Write(pssp.ToBytes(), 0, pssp.ToBytes().Length);
            // wysylam 2 razy zeby zabic wątek w IncomingCallMessageBox
            Global.AddtoHistory(new HistoryRecord(new Contact("Me", Global.GetLocalIPAddress()).ToString(), Global.dir, new Contact(Global.GetContactName(Global.remoteAddress), Global.remoteAddress).ToString(), DateTime.Now, calltime, "Answered"));
            tcpcli.Close();
            recvCli.Abort();
            waveIn.StopRecording();
            waveOut.Stop();
            recieve_thread.Abort();
            Global.Busy = false;
            this.Close();
        }
Example #4
0
 private void ButtonCall_Click(object sender, EventArgs e)
 {
     if (Global.ValidateIPv4(GetAddress()))
     {
         if (Global.GetLocalIPAddress() != GetAddress())
         {
             if (!locked)
             {
                 DialogResult dialogResult = MessageBox.Show(String.Format("Are you sure you want to call {0}?", Global.GetContactName(GetAddress())), "", MessageBoxButtons.YesNo);
                 if (dialogResult == DialogResult.Yes)
                 {
                     myTcpClient = new MyTcpClient(GetAddress());
                     LockUI('c');
                     ticks          = 0;
                     timer1.Enabled = true;
                 }
             }
             else
             {
                 try
                 {
                     PSSP pssp = new PSSP(PSSP.Type.END);
                     MyTcpClient.client.GetStream().Write(pssp.ToBytes(), 0, pssp.ToBytes().Length);
                     myTcpClient.sp.Stop();
                     Global.AddtoHistory(new HistoryRecord(new Contact("Me", Global.GetLocalIPAddress()).ToString(), "->", new Contact(Global.GetContactName(GetAddress()), GetAddress()).ToString(), DateTime.Now, "00:00:00", "Missed"));
                     UnlockUI();
                     DisableTimer();
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(ex);
                     myTcpClient.Stop();
                     UnlockUI();
                     DisableTimer();
                 }
             }
         }
         else
         {
             MessageBox.Show("You can't call to yourself!");
         }
     }
     else
     {
         MessageBox.Show("Invalid address!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Example #5
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);
            }
        }
Example #6
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");
            }
        }