⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dplayconnect_joinform.cs

📁 《.NET游戏编程入门经典-c#篇》
💻 CS
📖 第 1 页 / 共 2 页
字号:
		this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
		// 
		// btnCancel
		// 
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Location = new System.Drawing.Point(333, 227);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(74, 27);
		this.btnCancel.TabIndex = 3;
		this.btnCancel.Text = "Cancel";
		// 
		// CreateJoinForm
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.CancelButton = this.btnCancel;
		this.ClientSize = new System.Drawing.Size(411, 264);
		this.ControlBox = false;
		this.Controls.AddRange(new System.Windows.Forms.Control[] {
																	  this.btnCreate,
																	  this.btnJoin,
																	  this.btnSearch,
																	  this.btnCancel,
																	  this.lstSession,
																	  this.label1});
		this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
		this.MaximizeBox = false;
		this.MinimizeBox = false;
		this.Name = "CreateJoinForm";
		this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
		this.Text = "Join or create a session";
		this.ResumeLayout(false);

	}
	#endregion




	/// <summary>
	/// The selected session was changed.
	/// </summary>
	private void SelectedChange(object sender, System.EventArgs e)
	{
		if (amJoining)
			return; // Do nothing if we are already joining a session

		btnJoin.Enabled = (lstSession.SelectedItem != null);
	}




	/// <summary>
	/// We either want to start or stop searching
	/// </summary>
	private void btnSearch_Click(object sender, System.EventArgs e)
	{
		if (!isSearching)
		{
			if( hostAddress != null )
				hostAddress.Dispose();

			hostAddress = new Address();
			hostAddress.ServiceProvider = deviceAddress.ServiceProvider;

			// See if we should prompt the user for the remote address
			if (ConnectWizard.ProviderRequiresPort(hostAddress.ServiceProvider))
			{
				AddressForm addressDialog = new AddressForm(connectionWizard.DefaultPort);
				addressDialog.ShowDialog(this);

				// If the user cancelled the address form, abort the search
				if (addressDialog.DialogResult != DialogResult.OK)
					return;

				// If a port was specified, add the component
				if (addressDialog.Hostname != "")
					hostAddress.AddComponent(Address.KeyHostname, addressDialog.Hostname);
                
				// If a hostname was specified, add the component
				if (addressDialog.Port > 0)
					hostAddress.AddComponent(Address.KeyPort, addressDialog.Port);
			}

			//Time to enum our hosts
			ApplicationDescription desc = new ApplicationDescription();
			desc.GuidApplication = connectionWizard.ApplicationGuid;
		    
			// If the user was not already asked for address information, DirectPlay
			// should prompt with native UI
			FindHostsFlags flags = 0;
			if (!ConnectWizard.ProviderRequiresPort(deviceAddress.ServiceProvider))
				flags = FindHostsFlags.OkToQueryForAddressing;

			peer.FindHosts(desc,hostAddress,deviceAddress,null,Timeout.Infinite,0,Timeout.Infinite, flags, out findHostHandle);
			isSearching = true;
			btnCreate.Enabled = false;
			btnSearch.Text = "Stop Search";
		}
		else
		{
			btnSearch.Text = "Stopping...";
			btnSearch.Enabled = false;
			if (findHostHandle != 0)
				peer.CancelAsyncOperation(findHostHandle);
		}
	}




	/// <summary>
	/// A form was disposed
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	public void FormDisposed(object sender, EventArgs e)
	{
		if (sender == createSessionForm)
			createSessionForm = null;
	}




	/// <summary>
	/// We should create a new session.  Display a dialog allowing the user to set
	/// certain options
	/// </summary>
	private void btnCreate_Click(object sender, System.EventArgs e)
	{
		this.DialogResult = DialogResult.None;
		((Button)sender).DialogResult = DialogResult.None;
		if (createSessionForm == null)
		{
			createSessionForm = new CreateSessionForm(peer,deviceAddress,connectionWizard);
			createSessionForm.Disposed += new System.EventHandler(this.FormDisposed);
		}
		if (createSessionForm.ShowDialog(this) != DialogResult.Cancel)
		{
			((Button)sender).DialogResult = DialogResult.Yes;
			this.DialogResult = DialogResult.Yes;
		}
	}




	/// <summary>
	/// Determine if we need to expire any of our currently listed servers
	/// </summary>
	private void UpdateTimer(object sender, System.Timers.ElapsedEventArgs e)
	{
		lock (foundHosts)
		{
			for (int i = 0; i< foundHosts.Count; i++)
			{
				// First check to see if this session has expired
				if (Environment.TickCount - ((FindHostsResponseInformation)foundHosts[i]).LastFoundTime > EnumExpireThreshold)
				{
					foundHosts.RemoveAt(i);
					break;
				}
			}
			for (int i = 0; i< lstSession.Items.Count; i++)
			{
				// First check to see if this session has expired
				if (Environment.TickCount - ((FindHostsResponseInformation)lstSession.Items[i]).LastFoundTime > EnumExpireThreshold)
				{
					lstSession.Items.RemoveAt(i);
					break;
				}
			}
		}
	}




	/// <summary>
	/// Wait for a connect to complete
	/// </summary>
	private void ConnectTimer(object sender, System.Timers.ElapsedEventArgs e)
	{
		if (connectEvent.WaitOne(0,false)) // Wait for the Connect event to be fired
		{
			if (isConnected) // Are we connected?
			{
				// Get rid of the timer
				if (updateListTimer != null)
					updateListTimer.Stop();

				if (connectTimer != null)
					connectTimer.Stop();

				this.DialogResult = DialogResult.OK;
				this.Close();
				return;
			}
			else
			{
				MessageBox.Show(this,"Failed to connect.  The error code was: \n" + resultCode.ToString() ,"Failed to connect",MessageBoxButtons.OK,MessageBoxIcon.Information);
				// Restart our timer
				updateListTimer.Start();
			}
		}
	}




	/// <summary>
	/// Fired when a connect complete message is received from DirectPlay.  Fire
	/// our event to notify the sample we've connected
	/// </summary>
	private void ConnectResult(object sender, ConnectCompleteEventArgs e)
	{
		if (e.Message.ResultCode == 0)
		{
			isConnected = true;
		}
		else
			isConnected = false;

		resultCode = e.Message.ResultCode;
		// Notify the timer that we've connected.
		connectEvent.Set();
	}




	/// <summary>
	/// Join an existing session
	/// </summary>
	private void btnJoin_Click(object sender, System.EventArgs e)
	{
		FindHostsResponseInformation dpInfo;
		if (lstSession.SelectedItem == null)
		{
			MessageBox.Show(this,"Please select a session before clicking join.","No session",MessageBoxButtons.OK,MessageBoxIcon.Information);
			return;
		}

		// Turn off all buttons
		btnCreate.Enabled = false;
		btnCancel.Enabled = false;
		btnJoin.Enabled = false;
		btnSearch.Enabled = false;
		amJoining = true;
		// Stop our secondary timer
		updateListTimer.Stop();
		dpInfo = ((FindHostsResponseInformation)lstSession.SelectedItem);
		connectionWizard.SetUserInfo();
		peer.Connect(dpInfo.ApplicationDesc,dpInfo.sender, dpInfo.device,null,ConnectFlags.OkToQueryForAddressing);
		// Now start our 'Connect' timer
		connectTimer.Start();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -