123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Linq;
- using System.Net;
- using System.Security.Authentication;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.Extensions.Logging;
- using SuperSocket.Common;
- using SuperSocket.SocketBase;
- using SuperSocket.SocketBase.Command;
- using SuperSocket.SocketBase.Protocol;
- using SuperWebSocket.Protocol;
- using SuperWebSocket.SubProtocol;
- namespace SuperWebSocket
- {
-
-
-
- public interface IWebSocketSession : IAppSession
- {
-
-
-
-
-
-
- string Method { get; set; }
-
-
-
- string Host { get; }
-
-
-
-
-
-
- string Path { get; set; }
-
-
-
-
-
-
- string HttpVersion { get; set; }
-
-
-
- string SecWebSocketVersion { get; }
-
-
-
- string Origin { get; }
-
-
-
- string UriScheme { get; }
-
-
-
-
-
-
- bool Handshaked { get; }
-
-
-
-
-
-
- void SendRawData(byte[] data, int offset, int length);
-
-
-
-
-
-
-
- bool TrySendRawData(byte[] data, int offset, int length);
-
-
-
- new IWebSocketServer AppServer { get; }
-
-
-
-
-
-
- IProtocolProcessor ProtocolProcessor { get; set; }
-
-
-
-
-
- string GetAvailableSubProtocol(string protocol);
- }
-
-
-
- public class WebSocketSession : WebSocketSession<WebSocketSession>
- {
-
-
-
- public new WebSocketServer AppServer
- {
- get { return (WebSocketServer)base.AppServer; }
- }
- }
-
-
-
-
- public class WebSocketSession<TWebSocketSession> : AppSession<TWebSocketSession, IWebSocketFragment>, IWebSocketSession, IAppSession
- where TWebSocketSession : WebSocketSession<TWebSocketSession>, new()
- {
-
-
-
-
-
-
- public string Method { get; set; }
-
-
-
-
-
-
- public string Path { get; set; }
-
-
-
-
-
-
- public string HttpVersion { get; set; }
-
-
-
- public string Host { get { return this.Items.GetValue<string>(WebSocketConstant.Host, string.Empty); } }
-
-
-
- public string Origin { get; internal set; }
-
-
-
- public string Upgrade { get { return this.Items.GetValue<string>(WebSocketConstant.Upgrade, string.Empty); } }
-
-
-
- public string Connection { get { return this.Items.GetValue<string>(WebSocketConstant.Connection, string.Empty); } }
-
-
-
- public string SecWebSocketVersion { get { return this.Items.GetValue<string>(WebSocketConstant.SecWebSocketVersion, string.Empty); } }
-
-
-
- public string SecWebSocketProtocol { get { return this.Items.GetValue<string>(WebSocketConstant.SecWebSocketProtocol, string.Empty); } }
- internal List<WebSocketDataFrame> Frames { get; private set; }
- internal DateTime StartClosingHandshakeTime { get; private set; }
- private const string m_CurrentTokenSlotName = "CurrentRequestToken";
- internal LocalDataStoreSlot SetCurrentToken(string token)
- {
- var slot = Thread.GetNamedDataSlot(m_CurrentTokenSlotName);
- Thread.SetData(slot, token);
- return slot;
- }
-
-
-
- public string CurrentToken
- {
- get
- {
- return Thread.GetData(Thread.GetNamedDataSlot(m_CurrentTokenSlotName)) as string;
- }
- }
-
-
-
- public new WebSocketServer<TWebSocketSession> AppServer
- {
- get { return (WebSocketServer<TWebSocketSession>)base.AppServer; }
- }
- IWebSocketServer IWebSocketSession.AppServer
- {
- get { return (IWebSocketServer)base.AppServer; }
- }
- string IWebSocketSession.GetAvailableSubProtocol(string protocol)
- {
-
- if (string.IsNullOrEmpty(protocol))
- {
- SubProtocol = AppServer.DefaultSubProtocol;
- return string.Empty;
- }
- var arrNames = protocol.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- if (protocol.ToLower().Contains("ocpp2.0"))
- {
- arrNames = new string[] { "ocpp2.0" };
- }
- foreach (var name in arrNames)
- {
- var subProtocol = AppServer.GetSubProtocol(name);
- if (subProtocol != null)
- {
- SubProtocol = subProtocol;
- return name;
- }
- }
- return string.Empty;
- }
-
-
-
- public string UriScheme
- {
- get
- {
- if (SocketSession.SecureProtocol == SslProtocols.None)
- return WebSocketConstant.WsSchema;
- else
- return WebSocketConstant.WssSchema;
- }
- }
-
-
-
- public ISubProtocol<TWebSocketSession> SubProtocol { get; private set; }
- private bool m_Handshaked = false;
-
-
-
-
-
-
- public bool Handshaked
- {
- get { return m_Handshaked; }
- }
- internal void OnHandshakeSuccess()
- {
- m_Handshaked = true;
- SetCookie();
- OnSessionStarted();
- AppServer.FireOnNewSessionConnected(this);
- }
-
-
-
-
-
-
- public bool InClosing { get; private set; }
-
-
-
- protected override void OnInit()
- {
- Frames = new List<WebSocketDataFrame>();
- base.OnInit();
- }
- void IAppSession.StartSession()
- {
-
- }
-
-
-
- private void SetCookie()
- {
- string cookieValue = this.Items.GetValue<string>(WebSocketConstant.Cookie, string.Empty);
- if (string.IsNullOrEmpty(cookieValue))
- return;
- var cookies = new StringDictionary();
- this.Cookies = cookies;
- string[] pairs = cookieValue.Split(';');
- int pos;
- string key, value;
- foreach (var p in pairs)
- {
- pos = p.IndexOf('=');
- if (pos <= 0)
- continue;
- key = p.Substring(0, pos).Trim();
- pos += 1;
- if (pos < p.Length)
- value = p.Substring(pos).Trim();
- else
- value = string.Empty;
- if (string.IsNullOrEmpty(value))
- {
- cookies[key] = string.Empty;
- continue;
- }
- try
- {
- cookies[key] = Uri.UnescapeDataString(value);
- }
- catch (Exception e)
- {
- Logger.LogError(this, string.Format("Failed to read cookie, key: {0}, value: {1}.", key, value), e);
- }
- }
- }
-
-
-
- public StringDictionary Cookies { get; private set; }
-
-
-
-
- public override void Send(string message)
- {
- ProtocolProcessor.SendMessage(this, message);
- }
-
-
-
-
-
- public override bool TrySend(string message)
- {
- return ProtocolProcessor.TrySendMessage(this, message);
- }
-
-
-
-
-
-
- public override void Send(byte[] data, int offset, int length)
- {
- if (!ProtocolProcessor.CanSendBinaryData)
- {
- Logger.LogError("The websocket of this version cannot used for sending binary data!");
- return;
- }
- ProtocolProcessor.SendData(this, data, offset, length);
- }
-
-
-
-
-
- public override bool TrySend(ArraySegment<byte> segment)
- {
- if (!ProtocolProcessor.CanSendBinaryData)
- {
- Logger.LogError("The websocket of this version cannot used for sending binary data!");
- return false;
- }
- return ProtocolProcessor.TrySendData(this, segment.Array, segment.Offset, segment.Count);
- }
-
-
-
-
-
-
-
- public override bool TrySend(byte[] data, int offset, int length)
- {
- if (!ProtocolProcessor.CanSendBinaryData)
- {
- Logger.LogError("The websocket of this version cannot used for sending binary data!");
- return false;
- }
- return ProtocolProcessor.TrySendData(this, data, offset, length);
- }
-
-
-
-
- public override void Send(ArraySegment<byte> segment)
- {
- this.Send(segment.Array, segment.Offset, segment.Count);
- }
-
-
-
-
-
-
- void IWebSocketSession.SendRawData(byte[] data, int offset, int length)
- {
- base.Send(data, offset, length);
- }
-
-
-
-
-
-
-
-
-
- bool IWebSocketSession.TrySendRawData(byte[] data, int offset, int length)
- {
- return base.TrySend(new ArraySegment<byte>(data, offset, length));
- }
-
-
-
-
-
- internal bool TrySendRawData(IList<ArraySegment<byte>> segments)
- {
- return base.TrySend(segments);
- }
-
-
-
-
- public void CloseWithHandshake(string reasonText)
- {
- this.CloseWithHandshake(ProtocolProcessor.CloseStatusClode.NormalClosure, reasonText);
- }
-
-
-
-
-
- public void CloseWithHandshake(int statusCode, string reasonText)
- {
- if (!InClosing)
- InClosing = true;
- ProtocolProcessor.SendCloseHandshake(this, statusCode, reasonText);
- StartClosingHandshakeTime = DateTime.UtcNow;
- AppServer.PushToCloseHandshakeQueue(this);
- }
-
-
-
-
- public void SendCloseHandshakeResponse(int statusCode)
- {
- if (!InClosing)
- InClosing = true;
- ProtocolProcessor.SendCloseHandshake(this, statusCode, string.Empty);
- }
-
-
-
-
- public override void Close(CloseReason reason)
- {
- if (reason == CloseReason.TimeOut && ProtocolProcessor != null)
- {
- CloseWithHandshake(ProtocolProcessor.CloseStatusClode.NormalClosure, "Session timeOut");
- return;
- }
- base.Close(reason);
- }
-
-
-
-
-
-
- public IProtocolProcessor ProtocolProcessor { get; set; }
-
-
-
-
- internal protected virtual void HandleUnknownCommand(SubRequestInfo requestInfo)
- {
- }
-
-
-
-
- protected override void HandleUnknownRequest(IWebSocketFragment requestInfo)
- {
- base.Close();
- }
- }
- }
|