Ejemplo n.º 1
0
        private void Connect(IPEndPoint endPoint)
        {
            hostTcpClient = new TcpClient();
            var result  = hostTcpClient.BeginConnect(endPoint.Address, endPoint.Port, null, null);
            var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3));

            if (!success)
            {
                if (EndCall != null)
                {
                    EndCall.Invoke();
                }
                throw new Exception("Przekroczono czas połączenia.");
            }
            connected = true;
        }
Ejemplo n.º 2
0
        public void BreakCall(bool sendCancelCommand = false)
        {
            if (sendCancelCommand)
            {
                SendText(Commands.Bye);
                Trace.WriteLine(Commands.Bye);
            }

            if (hostTcpClient != null)
            {
                if (hostTcpClient.Connected)
                {
                    hostTcpClient.GetStream().Close();
                    hostTcpClient.Close();
                }
            }

            if (EndCall != null)
            {
                EndCall.Invoke();
            }
            isCalling = false;
        }
Ejemplo n.º 3
0
        //sender methods

        public async void MakeCall(IPEndPoint endPoint, string userName, string receiverName)
        {
            Trace.WriteLine("username: "******"receiver: " + receiverName);

            if (!isCalling)
            {
                remoteEndPointToSendVoice = endPoint;

                isCalling = true;

                if (MakeCallEvent != null)
                {
                    MakeCallEvent.Invoke("Dzwonię do: " + receiverName);
                }

                try
                {
                    Connect(endPoint);
                    string localIp = (((IPEndPoint)hostTcpClient.Client.LocalEndPoint).Address.ToString());
                    if (EncryptedCallSender)
                    {
                        SendText(Commands.Invite + ":" + localIp + ":" + userName + ":" + PrepareDHRequestString());
                    }
                    else
                    {
                        SendText(Commands.Invite + ":" + localIp + ":" + userName);
                    }

                    while (isCalling)
                    {
                        var msg = await ReceiveTextAsync();

                        if (msg.StartsWith(Commands.Busy))
                        {
                            ErrorEvent.Invoke("Zajęte");
                            BusyUserInfoEvent.Invoke("Użytkownik jest w trakcie rozmowy!");
                        }
                        else if (msg.StartsWith(Commands.Reject))
                        {
                            BreakCall();
                        }
                        else if (msg.StartsWith(Commands.Ack))
                        {
                            var msgAfterSplit = msg.Split(new char[] { ':' }, 2);
                            if (msgAfterSplit.Length == 2)
                            {
                                DHServer.HandleResponse(msgAfterSplit[1]);
                                DHKey = DHServer.Key;
                            }

                            if (UDPSenderStart != null)
                            {
                                UDPSenderStart.Invoke(remoteEndPointToSendVoice);
                            }

                            if (UDPListenerStart != null)
                            {
                                UDPListenerStart.Invoke(localEndPoint);
                            }

                            //Akceptacja połączenia - zmiana gui
                            if (TalkEvent != null)
                            {
                                TalkEvent.Invoke(receiverName);
                            }
                        }
                        else
                        {
                            ErrorEvent.Invoke(msg);
                        }
                    }
                }
                catch (Exception e)
                {
                    isCalling = false;

                    if (ErrorEvent != null)
                    {
                        ErrorEvent.Invoke(e.Message);
                    }

                    if (EndCall != null)
                    {
                        EndCall.Invoke();
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private void HandleConnection(TcpClient client)
        {
            StreamReader streamReader = new StreamReader(client.GetStream());
            StreamWriter streamWriter = new StreamWriter(client.GetStream())
            {
                AutoFlush = true
            };

            try
            {
                if (isBusy || isCalling)
                {
                    //odrzuc polaczenie
                    streamWriter.WriteLine(Commands.Busy);
                }
                else
                {
                    isBusy = true;
                    //akceptuj polaczenie
                    while (isBusy)
                    {
                        var message = streamReader.ReadLine();

                        if (message.StartsWith(Commands.Invite))
                        {
                            var msgAfterSplit = message.Split(new char[] { ':' }, 4);
                            if (msgAfterSplit.Length == 3)
                            {
                                EncryptedCallReceiver = false;
                                IncomingCallEvent.Invoke(msgAfterSplit[2]);
                                remoteEndPointToSendVoice = new IPEndPoint(IPAddress.Parse(msgAfterSplit[1]), localEndPoint.Port);
                            }
                            else if (msgAfterSplit.Length == 4)
                            {
                                EncryptedCallReceiver = true;
                                DHRequest             = msgAfterSplit[3];
                                //poinformuj o szyfrowaniu, potem odeslanie DH
                                IncomingCallEvent.Invoke(msgAfterSplit[2] + Environment.NewLine + "Rozmowa szyfrowana");
                                remoteEndPointToSendVoice = new IPEndPoint(IPAddress.Parse(msgAfterSplit[1]), localEndPoint.Port);
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (message.StartsWith(Commands.Cancel))
                        {
                            if (EndCall != null)
                            {
                                EndCall.Invoke();
                            }

                            if (UDPSenderStop != null)
                            {
                                UDPSenderStop.Invoke();
                            }
                            break;
                        }

                        if (message.StartsWith(Commands.Bye))
                        {
                            if (EndCall != null)
                            {
                                EndCall.Invoke();
                            }

                            if (UDPSenderStop != null)
                            {
                                UDPSenderStop.Invoke();
                            }
                            break;
                        }
                    }
                }
                if (ConnectedClient == client)
                {
                    ConnectedClient = null;
                    isBusy          = false;
                }

                client.GetStream().Close();
                client.Close();
            }
            catch (Exception e)
            {
                //disconnected
                if (ConnectedClient == client)
                {
                    ConnectedClient = null;
                    isBusy          = false;
                }

                if (client != null)
                {
                    client.Close();
                }
                ErrorEvent.Invoke(e.Message);
            }
        }
 protected virtual void OnEndCall(EventArgs e)
 {
     EndCall?.Invoke(this, e);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Data retrieved from server
        /// </summary>
        /// <param name="serverConnection">TCP connection with server</param>
        private static void ListenServer(TcpClient serverConnection)
        {
            serverConnection.ReceiveBufferSize = BUFFER_SIZE;
            serverConnection.SendBufferSize    = BUFFER_SIZE;

            NetworkStream networkStream = serverConnection.GetStream();

            while (serverConnection.Connected)
            {
                ServerObject data;
                try
                {
                    data = (ServerObject)formatter.Deserialize(networkStream);
                }
                #region POSSIBLE EXCEPTIONS
                catch (IOException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (SerializationException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (DecoderFallbackException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (OverflowException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (OutOfMemoryException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (NullReferenceException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (IndexOutOfRangeException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (FormatException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (InvalidCastException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (ArgumentOutOfRangeException)
                {
                    ExceptionHandler(networkStream);
                    continue;
                }
                catch (Exception e)
                {
                    ExceptionHandler(networkStream);
                    MessageBox.Show(e.Message, e.GetType().Name, MessageBoxButton.OK, MessageBoxImage.Error);
                    continue;
                }
                #endregion POSSIBLE EXCEPTIONS

                switch (data.ServerFlag)
                {
                case ServerFlag.ConnectionState:
                    Task.Run(() => SetConnectionState((KeyValuePair <ConnectionState, int>)data.Data));
                    break;

                case ServerFlag.Call:
                    IncomingCall?.Invoke((int)data.Data);
                    break;

                case ServerFlag.SendMessage:
                    Task.Run(() => SendMessage((SenderObjectRelation)data.Data));
                    break;

                case ServerFlag.SendingData:
                    Task.Run(() => SendingData((SenderObject)data.Data));
                    break;

                case ServerFlag.NewUserInCall:
                    NewUserCall?.Invoke((int)data.Data);
                    break;

                case ServerFlag.CallingToGroup:
                    CallingToGroup?.Invoke((int)data.Data);
                    break;

                case ServerFlag.DeclineCall:
                    CallDeclined?.Invoke((int)data.Data);
                    break;

                case ServerFlag.UserLeftRoom:
                    UserLeftRoom?.Invoke((int)data.Data);
                    break;

                case ServerFlag.RoomClosed:
                case ServerFlag.EndCall:
                    EndCall?.Invoke();
                    break;

                case ServerFlag.ConnectionChecking:
                    Task.Run(() => SendToServer(new ServerObject(ServerFlag.ConnectionChecking, Id)));
                    break;

                case ServerFlag.ApiConnection:
                    Task.Run(() => CheckApiConnectionFlag((ApiObject)data.Data));
                    break;

                default:
                    break;
                }
            }
            MessageBox.Show("Disconnected from server");
        }
Ejemplo n.º 7
0
Archivo: Port.cs Proyecto: MozderUC/ATE
 public void EnddCall(object obj, EndCallArgs e)
 {
     EndCall?.Invoke(this, e);
 }