/// <summary> /// Initializes a new instance of the <see cref="SMProtocol"/> class. /// </summary> /// <param name="uri">The URI.</param> /// <param name="streamsStore">The streams store.</param> /// <param name="options">Protocol options</param> internal SMProtocol(Uri uri, IStreamStore streamsStore, SMProtocolOptions options) { this.streamsStore = streamsStore; this.webSocket = new WebSocket(uri.ToString()); this.webSocket.OnOpen += this.OnSocketOpen; this.webSocket.OnPing += this.OnSocketPing; this.webSocket.OnClose += this.OnSocketClose; if (options != null) { this.options = options; } this.serializer = new FrameSerializer(this.options); this.builder = new FrameBuilder(); }
/// <summary> /// Initializes a new instance of the <see cref="SMSession"/> class. /// </summary> /// <param name="uri">the URI.</param> /// <param name="isServer">the Server flag.</param> /// <param name="options">Session options.</param> public SMSession(Uri uri, bool isServer, SMProtocolOptions options) { this.isFlowControlEnabled = options.IsFlowControl; this.CreditAddition = options.CreditAddition; this.CurrentCreditBalanceFromServer = Convert.ToInt64(CreditAddition); this.CurrentCreditBalanceToServer = Convert.ToInt64(CreditAddition); this.streams = new List <SMStream>(); this.closedStreams = new List <SMStream>(); this.isServer = isServer; this.protocol = new SMProtocol(uri, this, options); this.protocol.OnSessionFrame += this.OnSessionFrame; this.protocol.OnClose += this.OnProtocolClose; this.protocol.OnOpen += this.OnProtocolOpen; this.protocol.OnError += this.OnProtocolError; this.protocol.OnPing += this.OnProtocolPing; if (options.UseCompression) { this.Protocol.SetProcessors(new List <IMessageProcessor> { new CompressionProcessor() }); } }
/// <summary> /// Create SM session. Close if previously opened. /// </summary> /// <param name="uri">The URI</param> /// <param name="option">Options for session</param> /// <returns>The return code.</returns> private static int CreateSession(Uri uri, SMProtocolOptions option) { int res = 0; CloseSession(); session = new SMSession(uri, false, option); // URI can still be invalid, missing protocol prefix for example try { session.Open(); session.OnOpen += OnSessionOpened; session.OnClose += OnSessionClosed; session.OnError += OnSessionError; session.OnStreamOpened += OnStreamOpened; session.OnStreamClosed += OnStreamClosed; // wait until session changes its state // for high latency connections we cannot just start using this session sessionMonitorEvent.Reset(); timeoutSessionMonitor = false; ThreadPool.QueueUserWorkItem(new WaitCallback(SessionMonitorProc), 0); sessionMonitorEvent.WaitOne(300000); timeoutSessionMonitor = true; } catch { SMLogger.LogError("Unable to open session for " + uri); res = 1; } return res; }
/// <summary> /// Processes the specified input data. /// </summary> /// <param name="inputData">The input data.</param> /// <param name="type">The type.</param> /// <param name="optios">Options.</param> /// <param name="flags">Flags.</param> public void Process(ref byte[] inputData, DirectionProcessType type, SMProtocolOptions optios, int flags) { inputData = type == DirectionProcessType.Inbound ? Encrypt(inputData) : Decrypt(inputData); }
/// <summary> /// Initializes a new instance of the <see cref="FrameSerializer"/> class. /// </summary> /// <param name="options">The options.</param> public FrameSerializer(SMProtocolOptions options) { Option = options; }
/// <summary> /// Processes the specified headers. /// </summary> /// <param name="headers">The headers.</param> /// <param name="type">The type.</param> /// <param name="options">Options.</param> /// <param name="flags">Flags.</param> public void Process(ref byte[] headers, DirectionProcessType type, SMProtocolOptions options, int flags) { if (!options.UseCompression) return; if (!options.CompressionIsStateful) { _compressOutZStream.SetDictionary(CompressionDictionary.Dictionary); _decompressOutZStream.SetDictionary(CompressionDictionary.Dictionary); } if ((flags & (byte)FrameFlags.FlagNoHeaderCompression1) == 0 && (flags & (byte)FrameFlags.FlagNoHeaderCompression2) == 0) headers = type == DirectionProcessType.Inbound ? Decompress(headers) : Compress(headers); }