Explorar el Código

使用DotNetty代替原来的网络框架

张泳健 hace 7 años
padre
commit
13edb3595c

+ 10 - 10
GWSocketClient/Form1.Designer.cs Ver fichero

@@ -75,13 +75,13 @@
75 75
             this.toolStripStatusLabel2 = new System.Windows.Forms.ToolStripStatusLabel();
76 76
             this.tsslCCIDLEN = new System.Windows.Forms.ToolStripStatusLabel();
77 77
             this.toolStripStatusLabelHbShow = new System.Windows.Forms.ToolStripStatusLabel();
78
+            this.toolStripStatusLabelSendTime = new System.Windows.Forms.ToolStripStatusLabel();
78 79
             this.groupBox2 = new System.Windows.Forms.GroupBox();
79 80
             this.listBoxShow = new System.Windows.Forms.ListBox();
80 81
             this.cmslistboxRightMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
81 82
             this.rightMenuCopyCmd = new System.Windows.Forms.ToolStripMenuItem();
82 83
             this.rightMenuClearInfo = new System.Windows.Forms.ToolStripMenuItem();
83 84
             this.timerHeartBeat = new System.Windows.Forms.Timer(this.components);
84
-            this.toolStripStatusLabelSendTime = new System.Windows.Forms.ToolStripStatusLabel();
85 85
             this.groupBox1.SuspendLayout();
86 86
             this.menuStrip1.SuspendLayout();
87 87
             this.statusStrip1.SuspendLayout();
@@ -529,17 +529,23 @@
529 529
             this.toolStripStatusLabelHbShow.Size = new System.Drawing.Size(82, 17);
530 530
             this.toolStripStatusLabelHbShow.Text = "心跳时间: 5秒";
531 531
             // 
532
+            // toolStripStatusLabelSendTime
533
+            // 
534
+            this.toolStripStatusLabelSendTime.Name = "toolStripStatusLabelSendTime";
535
+            this.toolStripStatusLabelSendTime.Size = new System.Drawing.Size(85, 17);
536
+            this.toolStripStatusLabelSendTime.Text = "发送-回复时间";
537
+            // 
532 538
             // groupBox2
533 539
             // 
534 540
             this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
535 541
             | System.Windows.Forms.AnchorStyles.Left) 
536 542
             | System.Windows.Forms.AnchorStyles.Right)));
537 543
             this.groupBox2.Controls.Add(this.listBoxShow);
538
-            this.groupBox2.Location = new System.Drawing.Point(14, 380);
544
+            this.groupBox2.Location = new System.Drawing.Point(14, 195);
539 545
             this.groupBox2.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
540 546
             this.groupBox2.Name = "groupBox2";
541 547
             this.groupBox2.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
542
-            this.groupBox2.Size = new System.Drawing.Size(1123, 490);
548
+            this.groupBox2.Size = new System.Drawing.Size(1123, 675);
543 549
             this.groupBox2.TabIndex = 13;
544 550
             this.groupBox2.TabStop = false;
545 551
             this.groupBox2.Text = "显示信息栏";
@@ -558,7 +564,7 @@
558 564
             this.listBoxShow.Location = new System.Drawing.Point(9, 28);
559 565
             this.listBoxShow.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
560 566
             this.listBoxShow.Name = "listBoxShow";
561
-            this.listBoxShow.Size = new System.Drawing.Size(1106, 454);
567
+            this.listBoxShow.Size = new System.Drawing.Size(1106, 634);
562 568
             this.listBoxShow.TabIndex = 0;
563 569
             this.listBoxShow.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBoxShow_DrawItem);
564 570
             this.listBoxShow.DoubleClick += new System.EventHandler(this.listBoxShow_DoubleClick);
@@ -590,12 +596,6 @@
590 596
             this.timerHeartBeat.Interval = 5000;
591 597
             this.timerHeartBeat.Tick += new System.EventHandler(this.timerHeartBeat_Tick);
592 598
             // 
593
-            // toolStripStatusLabelSendTime
594
-            // 
595
-            this.toolStripStatusLabelSendTime.Name = "toolStripStatusLabelSendTime";
596
-            this.toolStripStatusLabelSendTime.Size = new System.Drawing.Size(85, 17);
597
-            this.toolStripStatusLabelSendTime.Text = "发送-回复时间";
598
-            // 
599 599
             // Form1
600 600
             // 
601 601
             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);

+ 36 - 0
GWSocketClient/GWSocketClient.csproj Ver fichero

@@ -38,11 +38,41 @@
38 38
     <ApplicationIcon>GWSocket.ico</ApplicationIcon>
39 39
   </PropertyGroup>
40 40
   <ItemGroup>
41
+    <Reference Include="DotNetty.Buffers, Version=0.4.6.0, Culture=neutral, PublicKeyToken=bc13ca065fa06c29, processorArchitecture=MSIL">
42
+      <HintPath>..\packages\DotNetty.Buffers.0.4.6\lib\net45\DotNetty.Buffers.dll</HintPath>
43
+    </Reference>
44
+    <Reference Include="DotNetty.Codecs, Version=0.4.6.0, Culture=neutral, PublicKeyToken=bc13ca065fa06c29, processorArchitecture=MSIL">
45
+      <HintPath>..\packages\DotNetty.Codecs.0.4.6\lib\net45\DotNetty.Codecs.dll</HintPath>
46
+    </Reference>
47
+    <Reference Include="DotNetty.Common, Version=0.4.6.0, Culture=neutral, PublicKeyToken=bc13ca065fa06c29, processorArchitecture=MSIL">
48
+      <HintPath>..\packages\DotNetty.Common.0.4.6\lib\net45\DotNetty.Common.dll</HintPath>
49
+    </Reference>
50
+    <Reference Include="DotNetty.Handlers, Version=0.4.6.0, Culture=neutral, PublicKeyToken=bc13ca065fa06c29, processorArchitecture=MSIL">
51
+      <HintPath>..\packages\DotNetty.Handlers.0.4.6\lib\net45\DotNetty.Handlers.dll</HintPath>
52
+    </Reference>
53
+    <Reference Include="DotNetty.Transport, Version=0.4.6.0, Culture=neutral, PublicKeyToken=bc13ca065fa06c29, processorArchitecture=MSIL">
54
+      <HintPath>..\packages\DotNetty.Transport.0.4.6\lib\net45\DotNetty.Transport.dll</HintPath>
55
+    </Reference>
56
+    <Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
57
+      <HintPath>..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.1.1.0\lib\netstandard1.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
58
+    </Reference>
59
+    <Reference Include="Microsoft.Extensions.Logging, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
60
+      <HintPath>..\packages\Microsoft.Extensions.Logging.1.1.1\lib\netstandard1.1\Microsoft.Extensions.Logging.dll</HintPath>
61
+    </Reference>
62
+    <Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
63
+      <HintPath>..\packages\Microsoft.Extensions.Logging.Abstractions.1.1.1\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
64
+    </Reference>
41 65
     <Reference Include="Newtonsoft.Json">
42 66
       <HintPath>dlls\Newtonsoft.Json.dll</HintPath>
43 67
     </Reference>
44 68
     <Reference Include="System" />
69
+    <Reference Include="System.ComponentModel.Composition" />
45 70
     <Reference Include="System.Core" />
71
+    <Reference Include="System.IO.Compression" />
72
+    <Reference Include="System.Numerics" />
73
+    <Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
74
+      <HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
75
+    </Reference>
46 76
     <Reference Include="System.Runtime.Serialization" />
47 77
     <Reference Include="System.Xml.Linq" />
48 78
     <Reference Include="System.Data.DataSetExtensions" />
@@ -64,7 +94,10 @@
64 94
     <Compile Include="AnaProtocol.Designer.cs">
65 95
       <DependentUpon>AnaProtocol.cs</DependentUpon>
66 96
     </Compile>
97
+    <Compile Include="config\ConfigBean.cs" />
67 98
     <Compile Include="db\AccessDbLoader.cs" />
99
+    <Compile Include="dotnetty\DotNettyClient.cs" />
100
+    <Compile Include="dotnetty\DotNettyClientHandler.cs" />
68 101
     <Compile Include="excel\ExcelLoader.cs" />
69 102
     <Compile Include="Form1.cs">
70 103
       <SubType>Form</SubType>
@@ -107,8 +140,10 @@
107 140
     <Compile Include="Program.cs" />
108 141
     <Compile Include="Properties\AssemblyInfo.cs" />
109 142
     <Compile Include="property\PropertiesLoader.cs" />
143
+    <Compile Include="util\AryUtil.cs" />
110 144
     <Compile Include="util\CheckUtil.cs" />
111 145
     <Compile Include="util\HexUtil.cs" />
146
+    <Compile Include="util\TimeUtil.cs" />
112 147
     <Compile Include="util\Utils.cs" />
113 148
     <Compile Include="WebViewForm.cs">
114 149
       <SubType>Form</SubType>
@@ -157,6 +192,7 @@
157 192
     <EmbeddedResource Include="WebViewForm.resx">
158 193
       <DependentUpon>WebViewForm.cs</DependentUpon>
159 194
     </EmbeddedResource>
195
+    <None Include="packages.config" />
160 196
     <None Include="Properties\Settings.settings">
161 197
       <Generator>SettingsSingleFileGenerator</Generator>
162 198
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>

+ 15 - 0
GWSocketClient/config/ConfigBean.cs Ver fichero

@@ -0,0 +1,15 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Text;
5
+using System.Threading.Tasks;
6
+
7
+namespace GWSocketClient.config
8
+{
9
+    public class ConfigBean
10
+    {
11
+        public static string Host;
12
+        public static int Port;
13
+        public static bool ConnAndLogin = true;
14
+    }
15
+}

+ 55 - 0
GWSocketClient/dotnetty/DotNettyClient.cs Ver fichero

@@ -0,0 +1,55 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Net;
5
+using System.Text;
6
+using System.Threading.Tasks;
7
+using DotNetty.Codecs;
8
+using DotNetty.Handlers.Timeout;
9
+using DotNetty.Transport.Bootstrapping;
10
+using DotNetty.Transport.Channels;
11
+using DotNetty.Transport.Channels.Sockets;
12
+
13
+namespace GWSocketClient.dotnetty
14
+{
15
+    public class DotNettyClient
16
+    {
17
+        private IPAddress Host;
18
+        private int Port;
19
+
20
+        public void StartDotNetty()
21
+        {
22
+            RunClientAsync().Wait();
23
+        }
24
+
25
+        async Task RunClientAsync()
26
+        {
27
+            var group = new MultithreadEventLoopGroup();
28
+            try
29
+            {
30
+                var bootstrap = new Bootstrap();
31
+                bootstrap
32
+                    .Group(group)
33
+                    .Channel<TcpSocketChannel>()
34
+                    .Option(ChannelOption.TcpNodelay, true)
35
+                    .Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
36
+                    {
37
+                        IChannelPipeline pipeline = channel.Pipeline;
38
+                        pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 10, 2, 1, 0));
39
+                        pipeline.AddLast("idle", new IdleStateHandler(10, 20, 15));
40
+                        pipeline.AddLast("echo", new DotNettyClientHandler());
41
+                    }));
42
+
43
+                IChannel clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(Host, Port));
44
+
45
+                Console.ReadLine();
46
+
47
+                await clientChannel.CloseAsync();
48
+            }
49
+            finally
50
+            {
51
+                await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
52
+            }
53
+        }
54
+    }
55
+}

+ 64 - 0
GWSocketClient/dotnetty/DotNettyClientHandler.cs Ver fichero

@@ -0,0 +1,64 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Text;
5
+using System.Threading.Tasks;
6
+using DotNetty.Buffers;
7
+using DotNetty.Handlers.Timeout;
8
+using DotNetty.Transport.Channels;
9
+using GWSocketClient.util;
10
+
11
+namespace GWSocketClient.dotnetty
12
+{
13
+    public class DotNettyClientHandler : ChannelHandlerAdapter
14
+    {
15
+        public override void ChannelActive(IChannelHandlerContext context)
16
+        {
17
+            Console.WriteLine("ChannelActive  " + DateTime.Now);
18
+        }
19
+
20
+        public override void ChannelRead(IChannelHandlerContext context, object message)
21
+        {
22
+            if (message is IByteBuffer byteBuffer)
23
+            {
24
+                Console.WriteLine("Received from server: " + byteBuffer.ToString(Encoding.UTF8));
25
+            }
26
+            context.WriteAsync(message);
27
+        }
28
+
29
+        public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
30
+
31
+
32
+        public override void UserEventTriggered(IChannelHandlerContext context, object evt)
33
+        {
34
+            base.UserEventTriggered(context, evt);
35
+            if (evt is IdleStateEvent state)
36
+            {
37
+                if (state == IdleStateEvent.ReaderIdleStateEvent)
38
+                {
39
+                    Console.WriteLine("Read超时");
40
+                }
41
+                else if (state == IdleStateEvent.WriterIdleStateEvent)
42
+                {
43
+                    Console.WriteLine("Write超时");
44
+                }
45
+                if (state == IdleStateEvent.AllIdleStateEvent)
46
+                {
47
+                    Console.WriteLine("All超时");
48
+                }
49
+            }
50
+        }
51
+
52
+        public override void ChannelInactive(IChannelHandlerContext context)
53
+        {
54
+            base.ChannelInactive(context);
55
+            Console.WriteLine("ChannelInactive");
56
+        }
57
+
58
+        public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
59
+        {
60
+            Console.WriteLine("Exception: " + exception);
61
+            context.CloseAsync();
62
+        }
63
+    }
64
+}

+ 44 - 0
GWSocketClient/packages.config Ver fichero

@@ -0,0 +1,44 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<packages>
3
+  <package id="DotNetty.Buffers" version="0.4.6" targetFramework="net452" />
4
+  <package id="DotNetty.Codecs" version="0.4.6" targetFramework="net452" />
5
+  <package id="DotNetty.Common" version="0.4.6" targetFramework="net452" />
6
+  <package id="DotNetty.Handlers" version="0.4.6" targetFramework="net452" />
7
+  <package id="DotNetty.Transport" version="0.4.6" targetFramework="net452" />
8
+  <package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.1.0" targetFramework="net452" />
9
+  <package id="Microsoft.Extensions.Logging" version="1.1.1" targetFramework="net452" />
10
+  <package id="Microsoft.Extensions.Logging.Abstractions" version="1.1.1" targetFramework="net452" />
11
+  <package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="net452" />
12
+  <package id="NETStandard.Library" version="1.6.1" targetFramework="net452" />
13
+  <package id="System.Collections" version="4.3.0" targetFramework="net452" />
14
+  <package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net452" />
15
+  <package id="System.ComponentModel" version="4.3.0" targetFramework="net452" />
16
+  <package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net452" />
17
+  <package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net452" />
18
+  <package id="System.Diagnostics.Tracing" version="4.3.0" targetFramework="net452" />
19
+  <package id="System.Globalization" version="4.3.0" targetFramework="net452" />
20
+  <package id="System.IO" version="4.3.0" targetFramework="net452" />
21
+  <package id="System.IO.Compression" version="4.3.0" targetFramework="net452" />
22
+  <package id="System.Linq" version="4.3.0" targetFramework="net452" />
23
+  <package id="System.Linq.Expressions" version="4.3.0" targetFramework="net452" />
24
+  <package id="System.Net.Http" version="4.3.0" targetFramework="net452" />
25
+  <package id="System.Net.Primitives" version="4.3.0" targetFramework="net452" />
26
+  <package id="System.ObjectModel" version="4.3.0" targetFramework="net452" />
27
+  <package id="System.Reflection" version="4.3.0" targetFramework="net452" />
28
+  <package id="System.Reflection.Extensions" version="4.3.0" targetFramework="net452" />
29
+  <package id="System.Reflection.Primitives" version="4.3.0" targetFramework="net452" />
30
+  <package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net452" />
31
+  <package id="System.Runtime" version="4.3.0" targetFramework="net452" />
32
+  <package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net452" />
33
+  <package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net452" />
34
+  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net452" />
35
+  <package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net452" />
36
+  <package id="System.Text.Encoding" version="4.3.0" targetFramework="net452" />
37
+  <package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net452" />
38
+  <package id="System.Text.RegularExpressions" version="4.3.0" targetFramework="net452" />
39
+  <package id="System.Threading" version="4.3.0" targetFramework="net452" />
40
+  <package id="System.Threading.Tasks" version="4.3.0" targetFramework="net452" />
41
+  <package id="System.Threading.Timer" version="4.3.0" targetFramework="net452" />
42
+  <package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net452" />
43
+  <package id="System.Xml.XDocument" version="4.3.0" targetFramework="net452" />
44
+</packages>

+ 28 - 0
GWSocketClient/util/AryUtil.cs Ver fichero

@@ -0,0 +1,28 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Text;
5
+using System.Threading.Tasks;
6
+
7
+namespace GWSocketClient.util
8
+{
9
+    public class AryUtil
10
+    {
11
+        public static string Bytes2Hex(byte[] bs)
12
+        {
13
+            return BitConverter.ToString(bs, 0, bs.Length).Replace("-", "").ToUpper();
14
+        }
15
+
16
+
17
+        public static byte[] Hex2Bytes(string hex)
18
+        {
19
+            hex = hex.Replace(" ", "");
20
+            if (hex.Length % 2 != 0)
21
+                hex += " ";
22
+            byte[] returnBytes = new byte[hex.Length / 2];
23
+            for (int i = 0; i < returnBytes.Length; i++)
24
+                returnBytes[i] = Convert.ToByte(hex.Substring(i * 2, 2).Trim(), 16);
25
+            return returnBytes;
26
+        }
27
+    }
28
+}

+ 16 - 0
GWSocketClient/util/TimeUtil.cs Ver fichero

@@ -0,0 +1,16 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Text;
5
+using System.Threading.Tasks;
6
+
7
+namespace GWSocketClient.util
8
+{
9
+    public class TimeUtil
10
+    {
11
+        public static string getTime()
12
+        {
13
+            return "";
14
+        }
15
+    }
16
+}

+ 19 - 0
GWSocketClient/util/Utils.cs Ver fichero

@@ -9,6 +9,25 @@ namespace GWSocketClient.util
9 9
 {
10 10
     public class Utils
11 11
     {
12
+
13
+
14
+        public static string Bytes2Hex(byte[] bs)
15
+        {
16
+            return BitConverter.ToString(bs, 0, bs.Length).Replace("-", "").ToUpper();
17
+        }
18
+
19
+
20
+        public static byte[] Hex2Bytes(string hex)
21
+        {
22
+            hex = hex.Replace(" ", "");
23
+            if (hex.Length % 2 != 0)
24
+                hex += " ";
25
+            byte[] returnBytes = new byte[hex.Length / 2];
26
+            for (int i = 0; i < returnBytes.Length; i++)
27
+                returnBytes[i] = Convert.ToByte(hex.Substring(i * 2, 2).Trim(), 16);
28
+            return returnBytes;
29
+        } 
30
+
12 31
         public static int hexToTen(string hex)
13 32
         {
14 33
             return int.Parse(hex, NumberStyles.HexNumber);

+ 16 - 4
README.md Ver fichero

@@ -41,10 +41,22 @@
41 41
 
42 42
 
43 43
 
44
-## 要解决问题
44
+## 使用DotNetty代替Socket
45
+
46
+`基于Visual Studio 2017`开发,没有`Nuget`程序,请下载安装.`VS2017自带`
47
+
48
+`工具->Nuget包管理器->程序包管理器控制台`输入以下指令
49
+
50
+```
51
+Install-Package DotNetty.Common -Version 0.4.6
52
+Install-Package DotNetty.Transport -Version 0.4.6
53
+Install-Package DotNetty.Codecs -Version 0.4.6
54
+Install-Package DotNetty.Handlers -Version 0.4.6
55
+```
56
+
57
+
58
+
59
+
45 60
 
46
-1. 服务器没开
47
-2. 超时
48
-3. 连接成功了,但服务器主动断开
49 61
 
50 62