diff --git a/Samples/MasterServerSample/MSClient/Form1.Designer.cs b/Samples/MasterServerSample/MSClient/Form1.Designer.cs new file mode 100644 index 0000000..ba0fe2b --- /dev/null +++ b/Samples/MasterServerSample/MSClient/Form1.Designer.cs @@ -0,0 +1,112 @@ +namespace MSClient +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.button1 = new System.Windows.Forms.Button(); + this.textBox1 = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.comboBox1 = new System.Windows.Forms.ComboBox(); + this.button2 = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(349, 10); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(165, 23); + this.button1.TabIndex = 1; + this.button1.Text = "Get host list"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // textBox1 + // + this.textBox1.Location = new System.Drawing.Point(163, 12); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(180, 22); + this.textBox1.TabIndex = 2; + this.textBox1.Text = "localhost"; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 15); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(118, 13); + this.label1.TabIndex = 3; + this.label1.Text = "Master server address"; + // + // comboBox1 + // + this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple; + this.comboBox1.FormattingEnabled = true; + this.comboBox1.Location = new System.Drawing.Point(15, 44); + this.comboBox1.Name = "comboBox1"; + this.comboBox1.Size = new System.Drawing.Size(499, 200); + this.comboBox1.TabIndex = 4; + // + // button2 + // + this.button2.Location = new System.Drawing.Point(241, 250); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(273, 23); + this.button2.TabIndex = 5; + this.button2.Text = "Request NAT punch thru from MS to this server"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(528, 289); + this.Controls.Add(this.button2); + this.Controls.Add(this.comboBox1); + this.Controls.Add(this.label1); + this.Controls.Add(this.textBox1); + this.Controls.Add(this.button1); + this.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.Name = "Form1"; + this.Text = "Client"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + public System.Windows.Forms.Button button1; + public System.Windows.Forms.TextBox textBox1; + public System.Windows.Forms.Label label1; + public System.Windows.Forms.ComboBox comboBox1; + public System.Windows.Forms.Button button2; + + } +} + diff --git a/Samples/MasterServerSample/MSClient/Form1.cs b/Samples/MasterServerSample/MSClient/Form1.cs new file mode 100644 index 0000000..4639c37 --- /dev/null +++ b/Samples/MasterServerSample/MSClient/Form1.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Windows.Forms; + +namespace MSClient +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + private void button1_Click(object sender, EventArgs e) + { + Program.GetServerList(textBox1.Text); + } + + private void button2_Click(object sender, EventArgs e) + { + string host = comboBox1.SelectedText; + Program.RequestNATIntroduction(host); + } + } +} diff --git a/Samples/MasterServerSample/MSClient/Form1.resx b/Samples/MasterServerSample/MSClient/Form1.resx new file mode 100644 index 0000000..ff31a6d --- /dev/null +++ b/Samples/MasterServerSample/MSClient/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Samples/MasterServerSample/MSClient/MSClient.csproj b/Samples/MasterServerSample/MSClient/MSClient.csproj new file mode 100644 index 0000000..ee849f5 --- /dev/null +++ b/Samples/MasterServerSample/MSClient/MSClient.csproj @@ -0,0 +1,93 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {E4FBEFAB-0D5F-4108-BAA9-E796E14AD6B2} + WinExe + Properties + MSClient + MSClient + v3.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + + + + + + + + Form + + + Form1.cs + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + {FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD} + Lidgren.Network + + + {DCD2BE10-1B14-4C80-8BD6-77A8B1F43346} + MSCommon + + + + + \ No newline at end of file diff --git a/Samples/MasterServerSample/MSClient/Program.cs b/Samples/MasterServerSample/MSClient/Program.cs new file mode 100644 index 0000000..b35b52a --- /dev/null +++ b/Samples/MasterServerSample/MSClient/Program.cs @@ -0,0 +1,100 @@ +using System; +using System.Net; +using System.Collections.Generic; +using System.Windows.Forms; + +using Lidgren.Network; + +using MSCommon; + +namespace MSClient +{ + static class Program + { + private static Form1 m_mainForm; + private static NetClient m_client; + private static IPEndPoint m_masterServer; + private static List m_hostList; + + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + m_mainForm = new Form1(); + + m_hostList = new List(); + + NetPeerConfiguration config = new NetPeerConfiguration("game"); + config.SetMessageTypeEnabled(NetIncomingMessageType.UnconnectedData, true); + m_client = new NetClient(config); + m_client.Start(); + + Application.Idle += new EventHandler(AppIdle); + Application.Run(m_mainForm); + } + + static void AppIdle(object sender, EventArgs e) + { + while (NativeMethods.AppStillIdle) + { + NetIncomingMessage inc; + while ((inc = m_client.ReadMessage()) != null) + { + switch (inc.MessageType) + { + case NetIncomingMessageType.UnconnectedData: + if (inc.SenderEndpoint.Equals(m_masterServer)) + { + // it's from the master server - must be a host + IPEndPoint hostInternal = inc.ReadIPEndpoint(); + IPEndPoint hostExternal = inc.ReadIPEndpoint(); + + m_hostList.Add(new IPEndPoint[] { hostInternal, hostExternal }); + + m_mainForm.comboBox1.Items.Add(hostExternal.ToString()); + } + break; + } + } + } + } + + public static void GetServerList(string masterServerAddress) + { + // + // Send request for server list to master server + // + m_masterServer = new IPEndPoint(NetUtility.Resolve(masterServerAddress), CommonConstants.MasterServerPort); + + NetOutgoingMessage listRequest = m_client.CreateMessage(); + listRequest.Write((byte)MasterServerMessageType.RequestHostList); + m_client.SendUnconnectedMessage(listRequest, m_masterServer); + } + + public static void RequestNATIntroduction(string host) + { + if (string.IsNullOrEmpty(host)) + { + MessageBox.Show("Select a host in the list first"); + return; + } + + if (m_masterServer == null) + throw new Exception("Must connect to master server first!"); + + NetOutgoingMessage om = m_client.CreateMessage(); + om.Write((byte)MasterServerMessageType.RequestIntroduction); + + // write internal ipendpoint + IPAddress mask; + om.Write(new IPEndPoint(NetUtility.GetMyAddress(out mask), m_client.Port)); + + // write external address of host to request introduction to + IPEndPoint hostEp = new IPEndPoint(NetUtility.Resolve(host), CommonConstants.GameServerPort); + om.Write(hostEp); + + m_client.SendUnconnectedMessage(om, m_masterServer); + } + } +} diff --git a/Samples/MasterServerSample/MSClient/Properties/AssemblyInfo.cs b/Samples/MasterServerSample/MSClient/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3b9df93 --- /dev/null +++ b/Samples/MasterServerSample/MSClient/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MSClient")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("MSClient")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2010")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("bedb3e73-43c8-4b0d-a982-528a08a745c6")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Samples/MasterServerSample/MSClient/Properties/Resources.Designer.cs b/Samples/MasterServerSample/MSClient/Properties/Resources.Designer.cs new file mode 100644 index 0000000..06f8527 --- /dev/null +++ b/Samples/MasterServerSample/MSClient/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.4927 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace MSClient.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MSClient.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/Samples/MasterServerSample/MSClient/Properties/Resources.resx b/Samples/MasterServerSample/MSClient/Properties/Resources.resx new file mode 100644 index 0000000..ffecec8 --- /dev/null +++ b/Samples/MasterServerSample/MSClient/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Samples/MasterServerSample/MSClient/Properties/Settings.Designer.cs b/Samples/MasterServerSample/MSClient/Properties/Settings.Designer.cs new file mode 100644 index 0000000..50261cc --- /dev/null +++ b/Samples/MasterServerSample/MSClient/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.4927 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace MSClient.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/Samples/MasterServerSample/MSClient/Properties/Settings.settings b/Samples/MasterServerSample/MSClient/Properties/Settings.settings new file mode 100644 index 0000000..abf36c5 --- /dev/null +++ b/Samples/MasterServerSample/MSClient/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Samples/MasterServerSample/MSCommon/CommonConstants.cs b/Samples/MasterServerSample/MSCommon/CommonConstants.cs new file mode 100644 index 0000000..8ba8ebc --- /dev/null +++ b/Samples/MasterServerSample/MSCommon/CommonConstants.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MSCommon +{ + public static class CommonConstants + { + public const int MasterServerPort = 14343; + public const int GameServerPort = 14242; + } +} diff --git a/Samples/MasterServerSample/MSCommon/MSCommon.csproj b/Samples/MasterServerSample/MSCommon/MSCommon.csproj new file mode 100644 index 0000000..4af659a --- /dev/null +++ b/Samples/MasterServerSample/MSCommon/MSCommon.csproj @@ -0,0 +1,57 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {DCD2BE10-1B14-4C80-8BD6-77A8B1F43346} + Library + Properties + MSCommon + MSCommon + v3.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Samples/MasterServerSample/MSCommon/MasterServerMessageType.cs b/Samples/MasterServerSample/MSCommon/MasterServerMessageType.cs new file mode 100644 index 0000000..1d6cf4d --- /dev/null +++ b/Samples/MasterServerSample/MSCommon/MasterServerMessageType.cs @@ -0,0 +1,11 @@ +using System; + +namespace MSCommon +{ + public enum MasterServerMessageType + { + RegisterHost, + RequestHostList, + RequestIntroduction, + } +} diff --git a/Samples/MasterServerSample/MSCommon/NativeMethods.cs b/Samples/MasterServerSample/MSCommon/NativeMethods.cs new file mode 100644 index 0000000..67bff25 --- /dev/null +++ b/Samples/MasterServerSample/MSCommon/NativeMethods.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +namespace MSCommon +{ + public static class NativeMethods + { + [DllImport("user32.dll", CharSet = CharSet.Auto)] + public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); + public const int WM_VSCROLL = 277; // Vertical scroll + public const int SB_BOTTOM = 7; // Scroll to bottom + + [StructLayout(LayoutKind.Sequential)] + public struct PeekMsg + { + public IntPtr hWnd; + public Message msg; + public IntPtr wParam; + public IntPtr lParam; + public uint time; + public System.Drawing.Point p; + } + + [System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously + [DllImport("User32.dll", CharSet = CharSet.Auto)] + public static extern bool PeekMessage(out PeekMsg msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags); + + public static bool AppStillIdle + { + get + { + PeekMsg msg; + return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0); + } + } + + public static void AppendText(RichTextBox box, string line) + { + try + { + box.AppendText(line + Environment.NewLine); + ScrollRichTextBox(box); + } + catch + { + } + } + + public static void ScrollRichTextBox(RichTextBox box) + { + if (box == null || box.IsDisposed || box.Disposing) + return; + SendMessage(box.Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, IntPtr.Zero); + } + } +} diff --git a/Samples/MasterServerSample/MSCommon/Properties/AssemblyInfo.cs b/Samples/MasterServerSample/MSCommon/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e8a399d --- /dev/null +++ b/Samples/MasterServerSample/MSCommon/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MSCommon")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("MSCommon")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2010")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("24cc031a-d470-439a-89f6-573e5ea1d969")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Samples/MasterServerSample/MasterServer/MasterServer.csproj b/Samples/MasterServerSample/MasterServer/MasterServer.csproj index 0130f57..ac03139 100644 --- a/Samples/MasterServerSample/MasterServer/MasterServer.csproj +++ b/Samples/MasterServerSample/MasterServer/MasterServer.csproj @@ -35,12 +35,6 @@ 3.5 - - 3.5 - - - 3.5 - @@ -53,6 +47,10 @@ {FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD} Lidgren.Network + + {DCD2BE10-1B14-4C80-8BD6-77A8B1F43346} + MSCommon +