PresetServer.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using UnityEngine;
  7. public class PresetServer : MonoBehaviour
  8. {
  9. public void Start()
  10. {
  11. int num = 47560;
  12. try
  13. {
  14. this.m_listener = new TcpListener(IPAddress.Any, num);
  15. this.m_listener.Start();
  16. Debug.Log("Port" + num.ToString() + "のListenを開始しました。");
  17. }
  18. catch (Exception ex)
  19. {
  20. NDebug.Assert(string.Concat(new object[]
  21. {
  22. "インターネット機能:ポート番号",
  23. num,
  24. "は既に利用されています。\n他の通信アプリケーションを終了してゲームを再起動して下さい。\n\n",
  25. ex.Message
  26. }), false);
  27. }
  28. }
  29. public void OnDestroy()
  30. {
  31. if (this.m_listener != null)
  32. {
  33. this.m_listener.Stop();
  34. }
  35. this.m_listener = null;
  36. if (this.nsWEB != null)
  37. {
  38. this.nsWEB.Close();
  39. }
  40. if (this.tcpWEB != null)
  41. {
  42. this.tcpWEB.Close();
  43. }
  44. this.nsWEB = null;
  45. this.tcpWEB = null;
  46. }
  47. [Obsolete("GameUty.RidMenuDic廃止により使用不可", true)]
  48. public void Update()
  49. {
  50. if (this.m_listener == null)
  51. {
  52. return;
  53. }
  54. if (this.nsWEB == null)
  55. {
  56. try
  57. {
  58. if (!this.m_listener.Pending())
  59. {
  60. return;
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. Debug.LogError("PresetServer::Pending 例外。" + ex.Message);
  66. this.m_listener = null;
  67. return;
  68. }
  69. this.tcpWEB = this.m_listener.AcceptTcpClient();
  70. if (this.tcpWEB == null)
  71. {
  72. return;
  73. }
  74. this.nsWEB = this.tcpWEB.GetStream();
  75. if (this.nsWEB == null)
  76. {
  77. this.tcpWEB.Close();
  78. this.nsWEB = null;
  79. this.tcpWEB = null;
  80. }
  81. this.tcpWEB_count = 0;
  82. return;
  83. }
  84. else
  85. {
  86. if (!this.nsWEB.DataAvailable)
  87. {
  88. this.tcpWEB_count++;
  89. if (this.tcpWEB_count > 100)
  90. {
  91. this.nsWEB.Close();
  92. this.tcpWEB.Close();
  93. this.nsWEB = null;
  94. this.tcpWEB = null;
  95. Debug.Log("NotCanread クライアントを切断しました");
  96. }
  97. return;
  98. }
  99. byte[] array = new byte[4096];
  100. MemoryStream memoryStream = new MemoryStream();
  101. for (;;)
  102. {
  103. int num = this.nsWEB.Read(array, 0, array.Length);
  104. if (num == 0)
  105. {
  106. break;
  107. }
  108. memoryStream.Write(array, 0, num);
  109. if (!this.nsWEB.DataAvailable)
  110. {
  111. goto Block_8;
  112. }
  113. }
  114. this.nsWEB.Close();
  115. this.tcpWEB.Close();
  116. this.nsWEB = null;
  117. this.tcpWEB = null;
  118. Debug.Log("クライアントから切断されました。");
  119. return;
  120. Block_8:
  121. string text = this.enc.GetString(memoryStream.ToArray());
  122. memoryStream.Close();
  123. Debug.Log(text);
  124. if (text == null)
  125. {
  126. this.nsWEB.Close();
  127. this.tcpWEB.Close();
  128. this.nsWEB = null;
  129. this.tcpWEB = null;
  130. Debug.Log("resMsg==null");
  131. return;
  132. }
  133. int num2 = text.IndexOf("token=");
  134. if (0 < num2)
  135. {
  136. text = WWW.UnEscapeURL(text.Substring(num2, text.Length - num2).Replace("token=", string.Empty));
  137. CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
  138. if (!characterMgr.IsBusy() && characterMgr.GetMaid(0) != null)
  139. {
  140. CharacterMgr.Preset preset = CharacterMgr.LoadePresetLowCapacityData(text);
  141. if (preset != null)
  142. {
  143. characterMgr.PresetSet(characterMgr.GetMaid(0), preset, false);
  144. string s = "HTTP/1.0 204 OK\r\nConnection: close\r\n\r\n";
  145. byte[] bytes = this.enc.GetBytes(s);
  146. this.nsWEB.Write(bytes, 0, bytes.Length);
  147. }
  148. else
  149. {
  150. Debug.Log("CM3D2のプリセットデータではありません");
  151. }
  152. }
  153. }
  154. else
  155. {
  156. string text2 = "HTTP/1.0 204 OK\r\nConnection: close\r\n\r\n";
  157. byte[] bytes2 = this.enc.GetBytes(text2);
  158. this.nsWEB.Write(bytes2, 0, bytes2.Length);
  159. Debug.Log(text2);
  160. }
  161. this.nsWEB.Close();
  162. this.tcpWEB.Close();
  163. this.nsWEB = null;
  164. this.tcpWEB = null;
  165. }
  166. }
  167. private Encoding enc = Encoding.UTF8;
  168. private NetworkStream nsWEB;
  169. private TcpClient tcpWEB;
  170. private TcpListener m_listener;
  171. private int tcpWEB_count;
  172. }