From 741819e11c0bbbac92b82f9dfcbee5a12f707bba Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Mon, 27 Jan 2020 12:52:26 +0300 Subject: [PATCH] Sockets --- .../Esyur.Stores.MongoDB.csproj | 2 +- Esyur/Data/DataConverter.cs | 4 +- Esyur/Esyur.csproj | 2 +- Esyur/Net/HTTP/HTTPConnection.cs | 21 +++-- Esyur/Net/NetworkConnection.cs | 72 ++++++++-------- Esyur/Net/Sockets/ISocket.cs | 3 +- Esyur/Net/Sockets/SSLSocket.cs | 5 ++ Esyur/Net/Sockets/TCPSocket.cs | 82 +++++++++++++------ Esyur/Net/Sockets/WSSocket.cs | 5 ++ 9 files changed, 124 insertions(+), 72 deletions(-) diff --git a/Esyur.Stores.MongoDB/Esyur.Stores.MongoDB.csproj b/Esyur.Stores.MongoDB/Esyur.Stores.MongoDB.csproj index 29b678e..a8974b8 100644 --- a/Esyur.Stores.MongoDB/Esyur.Stores.MongoDB.csproj +++ b/Esyur.Stores.MongoDB/Esyur.Stores.MongoDB.csproj @@ -11,7 +11,7 @@ http://www.esyur.com https://github.com/esyur/esyur-dotnet/ True - 1.3.2 + 1.3.3 diff --git a/Esyur/Data/DataConverter.cs b/Esyur/Data/DataConverter.cs index c1fe1d7..c4ecff7 100644 --- a/Esyur/Data/DataConverter.cs +++ b/Esyur/Data/DataConverter.cs @@ -1041,8 +1041,8 @@ namespace Esyur.Data if (data.Length < offset + length) return null; - if (length == data.Length && offset == 0) - return data; + // if (length == data.Length && offset == 0) + // return data.ToArray(); var b = new byte[length]; Buffer.BlockCopy(data, (int)offset, b, 0, (int)length); diff --git a/Esyur/Esyur.csproj b/Esyur/Esyur.csproj index 28f0b20..bef8684 100644 --- a/Esyur/Esyur.csproj +++ b/Esyur/Esyur.csproj @@ -7,7 +7,7 @@ https://github.com/Esyur/Esyur-dotnet/blob/master/LICENSE http://www.esyur.com true - 1.4.7 + 1.4.8 https://github.com/esyur/esyur-dotnet Ahmed Kh. Zamil 1.3.1.0 diff --git a/Esyur/Net/HTTP/HTTPConnection.cs b/Esyur/Net/HTTP/HTTPConnection.cs index b385bfe..e613771 100644 --- a/Esyur/Net/HTTP/HTTPConnection.cs +++ b/Esyur/Net/HTTP/HTTPConnection.cs @@ -36,6 +36,7 @@ using Esyur.Data; using Esyur.Net.Packets; using Esyur.Misc; using System.Security.Cryptography; +using Esyur.Core; namespace Esyur.Net.HTTP { @@ -226,10 +227,10 @@ namespace Esyur.Net.HTTP } - public void SendFile(string filename) + public async AsyncReply SendFile(string filename) { if (Response.Handled == true) - return; + return false; try @@ -248,7 +249,7 @@ namespace Esyur.Net.HTTP { Response.Number = HTTPResponsePacket.ResponseCode.HTTP_NOTFOUND; Send("File Not Found"); - return; + return true; } @@ -288,21 +289,23 @@ namespace Esyur.Net.HTTP using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { - var buffer = new byte[5000]; + var buffer = new byte[60000]; while (true) { - var n = fs.Read(buffer, 0, 5000); + var n = fs.Read(buffer, 0, 60000); if (n <= 0) break; //Thread.Sleep(50); - base.Send(buffer, 0, n); + await base.SendAsync(buffer, 0, n); } } + + return true; } catch @@ -311,7 +314,11 @@ namespace Esyur.Net.HTTP { Close(); } - finally { } + finally { + + } + + return false; } } } diff --git a/Esyur/Net/NetworkConnection.cs b/Esyur/Net/NetworkConnection.cs index a0abe1a..f70e516 100644 --- a/Esyur/Net/NetworkConnection.cs +++ b/Esyur/Net/NetworkConnection.cs @@ -38,10 +38,10 @@ using Esyur.Resource; namespace Esyur.Net { - public class NetworkConnection: IDestructible// : IResource where TS : NetworkSession + public class NetworkConnection : IDestructible// : IResource where TS : NetworkSession { private ISocket sock; -// private bool connected; + // private bool connected; private DateTime lastAction; @@ -53,16 +53,16 @@ namespace Esyur.Net public event DataReceivedEvent OnDataReceived; public event ConnectionClosedEvent OnClose; public event DestroyedEvent OnDestroy; - object receivingLock = new object(); + //object receivingLock = new object(); - object sendLock = new object(); + //object sendLock = new object(); bool processing = false; public void Destroy() { - // if (connected) + // if (connected) Close(); OnDestroy?.Invoke(this); } @@ -135,7 +135,7 @@ namespace Esyur.Net processing = false; } - + } catch (Exception ex) { @@ -147,7 +147,7 @@ namespace Esyur.Net { if (sock != null) { - // connected = false; + // connected = false; sock.OnClose -= Socket_OnClose; sock.OnConnect -= Socket_OnConnect; sock.OnReceive -= Socket_OnReceive; @@ -175,19 +175,19 @@ namespace Esyur.Net } } } - + public void Close() { //if (!connected) // return; - + try { if (sock != null) sock.Close(); } - catch(Exception ex) + catch (Exception ex) { Global.Log("NetworkConenction:Close", LogType.Error, ex.ToString()); @@ -227,7 +227,7 @@ namespace Esyur.Net } } - + public bool Connected { get @@ -260,41 +260,41 @@ namespace Esyur.Net } */ + public virtual AsyncReply SendAsync(byte[] message, int offset, int length) + { + try + { + return sock.SendAsync(message, offset, length); + } + catch + { + return new AsyncReply(false); + } + } + public virtual void Send(byte[] msg) { - lock (sendLock) + try + { + sock.Send(msg); + lastAction = DateTime.Now; + } + catch { - try - { - if (sock != null) - { - lastAction = DateTime.Now; - sock.Send(msg); - } - } - catch - { - } } } public virtual void Send(byte[] msg, int offset, int length) { - lock (sendLock) + try { - try - { - if (sock != null) - { - lastAction = DateTime.Now; - sock.Send(msg, offset, length); - } - } - catch (Exception ex) - { - Console.WriteLine(ex); - } + sock.Send(msg, offset, length); + lastAction = DateTime.Now; + } + catch + { + } } diff --git a/Esyur/Net/Sockets/ISocket.cs b/Esyur/Net/Sockets/ISocket.cs index 827c4a7..cde8e98 100644 --- a/Esyur/Net/Sockets/ISocket.cs +++ b/Esyur/Net/Sockets/ISocket.cs @@ -50,9 +50,10 @@ namespace Esyur.Net.Sockets event ISocketConnectEvent OnConnect; event ISocketCloseEvent OnClose; + AsyncReply SendAsync(byte[] message, int offset, int length); void Send(byte[] message); - void Send(byte[] message, int offset, int size); + void Send(byte[] message, int offset, int length); void Close(); AsyncReply Connect(string hostname, ushort port); bool Begin(); diff --git a/Esyur/Net/Sockets/SSLSocket.cs b/Esyur/Net/Sockets/SSLSocket.cs index dbba41f..b25f7b5 100644 --- a/Esyur/Net/Sockets/SSLSocket.cs +++ b/Esyur/Net/Sockets/SSLSocket.cs @@ -349,5 +349,10 @@ namespace Esyur.Net.Sockets { throw new NotImplementedException(); } + + public AsyncReply SendAsync(byte[] message, int offset, int length) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/Esyur/Net/Sockets/TCPSocket.cs b/Esyur/Net/Sockets/TCPSocket.cs index 1b78db3..9cb6f53 100644 --- a/Esyur/Net/Sockets/TCPSocket.cs +++ b/Esyur/Net/Sockets/TCPSocket.cs @@ -50,9 +50,9 @@ namespace Esyur.Net.Sockets readonly object sendLock = new object(); - Queue sendBufferQueue = new Queue(); + Queue, byte[]>> sendBufferQueue = new Queue, byte[]>>();// Queue(); - bool asyncSending; + bool asyncSending; bool began = false; @@ -349,21 +349,23 @@ namespace Esyur.Net.Sockets public void Send(byte[] message, int offset, int size) { + var msg = message.Clip((uint)offset, (uint)size); lock (sendLock) { + if (!sock.Connected) return; if (asyncSending || held) { - sendBufferQueue.Enqueue(msg); + sendBufferQueue.Enqueue(new KeyValuePair, byte[]>(null, msg));// message.Clip((uint)offset, (uint)size)); } else { asyncSending = true; - sock.BeginSend(msg, 0, size, SocketFlags.None, PacketSent, null); + sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, null); //sock.SendAsync(new ArraySegment(msg), SocketFlags.None).ContinueWith(DataSent); } } @@ -372,33 +374,38 @@ namespace Esyur.Net.Sockets private void PacketSent(IAsyncResult ar) { - try - { - lock (sendLock) - { - if (sendBufferQueue.Count > 0) - { - byte[] data = sendBufferQueue.Dequeue(); + if (ar != null && ar.AsyncState != null) + ((AsyncReply)ar.AsyncState).Trigger(true); - sock.BeginSend(data, 0, data.Length, SocketFlags.None, PacketSent, null); - } - else + lock (sendLock) + { + if (sendBufferQueue.Count > 0) + { + var kv = sendBufferQueue.Dequeue(); + + try { + sock.BeginSend(kv.Value, 0, kv.Value.Length, SocketFlags.None, PacketSent, kv.Key); + } + catch (Exception ex) + { + kv.Key.Trigger(false); + + if (state != SocketState.Closed && !sock.Connected) + { + state = SocketState.Terminated; + Close(); + } + asyncSending = false; + + Global.Log("TCPSocket", LogType.Error, ex.ToString()); } } - } - catch (Exception ex) - { - if (state != SocketState.Closed && !sock.Connected) + else { - state = SocketState.Terminated; - Close(); + asyncSending = false; } - - asyncSending = false; - - Global.Log("TCPSocket", LogType.Error, ex.ToString()); } } @@ -460,5 +467,32 @@ namespace Esyur.Net.Sockets held = false; } } + + public AsyncReply SendAsync(byte[] message, int offset, int length) + { + + var msg = message.Clip((uint)offset, (uint)length); + + lock (sendLock) + { + if (!sock.Connected) + return new AsyncReply(false); + + var rt = new AsyncReply(); + + if (asyncSending || held) + { + sendBufferQueue.Enqueue(new KeyValuePair, byte[]>(rt, msg)); + } + else + { + asyncSending = true; + sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, rt);// null); + //sock.SendAsync(new ArraySegment(msg), SocketFlags.None).ContinueWith(DataSent); + } + + return rt; + } + } } } \ No newline at end of file diff --git a/Esyur/Net/Sockets/WSSocket.cs b/Esyur/Net/Sockets/WSSocket.cs index 53dc6fb..a6345df 100644 --- a/Esyur/Net/Sockets/WSSocket.cs +++ b/Esyur/Net/Sockets/WSSocket.cs @@ -309,5 +309,10 @@ namespace Esyur.Net.Sockets } } + + public AsyncReply SendAsync(byte[] message, int offset, int length) + { + throw new NotImplementedException(); + } } } \ No newline at end of file