diff --git a/Lidgren.Network/NetConnection.cs b/Lidgren.Network/NetConnection.cs index 556a64b..929d548 100644 --- a/Lidgren.Network/NetConnection.cs +++ b/Lidgren.Network/NetConnection.cs @@ -295,10 +295,10 @@ namespace Lidgren.Network { case NetDeliveryMethod.Unreliable: case NetDeliveryMethod.UnreliableSequenced: - chan = new NetUnreliableSenderChannel(this, NetConstants.UnreliableWindowSize); + chan = new NetUnreliableSenderChannel(this, NetUtility.GetWindowSize(method)); break; case NetDeliveryMethod.ReliableOrdered: - chan = new NetReliableSenderChannel(this, NetConstants.ReliableOrderedWindowSize); + chan = new NetReliableSenderChannel(this, NetUtility.GetWindowSize(method)); break; case NetDeliveryMethod.ReliableSequenced: case NetDeliveryMethod.ReliableUnordered: @@ -306,7 +306,7 @@ namespace Lidgren.Network // // TODO: this is placeholder! // - chan = new NetReliableSenderChannel(this, 64); + chan = new NetReliableSenderChannel(this, NetUtility.GetWindowSize(method)); break; } @@ -424,8 +424,8 @@ namespace Lidgren.Network var chan = m_sendChannels[channelSlot]; if (chan == null) { - windowSize = 0; - freeWindowSlots = 0; + windowSize = NetUtility.GetWindowSize(method); + freeWindowSlots = windowSize; return; } diff --git a/Lidgren.Network/NetConstants.cs b/Lidgren.Network/NetConstants.cs index a79b328..a2caa56 100644 --- a/Lidgren.Network/NetConstants.cs +++ b/Lidgren.Network/NetConstants.cs @@ -36,6 +36,7 @@ namespace Lidgren.Network internal const int UnreliableWindowSize = 128; internal const int ReliableOrderedWindowSize = 64; internal const int ReliableSequencedWindowSize = 64; + internal const int DefaultWindowSize = 64; internal const int MaxFragmentationGroups = ushort.MaxValue - 1; diff --git a/Lidgren.Network/NetUtility.cs b/Lidgren.Network/NetUtility.cs index 02eb3c2..b0d6128 100644 --- a/Lidgren.Network/NetUtility.cs +++ b/Lidgren.Network/NetUtility.cs @@ -283,6 +283,27 @@ namespace Lidgren.Network return retval; } + public static int GetWindowSize(NetDeliveryMethod method) + { + switch (method) + { + case NetDeliveryMethod.Unknown: + return 0; + + case NetDeliveryMethod.Unreliable: + case NetDeliveryMethod.UnreliableSequenced: + return NetConstants.UnreliableWindowSize; + + case NetDeliveryMethod.ReliableOrdered: + return NetConstants.ReliableOrderedWindowSize; + + case NetDeliveryMethod.ReliableSequenced: + case NetDeliveryMethod.ReliableUnordered: + default: + return NetConstants.DefaultWindowSize; + } + } + // shell sort internal static void SortMembersList(System.Reflection.MemberInfo[] list) { diff --git a/Samples/Chat/ChatClient/Form1.cs b/Samples/Chat/ChatClient/Form1.cs index c39d141..e026791 100644 --- a/Samples/Chat/ChatClient/Form1.cs +++ b/Samples/Chat/ChatClient/Form1.cs @@ -49,7 +49,7 @@ namespace ChatClient int port; Int32.TryParse(textBox3.Text, out port); Program.Connect(textBox2.Text, port); - button2.Text = "Shut down"; + button2.Text = "Disconnect"; } else { diff --git a/Samples/Chat/ChatClient/Program.cs b/Samples/Chat/ChatClient/Program.cs index 65c236c..bf0e3e6 100644 --- a/Samples/Chat/ChatClient/Program.cs +++ b/Samples/Chat/ChatClient/Program.cs @@ -27,6 +27,8 @@ namespace ChatClient Application.Idle += new EventHandler(Application_Idle); Application.Run(s_form); + + s_client.Shutdown("Bye"); } private static void Output(string text) @@ -87,7 +89,8 @@ namespace ChatClient // called by the UI public static void Shutdown() { - s_client.Shutdown("Requested by user"); + s_client.Disconnect("Requested by user"); + // s_client.Shutdown("Requested by user"); } public static void Send(string text) diff --git a/Samples/File stream sample/FileStreamClient/FileStreamClient.csproj b/Samples/File stream sample/FileStreamClient/FileStreamClient.csproj new file mode 100644 index 0000000..52a39c1 --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/FileStreamClient.csproj @@ -0,0 +1,94 @@ + + + + Debug + AnyCPU + 9.0.21022 + 2.0 + {5BEF5715-8B73-4A33-BE99-1B8B20542847} + WinExe + Properties + FileStreamClient + FileStreamClient + 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 + Designer + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + {AE483C29-042E-4226-BA52-D247CE7676DA} + Lidgren.Network + + + {773069DA-B66E-4667-ADCB-0D215AD8CF3E} + SamplesCommon + + + + + \ No newline at end of file diff --git a/Samples/File stream sample/FileStreamClient/Form1.Designer.cs b/Samples/File stream sample/FileStreamClient/Form1.Designer.cs new file mode 100644 index 0000000..0d3a1b7 --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/Form1.Designer.cs @@ -0,0 +1,102 @@ +namespace FileStreamClient +{ + 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.textBox1 = new System.Windows.Forms.TextBox(); + this.textBox2 = new System.Windows.Forms.TextBox(); + this.button1 = new System.Windows.Forms.Button(); + this.progressBar1 = new System.Windows.Forms.ProgressBar(); + this.SuspendLayout(); + // + // textBox1 + // + this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.textBox1.Location = new System.Drawing.Point(12, 12); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(162, 22); + this.textBox1.TabIndex = 0; + this.textBox1.Text = "localhost"; + // + // textBox2 + // + this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.textBox2.Location = new System.Drawing.Point(180, 12); + this.textBox2.Name = "textBox2"; + this.textBox2.Size = new System.Drawing.Size(69, 22); + this.textBox2.TabIndex = 1; + this.textBox2.Text = "14242"; + // + // button1 + // + this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.button1.Location = new System.Drawing.Point(255, 10); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 2; + this.button1.Text = "Connect"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // progressBar1 + // + this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.progressBar1.Location = new System.Drawing.Point(12, 42); + this.progressBar1.Name = "progressBar1"; + this.progressBar1.Size = new System.Drawing.Size(319, 23); + this.progressBar1.Step = 2; + this.progressBar1.TabIndex = 3; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(343, 74); + this.Controls.Add(this.progressBar1); + this.Controls.Add(this.button1); + this.Controls.Add(this.textBox2); + this.Controls.Add(this.textBox1); + 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 = "File stream client"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.TextBox textBox2; + private System.Windows.Forms.Button button1; + public System.Windows.Forms.ProgressBar progressBar1; + } +} + diff --git a/Samples/File stream sample/FileStreamClient/Form1.cs b/Samples/File stream sample/FileStreamClient/Form1.cs new file mode 100644 index 0000000..ee1d799 --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/Form1.cs @@ -0,0 +1,26 @@ +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 FileStreamClient +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + progressBar1.Minimum = 0; + progressBar1.Maximum = 100; + } + + private void button1_Click(object sender, EventArgs e) + { + Program.Connect(textBox1.Text, Int32.Parse(textBox2.Text)); + } + } +} diff --git a/Samples/File stream sample/FileStreamClient/Form1.resx b/Samples/File stream sample/FileStreamClient/Form1.resx new file mode 100644 index 0000000..ff31a6d --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/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/File stream sample/FileStreamClient/Program.cs b/Samples/File stream sample/FileStreamClient/Program.cs new file mode 100644 index 0000000..327a9b4 --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/Program.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Windows.Forms; + +using Lidgren.Network; +using SamplesCommon; +using System.IO; + +namespace FileStreamClient +{ + static class Program + { + private static Form1 s_form; + private static NetClient s_client; + private static ulong s_length; + private static ulong s_received; + private static FileStream s_writeStream; + private static int s_timeStarted; + + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + s_form = new Form1(); + + NetPeerConfiguration config = new NetPeerConfiguration("filestream"); + s_client = new NetClient(config); + s_client.Start(); + + Application.Idle += new EventHandler(AppLoop); + Application.Run(s_form); + + s_client.Shutdown("Application exiting"); + } + + static void AppLoop(object sender, EventArgs e) + { + while (NativeMethods.AppStillIdle) + { + NetIncomingMessage inc; + while ((inc = s_client.ReadMessage()) != null) + { + switch (inc.MessageType) + { + case NetIncomingMessageType.Data: + int chunkLen = inc.LengthBytes; + if (s_length == 0) + { + s_length = inc.ReadUInt64(); + string filename = inc.ReadString(); + s_writeStream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None); + s_timeStarted = Environment.TickCount; + break; + } + + byte[] all = inc.ReadBytes(inc.LengthBytes); + s_received += (ulong)all.Length; + s_writeStream.Write(all, 0, all.Length); + + int v = (int)(((float)s_received / (float)s_length) * 100.0f); + if (s_form.progressBar1.Value != v) + { + s_form.progressBar1.Value = v; + int passed = Environment.TickCount - s_timeStarted; + double psec = (double)passed / 1000.0; + double bps = (double)s_received / psec; + + s_form.Text = NetUtility.ToHumanReadable((long)bps) + " per second"; + } + + if (s_received >= s_length) + { + int passed = Environment.TickCount - s_timeStarted; + double psec = (double)passed / 1000.0; + double bps = (double)s_received / psec; + s_form.Text = "Done at " + NetUtility.ToHumanReadable((long)bps) + " per second"; + s_form.progressBar1.Value = 100; + + s_writeStream.Flush(); + s_writeStream.Close(); + s_writeStream.Dispose(); + + s_client.Disconnect("Everything received, bye!"); + } + break; + } + } + } + } + + internal static void Connect(string host, int port) + { + s_client.Connect(host, port); + } + } +} diff --git a/Samples/File stream sample/FileStreamClient/Properties/AssemblyInfo.cs b/Samples/File stream sample/FileStreamClient/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..245d8d5 --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/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("FileStreamClient")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("FileStreamClient")] +[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("f9faa103-5a10-4081-9dff-b1820a9127b9")] + +// 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/File stream sample/FileStreamClient/Properties/Resources.Designer.cs b/Samples/File stream sample/FileStreamClient/Properties/Resources.Designer.cs new file mode 100644 index 0000000..9886ff7 --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.4952 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace FileStreamClient.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("FileStreamClient.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/File stream sample/FileStreamClient/Properties/Resources.resx b/Samples/File stream sample/FileStreamClient/Properties/Resources.resx new file mode 100644 index 0000000..ffecec8 --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/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/File stream sample/FileStreamClient/Properties/Settings.Designer.cs b/Samples/File stream sample/FileStreamClient/Properties/Settings.Designer.cs new file mode 100644 index 0000000..59f8fbd --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.4952 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace FileStreamClient.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/File stream sample/FileStreamClient/Properties/Settings.settings b/Samples/File stream sample/FileStreamClient/Properties/Settings.settings new file mode 100644 index 0000000..abf36c5 --- /dev/null +++ b/Samples/File stream sample/FileStreamClient/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Samples/File stream sample/FileStreamSample.sln b/Samples/File stream sample/FileStreamSample.sln new file mode 100644 index 0000000..a42f61f --- /dev/null +++ b/Samples/File stream sample/FileStreamSample.sln @@ -0,0 +1,38 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileStreamServer", "FileStreamServer\FileStreamServer.csproj", "{0DB2F69A-5B5E-4CC4-9D31-C34BF86B2508}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lidgren.Network", "..\..\Lidgren.Network\Lidgren.Network.csproj", "{AE483C29-042E-4226-BA52-D247CE7676DA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SamplesCommon", "..\SamplesCommon\SamplesCommon.csproj", "{773069DA-B66E-4667-ADCB-0D215AD8CF3E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileStreamClient", "FileStreamClient\FileStreamClient.csproj", "{5BEF5715-8B73-4A33-BE99-1B8B20542847}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0DB2F69A-5B5E-4CC4-9D31-C34BF86B2508}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0DB2F69A-5B5E-4CC4-9D31-C34BF86B2508}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0DB2F69A-5B5E-4CC4-9D31-C34BF86B2508}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0DB2F69A-5B5E-4CC4-9D31-C34BF86B2508}.Release|Any CPU.Build.0 = Release|Any CPU + {AE483C29-042E-4226-BA52-D247CE7676DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AE483C29-042E-4226-BA52-D247CE7676DA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AE483C29-042E-4226-BA52-D247CE7676DA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AE483C29-042E-4226-BA52-D247CE7676DA}.Release|Any CPU.Build.0 = Release|Any CPU + {773069DA-B66E-4667-ADCB-0D215AD8CF3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {773069DA-B66E-4667-ADCB-0D215AD8CF3E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {773069DA-B66E-4667-ADCB-0D215AD8CF3E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {773069DA-B66E-4667-ADCB-0D215AD8CF3E}.Release|Any CPU.Build.0 = Release|Any CPU + {5BEF5715-8B73-4A33-BE99-1B8B20542847}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5BEF5715-8B73-4A33-BE99-1B8B20542847}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5BEF5715-8B73-4A33-BE99-1B8B20542847}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5BEF5715-8B73-4A33-BE99-1B8B20542847}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Samples/File stream sample/FileStreamServer/FileStreamServer.csproj b/Samples/File stream sample/FileStreamServer/FileStreamServer.csproj new file mode 100644 index 0000000..5cda840 --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/FileStreamServer.csproj @@ -0,0 +1,95 @@ + + + + Debug + AnyCPU + 9.0.21022 + 2.0 + {0DB2F69A-5B5E-4CC4-9D31-C34BF86B2508} + WinExe + Properties + FileStreamServer + FileStreamServer + 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 + Designer + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + {AE483C29-042E-4226-BA52-D247CE7676DA} + Lidgren.Network + + + {773069DA-B66E-4667-ADCB-0D215AD8CF3E} + SamplesCommon + + + + + \ No newline at end of file diff --git a/Samples/File stream sample/FileStreamServer/Form1.Designer.cs b/Samples/File stream sample/FileStreamServer/Form1.Designer.cs new file mode 100644 index 0000000..845c08a --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/Form1.Designer.cs @@ -0,0 +1,76 @@ +namespace FileStreamServer +{ + 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.richTextBox1 = new System.Windows.Forms.RichTextBox(); + this.SuspendLayout(); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(12, 12); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(175, 23); + this.button1.TabIndex = 0; + this.button1.Text = "Select file and start serving"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // richTextBox1 + // + this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.richTextBox1.Location = new System.Drawing.Point(12, 41); + this.richTextBox1.Name = "richTextBox1"; + this.richTextBox1.Size = new System.Drawing.Size(531, 193); + this.richTextBox1.TabIndex = 1; + this.richTextBox1.Text = ""; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(555, 246); + this.Controls.Add(this.richTextBox1); + 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 = "File stream server"; + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Button button1; + public System.Windows.Forms.RichTextBox richTextBox1; + } +} + diff --git a/Samples/File stream sample/FileStreamServer/Form1.cs b/Samples/File stream sample/FileStreamServer/Form1.cs new file mode 100644 index 0000000..de1e6d1 --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/Form1.cs @@ -0,0 +1,31 @@ +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 FileStreamServer +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + private void button1_Click(object sender, EventArgs e) + { + OpenFileDialog dlg = new OpenFileDialog(); + dlg.CheckFileExists = true; + var res = dlg.ShowDialog(); + + if (res != DialogResult.OK) + return; + + Program.Start(dlg.FileName); + } + } +} diff --git a/Samples/File stream sample/FileStreamServer/Form1.resx b/Samples/File stream sample/FileStreamServer/Form1.resx new file mode 100644 index 0000000..ff31a6d --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/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/File stream sample/FileStreamServer/Program.cs b/Samples/File stream sample/FileStreamServer/Program.cs new file mode 100644 index 0000000..e55d09a --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/Program.cs @@ -0,0 +1,88 @@ +using System; +using System.Windows.Forms; + +using Lidgren.Network; +using SamplesCommon; + +namespace FileStreamServer +{ + static class Program + { + private static Form1 s_form; + private static NetServer s_server; + private static string s_fileName; + + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + s_form = new Form1(); + + NetPeerConfiguration config = new NetPeerConfiguration("filestream"); + config.Port = 14242; + s_server = new NetServer(config); + + Application.Idle += new EventHandler(AppLoop); + Application.Run(s_form); + + s_server.Shutdown("Application exiting"); + } + + public static void Output(string str) + { + if (s_form != null && s_form.richTextBox1 != null) + NativeMethods.AppendText(s_form.richTextBox1, str); + } + + static void AppLoop(object sender, EventArgs e) + { + while (NativeMethods.AppStillIdle) + { + NetIncomingMessage inc; + while((inc = s_server.ReadMessage()) != null) + { + switch (inc.MessageType) + { + case NetIncomingMessageType.DebugMessage: + case NetIncomingMessageType.WarningMessage: + case NetIncomingMessageType.ErrorMessage: + case NetIncomingMessageType.VerboseDebugMessage: + Output(inc.ReadString()); + break; + case NetIncomingMessageType.StatusChanged: + NetConnectionStatus status = (NetConnectionStatus)inc.ReadByte(); + switch (status) + { + case NetConnectionStatus.Connected: + // start streaming to this client + inc.SenderConnection.Tag = new StreamingClient(inc.SenderConnection, s_fileName); + Output("Starting streaming to " + inc.SenderConnection); + break; + default: + Output(inc.SenderConnection + ": " + status + " (" + inc.ReadString() + ")"); + break; + } + break; + } + } + + // stream to all connections + foreach (NetConnection conn in s_server.Connections) + { + StreamingClient client = conn.Tag as StreamingClient; + if (client != null) + client.Heartbeat(); + } + + System.Threading.Thread.Sleep(0); + } + } + + internal static void Start(string filename) + { + s_fileName = filename; + s_server.Start(); + } + } +} diff --git a/Samples/File stream sample/FileStreamServer/Properties/AssemblyInfo.cs b/Samples/File stream sample/FileStreamServer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1889334 --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/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("FileStreamServer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("FileStreamServer")] +[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("a9d17bc2-072c-49ee-8ded-eb5193554a94")] + +// 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/File stream sample/FileStreamServer/Properties/Resources.Designer.cs b/Samples/File stream sample/FileStreamServer/Properties/Resources.Designer.cs new file mode 100644 index 0000000..a2750d0 --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.4952 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace FileStreamServer.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("FileStreamServer.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/File stream sample/FileStreamServer/Properties/Resources.resx b/Samples/File stream sample/FileStreamServer/Properties/Resources.resx new file mode 100644 index 0000000..ffecec8 --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/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/File stream sample/FileStreamServer/Properties/Settings.Designer.cs b/Samples/File stream sample/FileStreamServer/Properties/Settings.Designer.cs new file mode 100644 index 0000000..049dd78 --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.4952 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace FileStreamServer.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/File stream sample/FileStreamServer/Properties/Settings.settings b/Samples/File stream sample/FileStreamServer/Properties/Settings.settings new file mode 100644 index 0000000..abf36c5 --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Samples/File stream sample/FileStreamServer/StreamingClient.cs b/Samples/File stream sample/FileStreamServer/StreamingClient.cs new file mode 100644 index 0000000..59bed65 --- /dev/null +++ b/Samples/File stream sample/FileStreamServer/StreamingClient.cs @@ -0,0 +1,67 @@ +using System; +using Lidgren.Network; +using System.IO; + +namespace FileStreamServer +{ + public class StreamingClient + { + private FileStream m_inputStream; + private int m_sentOffset; + private int m_chunkLen; + private byte[] m_tmpBuffer; + private NetConnection m_connection; + + public StreamingClient(NetConnection conn, string fileName) + { + m_connection = conn; + m_inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + m_chunkLen = m_connection.Peer.Configuration.MaximumTransmissionUnit - 20; + m_tmpBuffer = new byte[m_chunkLen]; + m_sentOffset = 0; + } + + public void Heartbeat() + { + if (m_inputStream == null) + return; + + int windowSize, freeWindowSlots; + m_connection.GetSendQueueInfo(NetDeliveryMethod.ReliableOrdered, 1, out windowSize, out freeWindowSlots); + if (freeWindowSlots > 0) + { + // send another part of the file! + int remaining = (int)(m_inputStream.Length - m_sentOffset); + int sendBytes = (remaining > m_chunkLen ? m_chunkLen : remaining); + + // just assume we can read the whole thing in one Read() + m_inputStream.Read(m_tmpBuffer, 0, sendBytes); + + NetOutgoingMessage om; + if (m_sentOffset == 0) + { + // first message; send length, chunk length and file name + om = m_connection.Peer.CreateMessage(sendBytes + 8); + om.Write((ulong)m_inputStream.Length); + om.Write(Path.GetFileName(m_inputStream.Name)); + m_connection.SendMessage(om, NetDeliveryMethod.ReliableOrdered, 1); + } + + om = m_connection.Peer.CreateMessage(sendBytes + 8); + om.Write(m_tmpBuffer, 0, sendBytes); + + m_connection.SendMessage(om, NetDeliveryMethod.ReliableOrdered, 1); + m_sentOffset += sendBytes; + + //Program.Output("Sent " + m_sentOffset + "/" + m_inputStream.Length + " bytes to " + m_connection); + + if (remaining - sendBytes <= 0) + { + m_inputStream.Close(); + m_inputStream.Dispose(); + m_inputStream = null; + } + } + } + } +}