Example #1
0
        //SERVER SIDE
        private void OnReceive(IAsyncResult ar)
        {
           // if (isSocketOpen)
                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.
                    XProtocol.Data msgReceived = new XProtocol.Data(byteData);

                    //Act according to the received message.
                    switch (msgReceived.cmdCommand)
                    {
                        //We have an incoming call.
                        case XProtocol.Command.Invite:
                            {
                                try
                                {
                                    IPEndPoint tempPoint = (IPEndPoint)receivedFromEP;

                                    //MessageBox.Show(tempPoint.Address.ToString() + ";IP = " + IP);

                                    if (IP == null) IP = "";

                                    if (String.Equals(IP, "") || String.Equals(tempPoint.Address.ToString(), IP))
                                    {
                                        otherPartyEP = receivedFromEP;
                                        otherPartyIP = (IPEndPoint)receivedFromEP;

                                        if (IP == "")
                                            IP = otherPartyIP.Address.ToString();

                                        //We have no active call.
                                        SendMessage(XProtocol.Command.OK, receivedFromEP);


                                        if (State == Statement.Nothing)
                                            State = Statement.Listening;
                                        else
                                            State = Statement.SpeakingAndListening;
                                        // button1.Enabled = false;
                                        InitCallOut();

                                        //button1.BeginInvoke(ChangeImg, new object[] { button1, Properties.Resources.microphonered_2904 });
                                        BeginInvoke(ShowTip, new object[] { notifyIcon1, 
                                    UserList.Find(x => IPAddress.Equals(x.IP, otherPartyIP.Address)).IP ,
                                "Входящий звонок", "Вам звонит "});
                                        BeginInvoke(CallUser, new object[] { BuddyList, otherPartyIP.Address, Color.Orange });
                                    }
                                    else
                                    {
                                        //We already have an existing call. Send a busy response.
                                        SendMessage(XProtocol.Command.Busy, receivedFromEP);
                                        BeginInvoke(ShowTip, new object[] { notifyIcon1, 
                                    UserList.Find(x => IPAddress.Equals(x.IP, tempPoint.Address)).IP ,
                                "Пропущенный звонок", "Вам звонил "});
                                    }
                                    
                                } catch (Exception exc)
                                {
                                    MessageBox.Show(exc.Message, "Invite Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }

                                break;
                            }

                        //Remote party is busy.
                        case XProtocol.Command.Busy:
                            {
                                ConnectingTimer.Stop();

                                State = Statement.Nothing;
                                IP = "";
                                MessageBox.Show("Пользователь занят.", "VoiceChat", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                break;
                            }

                        //OK is received in response to an Invite.
                        case XProtocol.Command.OK:
                            {
                                //IPEndPoint ipend = receivedFromEP as IPEndPoint;
                                //
                                //Start a call.
                                InitCallIn();
                                button1.BeginInvoke(ChangeImg, new object[] { button1, Properties.Resources.microphonered_2904 });

                                if (State == Statement.ConnectingAndListening)
                                    State = Statement.SpeakingAndListening;
                                else
                                    State = Statement.Speaking;
                                break;
                            }

                        case XProtocol.Command.Bye:
                            {
                                IPEndPoint ipend = receivedFromEP as IPEndPoint;
                                BeginInvoke(CallUser, new object[] { BuddyList, ipend.Address, Color.White });
                                bStop = true;
                                //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.
                                    //StopIncoming();
                                    //if (State != Statement.SpeakingAndListening)
                                        
                                    //nUdpClientFlag = 0;
                                    //bIsCallActive = false;
                                    if (State == Statement.SpeakingAndListening)
                                    {
                                        StopIncoming();
                                        State = Statement.Speaking;
                                    }
                                    else
                                    {
                                        State = Statement.Nothing;
                                        IP = "";
                                        StopOutcoming();
                                        StopIncoming();

                                        InitAdressBookListener();
                                    }
                                    IPEndPoint ipend1 = receivedFromEP as IPEndPoint;
                                    BeginInvoke(ShowTip, new object[] { notifyIcon1, 
                                    UserList.Find(x => IPAddress.Equals(x.IP, ipend1.Address)).IP ,
                                    "Собеседник отсоединился", ""});

                                    //button1.BeginInvoke(ChangeImg, new object[] { button1, Properties.Resources.microphoneblue_5728});
                                    // button1.Enabled = true;                               
                                //}
                                break;

                            }
                        case XProtocol.Command.Disconnect: 
                            State = Statement.Nothing;
                                        IP = "";
                                        StopOutcoming();
                                        StopIncoming();

                                        InitAdressBookListener();
                                    
                                    IPEndPoint ipend2 = receivedFromEP as IPEndPoint;
                                    BeginInvoke(ShowTip, new object[] { notifyIcon1, 
                                    UserList.Find(x => IPAddress.Equals(x.IP, ipend2.Address)).IP ,
                                    "Собеседник закрыл программу", ""});
                                    button1.BeginInvoke(ChangeImg, new object[] { button1, Properties.Resources.microphoneblue_5728 });
                                    IPEndPoint ipend0 = receivedFromEP as IPEndPoint;
                                BeginInvoke(CallUser, new object[] { BuddyList, ipend0.Address, Color.White });
                                nUdpClientFlag += 1;
                                StartThreadChecking(true);
                                    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 ex)
                {
                    //MessageBox.Show(ex.Message, "Ошибка принятия звонка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
        }
Example #2
0
        private void SendMessage(XProtocol.Command cmd, EndPoint sendToEP)
        {
            try
            {
                //Create the message to send.
                XProtocol.Data msgToSend = new XProtocol.Data();

                // msgToSend.strName = UserName;   //Name of the user.
                msgToSend.cmdCommand = cmd;         //Message to send.

                byte[] message = msgToSend.ToByte();

                //Send the message asynchronously.
                clientSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, sendToEP,
                    new AsyncCallback(OnSend), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка отправки сообщения клиенту", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }