using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;

public class PresetServer : MonoBehaviour
{
	public void Start()
	{
		int num = 47560;
		try
		{
			this.m_listener = new TcpListener(IPAddress.Any, num);
			this.m_listener.Start();
			Debug.Log("Port" + num.ToString() + "のListenを開始しました。");
		}
		catch (Exception ex)
		{
			NDebug.Assert(string.Concat(new object[]
			{
				"インターネット機能：ポート番号",
				num,
				"は既に利用されています。\n他の通信アプリケーションを終了してゲームを再起動して下さい。\n\n",
				ex.Message
			}), false);
		}
	}

	public void OnDestroy()
	{
		if (this.m_listener != null)
		{
			this.m_listener.Stop();
		}
		this.m_listener = null;
		if (this.nsWEB != null)
		{
			this.nsWEB.Close();
		}
		if (this.tcpWEB != null)
		{
			this.tcpWEB.Close();
		}
		this.nsWEB = null;
		this.tcpWEB = null;
	}

	public void Update()
	{
		if (this.m_listener == null)
		{
			return;
		}
		if (this.nsWEB == null)
		{
			try
			{
				if (!this.m_listener.Pending())
				{
					return;
				}
			}
			catch (Exception ex)
			{
				Debug.LogError("PresetServer::Pending 例外。" + ex.Message);
				this.m_listener = null;
				return;
			}
			this.tcpWEB = this.m_listener.AcceptTcpClient();
			if (this.tcpWEB == null)
			{
				return;
			}
			this.nsWEB = this.tcpWEB.GetStream();
			if (this.nsWEB == null)
			{
				this.tcpWEB.Close();
				this.nsWEB = null;
				this.tcpWEB = null;
			}
			this.tcpWEB_count = 0;
			return;
		}
		else
		{
			if (!this.nsWEB.DataAvailable)
			{
				this.tcpWEB_count++;
				if (this.tcpWEB_count > 100)
				{
					this.nsWEB.Close();
					this.tcpWEB.Close();
					this.nsWEB = null;
					this.tcpWEB = null;
					Debug.Log("NotCanread クライアントを切断しました");
				}
				return;
			}
			byte[] array = new byte[4096];
			MemoryStream memoryStream = new MemoryStream();
			for (;;)
			{
				int num = this.nsWEB.Read(array, 0, array.Length);
				if (num == 0)
				{
					break;
				}
				memoryStream.Write(array, 0, num);
				if (!this.nsWEB.DataAvailable)
				{
					goto Block_8;
				}
			}
			this.nsWEB.Close();
			this.tcpWEB.Close();
			this.nsWEB = null;
			this.tcpWEB = null;
			Debug.Log("クライアントから切断されました。");
			return;
			Block_8:
			string text = this.enc.GetString(memoryStream.ToArray());
			memoryStream.Close();
			Debug.Log(text);
			if (text == null)
			{
				this.nsWEB.Close();
				this.tcpWEB.Close();
				this.nsWEB = null;
				this.tcpWEB = null;
				Debug.Log("resMsg==null");
				return;
			}
			int num2 = text.IndexOf("token=");
			if (0 < num2)
			{
				text = WWW.UnEscapeURL(text.Substring(num2, text.Length - num2).Replace("token=", string.Empty));
				CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
				if (!characterMgr.IsBusy() && characterMgr.GetMaid(0) != null)
				{
					CharacterMgr.Preset preset = CharacterMgr.LoadePresetLowCapacityData(text);
					if (preset != null)
					{
						characterMgr.PresetSet(characterMgr.GetMaid(0), preset);
						string s = "HTTP/1.0 204 OK\r\nConnection: close\r\n\r\n";
						byte[] bytes = this.enc.GetBytes(s);
						this.nsWEB.Write(bytes, 0, bytes.Length);
					}
					else
					{
						Debug.Log("CM3D2のプリセットデータではありません");
					}
				}
			}
			else
			{
				string text2 = "HTTP/1.0 204 OK\r\nConnection: close\r\n\r\n";
				byte[] bytes2 = this.enc.GetBytes(text2);
				this.nsWEB.Write(bytes2, 0, bytes2.Length);
				Debug.Log(text2);
			}
			this.nsWEB.Close();
			this.tcpWEB.Close();
			this.nsWEB = null;
			this.tcpWEB = null;
		}
	}

	private Encoding enc = Encoding.UTF8;

	private NetworkStream nsWEB;

	private TcpClient tcpWEB;

	private TcpListener m_listener;

	private int tcpWEB_count;
}
