PresetServer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. public void Update()
  48. {
  49. if (this.m_listener == null)
  50. {
  51. return;
  52. }
  53. if (this.nsWEB == null)
  54. {
  55. try
  56. {
  57. if (!this.m_listener.Pending())
  58. {
  59. return;
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. Debug.LogError("PresetServer::Pending 例外。" + ex.Message);
  65. this.m_listener = null;
  66. return;
  67. }
  68. this.tcpWEB = this.m_listener.AcceptTcpClient();
  69. if (this.tcpWEB == null)
  70. {
  71. return;
  72. }
  73. this.nsWEB = this.tcpWEB.GetStream();
  74. if (this.nsWEB == null)
  75. {
  76. this.tcpWEB.Close();
  77. this.nsWEB = null;
  78. this.tcpWEB = null;
  79. }
  80. this.tcpWEB_count = 0;
  81. return;
  82. }
  83. else
  84. {
  85. if (!this.nsWEB.DataAvailable)
  86. {
  87. this.tcpWEB_count++;
  88. if (this.tcpWEB_count > 100)
  89. {
  90. this.nsWEB.Close();
  91. this.tcpWEB.Close();
  92. this.nsWEB = null;
  93. this.tcpWEB = null;
  94. Debug.Log("NotCanread クライアントを切断しました");
  95. }
  96. return;
  97. }
  98. byte[] array = new byte[4096];
  99. MemoryStream memoryStream = new MemoryStream();
  100. for (;;)
  101. {
  102. int num = this.nsWEB.Read(array, 0, array.Length);
  103. if (num == 0)
  104. {
  105. break;
  106. }
  107. memoryStream.Write(array, 0, num);
  108. if (!this.nsWEB.DataAvailable)
  109. {
  110. goto Block_8;
  111. }
  112. }
  113. this.nsWEB.Close();
  114. this.tcpWEB.Close();
  115. this.nsWEB = null;
  116. this.tcpWEB = null;
  117. Debug.Log("クライアントから切断されました。");
  118. return;
  119. Block_8:
  120. string text = this.enc.GetString(memoryStream.ToArray());
  121. memoryStream.Close();
  122. Debug.Log(text);
  123. if (text == null)
  124. {
  125. this.nsWEB.Close();
  126. this.tcpWEB.Close();
  127. this.nsWEB = null;
  128. this.tcpWEB = null;
  129. Debug.Log("resMsg==null");
  130. return;
  131. }
  132. int num2 = text.IndexOf("token=");
  133. if (0 < num2)
  134. {
  135. text = WWW.UnEscapeURL(text.Substring(num2, text.Length - num2).Replace("token=", string.Empty));
  136. CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
  137. if (!characterMgr.IsBusy() && characterMgr.GetMaid(0) != null)
  138. {
  139. CharacterMgr.Preset preset = CharacterMgr.LoadePresetLowCapacityData(text);
  140. if (preset != null)
  141. {
  142. characterMgr.PresetSet(characterMgr.GetMaid(0), preset);
  143. string s = "HTTP/1.0 204 OK\r\nConnection: close\r\n\r\n";
  144. byte[] bytes = this.enc.GetBytes(s);
  145. this.nsWEB.Write(bytes, 0, bytes.Length);
  146. }
  147. else
  148. {
  149. Debug.Log("CM3D2のプリセットデータではありません");
  150. }
  151. }
  152. }
  153. else
  154. {
  155. string text2 = "HTTP/1.0 204 OK\r\nConnection: close\r\n\r\n";
  156. byte[] bytes2 = this.enc.GetBytes(text2);
  157. this.nsWEB.Write(bytes2, 0, bytes2.Length);
  158. Debug.Log(text2);
  159. }
  160. this.nsWEB.Close();
  161. this.tcpWEB.Close();
  162. this.nsWEB = null;
  163. this.tcpWEB = null;
  164. }
  165. }
  166. private Encoding enc = Encoding.UTF8;
  167. private NetworkStream nsWEB;
  168. private TcpClient tcpWEB;
  169. private TcpListener m_listener;
  170. private int tcpWEB_count;
  171. }