<commit_before>﻿using System;
using System.Collections.Generic;
using System.Text;

using Newtonsoft.Json.Linq;

using Wizcorp.MageSDK.Log;
using Wizcorp.MageSDK.MageClient.Message.Client;

using Wizcorp.MageSDK.Unity;

namespace Wizcorp.MageSDK.MageClient.Message
{
	public class MessageStream
	{
		private static Mage Mage
		{
			get { return Mage.Instance; }
		}

		private static Logger Logger
		{
			get { return Mage.Logger("MessageStream"); }
		}

		// Endpoint and credentials
		private string endpoint;
		private Dictionary<string, string> headers;
		private string sessionKey;

		// Current transport client
		private TransportClient transportClient;

		// Current message stack
		private int currentMessageId;
		private int largestMessageId;
		private Dictionary<int, JToken> messageQueue;
		private List<string> confirmIds;


		//
		private void Initialize()
		{
			// Stop the transport client if it exists
			if (transportClient != null)
			{
				transportClient.Stop();
			}

			// Reset the session key
			sessionKey = null;

			// Initialize the message queue
			currentMessageId = -1;
			largestMessageId = -1;
			messageQueue = new Dictionary<int, JToken>();
			confirmIds = new List<string>();

			Logger.Debug("(Re)Initialized message queue");
		}


		// Constructor
		public MessageStream(TransportType transport = TransportType.LONGPOLLING)
		{
			// Start transport client when session is acquired
			Mage.EventManager.On("session.set", (sender, session) => {
				// Make sure we re-initialize between multiple session sets to prevent stale requests
				Initialize();

				// Setup key and start stream
				sessionKey = UnityEngine.WWW.EscapeURL(session["key"].ToString());
				transportClient.Start();
			});

			// Stop the message client when session is lost
			Mage.EventManager.On("session.unset", (sender, reason) => {
				Initialize();
			});

			// Also stop the message client when the application is stopped
			#if UNITY_EDITOR
			UnityEditorPlayMode.OnEditorModeChanged += newState => {
				if (newState == EditorPlayModeState.Stopped)
				{
					Initialize();
				}
				if (newState == EditorPlayModeState.Paused && transportClient.running)
				{
					transportClient.Stop();
				}
				if (newState == EditorPlayModeState.Playing && !transportClient.running && sessionKey != null)
				{
					transportClient.Start();
				}
			};
			#endif

			UnityApplicationState.Instance.OnAppStateChanged += pauseStatus => {
				if (pauseStatus && transportClient.running)
				{
					transportClient.Stop();
				}
				if (!pauseStatus && !transportClient.running && sessionKey != null)
				{
					transportClient.Start();
				}
			};

			// Set the selected transport client (or the default)
			SetTransport(transport);
		}


		//
		public void Dispose()
		{
			Initialize();
		}


		// Updates URI and credentials 
		public void SetEndpoint(string baseURL, Dictionary<string, string> headers = null)
		{
			this.endpoint = baseURL + "/msgstream";
			this.headers = headers;
		}

<commit_msg>fixed according to pr comments<commit_after>﻿using System;
using System.Collections.Generic;

using Newtonsoft.Json.Linq;

using Wizcorp.MageSDK.Log;
using Wizcorp.MageSDK.MageClient.Message.Client;

using Wizcorp.MageSDK.Unity;

namespace Wizcorp.MageSDK.MageClient.Message
{
	public class MessageStream
	{
		private static Mage Mage
		{
			get { return Mage.Instance; }
		}

		private static Logger Logger
		{
			get { return Mage.Logger("MessageStream"); }
		}

		// Endpoint and credentials
		private string endpoint;
		private Dictionary<string, string> headers;
		private string sessionKey;

		// Current transport client
		private TransportClient transportClient;

		// Current message stack
		private int currentMessageId;
		private int largestMessageId;
		private Dictionary<int, JToken> messageQueue;
		private List<string> confirmIds;


		// Resets the stream system
		private void Reset()
		{
			// Stop the transport client if it exists
			if (transportClient != null)
			{
				transportClient.Stop();
			}

			// Reset the session key
			sessionKey = null;

			// Initialize the message queue
			currentMessageId = -1;
			largestMessageId = -1;
			messageQueue = new Dictionary<int, JToken>();
			confirmIds = new List<string>();

			Logger.Debug("Message stream system reset");
		}


		// Constructor
		public MessageStream(TransportType transport = TransportType.LONGPOLLING)
		{
			// Start transport client when session is acquired
			Mage.EventManager.On("session.set", (sender, session) => {
				// Make sure we re-initialize between multiple session sets to prevent stale requests
				Reset();

				// Setup key and start stream
				sessionKey = UnityEngine.WWW.EscapeURL(session["key"].ToString());
				transportClient.Start();
			});

			// Stop the message client when session is lost
			Mage.EventManager.On("session.unset", (sender, reason) => {
				Reset();
			});

			// Also stop the message client when the application is stopped
			#if UNITY_EDITOR
			UnityEditorPlayMode.OnEditorModeChanged += newState => {
				if (newState == EditorPlayModeState.Stopped)
				{
					Reset();
				}
				if (newState == EditorPlayModeState.Paused && transportClient.running)
				{
					transportClient.Stop();
				}
				if (newState == EditorPlayModeState.Playing && !transportClient.running && sessionKey != null)
				{
					transportClient.Start();
				}
			};
			#endif

			UnityApplicationState.Instance.OnAppStateChanged += pauseStatus => {
				if (pauseStatus && transportClient.running)
				{
					transportClient.Stop();
				}
				if (!pauseStatus && !transportClient.running && sessionKey != null)
				{
					transportClient.Start();
				}
			};

			// Set the selected transport client (or the default)
			SetTransport(transport);
		}


		// This method is called when this instance of MessageStream is no longer required
		public void Dispose()
		{
			Reset();
		}


		// Updates URI and credentials 
		public void SetEndpoint(string baseURL, Dictionary<string, string> headers = null)
		{
			this.endpoint = baseURL + "/msgstream";
			this.headers = headers;
		}


{'fraction_non_alphanumeric': 0.10383229610219456, 'fraction_numerical': 0.0006550933508024893, 'mean_word_length': 3.325070821529745, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5267667', 'n_tokens_mistral': 2142, 'n_tokens_neox': 1711, 'n_words': 474}