Browse Source

commit: Peer direct comm PoC success

Local 5 years ago
parent
commit
c80261ce1e
35 changed files with 820 additions and 464 deletions
  1. 6 0
      NuGet.config
  2. 1 1
      UdPunching.Common/ExchangeMessage.cs
  3. 11 3
      UdPunching.Common/ExtensionMethods.cs
  4. 0 24
      UdPunching.Example.Another/App.config
  5. 0 1
      UdPunching.Example.Another/PeerProgram.cs
  6. 0 36
      UdPunching.Example.Another/Properties/AssemblyInfo.cs
  7. 0 53
      UdPunching.Example.Another/Properties/Settings.Designer.cs
  8. 0 15
      UdPunching.Example.Another/Properties/Settings.settings
  9. 0 24
      UdPunching.Example/App.config
  10. 0 125
      UdPunching.Example/PeerProgram.cs
  11. 0 1
      UdPunching.Example/PeerPublicKey.txt
  12. 0 1
      UdPunching.Example/Peers/ab9ae069-1bed-4fce-8e6a-feabb8c519a7.txt
  13. 0 53
      UdPunching.Example/Properties/Settings.Designer.cs
  14. 0 15
      UdPunching.Example/Properties/Settings.settings
  15. 0 1
      UdPunching.Example/README.md
  16. 0 82
      UdPunching.Example/UdPunching.Example.csproj
  17. 6 0
      UdPunching.ExampleW/App.config
  18. 179 0
      UdPunching.ExampleW/ExampleForm.Designer.cs
  19. 177 0
      UdPunching.ExampleW/ExampleForm.cs
  20. 123 0
      UdPunching.ExampleW/ExampleForm.resx
  21. 0 0
      UdPunching.ExampleW/PeerPublicKey/26531b6b-1fa1-4c70-8a1e-7c1b579742a5.txt
  22. 0 0
      UdPunching.ExampleW/PeerPublicKey/ab9ae069-1bed-4fce-8e6a-feabb8c519a6.txt
  23. 0 0
      UdPunching.ExampleW/PrivateKey/26531b6b-1fa1-4c70-8a1e-7c1b579742a5.txt
  24. 0 0
      UdPunching.ExampleW/PrivateKey/ab9ae069-1bed-4fce-8e6a-feabb8c519a6.txt
  25. 22 0
      UdPunching.ExampleW/Program.cs
  26. 3 3
      UdPunching.Example/Properties/AssemblyInfo.cs
  27. 71 0
      UdPunching.ExampleW/Properties/Resources.Designer.cs
  28. 117 0
      UdPunching.ExampleW/Properties/Resources.resx
  29. 30 0
      UdPunching.ExampleW/Properties/Settings.Designer.cs
  30. 7 0
      UdPunching.ExampleW/Properties/Settings.settings
  31. 0 0
      UdPunching.ExampleW/README.md
  32. 40 14
      UdPunching.Example.Another/UdPunching.Example.Another.csproj
  33. 17 0
      UdPunching.Serv/UdPunching.Serv.csproj
  34. 4 0
      UdPunching.Serv/packages.config
  35. 6 12
      UdPunching.sln

+ 6 - 0
NuGet.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?> 
+<configuration> 
+  <config> 
+    <add key="repositorypath" value="C:\NuGetLocalRepo" /> 
+  </config> 
+</configuration>

+ 1 - 1
UdPunching.Common/ExchangeMessage.cs

@@ -72,7 +72,7 @@ namespace UdPunching
             if (null != PeerEndPoint)
             {
                 var buf = new byte[17];
-                PeerEndPoint.Address.ToZeroPaddingString().ToAsciiBytes(buf);
+                PeerEndPoint.Address.ToPaddingString().ToAsciiBytes(buf);
                 PeerEndPoint.Port.ToLe16Bytes(buf, 15);
                 dic[ExchangeMessageSection.PeerEndPoint] = buf;
             }

+ 11 - 3
UdPunching.Common/ExtensionMethods.cs

@@ -33,10 +33,18 @@ namespace UdPunching
         {
             var strAddress = buf.GetAsciiString(15, index);
             var port = buf.GetLeInt16(index + 15);
-            ipEndPoint = new IPEndPoint(IPAddress.Parse(strAddress), port);
+            ipEndPoint = new IPEndPoint(IPAddress.Parse(strAddress.Trim()), port);
             return 17;
         }
 
+        public static IPEndPoint ParseToIpEndPointV4(this string str)
+        {
+            var parts = str.Split(':');
+            if (2 != parts.Length) throw new ArgumentException("invalid addr port parts");
+            if (IPAddress.TryParse(parts[0], out var addr) && int.TryParse(parts[1], out var port)) return new IPEndPoint(addr, port);
+            throw new FormatException("parse fail");
+        }
+
         //--------- underlying -------------
 
         public static byte[] ToLeBytes(this int value)
@@ -81,10 +89,10 @@ namespace UdPunching
                 ;
         }
 
-        public static string ToZeroPaddingString(this IPAddress ipAddress)
+        public static string ToPaddingString(this IPAddress ipAddress)
         {
             if (ipAddress.AddressFamily != AddressFamily.InterNetwork) throw new NotSupportedException("Only ipv4");
-            return string.Join(".", ipAddress.ToString().Split('.').Select(p => p.PadLeft(3, '0')));
+            return ipAddress.ToString().PadRight(15, ' ');
         }
 
         public static byte[] ToAsciiBytes(this string str) => Encoding.ASCII.GetBytes(str);

+ 0 - 24
UdPunching.Example.Another/App.config

@@ -1,24 +0,0 @@
-<?xml version="1.0" encoding="utf-8" ?>
-<configuration>
-  <configSections>
-    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
-      <section name="UdPunching.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
-    </sectionGroup>
-  </configSections>
-  <startup>
-    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
-  </startup>
-  <applicationSettings>
-    <UdPunching.Properties.Settings>
-      <setting name="Port" serializeAs="String">
-        <value>23330</value>
-      </setting>
-      <setting name="Host" serializeAs="String">
-        <value>127.0.0.1</value>
-      </setting>
-      <setting name="PeerId" serializeAs="String">
-        <value>ab9ae069-1bed-4fce-8e6a-feabb8c519a6</value>
-      </setting>
-    </UdPunching.Properties.Settings>
-  </applicationSettings>
-</configuration>

+ 0 - 1
UdPunching.Example.Another/PeerProgram.cs

@@ -1 +0,0 @@
-../UdPunching.Example/PeerProgram.cs

+ 0 - 36
UdPunching.Example.Another/Properties/AssemblyInfo.cs

@@ -1,36 +0,0 @@
-using System.Reflection;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-
-// 有关程序集的一般信息由以下
-// 控制。更改这些特性值可修改
-// 与程序集关联的信息。
-[assembly: AssemblyTitle("UdPunching.Example.Another")]
-[assembly: AssemblyDescription("")]
-[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("")]
-[assembly: AssemblyProduct("UdPunching.Example.Another")]
-[assembly: AssemblyCopyright("Copyright ©  2019")]
-[assembly: AssemblyTrademark("")]
-[assembly: AssemblyCulture("")]
-
-// 将 ComVisible 设置为 false 会使此程序集中的类型
-//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
-//请将此类型的 ComVisible 特性设置为 true。
-[assembly: ComVisible(false)]
-
-// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
-[assembly: Guid("71c75ea7-2a2e-4d9d-8e98-58babe0fddf3")]
-
-// 程序集的版本信息由下列四个值组成: 
-//
-//      主版本
-//      次版本
-//      生成号
-//      修订号
-//
-//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
-//通过使用 "*",如下所示:
-// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]

+ 0 - 53
UdPunching.Example.Another/Properties/Settings.Designer.cs

@@ -1,53 +0,0 @@
-//------------------------------------------------------------------------------
-// <auto-generated>
-//     此代码由工具生成。
-//     运行时版本:4.0.30319.42000
-//
-//     对此文件的更改可能会导致不正确的行为,并且如果
-//     重新生成代码,这些更改将会丢失。
-// </auto-generated>
-//------------------------------------------------------------------------------
-
-namespace UdPunching.Properties {
-    
-    
-    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
-    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.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;
-            }
-        }
-        
-        [global::System.Configuration.ApplicationScopedSettingAttribute()]
-        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("23330")]
-        public int Port {
-            get {
-                return ((int)(this["Port"]));
-            }
-        }
-        
-        [global::System.Configuration.ApplicationScopedSettingAttribute()]
-        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("127.0.0.1")]
-        public string Host {
-            get {
-                return ((string)(this["Host"]));
-            }
-        }
-        
-        [global::System.Configuration.ApplicationScopedSettingAttribute()]
-        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("ab9ae069-1bed-4fce-8e6a-feabb8c519a7")]
-        public global::System.Guid PeerId {
-            get {
-                return ((global::System.Guid)(this["PeerId"]));
-            }
-        }
-    }
-}

+ 0 - 15
UdPunching.Example.Another/Properties/Settings.settings

@@ -1,15 +0,0 @@
-<?xml version='1.0' encoding='utf-8'?>
-<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="UdPunching.Properties" GeneratedClassName="Settings">
-  <Profiles />
-  <Settings>
-    <Setting Name="Port" Type="System.Int32" Scope="Application">
-      <Value Profile="(Default)">23330</Value>
-    </Setting>
-    <Setting Name="Host" Type="System.String" Scope="Application">
-      <Value Profile="(Default)">127.0.0.1</Value>
-    </Setting>
-    <Setting Name="PeerId" Type="System.Guid" Scope="Application">
-      <Value Profile="(Default)">ab9ae069-1bed-4fce-8e6a-feabb8c519a7</Value>
-    </Setting>
-  </Settings>
-</SettingsFile>

+ 0 - 24
UdPunching.Example/App.config

@@ -1,24 +0,0 @@
-<?xml version="1.0" encoding="utf-8" ?>
-<configuration>
-  <configSections>
-    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
-      <section name="UdPunching.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
-    </sectionGroup>
-  </configSections>
-  <startup>
-    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
-  </startup>
-  <applicationSettings>
-    <UdPunching.Properties.Settings>
-      <setting name="Port" serializeAs="String">
-        <value>23330</value>
-      </setting>
-      <setting name="Host" serializeAs="String">
-        <value>127.0.0.1</value>
-      </setting>
-      <setting name="PeerId" serializeAs="String">
-        <value>26531b6b-1fa1-4c70-8a1e-7c1b579742a5</value>
-      </setting>
-    </UdPunching.Properties.Settings>
-  </applicationSettings>
-</configuration>

+ 0 - 125
UdPunching.Example/PeerProgram.cs

@@ -1,125 +0,0 @@
-using System;
-using System.IO;
-using System.Net;
-using System.Net.Sockets;
-using System.Security.Cryptography;
-using System.Threading;
-
-namespace UdPunching
-{
-    internal static class PeerProgram
-    {
-        private static RSACryptoServiceProvider _serverRsa;
-        private static RSACryptoServiceProvider _peerRsa;
-        private static IPEndPoint _serverEndPoint;
-
-        private static int packetSeq;
-        private static Socket _peerSocket;
-
-        private static void ProcessPacket(SocketAsyncEventArgs sae)
-        {
-            ++packetSeq;
-            Console.WriteLine($"Incoming packet#{packetSeq:0,000,000} from {sae.RemoteEndPoint}, BytesTransferred {sae.BytesTransferred}");
-
-            var peerId = TransferCodec.ReadId(sae.Buffer);
-            Console.WriteLine($"Incoming packet#{packetSeq:0,000,000} peer id {peerId}");
-
-            if (sae.RemoteEndPoint.IpEndPointEqualsTo(_serverEndPoint))
-            {
-                if (TransferCodec.InvalidPeerId == peerId)
-                {
-                    Console.WriteLine($"Incoming packet#{packetSeq:0,000,000} Server FAILURE");
-                }
-                else if (Guid.Empty == peerId)
-                {
-                    var msgData = TransferCodec.DecodeData(_peerRsa, sae.Buffer);
-                    Console.WriteLine($"Incoming packet#{packetSeq:0,000,000} data length {msgData.Length}");
-
-                    var msg = new ExchangeMessage(msgData);
-
-                    Console.WriteLine($"Incoming packet#{packetSeq:0,000,000} flag {msg.Flags}");
-                    if (msg.Flags.HasFlag(ExchangeMessageFlags.EchoEndPoint)) Console.WriteLine($"Incoming packet#{packetSeq:0,000,000} **** echo endpoint {msg.PeerEndPoint}");
-                }
-                //TODO: handle server message
-            }
-            else
-            {
-                //TODO: handle peers message
-            }
-        }
-
-        private static void DeadLoopKeepAlive()
-        {
-            var buf = new byte[7];// 1flag,1count,1section,4timestamp
-            var msg = new ExchangeMessage();
-            msg.Flags = ExchangeMessageFlags.PeerKeepAlive;
-
-            while (true)
-            {
-                msg.PeerTimeStamp = DateTime.Now;
-                msg.WriteToBuffer(buf);
-                var encode = TransferCodec.Encode(_serverRsa, Properties.Settings.Default.PeerId, buf);
-
-                Console.WriteLine($"Sending packet KeepAlive {msg.PeerTimeStamp}");
-                _peerSocket.SendTo(encode, _serverEndPoint);
-
-                Thread.Sleep(1000);
-            }
-        }
-
-        private static void Main(string[] args)
-        {
-            Console.WriteLine("Init...");
-
-            _serverRsa = new RSACryptoServiceProvider();
-            _serverRsa.FromXmlString(File.ReadAllText("ServerPublicKey.txt"));
-            _peerRsa = new RSACryptoServiceProvider();
-            _peerRsa.FromXmlString(File.ReadAllText("PeerPrivateKey.txt"));
-
-            _serverEndPoint = new IPEndPoint(IPAddress.Parse(Properties.Settings.Default.Host), Properties.Settings.Default.Port);
-
-            _peerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-            _peerSocket.Bind(new IPEndPoint(IPAddress.Any, 0));
-            Console.WriteLine($"Peer Bind on {_peerSocket.LocalEndPoint}");
-
-            const int receiveBufferSize = 1500;
-
-            var sae = new SocketAsyncEventArgs();
-            sae.SetBuffer(new byte[receiveBufferSize], 0, receiveBufferSize);
-            sae.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
-
-            Console.WriteLine($"Receive buffer:{receiveBufferSize}");
-
-            void SaeOnCompleted(object sender, SocketAsyncEventArgs e)
-            {
-                if (e.SocketError == SocketError.Success)
-                {
-                    try
-                    {
-                        ProcessPacket(sae);
-                    }
-                    catch (Exception exception)
-                    {
-                        Console.WriteLine(exception);
-                    }
-                }
-                else
-                {
-                    Console.WriteLine($"ERROR:{e.SocketError}");
-                }
-
-                sae.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
-                if (false == _peerSocket.ReceiveFromAsync(sae)) SaeOnCompleted(null, null);
-            }
-
-            sae.Completed += SaeOnCompleted;
-
-            if (false == _peerSocket.ReceiveFromAsync(sae))
-            {
-                SaeOnCompleted(null, null);
-            }
-
-            DeadLoopKeepAlive();
-        }
-    }
-}

File diff suppressed because it is too large
+ 0 - 1
UdPunching.Example/PeerPublicKey.txt


File diff suppressed because it is too large
+ 0 - 1
UdPunching.Example/Peers/ab9ae069-1bed-4fce-8e6a-feabb8c519a7.txt


+ 0 - 53
UdPunching.Example/Properties/Settings.Designer.cs

@@ -1,53 +0,0 @@
-//------------------------------------------------------------------------------
-// <auto-generated>
-//     此代码由工具生成。
-//     运行时版本:4.0.30319.42000
-//
-//     对此文件的更改可能会导致不正确的行为,并且如果
-//     重新生成代码,这些更改将会丢失。
-// </auto-generated>
-//------------------------------------------------------------------------------
-
-namespace UdPunching.Properties {
-    
-    
-    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
-    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.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;
-            }
-        }
-        
-        [global::System.Configuration.ApplicationScopedSettingAttribute()]
-        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("23330")]
-        public int Port {
-            get {
-                return ((int)(this["Port"]));
-            }
-        }
-        
-        [global::System.Configuration.ApplicationScopedSettingAttribute()]
-        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("127.0.0.1")]
-        public string Host {
-            get {
-                return ((string)(this["Host"]));
-            }
-        }
-        
-        [global::System.Configuration.ApplicationScopedSettingAttribute()]
-        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("26531b6b-1fa1-4c70-8a1e-7c1b579742a5")]
-        public global::System.Guid PeerId {
-            get {
-                return ((global::System.Guid)(this["PeerId"]));
-            }
-        }
-    }
-}

+ 0 - 15
UdPunching.Example/Properties/Settings.settings

@@ -1,15 +0,0 @@
-<?xml version='1.0' encoding='utf-8'?>
-<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="UdPunching.Example.Properties" GeneratedClassName="Settings">
-  <Profiles />
-  <Settings>
-    <Setting Name="Port" Type="System.Int32" Scope="Application">
-      <Value Profile="(Default)">23330</Value>
-    </Setting>
-    <Setting Name="Host" Type="System.String" Scope="Application">
-      <Value Profile="(Default)">127.0.0.1</Value>
-    </Setting>
-    <Setting Name="PeerId" Type="System.Guid" Scope="Application">
-      <Value Profile="(Default)">26531b6b-1fa1-4c70-8a1e-7c1b579742a5</Value>
-    </Setting>
-  </Settings>
-</SettingsFile>

+ 0 - 1
UdPunching.Example/README.md

@@ -1 +0,0 @@
-# Don't forget reGenerate RSA key!

+ 0 - 82
UdPunching.Example/UdPunching.Example.csproj

@@ -1,82 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
-  <PropertyGroup>
-    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
-    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProjectGuid>{A7F567D6-1AF7-436C-82F4-AC97B5CA1F76}</ProjectGuid>
-    <OutputType>Exe</OutputType>
-    <RootNamespace>UdPunching</RootNamespace>
-    <AssemblyName>UdPunching.Example</AssemblyName>
-    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
-    <FileAlignment>512</FileAlignment>
-    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
-    <Deterministic>true</Deterministic>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
-    <PlatformTarget>AnyCPU</PlatformTarget>
-    <DebugSymbols>true</DebugSymbols>
-    <DebugType>full</DebugType>
-    <Optimize>false</Optimize>
-    <OutputPath>bin\Debug\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
-    <PlatformTarget>AnyCPU</PlatformTarget>
-    <DebugType>pdbonly</DebugType>
-    <Optimize>true</Optimize>
-    <OutputPath>bin\Release\</OutputPath>
-    <DefineConstants>TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-  </PropertyGroup>
-  <ItemGroup>
-    <Reference Include="System" />
-    <Reference Include="System.Core" />
-    <Reference Include="System.Xml.Linq" />
-    <Reference Include="System.Data.DataSetExtensions" />
-    <Reference Include="Microsoft.CSharp" />
-    <Reference Include="System.Data" />
-    <Reference Include="System.Net.Http" />
-    <Reference Include="System.Xml" />
-  </ItemGroup>
-  <ItemGroup>
-    <Compile Include="PeerProgram.cs" />
-    <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="Properties\Settings.Designer.cs">
-      <AutoGen>True</AutoGen>
-      <DesignTimeSharedInput>True</DesignTimeSharedInput>
-      <DependentUpon>Settings.settings</DependentUpon>
-    </Compile>
-  </ItemGroup>
-  <ItemGroup>
-    <None Include="App.config" />
-    <None Include="Properties\Settings.settings">
-      <Generator>SettingsSingleFileGenerator</Generator>
-      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
-    </None>
-    <None Include="README.md" />
-  </ItemGroup>
-  <ItemGroup>
-    <Content Include="..\UdPunching.Serv\ServerPublicKey.txt">
-      <Link>ServerPublicKey.txt</Link>
-      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </Content>
-    <Content Include="PeerPublicKey.txt" />
-    <Content Include="PeerPrivateKey.txt">
-      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </Content>
-    <Content Include="Peers\ab9ae069-1bed-4fce-8e6a-feabb8c519a7.txt">
-      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </Content>
-  </ItemGroup>
-  <ItemGroup>
-    <ProjectReference Include="..\UdPunching.Common\UdPunching.Common.csproj">
-      <Project>{abc87f5e-7118-4d19-8afc-b597baa06569}</Project>
-      <Name>UdPunching.Common</Name>
-    </ProjectReference>
-  </ItemGroup>
-  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
-</Project>

+ 6 - 0
UdPunching.ExampleW/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
+    </startup>
+</configuration>

+ 179 - 0
UdPunching.ExampleW/ExampleForm.Designer.cs

@@ -0,0 +1,179 @@
+namespace UdPunching.ExampleW
+{
+    partial class ExampleForm
+    {
+        /// <summary>
+        /// 必需的设计器变量。
+        /// </summary>
+        private System.ComponentModel.IContainer components = null;
+
+        /// <summary>
+        /// 清理所有正在使用的资源。
+        /// </summary>
+        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing && (components != null))
+            {
+                components.Dispose();
+            }
+            base.Dispose(disposing);
+        }
+
+        #region Windows 窗体设计器生成的代码
+
+        /// <summary>
+        /// 设计器支持所需的方法 - 不要修改
+        /// 使用代码编辑器修改此方法的内容。
+        /// </summary>
+        private void InitializeComponent()
+        {
+            this.components = new System.ComponentModel.Container();
+            this.StartButton = new System.Windows.Forms.Button();
+            this.ServerIEndPointTextBox = new System.Windows.Forms.TextBox();
+            this.StopButton = new System.Windows.Forms.Button();
+            this.PublicEndPointTextBox = new System.Windows.Forms.TextBox();
+            this.KeepAliveTimer = new System.Windows.Forms.Timer(this.components);
+            this.SendButton = new System.Windows.Forms.Button();
+            this.SendToEndPointTextBox = new System.Windows.Forms.TextBox();
+            this.SendContentTextBox = new System.Windows.Forms.TextBox();
+            this.RecvTextBox = new System.Windows.Forms.TextBox();
+            this.PeerKetyDropDown = new System.Windows.Forms.ComboBox();
+            this.StatusLabel = new System.Windows.Forms.Label();
+            this.SuspendLayout();
+            // 
+            // StartButton
+            // 
+            this.StartButton.Location = new System.Drawing.Point(155, 44);
+            this.StartButton.Name = "StartButton";
+            this.StartButton.Size = new System.Drawing.Size(75, 23);
+            this.StartButton.TabIndex = 0;
+            this.StartButton.Text = "Start";
+            this.StartButton.UseVisualStyleBackColor = true;
+            this.StartButton.Click += new System.EventHandler(this.StartButton_Click);
+            // 
+            // ServerIEndPointTextBox
+            // 
+            this.ServerIEndPointTextBox.Location = new System.Drawing.Point(12, 44);
+            this.ServerIEndPointTextBox.Name = "ServerIEndPointTextBox";
+            this.ServerIEndPointTextBox.Size = new System.Drawing.Size(137, 21);
+            this.ServerIEndPointTextBox.TabIndex = 1;
+            this.ServerIEndPointTextBox.Text = "127.111.111.111:23330";
+            // 
+            // StopButton
+            // 
+            this.StopButton.Enabled = false;
+            this.StopButton.Location = new System.Drawing.Point(236, 44);
+            this.StopButton.Name = "StopButton";
+            this.StopButton.Size = new System.Drawing.Size(75, 23);
+            this.StopButton.TabIndex = 0;
+            this.StopButton.Text = "Stop";
+            this.StopButton.UseVisualStyleBackColor = true;
+            this.StopButton.Click += new System.EventHandler(this.StopButton_Click);
+            // 
+            // PublicEndPointTextBox
+            // 
+            this.PublicEndPointTextBox.Location = new System.Drawing.Point(317, 46);
+            this.PublicEndPointTextBox.Name = "PublicEndPointTextBox";
+            this.PublicEndPointTextBox.ReadOnly = true;
+            this.PublicEndPointTextBox.Size = new System.Drawing.Size(183, 21);
+            this.PublicEndPointTextBox.TabIndex = 3;
+            this.PublicEndPointTextBox.Text = "???.???.???.???:?????";
+            // 
+            // KeepAliveTimer
+            // 
+            this.KeepAliveTimer.Interval = 1000;
+            this.KeepAliveTimer.Tick += new System.EventHandler(this.KeepAliveTimer_Tick);
+            // 
+            // SendButton
+            // 
+            this.SendButton.Enabled = false;
+            this.SendButton.Location = new System.Drawing.Point(155, 71);
+            this.SendButton.Name = "SendButton";
+            this.SendButton.Size = new System.Drawing.Size(75, 23);
+            this.SendButton.TabIndex = 0;
+            this.SendButton.Text = "Send";
+            this.SendButton.UseVisualStyleBackColor = true;
+            this.SendButton.Click += new System.EventHandler(this.SendButton_Click);
+            // 
+            // SendToEndPointTextBox
+            // 
+            this.SendToEndPointTextBox.Location = new System.Drawing.Point(12, 71);
+            this.SendToEndPointTextBox.Name = "SendToEndPointTextBox";
+            this.SendToEndPointTextBox.Size = new System.Drawing.Size(137, 21);
+            this.SendToEndPointTextBox.TabIndex = 1;
+            this.SendToEndPointTextBox.Text = "???.???.???.???:00000";
+            // 
+            // SendContentTextBox
+            // 
+            this.SendContentTextBox.Location = new System.Drawing.Point(12, 100);
+            this.SendContentTextBox.Multiline = true;
+            this.SendContentTextBox.Name = "SendContentTextBox";
+            this.SendContentTextBox.Size = new System.Drawing.Size(241, 130);
+            this.SendContentTextBox.TabIndex = 4;
+            // 
+            // RecvTextBox
+            // 
+            this.RecvTextBox.Location = new System.Drawing.Point(259, 100);
+            this.RecvTextBox.Multiline = true;
+            this.RecvTextBox.Name = "RecvTextBox";
+            this.RecvTextBox.Size = new System.Drawing.Size(241, 130);
+            this.RecvTextBox.TabIndex = 4;
+            // 
+            // PeerKetyDropDown
+            // 
+            this.PeerKetyDropDown.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
+            this.PeerKetyDropDown.FormattingEnabled = true;
+            this.PeerKetyDropDown.Location = new System.Drawing.Point(12, 12);
+            this.PeerKetyDropDown.Name = "PeerKetyDropDown";
+            this.PeerKetyDropDown.Size = new System.Drawing.Size(488, 20);
+            this.PeerKetyDropDown.TabIndex = 5;
+            // 
+            // StatusLabel
+            // 
+            this.StatusLabel.AutoSize = true;
+            this.StatusLabel.Location = new System.Drawing.Point(293, 76);
+            this.StatusLabel.Name = "StatusLabel";
+            this.StatusLabel.Size = new System.Drawing.Size(41, 12);
+            this.StatusLabel.TabIndex = 6;
+            this.StatusLabel.Text = "Ready.";
+            // 
+            // ExampleForm
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.ClientSize = new System.Drawing.Size(520, 250);
+            this.Controls.Add(this.StatusLabel);
+            this.Controls.Add(this.PeerKetyDropDown);
+            this.Controls.Add(this.RecvTextBox);
+            this.Controls.Add(this.SendContentTextBox);
+            this.Controls.Add(this.PublicEndPointTextBox);
+            this.Controls.Add(this.SendToEndPointTextBox);
+            this.Controls.Add(this.ServerIEndPointTextBox);
+            this.Controls.Add(this.StopButton);
+            this.Controls.Add(this.SendButton);
+            this.Controls.Add(this.StartButton);
+            this.Name = "ExampleForm";
+            this.Text = "UdPoi Example";
+            this.Shown += new System.EventHandler(this.ExampleForm_Shown);
+            this.ResumeLayout(false);
+            this.PerformLayout();
+
+        }
+
+        #endregion
+
+        private System.Windows.Forms.Button StartButton;
+        private System.Windows.Forms.TextBox ServerIEndPointTextBox;
+        private System.Windows.Forms.Button StopButton;
+        private System.Windows.Forms.TextBox PublicEndPointTextBox;
+        private System.Windows.Forms.Timer KeepAliveTimer;
+        private System.Windows.Forms.Button SendButton;
+        private System.Windows.Forms.TextBox SendToEndPointTextBox;
+        private System.Windows.Forms.TextBox SendContentTextBox;
+        private System.Windows.Forms.TextBox RecvTextBox;
+        private System.Windows.Forms.ComboBox PeerKetyDropDown;
+        private System.Windows.Forms.Label StatusLabel;
+    }
+}
+

+ 177 - 0
UdPunching.ExampleW/ExampleForm.cs

@@ -0,0 +1,177 @@
+using System;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Security.Cryptography;
+using System.Text;
+using System.Windows.Forms;
+
+namespace UdPunching.ExampleW
+{
+    public partial class ExampleForm : Form
+    {
+        private const int ReceiveBufferSize = 1500;
+
+        private static readonly IPEndPoint AnyEndPoint = new IPEndPoint(IPAddress.Any, 0);
+
+        private IPEndPoint _serverEndPoint;
+        private RSACryptoServiceProvider _serverRsa;
+        private SocketAsyncEventArgs _saeRecv;
+
+        private Guid _peerId;
+        private RSACryptoServiceProvider _peerRsa;
+
+        private Socket _peerSocket;
+
+        public ExampleForm()
+        {
+            InitializeComponent();
+        }
+
+        private void ExampleForm_Shown(object sender, EventArgs e)
+        {
+            _serverRsa = new RSACryptoServiceProvider();
+            _serverRsa.FromXmlString(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ServerPublicKey.txt")));
+
+            var files = Directory
+                .GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PrivateKey"))
+                .Select(Path.GetFileName)
+                .ToArray();
+
+            PeerKetyDropDown.DataSource = files;
+        }
+
+        private void StartButton_Click(object sender, EventArgs e)
+        {
+            _serverEndPoint = ServerIEndPointTextBox.Text.ParseToIpEndPointV4();
+
+            _peerRsa = new RSACryptoServiceProvider();
+            var peerPrivateKeyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PrivateKey", PeerKetyDropDown.Text);
+            _peerRsa.FromXmlString(
+                File.ReadAllText(
+                    peerPrivateKeyPath
+                )
+            );
+            _peerId = new Guid(Path.GetFileNameWithoutExtension(peerPrivateKeyPath));
+
+            _peerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+            _peerSocket.Bind(AnyEndPoint);
+
+            _saeRecv = new SocketAsyncEventArgs();
+            _saeRecv.SetBuffer(new byte[ReceiveBufferSize], 0, ReceiveBufferSize);
+            _saeRecv.Completed += _saeRecv_Completed;
+            BeginRecv();
+
+            KeepAliveTimer.Start();
+
+            StartButton.Enabled = false;
+            StopButton.Enabled = true;
+            SendButton.Enabled = true;
+        }
+
+        private void StopButton_Click(object sender, EventArgs e)
+        {
+            KeepAliveTimer.Stop();
+            _peerSocket.Dispose();
+            _saeRecv.Dispose();
+            _peerRsa.Dispose();
+
+            StartButton.Enabled = true;
+            StopButton.Enabled = false;
+            SendButton.Enabled = false;
+        }
+
+        private void _saeRecv_Completed(object sender, SocketAsyncEventArgs e)
+        {
+            if (e.SocketError == SocketError.Success)
+            {
+                try
+                {
+                    ProcessPacket();
+                    if (false == _peerSocket.ReceiveFromAsync(_saeRecv)) _saeRecv_Completed(null, null);
+                }
+                catch (Exception exception)
+                {
+                    Invoke(new Action(() =>
+                    {
+                        StatusLabel.Text = $"ERROR:{exception.Message}";
+                        StopButton_Click(null, null);
+                    }));
+                }
+            }
+            else
+            {
+                Invoke(new Action(() =>
+                {
+                    StatusLabel.Text = $"ERROR:{e.SocketError}";
+                    StopButton_Click(null, null);
+                }));
+            }
+        }
+
+        private void ProcessPacket()
+        {
+            var peerId = TransferCodec.ReadId(_saeRecv.Buffer);
+            if (_saeRecv.RemoteEndPoint.IpEndPointEqualsTo(_serverEndPoint))
+            {
+                if (TransferCodec.InvalidPeerId == peerId)
+                {
+                    Invoke(new Action(() => { StatusLabel.Text = "Server FAILURE"; }));
+                }
+                else if (Guid.Empty == peerId)
+                {
+                    var msgData = TransferCodec.DecodeData(_peerRsa, _saeRecv.Buffer);
+
+                    var msg = new ExchangeMessage(msgData);
+
+                    if (msg.Flags.HasFlag(ExchangeMessageFlags.EchoEndPoint))
+                    {
+                        Invoke(new Action(() =>
+                        {
+                            PublicEndPointTextBox.Text = msg.PeerEndPoint.ToString();
+                        }));
+                    }
+                }
+            }
+            else
+            {
+                Invoke(new Action(() =>
+                {
+                    RecvTextBox.Text =
+                        $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {_saeRecv.RemoteEndPoint}"
+                        + $"{Environment.NewLine} {Encoding.UTF8.GetString(_saeRecv.Buffer, 0, _saeRecv.BytesTransferred)}"
+                        + $"{Environment.NewLine}"
+                        + RecvTextBox.Text;
+                }));
+            }
+        }
+
+        private void BeginRecv()
+        {
+            _saeRecv.RemoteEndPoint = AnyEndPoint;
+            if (false == _peerSocket.ReceiveFromAsync(_saeRecv))
+            {
+                _saeRecv_Completed(null, _saeRecv);
+            }
+        }
+
+        private readonly byte[] _buf = new byte[7];// 1flag,1count,1section,4timestamp
+        private readonly ExchangeMessage _msg = new ExchangeMessage { Flags = ExchangeMessageFlags.PeerKeepAlive };
+
+        private void KeepAliveTimer_Tick(object sender, EventArgs e)
+        {
+            _msg.PeerTimeStamp = DateTime.Now;
+            _msg.WriteToBuffer(_buf);
+            StatusLabel.Text = "Keeping alive...";
+            var encode = TransferCodec.Encode(_serverRsa, _peerId, _buf);
+            _peerSocket.SendTo(encode, _serverEndPoint);
+        }
+
+        private void SendButton_Click(object sender, EventArgs e)
+        {
+            var to = SendToEndPointTextBox.Text.ParseToIpEndPointV4();
+            var sent = _peerSocket.SendTo(Encoding.UTF8.GetBytes(SendContentTextBox.Text), to);
+        }
+    }
+}

+ 123 - 0
UdPunching.ExampleW/ExampleForm.resx

@@ -0,0 +1,123 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <metadata name="KeepAliveTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>17, 17</value>
+  </metadata>
+</root>

UdPunching.Example.Another/Peers/26531b6b-1fa1-4c70-8a1e-7c1b579742a5.txt → UdPunching.ExampleW/PeerPublicKey/26531b6b-1fa1-4c70-8a1e-7c1b579742a5.txt


UdPunching.Example.Another/PeerPublicKey.txt → UdPunching.ExampleW/PeerPublicKey/ab9ae069-1bed-4fce-8e6a-feabb8c519a6.txt


UdPunching.Example/PeerPrivateKey.txt → UdPunching.ExampleW/PrivateKey/26531b6b-1fa1-4c70-8a1e-7c1b579742a5.txt


UdPunching.Example.Another/PeerPrivateKey.txt → UdPunching.ExampleW/PrivateKey/ab9ae069-1bed-4fce-8e6a-feabb8c519a6.txt


+ 22 - 0
UdPunching.ExampleW/Program.cs

@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace UdPunching.ExampleW
+{
+    static class Program
+    {
+        /// <summary>
+        /// 应用程序的主入口点。
+        /// </summary>
+        [STAThread]
+        static void Main()
+        {
+            Application.EnableVisualStyles();
+            Application.SetCompatibleTextRenderingDefault(false);
+            Application.Run(new ExampleForm());
+        }
+    }
+}

+ 3 - 3
UdPunching.Example/Properties/AssemblyInfo.cs

@@ -5,11 +5,11 @@ using System.Runtime.InteropServices;
 // 有关程序集的一般信息由以下
 // 控制。更改这些特性值可修改
 // 与程序集关联的信息。
-[assembly: AssemblyTitle("UdPunching.Example")]
+[assembly: AssemblyTitle("UdPunching.ExampleW")]
 [assembly: AssemblyDescription("")]
 [assembly: AssemblyConfiguration("")]
 [assembly: AssemblyCompany("")]
-[assembly: AssemblyProduct("UdPunching.Example")]
+[assembly: AssemblyProduct("UdPunching.ExampleW")]
 [assembly: AssemblyCopyright("Copyright ©  2019")]
 [assembly: AssemblyTrademark("")]
 [assembly: AssemblyCulture("")]
@@ -20,7 +20,7 @@ using System.Runtime.InteropServices;
 [assembly: ComVisible(false)]
 
 // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
-[assembly: Guid("a7f567d6-1af7-436c-82f4-ac97b5ca1f76")]
+[assembly: Guid("e34269b6-06ea-4f48-aa62-c8079558664b")]
 
 // 程序集的版本信息由下列四个值组成: 
 //

+ 71 - 0
UdPunching.ExampleW/Properties/Resources.Designer.cs

@@ -0,0 +1,71 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+//     此代码由工具生成。
+//     运行时版本: 4.0.30319.42000
+//
+//     对此文件的更改可能导致不正确的行为,如果
+//     重新生成代码,则所做更改将丢失。
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace UdPunching.ExampleW.Properties
+{
+
+
+    /// <summary>
+    ///   强类型资源类,用于查找本地化字符串等。
+    /// </summary>
+    // 此类是由 StronglyTypedResourceBuilder
+    // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
+    // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen
+    // (以 /str 作为命令选项),或重新生成 VS 项目。
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.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()
+        {
+        }
+
+        /// <summary>
+        ///   返回此类使用的缓存 ResourceManager 实例。
+        /// </summary>
+        [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("UdPunching.ExampleW.Properties.Resources", typeof(Resources).Assembly);
+                    resourceMan = temp;
+                }
+                return resourceMan;
+            }
+        }
+
+        /// <summary>
+        ///   覆盖当前线程的 CurrentUICulture 属性
+        ///   使用此强类型的资源类的资源查找。
+        /// </summary>
+        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+        internal static global::System.Globalization.CultureInfo Culture
+        {
+            get
+            {
+                return resourceCulture;
+            }
+            set
+            {
+                resourceCulture = value;
+            }
+        }
+    }
+}

+ 117 - 0
UdPunching.ExampleW/Properties/Resources.resx

@@ -0,0 +1,117 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+</root>

+ 30 - 0
UdPunching.ExampleW/Properties/Settings.Designer.cs

@@ -0,0 +1,30 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+//     This code was generated by a tool.
+//     Runtime Version:4.0.30319.42000
+//
+//     Changes to this file may cause incorrect behavior and will be lost if
+//     the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace UdPunching.ExampleW.Properties
+{
+
+
+    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.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;
+            }
+        }
+    }
+}

+ 7 - 0
UdPunching.ExampleW/Properties/Settings.settings

@@ -0,0 +1,7 @@
+<?xml version='1.0' encoding='utf-8'?>
+<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
+  <Profiles>
+    <Profile Name="(Default)" />
+  </Profiles>
+  <Settings />
+</SettingsFile>

UdPunching.Example.Another/README.md → UdPunching.ExampleW/README.md


+ 40 - 14
UdPunching.Example.Another/UdPunching.Example.Another.csproj

@@ -4,10 +4,10 @@
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProjectGuid>{71C75EA7-2A2E-4D9D-8E98-58BABE0FDDF3}</ProjectGuid>
-    <OutputType>Exe</OutputType>
-    <RootNamespace>UdPunching</RootNamespace>
-    <AssemblyName>UdPunching.Example.Another</AssemblyName>
+    <ProjectGuid>{E34269B6-06EA-4F48-AA62-C8079558664B}</ProjectGuid>
+    <OutputType>WinExe</OutputType>
+    <RootNamespace>UdPunching.ExampleW</RootNamespace>
+    <AssemblyName>UdPunching.ExampleW</AssemblyName>
     <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
@@ -39,27 +39,48 @@
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="System.Data" />
+    <Reference Include="System.Deployment" />
+    <Reference Include="System.Drawing" />
     <Reference Include="System.Net.Http" />
+    <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="PeerProgram.cs" />
+    <Compile Include="ExampleForm.cs">
+      <SubType>Form</SubType>
+    </Compile>
+    <Compile Include="ExampleForm.Designer.cs">
+      <DependentUpon>ExampleForm.cs</DependentUpon>
+    </Compile>
+    <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="Properties\Settings.Designer.cs">
-      <DependentUpon>Settings.settings</DependentUpon>
+    <EmbeddedResource Include="ExampleForm.resx">
+      <DependentUpon>ExampleForm.cs</DependentUpon>
+    </EmbeddedResource>
+    <EmbeddedResource Include="Properties\Resources.resx">
+      <Generator>ResXFileCodeGenerator</Generator>
+      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
+      <SubType>Designer</SubType>
+    </EmbeddedResource>
+    <Compile Include="Properties\Resources.Designer.cs">
       <AutoGen>True</AutoGen>
-      <DesignTimeSharedInput>True</DesignTimeSharedInput>
+      <DependentUpon>Resources.resx</DependentUpon>
     </Compile>
-  </ItemGroup>
-  <ItemGroup>
-    <None Include="App.config" />
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
     </None>
+    <Compile Include="Properties\Settings.Designer.cs">
+      <AutoGen>True</AutoGen>
+      <DependentUpon>Settings.settings</DependentUpon>
+      <DesignTimeSharedInput>True</DesignTimeSharedInput>
+    </Compile>
     <None Include="README.md" />
   </ItemGroup>
   <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <ItemGroup>
     <ProjectReference Include="..\UdPunching.Common\UdPunching.Common.csproj">
       <Project>{abc87f5e-7118-4d19-8afc-b597baa06569}</Project>
       <Name>UdPunching.Common</Name>
@@ -71,11 +92,16 @@
       <Link>ServerPublicKey.txt</Link>
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
-    <Content Include="PeerPrivateKey.txt">
+    <Content Include="PrivateKey\ab9ae069-1bed-4fce-8e6a-feabb8c519a6.txt">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+    <Content Include="PrivateKey\26531b6b-1fa1-4c70-8a1e-7c1b579742a5.txt">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+    <Content Include="PeerPublicKey\ab9ae069-1bed-4fce-8e6a-feabb8c519a6.txt">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
-    <Content Include="PeerPublicKey.txt" />
-    <Content Include="Peers\26531b6b-1fa1-4c70-8a1e-7c1b579742a5.txt">
+    <Content Include="PeerPublicKey\26531b6b-1fa1-4c70-8a1e-7c1b579742a5.txt">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
   </ItemGroup>

+ 17 - 0
UdPunching.Serv/UdPunching.Serv.csproj

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="C:\NuGetLocalRepo\ILRepack.2.0.17\build\ILRepack.props" Condition="Exists('C:\NuGetLocalRepo\ILRepack.2.0.17\build\ILRepack.props')" />
   <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -12,6 +13,8 @@
     <FileAlignment>512</FileAlignment>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     <Deterministic>true</Deterministic>
+    <NuGetPackageImportStamp>
+    </NuGetPackageImportStamp>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>
@@ -32,6 +35,9 @@
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
+  <PropertyGroup>
+    <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
     <Reference Include="System.Core" />
@@ -53,6 +59,7 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />
+    <None Include="packages.config" />
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -79,4 +86,14 @@
   </ItemGroup>
   <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+    <PropertyGroup>
+      <ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
+    </PropertyGroup>
+    <Error Condition="!Exists('C:\NuGetLocalRepo\ILRepack.2.0.17\build\ILRepack.props')" Text="$([System.String]::Format('$(ErrorText)', 'C:\NuGetLocalRepo\ILRepack.2.0.17\build\ILRepack.props'))" />
+  </Target>
+  <PropertyGroup>
+    <PostBuildEvent>if not exist "$(TargetDir)Packed" md "$(TargetDir)Packed"
+$(ILRepack) /ndebug "/out:$(TargetDir)Packed\$(TargetFileName)" "$(TargetPath)" UdPunching.dll</PostBuildEvent>
+  </PropertyGroup>
 </Project>

+ 4 - 0
UdPunching.Serv/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="ILRepack" version="2.0.17" targetFramework="net461" />
+</packages>

+ 6 - 12
UdPunching.sln

@@ -7,16 +7,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UdPunching.Common", "UdPunc
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UdPunching.Serv", "UdPunching.Serv\UdPunching.Serv.csproj", "{61003BB5-055E-4E55-B725-3A5B48256D5D}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UdPunching.Example", "UdPunching.Example\UdPunching.Example.csproj", "{A7F567D6-1AF7-436C-82F4-AC97B5CA1F76}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UdPunching.Example.Another", "UdPunching.Example.Another\UdPunching.Example.Another.csproj", "{71C75EA7-2A2E-4D9D-8E98-58BABE0FDDF3}"
-EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "@", "@", "{9FB8D149-8F3B-4FC0-937D-19CA4391193B}"
 	ProjectSection(SolutionItems) = preProject
 		.gitignore = .gitignore
 		README.md = README.md
 	EndProjectSection
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UdPunching.ExampleW", "UdPunching.ExampleW\UdPunching.ExampleW.csproj", "{E34269B6-06EA-4F48-AA62-C8079558664B}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -31,14 +29,10 @@ Global
 		{61003BB5-055E-4E55-B725-3A5B48256D5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{61003BB5-055E-4E55-B725-3A5B48256D5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{61003BB5-055E-4E55-B725-3A5B48256D5D}.Release|Any CPU.Build.0 = Release|Any CPU
-		{A7F567D6-1AF7-436C-82F4-AC97B5CA1F76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{A7F567D6-1AF7-436C-82F4-AC97B5CA1F76}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{A7F567D6-1AF7-436C-82F4-AC97B5CA1F76}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{A7F567D6-1AF7-436C-82F4-AC97B5CA1F76}.Release|Any CPU.Build.0 = Release|Any CPU
-		{71C75EA7-2A2E-4D9D-8E98-58BABE0FDDF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{71C75EA7-2A2E-4D9D-8E98-58BABE0FDDF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{71C75EA7-2A2E-4D9D-8E98-58BABE0FDDF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{71C75EA7-2A2E-4D9D-8E98-58BABE0FDDF3}.Release|Any CPU.Build.0 = Release|Any CPU
+		{E34269B6-06EA-4F48-AA62-C8079558664B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{E34269B6-06EA-4F48-AA62-C8079558664B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{E34269B6-06EA-4F48-AA62-C8079558664B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{E34269B6-06EA-4F48-AA62-C8079558664B}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE