IPeerChannelWrapper mParticipant; //our interface to the channel public P2PLib() { mNodeName = mPassword = mNetworkName = null; mFactory = null; mParticipant = null; mDisposed = false; }
/// <summary> /// Async call back from beginning to open the graph. /// </summary> /// <param name="result">the result returned</param> void ProcessOpen(IAsyncResult result) { //if they canceled out while we were connecting the object will be disposed. //if so just do nothing. if (false == mDisposed) { try { mParticipant.EndOpen(result); } catch (TimeoutException) { System.Windows.MessageBox.Show(String.Format("Unable to join the Network because the maximum time allowed was exceeded")); return; } catch (Exception e) { if (null != e.InnerException) { System.Windows.MessageBox.Show(String.Format("Unable to join the network because: {0}", e.InnerException.Message)); } mParticipant = null; return; } mParticipant.Join(mNodeName); //make a service call so the other nodes know we have just joined. } }
public void Dispose() { if (mDisposed) { return; } try { if (mParticipant != null) { mParticipant.Leave(mNodeName); //mParticipant.Close(); //mParticipant.Abort(); } } catch (InvalidOperationException) { //safely ignore. This exception occurs when we are in the process of opening when they quit the application. //catch the exception to ignore it. } if (mFactory != null) { mFactory.Close(); } mParticipant.Abort(); mParticipant.Dispose(); mParticipant = null; mDisposed = true; }
public StreamingHelper(IPeerChannelWrapper participant, string nodeName, ShareStreamHandler s) { mNodeName = nodeName; streamedState = StreamedStateType.Initial; mChunk = 0; mLastChunk = -1; del = s; ////////////////////////////////////////////////////////////////////////// // Start up an Audio Watchdog timer so if we don't receive a packet after the hard-coded // 20000ms we'll timeout and assume the sending node was lost. ////////////////////////////////////////////////////////////////////////// mWatchDog = new Timer(new TimerCallback(Watchdog)); mWatchDog.Change(0, 20000);//every 20000ms get a callback }
/// <summary> /// The constructor for this. In addition to initializing all of the parameters it attempts to open and connect /// to the network. This is done asynchronously with ProcessOpen being called either after a timeout (hard coded to 1 minute) /// or when a response is received. /// </summary> /// <param name="nodeName">the unique nodename</param> /// <param name="networkName">the name of the network</param> /// <param name="password">the password for the network</param> public bool PeerChannelWrapperStart(String nodeName, String networkName, String password) { CheckDisposed(); ////////////////////////////////////////////////////////////////////////// //Initialize all the parameters... ////////////////////////////////////////////////////////////////////////// mNodeName = nodeName; mNetworkName = networkName; //password cannot be empty otherwise mFactory.CreateChannel() below will throw an exception if (string.IsNullOrEmpty(password)) { mPassword = "******"; } else { mPassword = password.Trim(); } //Construct InstanceContext to handle messages on callback interface. mInstanceContext = new InstanceContext(this); ////////////////////////////////////////////////////////////////////////// //Create the PeerTcpBinding ////////////////////////////////////////////////////////////////////////// NetPeerTcpBinding p2pBinding = new NetPeerTcpBinding(); p2pBinding.Name = "BindingDefault"; p2pBinding.Port = 0; //dynamic port p2pBinding.MaxReceivedMessageSize = 70000000; //set the max message size in bytes (70mb) p2pBinding.Resolver.Mode = PeerResolverMode.Pnrp; //use the standard pnrp for our resolver to find the network ////////////////////////////////////////////////////////////////////////// //create the Endpoint Address and Service Endpoint. ////////////////////////////////////////////////////////////////////////// String bindingInfo = "net.p2p://"; //the endpoint address for a NetPeerTcpBinding must start with net.p2p EndpointAddress epa = new EndpointAddress(String.Concat(bindingInfo, mNetworkName)); ServiceEndpoint serviceEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IPeerChannel)), p2pBinding, epa); ////////////////////////////////////////////////////////////////////////// // Create the participant with the given endpoint configuration // Each participant opens a duplex channel to the mesh // participant is an instance of the channel to the mesh ////////////////////////////////////////////////////////////////////////// mFactory = new DuplexChannelFactory <IPeerChannelWrapper>(mInstanceContext, serviceEndpoint); mFactory.Credentials.Peer.MeshPassword = mPassword; mParticipant = mFactory.CreateChannel(); mPicture = new PictureHelper(mParticipant.SharePicture, mNodeName); mTextPeerChannelHelper = new ChatHelper(mParticipant.ShareTextMessage); mFilePeerChannelHelper = new FileHelper(mParticipant.ShareFile); mStreamedAudio = new StreamingHelper(mParticipant, mNodeName, mParticipant.ShareStreamedAudio); mStreamedVideo = new StreamingHelper(mParticipant, mNodeName, mParticipant.ShareStreamedVideo); ////////////////////////////////////////////////////////////////////////// //Start an asynchronous call that will try to open a connection to the network. //if one is not found, but the pnrp resolver is running then a new network will be //created. ////////////////////////////////////////////////////////////////////////// try { AsyncCallback callBack = new AsyncCallback(ProcessOpen); TimeSpan ts = new TimeSpan(0, 1, 0); //wait a minute mParticipant.BeginOpen(ts, callBack, this); } catch (CommunicationException e) { System.Windows.MessageBox.Show(String.Format("Error while joining the network: {0}", e.Message)); mParticipant = null; return(false); } return(true); }