protected void mContratoDeServicioThreadCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show(String.Format("Ocurrió un error en el hilo de ServiceContract: {0}", e.Error.Message)); } else { if (mEstado == EstadoRed.Conectado) { mCanalP2PThreadBGWorker = null; mContratoDeServicio = null; if (mPseudoServidorAudioHttpListener == null && mPseudoServidorVideoHttpListener == null) { Utilidades.SetTextoTextBox(TextBoxNombreUsuario, ""); Utilidades.SetTextoTextBox(TextBoxNombreRed, ""); Utilidades.SetTextoTextBox(TextBoxPassword, ""); } } else if (mEstado == EstadoRed.UsuarioFallido) { Utilidades.SetTextoTextBox(TextBoxNombreUsuario, ""); } mEstado = EstadoRed.Desconectado; Utilidades.SetTextoLabel(LabelValorEstado, "Desconectado"); Utilidades.SetEstadoActivadoContentControl(lblTitulo, false); Utilidades.SetEstadoActivadoContentControl(TextBoxNombreUsuario, true); Utilidades.SetEstadoActivadoContentControl(TextBoxNombreRed, true); Utilidades.SetEstadoActivadoContentControl(TextBoxPassword, true); Utilidades.SetEstadoActivadoContentControl(BotonConectar, true); } }
protected void VideoBGWorkerThreadFinished(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show(String.Format("Ocurrió un error en el hilo que procesa el video:{0}", e.Error.Message)); } else { mPseudoServidorVideoHttpListener = null; if (mCanalP2PThreadBGWorker == null && mPseudoServidorAudioHttpListener == null) { Utilidades.SetTextoLabel(LabelValorEstado, "Desconectado"); mEstado = EstadoRed.Desconectado; Utilidades.SetEstadoActivadoContentControl(TextBoxNombreUsuario, true); Utilidades.SetEstadoActivadoContentControl(TextBoxNombreRed, true); Utilidades.SetEstadoActivadoContentControl(TextBoxPassword, true); Utilidades.SetEstadoActivadoContentControl(BotonConectar, true); } } }
protected void AbrirConexion() { Regex regex = new Regex("[a-zA-Z0-9]"); if (!string.IsNullOrEmpty(TextBoxNombreUsuario.Text) && !string.IsNullOrEmpty(TextBoxNombreRed.Text) && regex.IsMatch(TextBoxNombreUsuario.Text) && regex.IsMatch(TextBoxNombreRed.Text) && !TextBoxNombreRed.Text.Trim().Contains(' ') && !TextBoxNombreUsuario.Text.Trim().Contains(' ')) { if (!(null == mContratoDeServicio && mCanalP2PThreadBGWorker == null)) { MessageBox.Show("Ocurrió un error crítico al inicializar la red.\n" + "\nLo sentimos, la aplicación se cerrará.", "Error crítico.", MessageBoxButton.OK, MessageBoxImage.Error); Application.Current.Shutdown(); } mEstado = EstadoRed.Conectando; Utilidades.SetTextoLabel(this.LabelValorEstado, "Conectando..."); mCanalP2PThreadBGWorker = new System.ComponentModel.BackgroundWorker(); mCanalP2PThreadBGWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(mServiceContractThread); mCanalP2PThreadBGWorker.WorkerReportsProgress = true; mCanalP2PThreadBGWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(mContratoDeServicioThreadCompleted); mCanalP2PThreadBGWorker.WorkerSupportsCancellation = true; mCanalP2PThreadBGWorker.RunWorkerAsync(); Utilidades.SetEstadoActivadoContentControl(TextBoxNombreUsuario, false); Utilidades.SetEstadoActivadoContentControl(TextBoxNombreRed, false); Utilidades.SetEstadoActivadoContentControl(TextBoxPassword, false); Utilidades.SetEstadoActivadoContentControl(BotonConectar, false); } else { MessageBox.Show("Por favor introduzca un Nombre de Usuario y un Nombre de Red que contengan solamente caracteres alfanuméricos y sin espacios.", "Login inválido.", MessageBoxButton.OK, MessageBoxImage.Error); Utilidades.SetEstadoActivadoContentControl(TextBoxNombreUsuario, true); Utilidades.SetEstadoActivadoContentControl(TextBoxNombreRed, true); Utilidades.SetEstadoActivadoContentControl(TextBoxPassword, true); } }
protected void MiEstadoRedChanged(object sender, Peer2PeerApp.Comunicacion.EstadoRedChangedEventArgs e) { if (Chat != null) { Chat.SetEstadoRedCambiado(Utilidades.GetTextoTextbox(TextBoxNombreUsuario), e); } if (Imagen != null) { Imagen.SetEstadoRedCambiado(Utilidades.GetTextoTextbox(TextBoxNombreUsuario), e); } if (Fichero != null) { Fichero.SetEstadoRedCambiado(Utilidades.GetTextoTextbox(TextBoxNombreUsuario), e); } if (Video != null) { Video.SetEstadoRedCambiado(Utilidades.GetTextoTextbox(TextBoxNombreUsuario), e); } if (Audio != null) { Audio.SetEstadoRedCambiado(Utilidades.GetTextoTextbox(TextBoxNombreUsuario), e); } if (Descarga != null) { Descarga.SetEstadoRedCambiado(Utilidades.GetTextoTextbox(TextBoxNombreUsuario), e); } if (e.Miembro == Utilidades.GetTextoTextbox(TextBoxNombreUsuario)) { if (e.EsNuevoNodoUnidoARed) { Utilidades.SetTextoLabel(LabelValorEstado, "¡Conectado!"); Utilidades.SetEstadoActivadoContentControl(lblTitulo, true); Utilidades.SetEstadoActivadoContentControl(BotonConectar, true); mEstado = EstadoRed.Conectado; } if (e.EsNodoQueAbandonaRed) { Utilidades.SetTextoLabel(LabelValorEstado, "Desconectado"); Utilidades.SetEstadoActivadoContentControl(lblTitulo, false); Utilidades.SetEstadoActivadoContentControl(BotonConectar, true); mEstado = EstadoRed.Desconectado; } } else { if (e.EsNuevoNodoUnidoARed) { Chat_ChatChanged(this, new ChatChangedEventArgs(string.Format("{0} se unió a la red P2P.\n", e.Miembro))); } if (e.EsNodoQueAbandonaRed) { Chat_ChatChanged(this, new ChatChangedEventArgs(string.Format("{0} abandonó la red P2P.\n", e.Miembro))); } } }
private void mServiceContractThread(object sender, DoWorkEventArgs doWorkArgs) { mContratoDeServicio = new ContratoDeServicio(); if (false == mContratoDeServicio.IniciarCanalP2P(Utilidades.GetTextoTextbox(TextBoxNombreUsuario), Utilidades.GetTextoTextbox(TextBoxNombreRed), Utilidades.GetTextoTextbox(TextBoxPassword))) { CerrarConexion(); } mContratoDeServicio.EstadoRedChanged += new EventHandler <EstadoRedChangedEventArgs>(MiEstadoRedChanged); mContratoDeServicio.AyudanteChat.ChatChanged += new EventHandler <ChatChangedEventArgs>(Chat_ChatChanged); mContratoDeServicio.AyudanteImagen.ImagenChanged += new EventHandler <ImagenChangedEventArgs>(Imagen_ImagenChanged); mContratoDeServicio.AyudanteFichero.FicheroChanged += new EventHandler <FicheroChangedEventArgs>(Fichero_FicheroChanged); mContratoDeServicio.AyudanteDescarga.DescargaChanged += new EventHandler <DescargaChangedEventArgs>(Descarga_DescargaChanged); if (Audio != null) { mContratoDeServicio.AyudanteAudio.StreamChanged += new EventHandler <StreamChangedEventArgs>(mContratoDeServicio_StreamAudioListener); mPseudoServidorAudioHttpListener = new PseudoServidorHttpListener(Utilidades.GetTextoTextbox(TextBoxNombreUsuario) + Utilidades.GetTextoTextbox(TextBoxNombreRed) + "/audio/", AudioBGWorkerThreadFinished, MensajeDeEstadoAudio, mContratoDeServicio.AyudanteAudio.IniciarStream, mContratoDeServicio.AyudanteAudio.TrozoStreamEnviado); } if (Video != null) { mContratoDeServicio.AyudanteVideo.StreamChanged += new EventHandler <StreamChangedEventArgs>(mContratoDeServicio_StreamVideoListener); mPseudoServidorVideoHttpListener = new PseudoServidorHttpListener(Utilidades.GetTextoTextbox(TextBoxNombreUsuario) + Utilidades.GetTextoTextbox(TextBoxNombreRed) + "/video/", VideoBGWorkerThreadFinished, MensajeDeEstadoVideo, mContratoDeServicio.AyudanteVideo.IniciarStream, mContratoDeServicio.AyudanteVideo.TrozoStreamEnviado); } while ((mPseudoServidorAudioHttpListener != null && mPseudoServidorVideoHttpListener != null) && (!(mPseudoServidorAudioHttpListener.EstaPseudoServidorPreparado || mPseudoServidorVideoHttpListener.EstaPseudoServidorPreparado))) { System.Threading.Thread.Sleep(0); } if (mPseudoServidorAudioHttpListener != null && mPseudoServidorVideoHttpListener != null) { while (mEstado == EstadoRed.Conectando) { System.Threading.Thread.Sleep(100); progressBarEstadoRed.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate { progressBarEstadoRed.Value = ((progressBarEstadoRed.Value >= 100) ? 0 : progressBarEstadoRed.Value + 1); }); if (mCanalP2PThreadBGWorker.CancellationPending) { progressBarEstadoRed.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate { progressBarEstadoRed.Value = 0; }); mCanalP2PThreadBGWorker = null; break; } } progressBarEstadoRed.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate { progressBarEstadoRed.Value = 100; }); while (true) { System.Threading.Thread.Sleep(1000); if (mCanalP2PThreadBGWorker.CancellationPending) { progressBarEstadoRed.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate { progressBarEstadoRed.Value = 0; }); mCanalP2PThreadBGWorker = null; break; } } } else { mEstado = EstadoRed.UsuarioFallido; Utilidades.SetTextoLabel(lblValorEstado, "Desconectando..."); Utilidades.SetTextoTextBox(TextBoxNombreUsuario, "USUARIO FALLIDO - Por favor, espere unos segundos..."); CerrarConexion(); mContratoDeServicio = null; mCanalP2PThreadBGWorker = null; MessageBox.Show("Lo sentimos en este momento su Nombre de Usuario ya existe en la red.\n" + "\nPor favor, vuelva a intentarlo con un Nombre de Usuario distinto.", "Nombre de Usuario actualmente en uso.", MessageBoxButton.OK, MessageBoxImage.Error); } }