mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 03:32:57 +00:00
Sockets
This commit is contained in:
parent
eca142b37b
commit
741819e11c
@ -11,7 +11,7 @@
|
||||
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/esyur/esyur-dotnet/</RepositoryUrl>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<Version>1.3.2</Version>
|
||||
<Version>1.3.3</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -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);
|
||||
|
@ -7,7 +7,7 @@
|
||||
<PackageLicenseUrl>https://github.com/Esyur/Esyur-dotnet/blob/master/LICENSE</PackageLicenseUrl>
|
||||
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>1.4.7</Version>
|
||||
<Version>1.4.8</Version>
|
||||
<RepositoryUrl>https://github.com/esyur/esyur-dotnet</RepositoryUrl>
|
||||
<Authors>Ahmed Kh. Zamil</Authors>
|
||||
<AssemblyVersion>1.3.1.0</AssemblyVersion>
|
||||
|
@ -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<bool> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -38,10 +38,10 @@ using Esyur.Resource;
|
||||
|
||||
namespace Esyur.Net
|
||||
{
|
||||
public class NetworkConnection: IDestructible// <TS>: IResource where TS : NetworkSession
|
||||
public class NetworkConnection : IDestructible// <TS>: 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<bool> SendAsync(byte[] message, int offset, int length)
|
||||
{
|
||||
try
|
||||
{
|
||||
return sock.SendAsync(message, offset, length);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new AsyncReply<bool>(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
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,9 +50,10 @@ namespace Esyur.Net.Sockets
|
||||
event ISocketConnectEvent OnConnect;
|
||||
event ISocketCloseEvent OnClose;
|
||||
|
||||
AsyncReply<bool> 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<bool> Connect(string hostname, ushort port);
|
||||
bool Begin();
|
||||
|
@ -349,5 +349,10 @@ namespace Esyur.Net.Sockets
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -50,9 +50,9 @@ namespace Esyur.Net.Sockets
|
||||
|
||||
readonly object sendLock = new object();
|
||||
|
||||
Queue<byte[]> sendBufferQueue = new Queue<byte[]>();
|
||||
Queue<KeyValuePair<AsyncReply<bool>, byte[]>> sendBufferQueue = new Queue<KeyValuePair<AsyncReply<bool>, byte[]>>();// Queue<byte[]>();
|
||||
|
||||
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<AsyncReply<bool>, 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<byte>(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<bool>)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<bool> SendAsync(byte[] message, int offset, int length)
|
||||
{
|
||||
|
||||
var msg = message.Clip((uint)offset, (uint)length);
|
||||
|
||||
lock (sendLock)
|
||||
{
|
||||
if (!sock.Connected)
|
||||
return new AsyncReply<bool>(false);
|
||||
|
||||
var rt = new AsyncReply<bool>();
|
||||
|
||||
if (asyncSending || held)
|
||||
{
|
||||
sendBufferQueue.Enqueue(new KeyValuePair<AsyncReply<bool>, byte[]>(rt, msg));
|
||||
}
|
||||
else
|
||||
{
|
||||
asyncSending = true;
|
||||
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, rt);// null);
|
||||
//sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent);
|
||||
}
|
||||
|
||||
return rt;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -309,5 +309,10 @@ namespace Esyur.Net.Sockets
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user