203 lines
6.9 KiB
C#
203 lines
6.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using ln.logging;
|
|
using ln.http.exceptions;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using ln.type;
|
|
|
|
namespace ln.http.websocket
|
|
{
|
|
public enum WebSocketOpcode : int
|
|
{
|
|
CONTINUATION = 0x00,
|
|
TEXT = 0x01,
|
|
BINARY = 0x02,
|
|
CLOSE = 0x08,
|
|
PING = 0x09,
|
|
PONG = 0x0A,
|
|
|
|
INVALIDOPCODE = -1
|
|
}
|
|
|
|
public enum WebSocketState
|
|
{
|
|
HANDSHAKE,
|
|
OPEN,
|
|
CLOSING,
|
|
CLOSED,
|
|
ERROR
|
|
}
|
|
|
|
public delegate void WebSocketStateChanged(WebSocket webSocket,WebSocketState newState);
|
|
public delegate void WebSocketTextReceived(WebSocket webSocket, string text);
|
|
public delegate void WebSocketBytesReceived(WebSocket webSocket, byte[] bytes);
|
|
|
|
public class WebSocket
|
|
{
|
|
public event WebSocketTextReceived TextReceived;
|
|
public event WebSocketBytesReceived BytesReceived;
|
|
public event WebSocketStateChanged StateChanged;
|
|
|
|
public HttpRequestContext RequestContext { get; }
|
|
public Stream Stream { get; }
|
|
|
|
private WebSocketState _state = WebSocketState.HANDSHAKE;
|
|
public WebSocketState State
|
|
{
|
|
get => _state;
|
|
private set
|
|
{
|
|
_state = value;
|
|
StateChanged?.Invoke(this, _state);
|
|
}
|
|
}
|
|
|
|
public WebSocket(HttpRequestContext requestContext)
|
|
{
|
|
RequestContext = requestContext;
|
|
Stream = requestContext.ConnectionStream;
|
|
|
|
if ((!requestContext.Request.GetRequestHeader("upgrade", "").Contains("websocket")) && (!requestContext.Request.GetRequestHeader("connection", "").Contains("Upgrade")))
|
|
throw new HttpException(HttpStatusCode.BadRequest, "This resource is a websocket endpoint only");
|
|
|
|
if (!requestContext.Request.GetRequestHeader("Sec-WebSocket-Version", "").Equals("13"))
|
|
throw new HttpException(HttpStatusCode.BadRequest, "Unsupported Protocol Version (WebSocket)");
|
|
|
|
String wsKey = requestContext.Request.GetRequestHeader("Sec-WebSocket-Key");
|
|
|
|
HttpResponse httpResponse = new HttpResponse(HttpStatusCode.SwitchingProtocols);
|
|
httpResponse.AddHeader("upgrade", "websocket");
|
|
httpResponse.AddHeader("connection", "Upgrade");
|
|
httpResponse.AddHeader("Sec-WebSocket-Version", "13");
|
|
|
|
string acceptKey = String.Format("{0}258EAFA5-E914-47DA-95CA-C5AB0DC85B11", requestContext.Request.GetRequestHeader("Sec-WebSocket-Key"));
|
|
|
|
httpResponse.AddHeader(
|
|
"Sec-Websocket-Accept",
|
|
Convert.ToBase64String(SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(acceptKey)))
|
|
);
|
|
|
|
requestContext.Response = httpResponse;
|
|
|
|
requestContext.HttpConnection.SendResponse(requestContext);
|
|
State = WebSocketState.OPEN;
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
switch (State)
|
|
{
|
|
case WebSocketState.HANDSHAKE:
|
|
case WebSocketState.ERROR:
|
|
case WebSocketState.CLOSING:
|
|
State = WebSocketState.CLOSED;
|
|
Stream.Close();
|
|
break;
|
|
case WebSocketState.CLOSED:
|
|
break;
|
|
case WebSocketState.OPEN:
|
|
WebSocketFrame closeFrame = new WebSocketFrame(WebSocketOpcode.CLOSE);
|
|
lock (Stream)
|
|
{
|
|
Send(closeFrame);
|
|
State = WebSocketState.CLOSING;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void Run()
|
|
{
|
|
try
|
|
{
|
|
while (State != WebSocketState.CLOSED)
|
|
{
|
|
WebSocketFrame webSocketFrame = new WebSocketFrame(Stream);
|
|
switch (webSocketFrame.Opcode)
|
|
{
|
|
case WebSocketOpcode.TEXT:
|
|
_received(Encoding.UTF8.GetString(webSocketFrame.ApplicationData));
|
|
break;
|
|
case WebSocketOpcode.BINARY:
|
|
_received(webSocketFrame.ApplicationData);
|
|
break;
|
|
case WebSocketOpcode.CLOSE:
|
|
if (State == WebSocketState.OPEN)
|
|
{
|
|
WebSocketFrame closeFrame = new WebSocketFrame(WebSocketOpcode.CLOSE);
|
|
closeFrame.FIN = true;
|
|
lock (Stream)
|
|
{
|
|
Send(closeFrame);
|
|
State = WebSocketState.CLOSING;
|
|
}
|
|
}
|
|
|
|
State = WebSocketState.CLOSED;
|
|
Stream.Close();
|
|
break;
|
|
case WebSocketOpcode.PING:
|
|
WebSocketFrame pong = new WebSocketFrame(WebSocketOpcode.PONG);
|
|
pong.ApplicationData = webSocketFrame.ApplicationData;
|
|
Send(pong);
|
|
break;
|
|
}
|
|
}
|
|
} catch (IOException)
|
|
{
|
|
State = WebSocketState.ERROR;
|
|
Close();
|
|
} catch (Exception e)
|
|
{
|
|
Console.Error.WriteLine("WebSocket: Exception: {0}", e.ToString());
|
|
Console.Error.WriteLine(" {0}", e.StackTrace.ToString());
|
|
} finally
|
|
{
|
|
Stream.Close();
|
|
}
|
|
}
|
|
|
|
private void Send(WebSocketFrame frame)
|
|
{
|
|
lock (Stream)
|
|
{
|
|
if (State == WebSocketState.OPEN)
|
|
{
|
|
try
|
|
{
|
|
frame.WriteTo(Stream);
|
|
} catch (IOException)
|
|
{
|
|
if (State != WebSocketState.ERROR)
|
|
{
|
|
State = WebSocketState.ERROR;
|
|
Close();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
throw new IOException("WebSocket is not open");
|
|
}
|
|
}
|
|
|
|
private void _received(string textMessage)
|
|
{
|
|
TextReceived?.Invoke(this, textMessage);
|
|
Received(textMessage);
|
|
}
|
|
private void _received(byte[] bytes)
|
|
{
|
|
BytesReceived?.Invoke(this, bytes);
|
|
Received(bytes);
|
|
}
|
|
|
|
public virtual void Received(string text){}
|
|
public virtual void Received(byte[] bytes){}
|
|
|
|
public void Send(string textMessage) => Send(new WebSocketFrame(textMessage));
|
|
public void Send(byte[] binaryMessage) => Send(new WebSocketFrame(binaryMessage));
|
|
|
|
}
|
|
}
|