mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 11:32:59 +00:00
Sockets
This commit is contained in:
parent
eca142b37b
commit
741819e11c
@ -11,7 +11,7 @@
|
|||||||
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
|
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
|
||||||
<RepositoryUrl>https://github.com/esyur/esyur-dotnet/</RepositoryUrl>
|
<RepositoryUrl>https://github.com/esyur/esyur-dotnet/</RepositoryUrl>
|
||||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||||
<Version>1.3.2</Version>
|
<Version>1.3.3</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1041,8 +1041,8 @@ namespace Esyur.Data
|
|||||||
if (data.Length < offset + length)
|
if (data.Length < offset + length)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (length == data.Length && offset == 0)
|
// if (length == data.Length && offset == 0)
|
||||||
return data;
|
// return data.ToArray();
|
||||||
|
|
||||||
var b = new byte[length];
|
var b = new byte[length];
|
||||||
Buffer.BlockCopy(data, (int)offset, b, 0, (int)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>
|
<PackageLicenseUrl>https://github.com/Esyur/Esyur-dotnet/blob/master/LICENSE</PackageLicenseUrl>
|
||||||
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
|
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Version>1.4.7</Version>
|
<Version>1.4.8</Version>
|
||||||
<RepositoryUrl>https://github.com/esyur/esyur-dotnet</RepositoryUrl>
|
<RepositoryUrl>https://github.com/esyur/esyur-dotnet</RepositoryUrl>
|
||||||
<Authors>Ahmed Kh. Zamil</Authors>
|
<Authors>Ahmed Kh. Zamil</Authors>
|
||||||
<AssemblyVersion>1.3.1.0</AssemblyVersion>
|
<AssemblyVersion>1.3.1.0</AssemblyVersion>
|
||||||
|
@ -36,6 +36,7 @@ using Esyur.Data;
|
|||||||
using Esyur.Net.Packets;
|
using Esyur.Net.Packets;
|
||||||
using Esyur.Misc;
|
using Esyur.Misc;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using Esyur.Core;
|
||||||
|
|
||||||
namespace Esyur.Net.HTTP
|
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)
|
if (Response.Handled == true)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -248,7 +249,7 @@ namespace Esyur.Net.HTTP
|
|||||||
{
|
{
|
||||||
Response.Number = HTTPResponsePacket.ResponseCode.HTTP_NOTFOUND;
|
Response.Number = HTTPResponsePacket.ResponseCode.HTTP_NOTFOUND;
|
||||||
Send("File Not Found");
|
Send("File Not Found");
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -288,22 +289,24 @@ namespace Esyur.Net.HTTP
|
|||||||
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
|
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||||
{
|
{
|
||||||
|
|
||||||
var buffer = new byte[5000];
|
var buffer = new byte[60000];
|
||||||
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var n = fs.Read(buffer, 0, 5000);
|
var n = fs.Read(buffer, 0, 60000);
|
||||||
|
|
||||||
if (n <= 0)
|
if (n <= 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//Thread.Sleep(50);
|
//Thread.Sleep(50);
|
||||||
base.Send(buffer, 0, n);
|
await base.SendAsync(buffer, 0, n);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@ -311,7 +314,11 @@ namespace Esyur.Net.HTTP
|
|||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
finally { }
|
finally {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,10 +38,10 @@ using Esyur.Resource;
|
|||||||
|
|
||||||
namespace Esyur.Net
|
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 ISocket sock;
|
||||||
// private bool connected;
|
// private bool connected;
|
||||||
|
|
||||||
private DateTime lastAction;
|
private DateTime lastAction;
|
||||||
|
|
||||||
@ -53,9 +53,9 @@ namespace Esyur.Net
|
|||||||
public event DataReceivedEvent OnDataReceived;
|
public event DataReceivedEvent OnDataReceived;
|
||||||
public event ConnectionClosedEvent OnClose;
|
public event ConnectionClosedEvent OnClose;
|
||||||
public event DestroyedEvent OnDestroy;
|
public event DestroyedEvent OnDestroy;
|
||||||
object receivingLock = new object();
|
//object receivingLock = new object();
|
||||||
|
|
||||||
object sendLock = new object();
|
//object sendLock = new object();
|
||||||
|
|
||||||
bool processing = false;
|
bool processing = false;
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ namespace Esyur.Net
|
|||||||
if (sock != null)
|
if (sock != null)
|
||||||
sock.Close();
|
sock.Close();
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Global.Log("NetworkConenction:Close", LogType.Error, ex.ToString());
|
Global.Log("NetworkConenction:Close", LogType.Error, ex.ToString());
|
||||||
|
|
||||||
@ -260,41 +260,41 @@ namespace Esyur.Net
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public virtual void Send(byte[] msg)
|
public virtual AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||||
{
|
|
||||||
lock (sendLock)
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (sock != null)
|
return sock.SendAsync(message, offset, length);
|
||||||
{
|
|
||||||
lastAction = DateTime.Now;
|
|
||||||
sock.Send(msg);
|
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return new AsyncReply<bool>(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void Send(byte[] msg)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
sock.Send(msg);
|
||||||
|
lastAction = DateTime.Now;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void Send(byte[] msg, int offset, int length)
|
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);
|
sock.Send(msg, offset, length);
|
||||||
|
lastAction = DateTime.Now;
|
||||||
}
|
}
|
||||||
}
|
catch
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,9 +50,10 @@ namespace Esyur.Net.Sockets
|
|||||||
event ISocketConnectEvent OnConnect;
|
event ISocketConnectEvent OnConnect;
|
||||||
event ISocketCloseEvent OnClose;
|
event ISocketCloseEvent OnClose;
|
||||||
|
|
||||||
|
AsyncReply<bool> SendAsync(byte[] message, int offset, int length);
|
||||||
|
|
||||||
void Send(byte[] message);
|
void Send(byte[] message);
|
||||||
void Send(byte[] message, int offset, int size);
|
void Send(byte[] message, int offset, int length);
|
||||||
void Close();
|
void Close();
|
||||||
AsyncReply<bool> Connect(string hostname, ushort port);
|
AsyncReply<bool> Connect(string hostname, ushort port);
|
||||||
bool Begin();
|
bool Begin();
|
||||||
|
@ -349,5 +349,10 @@ namespace Esyur.Net.Sockets
|
|||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -50,7 +50,7 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
readonly object sendLock = new object();
|
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;
|
bool began = false;
|
||||||
@ -349,21 +349,23 @@ namespace Esyur.Net.Sockets
|
|||||||
public void Send(byte[] message, int offset, int size)
|
public void Send(byte[] message, int offset, int size)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
var msg = message.Clip((uint)offset, (uint)size);
|
var msg = message.Clip((uint)offset, (uint)size);
|
||||||
|
|
||||||
lock (sendLock)
|
lock (sendLock)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!sock.Connected)
|
if (!sock.Connected)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (asyncSending || held)
|
if (asyncSending || held)
|
||||||
{
|
{
|
||||||
sendBufferQueue.Enqueue(msg);
|
sendBufferQueue.Enqueue(new KeyValuePair<AsyncReply<bool>, byte[]>(null, msg));// message.Clip((uint)offset, (uint)size));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
asyncSending = true;
|
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);
|
//sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -372,24 +374,23 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
private void PacketSent(IAsyncResult ar)
|
private void PacketSent(IAsyncResult ar)
|
||||||
{
|
{
|
||||||
try
|
if (ar != null && ar.AsyncState != null)
|
||||||
{
|
((AsyncReply<bool>)ar.AsyncState).Trigger(true);
|
||||||
|
|
||||||
lock (sendLock)
|
lock (sendLock)
|
||||||
{
|
{
|
||||||
if (sendBufferQueue.Count > 0)
|
if (sendBufferQueue.Count > 0)
|
||||||
{
|
{
|
||||||
byte[] data = sendBufferQueue.Dequeue();
|
var kv = sendBufferQueue.Dequeue();
|
||||||
|
|
||||||
sock.BeginSend(data, 0, data.Length, SocketFlags.None, PacketSent, null);
|
try
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
asyncSending = false;
|
sock.BeginSend(kv.Value, 0, kv.Value.Length, SocketFlags.None, PacketSent, kv.Key);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
kv.Key.Trigger(false);
|
||||||
|
|
||||||
if (state != SocketState.Closed && !sock.Connected)
|
if (state != SocketState.Closed && !sock.Connected)
|
||||||
{
|
{
|
||||||
state = SocketState.Terminated;
|
state = SocketState.Terminated;
|
||||||
@ -401,6 +402,12 @@ namespace Esyur.Net.Sockets
|
|||||||
Global.Log("TCPSocket", LogType.Error, ex.ToString());
|
Global.Log("TCPSocket", LogType.Error, ex.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
asyncSending = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool Trigger(ResourceTrigger trigger)
|
public bool Trigger(ResourceTrigger trigger)
|
||||||
{
|
{
|
||||||
@ -460,5 +467,32 @@ namespace Esyur.Net.Sockets
|
|||||||
held = false;
|
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