mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
Blazor support
This commit is contained in:
221
Esiur/Net/Sockets/ClientWSocket.cs
Normal file
221
Esiur/Net/Sockets/ClientWSocket.cs
Normal file
@ -0,0 +1,221 @@
|
||||
using Esiur.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Net.WebSockets;
|
||||
using Esiur.Net.Packets;
|
||||
using Esiur.Resource;
|
||||
using Esiur.Misc;
|
||||
using System.Drawing;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Esiur.Net.Sockets
|
||||
{
|
||||
public class ClientWSocket : ISocket
|
||||
{
|
||||
|
||||
ClientWebSocket sock;
|
||||
|
||||
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
|
||||
NetworkBuffer sendNetworkBuffer = new NetworkBuffer();
|
||||
|
||||
byte[] websocketReceiveBuffer = new byte[10240];
|
||||
ArraySegment<byte> websocketReceiveBufferSegment;
|
||||
|
||||
object sendLock = new object();
|
||||
bool held;
|
||||
|
||||
public event DestroyedEvent OnDestroy;
|
||||
|
||||
long totalSent, totalReceived;
|
||||
|
||||
|
||||
public IPEndPoint LocalEndPoint { get; } = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
||||
public IPEndPoint RemoteEndPoint { get; } = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
||||
|
||||
public SocketState State { get; internal set; } = SocketState.Closed;
|
||||
|
||||
public INetworkReceiver<ISocket> Receiver { get; set; }
|
||||
|
||||
public ClientWSocket()
|
||||
{
|
||||
websocketReceiveBufferSegment = new ArraySegment<byte>(websocketReceiveBuffer);
|
||||
}
|
||||
|
||||
|
||||
public void Send(byte[] message)
|
||||
{
|
||||
|
||||
lock (sendLock)
|
||||
{
|
||||
if (held)
|
||||
{
|
||||
sendNetworkBuffer.Write(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
totalSent += message.Length;
|
||||
sock.SendAsync(new ArraySegment<byte>(message), WebSocketMessageType.Binary,
|
||||
true, new System.Threading.CancellationToken());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Send(byte[] message, int offset, int size)
|
||||
{
|
||||
lock (sendLock)
|
||||
{
|
||||
if (held)
|
||||
{
|
||||
sendNetworkBuffer.Write(message, (uint)offset, (uint)size);
|
||||
}
|
||||
else
|
||||
{
|
||||
totalSent += size;
|
||||
|
||||
sock.SendAsync(new ArraySegment<byte>(message, offset, size),
|
||||
WebSocketMessageType.Binary, true, new System.Threading.CancellationToken());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Close()
|
||||
{
|
||||
sock.CloseAsync(WebSocketCloseStatus.NormalClosure, "", new System.Threading.CancellationToken());
|
||||
}
|
||||
|
||||
public bool Secure { get; set; }
|
||||
|
||||
public async AsyncReply<bool> Connect(string hostname, ushort port)
|
||||
{
|
||||
var url = new Uri($"{(Secure ? "wss" : "ws")}://{hostname}:{port}");
|
||||
|
||||
sock = new ClientWebSocket();
|
||||
await sock.ConnectAsync(url, new CancellationToken());
|
||||
|
||||
State = SocketState.Established;
|
||||
|
||||
sock.ReceiveAsync(websocketReceiveBufferSegment, new CancellationToken())
|
||||
.ContinueWith(NetworkReceive);
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public bool Begin()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Trigger(ResourceTrigger trigger)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
Close();
|
||||
|
||||
receiveNetworkBuffer = null;
|
||||
Receiver = null;
|
||||
|
||||
sock = null;
|
||||
OnDestroy?.Invoke(this);
|
||||
OnDestroy = null;
|
||||
}
|
||||
|
||||
public AsyncReply<ISocket> AcceptAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Hold()
|
||||
{
|
||||
held = true;
|
||||
}
|
||||
|
||||
public void Unhold()
|
||||
{
|
||||
lock (sendLock)
|
||||
{
|
||||
held = false;
|
||||
|
||||
var message = sendNetworkBuffer.Read();
|
||||
|
||||
if (message == null)
|
||||
return;
|
||||
|
||||
totalSent += message.Length;
|
||||
|
||||
sock.SendAsync(new ArraySegment<byte>(message), WebSocketMessageType.Binary,
|
||||
true, new System.Threading.CancellationToken());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public async AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||
{
|
||||
if (held)
|
||||
{
|
||||
sendNetworkBuffer.Write(message, (uint)offset, (uint)length);
|
||||
}
|
||||
else
|
||||
{
|
||||
totalSent += length;
|
||||
|
||||
await sock.SendAsync(new ArraySegment<byte>(message, offset, length),
|
||||
WebSocketMessageType.Binary, true, new System.Threading.CancellationToken());
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public ISocket Accept()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> BeginAsync()
|
||||
{
|
||||
return new AsyncReply<bool>(true);
|
||||
}
|
||||
|
||||
|
||||
private void NetworkReceive(Task<WebSocketReceiveResult> task)
|
||||
{
|
||||
|
||||
if (sock.State == WebSocketState.Closed)
|
||||
{
|
||||
Receiver?.NetworkClose(this);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var receivedLength = task.Result.Count;
|
||||
|
||||
receiveNetworkBuffer.Write(websocketReceiveBuffer, 0, (uint)receivedLength);
|
||||
|
||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
|
||||
sock.ReceiveAsync(websocketReceiveBufferSegment, new CancellationToken())
|
||||
.ContinueWith(NetworkReceive);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void NetworkConnect(ISocket sender)
|
||||
{
|
||||
Receiver?.NetworkConnect(this);
|
||||
}
|
||||
}
|
||||
}
|
@ -36,5 +36,4 @@ public enum SocketState
|
||||
Connecting,
|
||||
Established,
|
||||
Closed,
|
||||
//Terminated
|
||||
}
|
||||
|
@ -172,118 +172,7 @@ public class TCPSocket : ISocket
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
|
||||
//private void DataReceived(Task<int> task)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// // SocketError err;
|
||||
|
||||
// if (state == SocketState.Closed || state == SocketState.Terminated)
|
||||
// return;
|
||||
|
||||
// if (task.Result <= 0)
|
||||
// {
|
||||
// Close();
|
||||
// return;
|
||||
// }
|
||||
|
||||
// receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
|
||||
// //OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
// Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
// if (state == SocketState.Established)
|
||||
// sock.ReceiveAsync(receiveBufferSegment, SocketFlags.None).ContinueWith(DataReceived);
|
||||
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// if (state != SocketState.Closed && !sock.Connected)
|
||||
// {
|
||||
// state = SocketState.Terminated;
|
||||
// Close();
|
||||
// }
|
||||
|
||||
// Global.Log("TCPSocket", LogType.Error, ex.ToString());
|
||||
// }
|
||||
//}
|
||||
|
||||
//private void SocketArgs_Completed(object sender, SocketAsyncEventArgs e)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// if (state != SocketState.Established)
|
||||
// return;
|
||||
|
||||
// if (e.BytesTransferred <= 0)
|
||||
// {
|
||||
// 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);
|
||||
// Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
|
||||
// if (state == SocketState.Established)
|
||||
// while (!sock.ReceiveAsync(e))
|
||||
// {
|
||||
// if (e.SocketError != SocketError.Success)
|
||||
// {
|
||||
// Close();
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (State != SocketState.Established)
|
||||
// return;
|
||||
|
||||
// //if (e.BytesTransferred < 0)
|
||||
// // Console.WriteLine("BytesTransferred is less than zero");
|
||||
|
||||
// if (e.BytesTransferred <= 0)
|
||||
// {
|
||||
// Close();
|
||||
// return;
|
||||
// }
|
||||
// else if (e.SocketError != SocketError.Success)
|
||||
// {
|
||||
// Close();
|
||||
// return;
|
||||
// }
|
||||
|
||||
|
||||
// //if (e.BytesTransferred > 100000)
|
||||
// // Console.WriteLine("BytesTransferred is large " + e.BytesTransferred);
|
||||
|
||||
// recCount = e.BytesTransferred > e.Count ? e.Count : e.BytesTransferred;
|
||||
|
||||
// receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
|
||||
|
||||
// //OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
// Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
// }
|
||||
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// if (state != SocketState.Closed && !sock.Connected)
|
||||
// {
|
||||
// state = SocketState.Terminated;
|
||||
// Close();
|
||||
// }
|
||||
|
||||
// Global.Log("TCPSocket", LogType.Error, ex.ToString());
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
public IPEndPoint LocalEndPoint
|
||||
{
|
||||
get { return (IPEndPoint)sock.LocalEndPoint; }
|
||||
@ -312,40 +201,7 @@ public class TCPSocket : ISocket
|
||||
Connect(hostname, port);
|
||||
|
||||
}
|
||||
|
||||
//private void DataSent(Task<int> task)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// lock (sendLock)
|
||||
// {
|
||||
|
||||
// if (sendBufferQueue.Count > 0)
|
||||
// {
|
||||
// byte[] data = sendBufferQueue.Dequeue();
|
||||
// //Console.WriteLine(Encoding.UTF8.GetString(data));
|
||||
// sock.SendAsync(new ArraySegment<byte>(data), SocketFlags.None).ContinueWith(DataSent);
|
||||
// }
|
||||
|
||||
// else
|
||||
// {
|
||||
// asyncSending = false;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// if (state != SocketState.Closed && !sock.Connected)
|
||||
// {
|
||||
// state = SocketState.Terminated;
|
||||
// Close();
|
||||
// }
|
||||
|
||||
// asyncSending = false;
|
||||
|
||||
// Global.Log("TCPSocket", LogType.Error, ex.ToString());
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
public TCPSocket(IPEndPoint localEndPoint)
|
||||
{
|
||||
@ -368,9 +224,6 @@ public class TCPSocket : ISocket
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public IPEndPoint RemoteEndPoint
|
||||
{
|
||||
get { return (IPEndPoint)sock.RemoteEndPoint; }
|
||||
@ -616,14 +469,14 @@ public class TCPSocket : ISocket
|
||||
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||
{
|
||||
|
||||
if (state == SocketState.Closed)// || state == SocketState.Terminated)
|
||||
if (state == SocketState.Closed)
|
||||
return new AsyncReply<bool>(false);
|
||||
|
||||
var msg = message.Clip((uint)offset, (uint)length);
|
||||
|
||||
lock (sendLock)
|
||||
{
|
||||
if (state == SocketState.Closed)// || state == SocketState.Terminated)
|
||||
if (state == SocketState.Closed)
|
||||
return new AsyncReply<bool>(false);
|
||||
|
||||
if (!sock.Connected)
|
||||
@ -641,13 +494,12 @@ public class TCPSocket : ISocket
|
||||
try
|
||||
{
|
||||
currentReply = rt;
|
||||
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, sendCallback, this);// null);
|
||||
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, sendCallback, this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
rt.TriggerError(ex);
|
||||
asyncSending = false;
|
||||
//state = SocketState.Terminated;
|
||||
Close();
|
||||
}
|
||||
//sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent);
|
||||
|
@ -56,7 +56,7 @@ public class WSocket : ISocket, INetworkReceiver<ISocket>
|
||||
|
||||
long totalSent, totalReceived;
|
||||
|
||||
bool processing = false;
|
||||
|
||||
|
||||
public IPEndPoint LocalEndPoint
|
||||
{
|
||||
@ -254,14 +254,12 @@ public class WSocket : ISocket, INetworkReceiver<ISocket>
|
||||
public void NetworkReceive(ISocket sender, NetworkBuffer buffer)
|
||||
{
|
||||
|
||||
if (sock.State == SocketState.Closed)// || sock.State == SocketState.Terminated)
|
||||
if (sock.State == SocketState.Closed)
|
||||
return;
|
||||
|
||||
if (buffer.Protected)
|
||||
return;
|
||||
|
||||
if (processing)
|
||||
return;
|
||||
|
||||
|
||||
var msg = buffer.Read();
|
||||
@ -270,7 +268,6 @@ public class WSocket : ISocket, INetworkReceiver<ISocket>
|
||||
return;
|
||||
|
||||
var wsPacketLength = pkt_receive.Parse(msg, 0, (uint)msg.Length);
|
||||
//Console.WriteLine("WSP: " + wsPacketLength);
|
||||
|
||||
if (wsPacketLength < 0)
|
||||
{
|
||||
@ -289,12 +286,14 @@ public class WSocket : ISocket, INetworkReceiver<ISocket>
|
||||
}
|
||||
else if (pkt_receive.Opcode == WebsocketPacket.WSOpcode.Ping)
|
||||
{
|
||||
var pkt_pong = new WebsocketPacket();
|
||||
var pkt_pong = new WebsocketPacket()
|
||||
{
|
||||
FIN = true,
|
||||
Mask = false,
|
||||
Opcode = WebsocketPacket.WSOpcode.Pong,
|
||||
Message = pkt_receive.Message
|
||||
};
|
||||
|
||||
pkt_pong.FIN = true;
|
||||
pkt_pong.Mask = false;
|
||||
pkt_pong.Opcode = WebsocketPacket.WSOpcode.Pong;
|
||||
pkt_pong.Message = pkt_receive.Message;
|
||||
offset += (uint)wsPacketLength;
|
||||
|
||||
Send(pkt_pong);
|
||||
@ -322,7 +321,6 @@ public class WSocket : ISocket, INetworkReceiver<ISocket>
|
||||
if (offset == msg.Length)
|
||||
{
|
||||
|
||||
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
return;
|
||||
}
|
||||
@ -330,25 +328,20 @@ public class WSocket : ISocket, INetworkReceiver<ISocket>
|
||||
wsPacketLength = pkt_receive.Parse(msg, offset, (uint)msg.Length);
|
||||
}
|
||||
|
||||
if (wsPacketLength < 0)//(offset < msg.Length) && (offset > 0))
|
||||
if (wsPacketLength < 0)
|
||||
{
|
||||
//receiveNetworkBuffer.HoldFor(msg, offset, (uint)(msg.Length - offset), (uint)msg.Length + (uint)-wsPacketLength);
|
||||
// save the incomplete packet to the heldBuffer queue
|
||||
|
||||
buffer.HoldFor(msg, offset, (uint)(msg.Length - offset), (uint)(msg.Length - offset) + (uint)-wsPacketLength);
|
||||
|
||||
}
|
||||
|
||||
//Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
|
||||
|
||||
//OnReceive?.Invoke(receiveNetworkBuffer);
|
||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||
|
||||
processing = false;
|
||||
|
||||
if (buffer.Available > 0 && !buffer.Protected)
|
||||
Receiver?.NetworkReceive(this, buffer);
|
||||
//Sock_OnReceive(buffer);
|
||||
NetworkReceive(this, buffer);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user