public ContratoDeServicio()
 {
     mNombreNodo             = mPassword = mNombreRed = null;
     mFabricaDeCanalesDuplex = null;
     mParticipanteP2P        = null;
     mDispose = false;
 }
        public bool IniciarCanalP2P(String nombreNodo, String nombreRed, String password)
        {
            CheckDispose();

            mNombreNodo = nombreNodo;
            mNombreRed  = nombreRed;
            mPassword   = password;

            if (string.IsNullOrEmpty(password.Trim()))
            {
                mPassword = "******";
            }
            else
            {
                mPassword = password.Trim();
            }

            mContextoDeInstancia = new InstanceContext(this);

            NetPeerTcpBinding p2pBinding = new NetPeerTcpBinding();

            p2pBinding.Name = "BindingDefault";
            p2pBinding.Port = 0;                          //puerto dinámico
            p2pBinding.MaxReceivedMessageSize = 70000000; //(70mb)
            p2pBinding.Resolver.Mode          = PeerResolverMode.Pnrp;

            String          bindingInfo     = "net.p2p://";
            EndpointAddress epa             = new EndpointAddress(String.Concat(bindingInfo, mNombreRed));
            ServiceEndpoint serviceEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IContratoDeServicio)), p2pBinding, epa);

            mFabricaDeCanalesDuplex = new DuplexChannelFactory <IContratoDeServicioCliente>(mContextoDeInstancia, serviceEndpoint);
            mFabricaDeCanalesDuplex.Credentials.Peer.MeshPassword = mPassword;
            mParticipanteP2P = mFabricaDeCanalesDuplex.CreateChannel();

            mAyudanteImagen   = new AyudanteImagen(mParticipanteP2P.ServicioGestionarImagen, mNombreNodo);
            mAyudanteChat     = new AyudanteChat(mParticipanteP2P.ServicioGestionarChat);
            mAyudanteFichero  = new AyudanteFichero(mParticipanteP2P.ServicioGestionarFichero);
            mAyudanteAudio    = new AyudanteStream(mParticipanteP2P, mNombreNodo, mParticipanteP2P.ServicioGestionarAudio);
            mAyudanteVideo    = new AyudanteStream(mParticipanteP2P, mNombreNodo, mParticipanteP2P.ServicioGestionarVideo);
            mAyudanteDescarga = new AyudanteDescarga(mParticipanteP2P.ServicioGestionarDescarga);

            try
            {
                AsyncCallback callBackAsync = new AsyncCallback(AbrirProceso);
                TimeSpan      ts            = new TimeSpan(0, 1, 0);
                mParticipanteP2P.BeginOpen(ts, callBackAsync, this);
            }
            catch (CommunicationException e)
            {
                System.Windows.MessageBox.Show(String.Format("Error al acceder a la red: {0}", e.Message));
                mParticipanteP2P = null;
                return(false);
            }
            return(true);
        }
        public AyudanteStream(IContratoDeServicioCliente participante, string nombreNodo, CompartirStreamHandler stream)
        {
            mNombreNodo     = nombreNodo;
            estadoStream    = EstadoStream.Inicial;
            mTrozo          = 0;
            mUltimoTrozo    = -1;
            compartirStream = stream;

            mTimer = new Timer(new TimerCallback(Vigilante));
            mTimer.Change(0, 20000);
        }
        void AbrirProceso(IAsyncResult resultAsync)
        {
            if (false == mDispose)
            {
                try
                {
                    mParticipanteP2P.EndOpen(resultAsync);
                }
                catch (TimeoutException)
                {
                    MessageBox.Show("Ha sido imposible inicializar la red. Excedido el tiempo máximo de espera permitido.\n" +
                                    "\nLo sentimos, la aplicación se cerrará.",
                                    "Error crítico.",
                                    MessageBoxButton.OK, MessageBoxImage.Error);

                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Action(() =>
                    {
                        Application.Current.Shutdown();
                    }));
                }
                catch (Exception e)
                {
                    if (null != e.InnerException)
                    {
                        MessageBox.Show(String.Format("Detectada conexión a Internet a través de Proxy: {0}\n" +
                                                      "\nLo sentimos, la aplicación se cerrará.",
                                                      e.InnerException.Message),
                                        "Error crítico.",
                                        MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    mParticipanteP2P = null;
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Action(() =>
                    {
                        Application.Current.Shutdown();
                    }));
                }

                if (mFabricaDeCanalesDuplex.State != CommunicationState.Faulted)
                {
                    if (mParticipanteP2P != null)
                    {
                        mParticipanteP2P.ServicioUnirseARed(mNombreNodo);
                    }
                }
            }
        }
        public void Dispose()
        {
            if (mDispose)
            {
                return;
            }

            try
            {
                if (mParticipanteP2P != null)
                {
                    mParticipanteP2P.ServicioAbandonarRed(mNombreNodo);
                }

                if (mFabricaDeCanalesDuplex != null)
                {
                    mFabricaDeCanalesDuplex.Close();
                }
            }
            catch (InvalidOperationException)
            {
            }
            catch (CommunicationObjectFaultedException)
            {
                //canal de comuniación en estado Faulted
                mFabricaDeCanalesDuplex.Abort();
            }
            finally
            {
                if (mParticipanteP2P != null)
                {
                    mParticipanteP2P.Abort();
                    mParticipanteP2P.Dispose();
                }
                mParticipanteP2P = null;
                mDispose         = true;
            }
        }