mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-09-14 04:53:17 +00:00
Bugfix
This commit is contained in:
@@ -38,17 +38,19 @@ using Esyur.Core;
|
||||
|
||||
namespace Esyur.Net.Sockets
|
||||
{
|
||||
public delegate void ISocketReceiveEvent(NetworkBuffer buffer);
|
||||
public delegate void ISocketConnectEvent();
|
||||
public delegate void ISocketCloseEvent();
|
||||
//public delegate void ISocketReceiveEvent(NetworkBuffer buffer);
|
||||
//public delegate void ISocketConnectEvent();
|
||||
//public delegate void ISocketCloseEvent();
|
||||
|
||||
public interface ISocket : IDestructible
|
||||
{
|
||||
SocketState State { get; }
|
||||
|
||||
event ISocketReceiveEvent OnReceive;
|
||||
event ISocketConnectEvent OnConnect;
|
||||
event ISocketCloseEvent OnClose;
|
||||
//event ISocketReceiveEvent OnReceive;
|
||||
//event ISocketConnectEvent OnConnect;
|
||||
//event ISocketCloseEvent OnClose;
|
||||
|
||||
INetworkReceiver<ISocket> Receiver { get; set; }
|
||||
|
||||
AsyncReply<bool> SendAsync(byte[] message, int offset, int length);
|
||||
|
||||
|
@@ -41,6 +41,9 @@ namespace Esyur.Net.Sockets
|
||||
{
|
||||
public class SSLSocket : ISocket
|
||||
{
|
||||
|
||||
public INetworkReceiver<ISocket> Receiver { get; set; }
|
||||
|
||||
Socket sock;
|
||||
byte[] receiveBuffer;
|
||||
|
||||
@@ -59,12 +62,11 @@ namespace Esyur.Net.Sockets
|
||||
|
||||
SocketState state = SocketState.Initial;
|
||||
|
||||
public event ISocketReceiveEvent OnReceive;
|
||||
public event ISocketConnectEvent OnConnect;
|
||||
public event ISocketCloseEvent OnClose;
|
||||
//public event ISocketReceiveEvent OnReceive;
|
||||
//public event ISocketConnectEvent OnConnect;
|
||||
//public event ISocketCloseEvent OnClose;
|
||||
public event DestroyedEvent OnDestroy;
|
||||
|
||||
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
|
||||
|
||||
SslStream ssl;
|
||||
X509Certificate2 cert;
|
||||
@@ -87,8 +89,8 @@ namespace Esyur.Net.Sockets
|
||||
{
|
||||
await BeginAsync();
|
||||
state = SocketState.Established;
|
||||
OnConnect?.Invoke();
|
||||
|
||||
//OnConnect?.Invoke();
|
||||
Receiver?.NetworkConnect(this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -271,7 +273,8 @@ namespace Esyur.Net.Sockets
|
||||
}
|
||||
}
|
||||
|
||||
OnClose?.Invoke();
|
||||
Receiver?.NetworkClose(this);
|
||||
//OnClose?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -428,7 +431,9 @@ namespace Esyur.Net.Sockets
|
||||
|
||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)bytesReceived);
|
||||
|
||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
|
||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
|
||||
ssl.BeginRead(receiveBuffer, 0, receiveBuffer.Length, ReceiveCallback, this);
|
||||
|
||||
@@ -453,7 +458,10 @@ namespace Esyur.Net.Sockets
|
||||
public void Destroy()
|
||||
{
|
||||
Close();
|
||||
Receiver = null;
|
||||
receiveNetworkBuffer = null;
|
||||
OnDestroy?.Invoke(this);
|
||||
OnDestroy = null;
|
||||
}
|
||||
|
||||
public async AsyncReply<ISocket> AcceptAsync()
|
||||
|
@@ -39,6 +39,8 @@ namespace Esyur.Net.Sockets
|
||||
{
|
||||
public class TCPSocket : ISocket
|
||||
{
|
||||
public INetworkReceiver<ISocket> Receiver { get; set; }
|
||||
|
||||
Socket sock;
|
||||
byte[] receiveBuffer;
|
||||
|
||||
@@ -58,9 +60,9 @@ namespace Esyur.Net.Sockets
|
||||
|
||||
SocketState state = SocketState.Initial;
|
||||
|
||||
public event ISocketReceiveEvent OnReceive;
|
||||
public event ISocketConnectEvent OnConnect;
|
||||
public event ISocketCloseEvent OnClose;
|
||||
//public event ISocketReceiveEvent OnReceive;
|
||||
//public event ISocketConnectEvent OnConnect;
|
||||
//public event ISocketCloseEvent OnClose;
|
||||
public event DestroyedEvent OnDestroy;
|
||||
|
||||
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
|
||||
@@ -103,7 +105,8 @@ namespace Esyur.Net.Sockets
|
||||
{
|
||||
|
||||
state = SocketState.Established;
|
||||
OnConnect?.Invoke();
|
||||
//OnConnect?.Invoke();
|
||||
Receiver?.NetworkConnect(this);
|
||||
Begin();
|
||||
rt.Trigger(true);
|
||||
}
|
||||
@@ -134,7 +137,8 @@ namespace Esyur.Net.Sockets
|
||||
}
|
||||
|
||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
|
||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
if (state == SocketState.Established)
|
||||
sock.ReceiveAsync(receiveBufferSegment, SocketFlags.None).ContinueWith(DataReceived);
|
||||
|
||||
@@ -163,11 +167,18 @@ namespace Esyur.Net.Sockets
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
else if (e.SocketError != SocketError.Success)
|
||||
{
|
||||
Close();
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
var recCount = e.BytesTransferred > e.Count ? e.Count : e.BytesTransferred;
|
||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
|
||||
|
||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
|
||||
if (state == SocketState.Established)
|
||||
while (!sock.ReceiveAsync(e))
|
||||
@@ -189,6 +200,12 @@ namespace Esyur.Net.Sockets
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
else if (e.SocketError != SocketError.Success)
|
||||
{
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//if (e.BytesTransferred > 100000)
|
||||
// Console.WriteLine("BytesTransferred is large " + e.BytesTransferred);
|
||||
@@ -197,7 +214,8 @@ namespace Esyur.Net.Sockets
|
||||
|
||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
|
||||
|
||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -341,7 +359,8 @@ namespace Esyur.Net.Sockets
|
||||
}
|
||||
}
|
||||
|
||||
OnClose?.Invoke();
|
||||
//OnClose?.Invoke();
|
||||
Receiver?.NetworkClose(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -454,7 +473,15 @@ namespace Esyur.Net.Sockets
|
||||
public void Destroy()
|
||||
{
|
||||
Close();
|
||||
//OnClose = null;
|
||||
//OnConnect = null;
|
||||
//OnReceive = null;
|
||||
Receiver = null;
|
||||
receiveNetworkBuffer = null;
|
||||
socketArgs.Completed -= SocketArgs_Completed;
|
||||
socketArgs = null;
|
||||
OnDestroy?.Invoke(this);
|
||||
OnDestroy = null;
|
||||
}
|
||||
|
||||
public ISocket Accept()
|
||||
|
@@ -34,10 +34,11 @@ using System.IO;
|
||||
using Esyur.Core;
|
||||
using Esyur.Resource;
|
||||
using Esyur.Data;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Esyur.Net.Sockets
|
||||
{
|
||||
public class WSSocket : ISocket
|
||||
public class WSSocket : ISocket, INetworkReceiver<ISocket>
|
||||
{
|
||||
WebsocketPacket pkt_receive = new WebsocketPacket();
|
||||
WebsocketPacket pkt_send = new WebsocketPacket();
|
||||
@@ -49,9 +50,9 @@ namespace Esyur.Net.Sockets
|
||||
object sendLock = new object();
|
||||
bool held;
|
||||
|
||||
public event ISocketReceiveEvent OnReceive;
|
||||
public event ISocketConnectEvent OnConnect;
|
||||
public event ISocketCloseEvent OnClose;
|
||||
//public event ISocketReceiveEvent OnReceive;
|
||||
//public event ISocketConnectEvent OnConnect;
|
||||
//public event ISocketCloseEvent OnClose;
|
||||
public event DestroyedEvent OnDestroy;
|
||||
|
||||
long totalSent, totalReceived;
|
||||
@@ -63,8 +64,6 @@ namespace Esyur.Net.Sockets
|
||||
get { return (IPEndPoint)sock.LocalEndPoint; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
public IPEndPoint RemoteEndPoint
|
||||
{
|
||||
get { return sock.RemoteEndPoint; }
|
||||
@@ -79,6 +78,7 @@ namespace Esyur.Net.Sockets
|
||||
}
|
||||
}
|
||||
|
||||
public INetworkReceiver<ISocket> Receiver { get; set; }
|
||||
|
||||
public WSSocket(ISocket socket)
|
||||
{
|
||||
@@ -86,12 +86,175 @@ namespace Esyur.Net.Sockets
|
||||
pkt_send.Mask = false;
|
||||
pkt_send.Opcode = WebsocketPacket.WSOpcode.BinaryFrame;
|
||||
sock = socket;
|
||||
sock.OnClose += Sock_OnClose;
|
||||
sock.OnConnect += Sock_OnConnect;
|
||||
sock.OnReceive += Sock_OnReceive;
|
||||
|
||||
sock.Receiver = this;
|
||||
|
||||
//sock.OnClose += Sock_OnClose;
|
||||
//sock.OnConnect += Sock_OnConnect;
|
||||
//sock.OnReceive += Sock_OnReceive;
|
||||
}
|
||||
|
||||
private void Sock_OnReceive(NetworkBuffer buffer)
|
||||
//private void Sock_OnReceive(NetworkBuffer buffer)
|
||||
//{
|
||||
|
||||
//}
|
||||
|
||||
//private void Sock_OnConnect()
|
||||
//{
|
||||
// OnConnect?.Invoke();
|
||||
//}
|
||||
|
||||
//private void Sock_OnClose()
|
||||
//{
|
||||
// OnClose?.Invoke();
|
||||
//}
|
||||
|
||||
public void Send(WebsocketPacket packet)
|
||||
{
|
||||
lock (sendLock)
|
||||
if (packet.Compose())
|
||||
sock.Send(packet.Data);
|
||||
}
|
||||
|
||||
public void Send(byte[] message)
|
||||
{
|
||||
lock (sendLock)
|
||||
{
|
||||
if (held)
|
||||
{
|
||||
sendNetworkBuffer.Write(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
totalSent += message.Length;
|
||||
//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)
|
||||
{
|
||||
if (held)
|
||||
{
|
||||
sendNetworkBuffer.Write(message, (uint)offset, (uint)size);
|
||||
}
|
||||
else
|
||||
{
|
||||
totalSent += size;
|
||||
//Console.WriteLine("TX " + size + "/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
|
||||
|
||||
pkt_send.Message = new byte[size];
|
||||
Buffer.BlockCopy(message, offset, pkt_send.Message, 0, size);
|
||||
if (pkt_send.Compose())
|
||||
sock.Send(pkt_send.Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Close()
|
||||
{
|
||||
sock?.Close();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> Connect(string hostname, ushort port)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
public bool Begin()
|
||||
{
|
||||
return sock.Begin();
|
||||
}
|
||||
|
||||
public bool Trigger(ResourceTrigger trigger)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
Close();
|
||||
//OnClose = null;
|
||||
//OnConnect = null;
|
||||
//OnReceive = null;
|
||||
receiveNetworkBuffer = null;
|
||||
//sock.OnReceive -= Sock_OnReceive;
|
||||
//sock.OnClose -= Sock_OnClose;
|
||||
//sock.OnConnect -= Sock_OnConnect;
|
||||
sock.Receiver = null;
|
||||
sock = null;
|
||||
OnDestroy?.Invoke(this);
|
||||
OnDestroy = null;
|
||||
}
|
||||
|
||||
public AsyncReply<ISocket> AcceptAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Hold()
|
||||
{
|
||||
//Console.WriteLine("WS Hold ");
|
||||
held = true;
|
||||
}
|
||||
|
||||
public void Unhold()
|
||||
{
|
||||
lock (sendLock)
|
||||
{
|
||||
held = false;
|
||||
|
||||
var message = sendNetworkBuffer.Read();
|
||||
|
||||
//Console.WriteLine("WS Unhold {0}", message == null ? 0 : message.Length);
|
||||
|
||||
if (message == null)
|
||||
return;
|
||||
|
||||
totalSent += message.Length;
|
||||
|
||||
pkt_send.Message = message;
|
||||
if (pkt_send.Compose())
|
||||
sock.Send(pkt_send.Data);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISocket Accept()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> BeginAsync()
|
||||
{
|
||||
return sock.BeginAsync();
|
||||
}
|
||||
|
||||
public void NetworkClose(ISocket sender)
|
||||
{
|
||||
Receiver?.NetworkClose(sender);
|
||||
}
|
||||
|
||||
public void NetworkReceive(ISocket sender, NetworkBuffer buffer)
|
||||
{
|
||||
|
||||
if (sock.State == SocketState.Closed || sock.State == SocketState.Terminated)
|
||||
@@ -161,8 +324,9 @@ namespace Esyur.Net.Sockets
|
||||
|
||||
if (offset == msg.Length)
|
||||
{
|
||||
// Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
|
||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
|
||||
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -180,152 +344,20 @@ namespace Esyur.Net.Sockets
|
||||
|
||||
//Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
|
||||
|
||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
|
||||
processing = false;
|
||||
|
||||
if (buffer.Available > 0 && !buffer.Protected)
|
||||
Sock_OnReceive(buffer);
|
||||
Receiver?.NetworkReceive(this, buffer);
|
||||
//Sock_OnReceive(buffer);
|
||||
}
|
||||
|
||||
private void Sock_OnConnect()
|
||||
|
||||
public void NetworkConnect(ISocket sender)
|
||||
{
|
||||
OnConnect?.Invoke();
|
||||
}
|
||||
|
||||
private void Sock_OnClose()
|
||||
{
|
||||
OnClose?.Invoke();
|
||||
}
|
||||
|
||||
public void Send(WebsocketPacket packet)
|
||||
{
|
||||
lock (sendLock)
|
||||
if (packet.Compose())
|
||||
sock.Send(packet.Data);
|
||||
}
|
||||
|
||||
public void Send(byte[] message)
|
||||
{
|
||||
lock (sendLock)
|
||||
{
|
||||
if (held)
|
||||
{
|
||||
sendNetworkBuffer.Write(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
totalSent += message.Length;
|
||||
//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)
|
||||
{
|
||||
if (held)
|
||||
{
|
||||
sendNetworkBuffer.Write(message, (uint)offset, (uint)size);
|
||||
}
|
||||
else
|
||||
{
|
||||
totalSent += size;
|
||||
//Console.WriteLine("TX " + size + "/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
|
||||
|
||||
pkt_send.Message = new byte[size];
|
||||
Buffer.BlockCopy(message, offset, pkt_send.Message, 0, size);
|
||||
if (pkt_send.Compose())
|
||||
sock.Send(pkt_send.Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Close()
|
||||
{
|
||||
sock.Close();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> Connect(string hostname, ushort port)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
public bool Begin()
|
||||
{
|
||||
return sock.Begin();
|
||||
}
|
||||
|
||||
public bool Trigger(ResourceTrigger trigger)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
Close();
|
||||
OnDestroy?.Invoke(this);
|
||||
}
|
||||
|
||||
public AsyncReply<ISocket> AcceptAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Hold()
|
||||
{
|
||||
//Console.WriteLine("WS Hold ");
|
||||
held = true;
|
||||
}
|
||||
|
||||
public void Unhold()
|
||||
{
|
||||
lock (sendLock)
|
||||
{
|
||||
held = false;
|
||||
|
||||
var message = sendNetworkBuffer.Read();
|
||||
|
||||
//Console.WriteLine("WS Unhold {0}", message == null ? 0 : message.Length);
|
||||
|
||||
if (message == null)
|
||||
return;
|
||||
|
||||
totalSent += message.Length;
|
||||
|
||||
pkt_send.Message = message;
|
||||
if (pkt_send.Compose())
|
||||
sock.Send(pkt_send.Data);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISocket Accept()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> BeginAsync()
|
||||
{
|
||||
return sock.BeginAsync();
|
||||
Receiver?.NetworkConnect(this);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user