2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 13:33:13 +00:00
This commit is contained in:
2020-02-26 03:14:22 +03:00
parent 7a21f6a928
commit fde1b1d8ad
39 changed files with 1108 additions and 502 deletions

View File

@ -58,7 +58,9 @@ namespace Esyur.Net.Sockets
AsyncReply<bool> Connect(string hostname, ushort port);
bool Begin();
//ISocket Accept();
AsyncReply<ISocket> Accept();
AsyncReply<ISocket> AcceptAsync();
ISocket Accept();
IPEndPoint RemoteEndPoint { get; }
IPEndPoint LocalEndPoint { get; }

View File

@ -311,24 +311,26 @@ namespace Esyur.Net.Sockets
OnDestroy?.Invoke(this);
}
public AsyncReply<ISocket> Accept()
public async AsyncReply<ISocket> AcceptAsync()
{
var reply = new AsyncReply<ISocket>();
//var reply = new AsyncReply<ISocket>();
try
{
sock.AcceptAsync().ContinueWith((x) =>
{
try
{
reply.Trigger(new SSLSocket(x.Result, cert, true));
}
catch
{
reply.Trigger(null);
}
return new SSLSocket(await sock.AcceptAsync(), cert, true);
}, null);
//sock.AcceptAsync().ContinueWith((x) =>
//{
// try
// {
// reply.Trigger(new SSLSocket(x.Result, cert, true));
// }
// catch
// {
// reply.Trigger(null);
// }
//}, null);
}
catch
@ -337,7 +339,7 @@ namespace Esyur.Net.Sockets
return null;
}
return reply;
//return reply;
}
public void Hold()
@ -354,5 +356,18 @@ namespace Esyur.Net.Sockets
{
throw new NotImplementedException();
}
public ISocket Accept()
{
try
{
return new SSLSocket(sock.Accept(), cert, true);
}
catch
{
state = SocketState.Terminated;
return null;
}
}
}
}

View File

@ -178,7 +178,9 @@ namespace Esyur.Net.Sockets
//lock (receiveNetworkBuffer.SyncLock)
// Console.WriteLine(e. + " " + e.BytesTransferred);
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)e.BytesTransferred);
var recCount = e.BytesTransferred > e.Count ? e.Count : e.BytesTransferred;
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
//Console.WriteLine("TC IN: " + (uint)e.BytesTransferred + " " + DC.ToHex(receiveBuffer, 0, (uint)e.BytesTransferred));
@ -365,7 +367,15 @@ namespace Esyur.Net.Sockets
else
{
asyncSending = true;
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, null);
try
{
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, null);
}
catch {
asyncSending = false;
state = SocketState.Terminated;
Close();
}
//sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent);
}
}
@ -403,7 +413,7 @@ namespace Esyur.Net.Sockets
}
catch (Exception ex2)
{
Console.WriteLine("Level 2 {0}", ex2);
state = SocketState.Terminated;
}
Global.Log("TCPSocket", LogType.Error, ex.ToString());
@ -427,33 +437,35 @@ namespace Esyur.Net.Sockets
OnDestroy?.Invoke(this);
}
public AsyncReply<ISocket> Accept()
public ISocket Accept()
{
var reply = new AsyncReply<ISocket>();
try
{
sock.AcceptAsync().ContinueWith((x) =>
{
try
{
reply.Trigger(new TCPSocket(x.Result));
}
catch
{
reply.Trigger(null);
}
});
var s = sock.Accept();
return new TCPSocket(s);
}
catch
{
state = SocketState.Terminated;
return null;
}
return reply;
}
public async AsyncReply<ISocket> AcceptAsync()
{
try
{
var s = await sock.AcceptAsync();
return new TCPSocket(s);
}
catch
{
state = SocketState.Terminated;
return null;
}
}
public void Hold()
{
held = true;
@ -494,12 +506,23 @@ namespace Esyur.Net.Sockets
else
{
asyncSending = true;
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, rt);// null);
try
{
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, rt);// null);
}
catch
{
asyncSending = false;
state = SocketState.Terminated;
Close();
}
//sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent);
}
return rt;
}
}
}
}

View File

@ -63,13 +63,13 @@ namespace Esyur.Net.Sockets
get { return (IPEndPoint)sock.LocalEndPoint; }
}
public IPEndPoint RemoteEndPoint
{
get { return sock.RemoteEndPoint; }
}
public IPEndPoint RemoteEndPoint
{
get { return sock.RemoteEndPoint; }
}
public SocketState State
{
@ -79,7 +79,7 @@ namespace Esyur.Net.Sockets
}
}
public WSSocket(ISocket socket)
{
pkt_send.FIN = true;
@ -103,7 +103,7 @@ namespace Esyur.Net.Sockets
if (processing)
return;
var msg = buffer.Read();
if (msg == null)
@ -161,7 +161,7 @@ namespace Esyur.Net.Sockets
if (offset == msg.Length)
{
// Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
// Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
OnReceive?.Invoke(receiveNetworkBuffer);
return;
}
@ -200,14 +200,14 @@ namespace Esyur.Net.Sockets
public void Send(WebsocketPacket packet)
{
lock(sendLock)
lock (sendLock)
if (packet.Compose())
sock.Send(packet.Data);
}
public void Send(byte[] message)
{
lock(sendLock)
lock (sendLock)
{
if (held)
{
@ -219,13 +219,16 @@ namespace Esyur.Net.Sockets
//Console.WriteLine("TX " + message.Length +"/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
pkt_send.Message = message;
if (pkt_send.Compose())
sock.Send(pkt_send.Data);
}
}
}
public void Send(byte[] message, int offset, int size)
{
lock (sendLock)
@ -258,7 +261,7 @@ namespace Esyur.Net.Sockets
throw new NotImplementedException();
}
public bool Begin()
{
return sock.Begin();
@ -275,20 +278,20 @@ namespace Esyur.Net.Sockets
OnDestroy?.Invoke(this);
}
public AsyncReply<ISocket> Accept()
public AsyncReply<ISocket> AcceptAsync()
{
throw new NotImplementedException();
}
public void Hold()
{
//Console.WriteLine("WS Hold ");
//Console.WriteLine("WS Hold ");
held = true;
}
public void Unhold()
{
lock(sendLock)
lock (sendLock)
{
held = false;
@ -305,7 +308,7 @@ namespace Esyur.Net.Sockets
if (pkt_send.Compose())
sock.Send(pkt_send.Data);
}
}
@ -314,5 +317,10 @@ namespace Esyur.Net.Sockets
{
throw new NotImplementedException();
}
public ISocket Accept()
{
throw new NotImplementedException();
}
}
}