//----------------------------------------------------------------------------- // File: DPlayConnect.cs // // Desc: Application class for the DirectPlay samples framework. // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using System; using System.Collections; using System.Windows.Forms; using System.Threading; using System.Timers; using Microsoft.DirectX; using Microsoft.DirectX.DirectPlay; using System.Runtime.InteropServices; /// /// The connection wizard will allow our users to pick a service provider, /// and then connect to an existing application, or create a new one as the host. /// public class ConnectWizard { private Peer peerObject; private Address deviceAddress; private ChooseServiceProviderForm serviceProviderForm; private CreateJoinForm createJoinForm; private Guid serviceProviderGuid; private string username; private string samplename; private bool isInSession = false; private bool isHost = false; private int port = 0; private Guid applicationGuid; /// /// Constructor /// /// /// /// public ConnectWizard(Peer peer, Guid application, string sampleName) { peerObject = peer; applicationGuid = application; samplename = sampleName; } /// /// The guid for the current service provider /// public Guid ServiceProvider { get { return serviceProviderGuid; } set { serviceProviderGuid = value; } } /// /// The game's default port number /// public int DefaultPort { get { return port; } set { port = value; } } /// /// Am I the host /// public bool IsHost { get { return isHost; } set { isHost = value; } } /// /// The username /// public string Username { get { return username; } set { username = value; } } /// /// The sample name /// public string SampleName { get { return samplename; } } /// /// The applications guid /// public Guid ApplicationGuid { get { return applicationGuid; } } /// /// Are we in a session /// public bool InSession { get { return isInSession; } set { isInSession = value; } } /// /// Returns true if the given provider requires a port component /// /// ServiceProvider Guid /// static public bool ProviderRequiresPort(Guid provider) { return (provider != Address.ServiceProviderSerial && provider != Address.ServiceProviderModem && provider != Address.ServiceProviderBlueTooth); } /// /// Handler for when our form is disposed /// public void FormDisposed(object sender, EventArgs e) { if (sender == createJoinForm) createJoinForm = null; if (sender == serviceProviderForm) serviceProviderForm = null; } /// /// Set the user information /// public void SetUserInfo() { //Before we call host, let's actually call SetPeerInformation PlayerInformation myinformation = new PlayerInformation(); myinformation.Name = username; peerObject.SetPeerInformation(myinformation, SyncFlags.PeerInformation); } /// /// Show the service providers form /// /// True if a service provider was picked, false otherwise public bool DoShowServiceProviders() { if (serviceProviderForm == null) { username = null; serviceProviderForm = new ChooseServiceProviderForm(peerObject,this); serviceProviderForm.Disposed += new System.EventHandler(this.FormDisposed); } if (serviceProviderForm.ShowDialog() == DialogResult.OK) return true; // The didn't hit ok return false; } /// /// Show the create or join screen /// /// True if we will be in a session, false otherwise public bool DoCreateJoinGame() { if (deviceAddress != null) deviceAddress.Dispose(); deviceAddress = new Address(); if (createJoinForm != null) createJoinForm.Dispose(); createJoinForm = new CreateJoinForm(peerObject, deviceAddress, this); createJoinForm.Disposed += new System.EventHandler(this.FormDisposed); //Set the address's service provider (this will be the device address) deviceAddress.ServiceProvider = serviceProviderGuid; DialogResult drCreateJoin = createJoinForm.ShowDialog(); if (drCreateJoin == DialogResult.Cancel) return false; this.isHost = (drCreateJoin == DialogResult.Yes); return true; } /// /// Start the wizard /// /// True if we are in a session, false otherwise public bool StartWizard() { isInSession = false; while (this.DoShowServiceProviders()) { //Now let's create a game or join a session if (this.DoCreateJoinGame()) { // A game has been created or joined now, set our flag isInSession = true; break; } } return isInSession; } }