From a220f6aaf38ab69e1070c3dc2e1ebf463f1854c9 Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 25 Sep 2021 16:49:21 +0800 Subject: [PATCH] =?UTF-8?q?InvokeAfterReceiveEnd=E5=A2=9E=E5=8A=A0try=20ca?= =?UTF-8?q?tch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Mozi.HttpEmbedded.Test/Program.cs | 2 +- Mozi.HttpEmbedded/HttpRequest.cs | 8 ++--- Mozi.HttpEmbedded/SocketServer.cs | 56 +++++++++++++++++++------------ 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/Mozi.HttpEmbedded.Test/Program.cs b/Mozi.HttpEmbedded.Test/Program.cs index 461e8cb..b473364 100644 --- a/Mozi.HttpEmbedded.Test/Program.cs +++ b/Mozi.HttpEmbedded.Test/Program.cs @@ -27,7 +27,7 @@ namespace Mozi.HttpEmbedded.Test //启用HTTPS //hs.UseHttps().LoadCert(AppDomain.CurrentDomain.BaseDirectory + @"Cert\ServerCert.pfx", "12345678"); //配置端口并启动服务器 - hs.SetPort(9000).Start(); + hs.SetPort(8080).Start(); //开启认证 //hs.UseAuth(AuthorizationType.Basic).SetUser("admin", "admin"); diff --git a/Mozi.HttpEmbedded/HttpRequest.cs b/Mozi.HttpEmbedded/HttpRequest.cs index f773aab..d409210 100644 --- a/Mozi.HttpEmbedded/HttpRequest.cs +++ b/Mozi.HttpEmbedded/HttpRequest.cs @@ -151,7 +151,7 @@ namespace Mozi.HttpEmbedded int posCaret = 0; int count = 0; - int index = 0; + int indLine = 0; int dataLength = data.Length; while ((posCR < dataLength) && Array.IndexOf(data, ASCIICode.CR, posCR + 1) > 0) { @@ -160,7 +160,7 @@ namespace Mozi.HttpEmbedded //CR byte[] fragement = new byte[posCR - posCaret]; Array.Copy(data, posCaret, fragement, 0, posCR - posCaret); - if (index == 0) + if (indLine == 0) { ParseRequestLine(ref req, fragement); } @@ -175,7 +175,7 @@ namespace Mozi.HttpEmbedded } //ָֽڶ posCaret = posCR + 2; - index++; + indLine++; //TODO ÿն } @@ -594,7 +594,7 @@ namespace Mozi.HttpEmbedded req.Method = rm; //жϷǷ֪ - if (object.Equals(req.Method,null)) + if (Equals(req.Method,null)) { req.Method = new RequestMethod(sMethod); } diff --git a/Mozi.HttpEmbedded/SocketServer.cs b/Mozi.HttpEmbedded/SocketServer.cs index cff8c7a..87723dd 100644 --- a/Mozi.HttpEmbedded/SocketServer.cs +++ b/Mozi.HttpEmbedded/SocketServer.cs @@ -160,25 +160,32 @@ namespace Mozi.HttpEmbedded Socket client = so.WorkSocket; if (client.Connected) { - int iByteRead = client.EndReceive(iar); - - if (iByteRead > 0) + try { - //置空数据缓冲区 - so.ResetBuffer(iByteRead); - if (client.Available > 0) + int iByteRead = client.EndReceive(iar); + + if (iByteRead > 0) { - //Thread.Sleep(10); - client.BeginReceive(so.Buffer, 0, so.Buffer.Length, SocketFlags.None, CallbackReceived, so); + //置空数据缓冲区 + so.ResetBuffer(iByteRead); + if (client.Available > 0) + { + //Thread.Sleep(10); + client.BeginReceive(so.Buffer, 0, so.Buffer.Length, SocketFlags.None, CallbackReceived, so); + } + else + { + InvokeAfterReceiveEnd(so, client); + } } else { InvokeAfterReceiveEnd(so, client); } } - else + finally { - InvokeAfterReceiveEnd(so, client); + } } else @@ -188,19 +195,24 @@ namespace Mozi.HttpEmbedded } private void InvokeAfterReceiveEnd(StateObject so, Socket client) { - RemoveClientSocket(so); - if (AfterReceiveEnd != null) + try { - AfterReceiveEnd(this, - new DataTransferArgs() - { - Data = so.Data.ToArray(), - IP = so.IP, - Port = so.RemotePort, - Socket = so.WorkSocket, - Client = client, - State = so - }); + RemoveClientSocket(so); + if (AfterReceiveEnd != null) + { + AfterReceiveEnd(this, + new DataTransferArgs() + { + Data = so.Data.ToArray(), + IP = so.IP, + Port = so.RemotePort, + Socket = so.WorkSocket, + Client = client, + State = so + }); + } + }finally{ + } } //TODO 此处开启Socket状态监听,对断开的链接进行关闭销毁 -- GitLab