├── .gitignore ├── Common └── SetMaxTCPConnection.reg ├── ITnmg.IOCPSocket.Interface ├── ISocketBufferProcess.cs ├── ITnmg.IOCPSocket.Interface.csproj ├── Properties │ └── AssemblyInfo.cs ├── SocketBuffer.cs ├── SocketBufferQueue.cs └── SocketProtocolTcpBase.cs ├── ITnmg.IOCPSocket.WPFServer ├── App.config ├── App.xaml ├── App.xaml.cs ├── ITnmg.IOCPSocket.WPFServer.csproj ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── Resources │ ├── Entypo-license.txt │ ├── Entypo.ttf │ ├── Icons.xaml │ ├── IconsNonShared.xaml │ └── WindowsIcons-license.txt ├── ViewModel │ ├── MainViewModel.cs │ └── ViewModelLocator.cs └── packages.config ├── ITnmg.IOCPSocket.WinFormClient ├── App.config ├── F_Main.Designer.cs ├── F_Main.cs ├── F_Main.resx ├── ITnmg.IOCPSocket.WinFormClient.csproj ├── Program.cs └── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── ITnmg.IOCPSocket.WinFormServer ├── App.config ├── F_Main.Designer.cs ├── F_Main.cs ├── F_Main.resx ├── ITnmg.IOCPSocket.WinFormServer.csproj ├── Program.cs └── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── ITnmg.IOCPSocket.sln ├── ITnmg.IOCPSocket ├── ITnmg.IOCPSocket.csproj ├── Properties │ └── AssemblyInfo.cs ├── SocketAsyncEventArgsPool.cs ├── SocketClientManager.cs ├── SocketManagerBase.cs ├── SocketServerManager.cs ├── SocketStatusChangeArgs.cs ├── SocketUserToken.cs └── TextProtocol.cs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | [Xx]64/ 19 | [Xx]86/ 20 | [Bb]uild/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | 85 | # Visual Studio profiler 86 | *.psess 87 | *.vsp 88 | *.vspx 89 | *.sap 90 | 91 | # TFS 2012 Local Workspace 92 | $tf/ 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | *.DotSettings.user 101 | 102 | # JustCode is a .NET coding add-in 103 | .JustCode 104 | 105 | # TeamCity is a build add-in 106 | _TeamCity* 107 | 108 | # DotCover is a Code Coverage Tool 109 | *.dotCover 110 | 111 | # NCrunch 112 | _NCrunch_* 113 | .*crunch*.local.xml 114 | nCrunchTemp_* 115 | 116 | # MightyMoose 117 | *.mm.* 118 | AutoTest.Net/ 119 | 120 | # Web workbench (sass) 121 | .sass-cache/ 122 | 123 | # Installshield output folder 124 | [Ee]xpress/ 125 | 126 | # DocProject is a documentation generator add-in 127 | DocProject/buildhelp/ 128 | DocProject/Help/*.HxT 129 | DocProject/Help/*.HxC 130 | DocProject/Help/*.hhc 131 | DocProject/Help/*.hhk 132 | DocProject/Help/*.hhp 133 | DocProject/Help/Html2 134 | DocProject/Help/html 135 | 136 | # Click-Once directory 137 | publish/ 138 | 139 | # Publish Web Output 140 | *.[Pp]ublish.xml 141 | *.azurePubxml 142 | 143 | # TODO: Un-comment the next line if you do not want to checkin 144 | # your web deploy settings because they may include unencrypted 145 | # passwords 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # NuGet Packages 150 | *.nupkg 151 | # The packages folder can be ignored because of Package Restore 152 | **/packages/* 153 | # except build/, which is used as an MSBuild target. 154 | !**/packages/build/ 155 | # Uncomment if necessary however generally it will be regenerated when needed 156 | #!**/packages/repositories.config 157 | # NuGet v3's project.json files produces more ignoreable files 158 | *.nuget.props 159 | *.nuget.targets 160 | 161 | # Microsoft Azure Build Output 162 | csx/ 163 | *.build.csdef 164 | 165 | # Microsoft Azure Emulator 166 | ecf/ 167 | rcf/ 168 | 169 | # Windows Store app package directory 170 | AppPackages/ 171 | BundleArtifacts/ 172 | 173 | # Visual Studio cache files 174 | # files ending in .cache can be ignored 175 | *.[Cc]ache 176 | # but keep track of directories ending in .cache 177 | !*.[Cc]ache/ 178 | 179 | # Others 180 | ClientBin/ 181 | [Ss]tyle[Cc]op.* 182 | ~$* 183 | *~ 184 | *.dbmdl 185 | *.dbproj.schemaview 186 | *.pfx 187 | *.publishsettings 188 | node_modules/ 189 | orleans.codegen.cs 190 | 191 | # RIA/Silverlight projects 192 | Generated_Code/ 193 | 194 | # Backup & report files from converting an old project file 195 | # to a newer Visual Studio version. Backup files are not needed, 196 | # because we have git ;-) 197 | _UpgradeReport_Files/ 198 | Backup*/ 199 | UpgradeLog*.XML 200 | UpgradeLog*.htm 201 | 202 | # SQL Server files 203 | *.mdf 204 | *.ldf 205 | 206 | # Business Intelligence projects 207 | *.rdl.data 208 | *.bim.layout 209 | *.bim_*.settings 210 | 211 | # Microsoft Fakes 212 | FakesAssemblies/ 213 | 214 | # GhostDoc plugin setting file 215 | *.GhostDoc.xml 216 | 217 | # Node.js Tools for Visual Studio 218 | .ntvs_analysis.dat 219 | 220 | # Visual Studio 6 build log 221 | *.plg 222 | 223 | # Visual Studio 6 workspace options file 224 | *.opt 225 | 226 | # Visual Studio LightSwitch build output 227 | **/*.HTMLClient/GeneratedArtifacts 228 | **/*.DesktopClient/GeneratedArtifacts 229 | **/*.DesktopClient/ModelManifest.xml 230 | **/*.Server/GeneratedArtifacts 231 | **/*.Server/ModelManifest.xml 232 | _Pvt_Extensions 233 | 234 | # LightSwitch generated files 235 | GeneratedArtifacts/ 236 | ModelManifest.xml 237 | 238 | # Paket dependency manager 239 | .paket/paket.exe 240 | 241 | # FAKE - F# Make 242 | .fake/ 243 | -------------------------------------------------------------------------------- /Common/SetMaxTCPConnection.reg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jgh004/ITnmg.IOCPSocket/aa7bda956e53184f7a830347bb342f348cd236bd/Common/SetMaxTCPConnection.reg -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.Interface/ISocketBufferProcess.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ITnmg.IOCPSocket 8 | { 9 | /// 10 | /// Socket 协议 11 | /// 12 | public interface ISocketBufferProcess 13 | { 14 | /// 15 | /// 处理 Socket 接收到的数据, 返回处理结果, 同时输出未能处理的数据(因为粘包或数据不全等原因) 16 | /// 17 | /// 收到的数据 18 | /// buffer 中有效数据的长度 19 | /// 处理结果 20 | bool ProcessReceive( byte[] buffer, int effectiveLength ); 21 | 22 | /// 23 | /// 返回下一个要发送的数据 24 | /// 25 | /// 要发送的数据, 为避免频繁创建对象影响性能, 应直接向 buffer 写入数据, 不要用 new buffer[]. 26 | /// 如果 buffer 容纳不下当前要发送的数据则先写入 buffer 最大容量的数据, 其他的数据下次再发. 27 | /// buffer 中有效数据的长度 28 | void GetNextSendBuffer( ref byte[] buffer, ref int effectiveLength ); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.Interface/ITnmg.IOCPSocket.Interface.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {AEFCEBA4-0E8E-429D-AD10-4ACE3BF794FF} 8 | Library 9 | Properties 10 | ITnmg.IOCPSocket 11 | ITnmg.IOCPSocket.Interface 12 | v4.6.2 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | true 35 | bin\x64\Debug\ 36 | DEBUG;TRACE 37 | full 38 | x64 39 | prompt 40 | MinimumRecommendedRules.ruleset 41 | 42 | 43 | bin\x64\Release\ 44 | TRACE 45 | true 46 | pdbonly 47 | x64 48 | prompt 49 | MinimumRecommendedRules.ruleset 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 75 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.Interface/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("SocketStressTestInterface")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("SocketStressTestInterface")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | //将 ComVisible 设置为 false 将使此程序集中的类型 18 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("aefceba4-0e8e-429d-ad10-4ace3bf794ff")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.Interface/SocketBuffer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ITnmg.IOCPSocket 8 | { 9 | /// 10 | /// Socket 收发数据的缓存 11 | /// 12 | public class SocketBuffer 13 | { 14 | /// 15 | /// 获取或设置缓存内容 16 | /// 17 | public byte[] Buffer 18 | { 19 | get; set; 20 | } 21 | 22 | /// 23 | /// 获取或设置缓存中有效数据的长度 24 | /// 25 | public int Length 26 | { 27 | get; set; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.Interface/SocketBufferQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace ITnmg.IOCPSocket 9 | { 10 | /// 11 | /// Socket 缓存队列 12 | /// 13 | public class SocketBufferQueue 14 | { 15 | /// 16 | /// 获取或设置不完整的缓存数组中有效数据长度 17 | /// 18 | public int FragmentaryBufferLength 19 | { 20 | get; set; 21 | } 22 | 23 | /// 24 | /// 获取或设置处理过后返回的不完整的数据 25 | /// 26 | public byte[] FragmentaryBuffer 27 | { 28 | get; set; 29 | } 30 | 31 | /// 32 | /// Socket 缓存队列 33 | /// 34 | public ConcurrentQueue BufferQueue 35 | { 36 | get; set; 37 | } 38 | 39 | 40 | public SocketBufferQueue() 41 | { 42 | BufferQueue = new ConcurrentQueue(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.Interface/SocketProtocolTcpBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ITnmg.IOCPSocket 8 | { 9 | public abstract class SocketBufferProcessTcpBase : ISocketBufferProcess 10 | { 11 | /// 12 | /// 接收缓存锁定用 13 | /// 14 | private object receiveLock = new object(); 15 | 16 | private SocketBufferQueue receiveBufferQueue; 17 | 18 | private SocketBufferQueue sendBufferQueue; 19 | 20 | 21 | /// 22 | /// 处理接收到的数据 23 | /// 24 | /// 25 | public virtual bool ProcessReceive( byte[] buffer, int effectiveLength ) 26 | { 27 | bool result = false; 28 | 29 | lock ( receiveLock ) 30 | { 31 | //待发给协议处理的数据 32 | //byte[] buffer; 33 | 34 | ////如果当前缓存有效数据长度大于0 35 | //if ( receiveBufferLength > 0 ) 36 | //{ 37 | // //从缓存池中取一段缓存, 长度等于当前有效长度+新收到的有效长度 38 | // buffer = bufferManager.TakeBuffer( ReceiveArgs.BytesTransferred + receiveBufferLength ); 39 | // //先把有当前缓存有效数据写入新的大缓存 40 | // Buffer.BlockCopy( receiveBuffer, 0, buffer, 0, receiveBufferLength ); 41 | // //再将新收到的数据写入大缓存,接在之前添加的数据后 42 | // Buffer.BlockCopy( ReceiveArgs.Buffer, ReceiveArgs.Offset, buffer, receiveBufferLength, ReceiveArgs.BytesTransferred ); 43 | // //归还缓冲区 44 | // bufferManager.ReturnBuffer( receiveBuffer ); 45 | // //清除有效长度 46 | // receiveBufferLength = 0; 47 | //} 48 | //else 49 | //{ 50 | // //申请一段能放下新收到数据的缓存 51 | // buffer = bufferManager.TakeBuffer( ReceiveArgs.BytesTransferred ); 52 | // //拷贝有效数据 53 | // Buffer.BlockCopy( ReceiveArgs.Buffer, ReceiveArgs.Offset, buffer, 0, ReceiveArgs.BytesTransferred ); 54 | // //归还缓冲区 55 | // bufferManager.ReturnBuffer( receiveBuffer ); 56 | // //将新的缓存赋给接收缓存 57 | // receiveBuffer = buffer; 58 | // //设置有效长度 59 | // receiveBufferLength = ReceiveArgs.BytesTransferred; 60 | //} 61 | 62 | 63 | //Protocol.ProcessReceive( receiveBuffer, receiveBufferLength, out 64 | } 65 | 66 | return result; 67 | } 68 | 69 | public void GetNextSendBuffer( ref byte[] buffer, ref int effectiveLength ) 70 | { 71 | throw new NotImplementedException(); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.WPFServer/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.WPFServer/App.xaml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.WPFServer/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | 9 | namespace ITnmg.IOCPSocket.WPFServer 10 | { 11 | /// 12 | /// App.xaml 的交互逻辑 13 | /// 14 | public partial class App : Application 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.WPFServer/ITnmg.IOCPSocket.WPFServer.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {EB08C901-88B9-44AA-AAA9-B771FEED7CB8} 8 | WinExe 9 | Properties 10 | ITnmg.IOCPSocket.WPFServer 11 | ITnmg.IOCPSocket.WPFServer 12 | v4.6.2 13 | 512 14 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | 4 16 | true 17 | 18 | 19 | AnyCPU 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | AnyCPU 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | true 39 | bin\x64\Debug\ 40 | DEBUG;TRACE 41 | full 42 | x64 43 | prompt 44 | MinimumRecommendedRules.ruleset 45 | true 46 | 47 | 48 | bin\x64\Release\ 49 | TRACE 50 | true 51 | pdbonly 52 | x64 53 | prompt 54 | MinimumRecommendedRules.ruleset 55 | true 56 | 57 | 58 | 59 | ..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.dll 60 | True 61 | 62 | 63 | ..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.Extras.dll 64 | True 65 | 66 | 67 | ..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.Platform.dll 68 | True 69 | 70 | 71 | ..\packages\MahApps.Metro.1.4.0\lib\net45\MahApps.Metro.dll 72 | True 73 | 74 | 75 | ..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll 76 | True 77 | 78 | 79 | 80 | 81 | ..\packages\MvvmLightLibs.5.3.0.0\lib\net45\System.Windows.Interactivity.dll 82 | True 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 4.0 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | MSBuild:Compile 100 | Designer 101 | 102 | 103 | 104 | 105 | MSBuild:Compile 106 | Designer 107 | 108 | 109 | App.xaml 110 | Code 111 | 112 | 113 | MainWindow.xaml 114 | Code 115 | 116 | 117 | MSBuild:Compile 118 | Designer 119 | 120 | 121 | MSBuild:Compile 122 | Designer 123 | 124 | 125 | 126 | 127 | Code 128 | 129 | 130 | True 131 | True 132 | Resources.resx 133 | 134 | 135 | True 136 | Settings.settings 137 | True 138 | 139 | 140 | ResXFileCodeGenerator 141 | Resources.Designer.cs 142 | 143 | 144 | 145 | SettingsSingleFileGenerator 146 | Settings.Designer.cs 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | {aefceba4-0e8e-429d-ad10-4ace3bf794ff} 157 | ITnmg.IOCPSocket.Interface 158 | 159 | 160 | {2d636f35-8ec9-4002-8af6-71cbe237c359} 161 | ITnmg.IOCPSocket 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 178 | -------------------------------------------------------------------------------- /ITnmg.IOCPSocket.WPFServer/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  13 | 14 | 19 | 23 | 27 | 31 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |