ConsoleProRemoteServer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using UnityEngine;
  2. using System;
  3. using System.Net;
  4. using System.Collections.Generic;
  5. namespace FlyingWormConsole3
  6. {
  7. public class ConsoleProRemoteServer : MonoBehaviour
  8. {
  9. #if !NETFX_CORE && !UNITY_WEBPLAYER && !UNITY_WP8 && !UNITY_METRO
  10. public class HTTPContext
  11. {
  12. public HttpListenerContext context;
  13. public string path;
  14. public string Command
  15. {
  16. get
  17. {
  18. return WWW.UnEscapeURL(context.Request.Url.AbsolutePath);
  19. }
  20. }
  21. public HttpListenerRequest Request
  22. {
  23. get
  24. {
  25. return context.Request;
  26. }
  27. }
  28. public HttpListenerResponse Response
  29. {
  30. get
  31. {
  32. return context.Response;
  33. }
  34. }
  35. public HTTPContext(HttpListenerContext inContext)
  36. {
  37. context = inContext;
  38. }
  39. public void RespondWithString(string inString)
  40. {
  41. Response.StatusDescription = "OK";
  42. Response.StatusCode = (int)HttpStatusCode.OK;
  43. if (!string.IsNullOrEmpty(inString))
  44. {
  45. Response.ContentType = "text/plain";
  46. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(inString);
  47. Response.ContentLength64 = buffer.Length;
  48. Response.OutputStream.Write(buffer,0,buffer.Length);
  49. }
  50. }
  51. }
  52. [System.SerializableAttribute]
  53. public class QueuedLog
  54. {
  55. public string message;
  56. public string stackTrace;
  57. public LogType type;
  58. }
  59. public int port = 51000;
  60. private static HttpListener listener = new HttpListener();
  61. [NonSerializedAttribute]
  62. public List<QueuedLog> logs = new List<QueuedLog>();
  63. void Awake()
  64. {
  65. DontDestroyOnLoad(gameObject);
  66. Debug.Log("Starting Console Pro Server on port : " + port);
  67. listener.Prefixes.Add("http://*:"+port+"/");
  68. listener.Start();
  69. listener.BeginGetContext(ListenerCallback, null);
  70. }
  71. #if UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6
  72. void OnEnable()
  73. {
  74. Application.RegisterLogCallback(LogCallback);
  75. }
  76. void Update()
  77. {
  78. Application.RegisterLogCallback(LogCallback);
  79. }
  80. void LateUpdate()
  81. {
  82. Application.RegisterLogCallback(LogCallback);
  83. }
  84. void OnDisable()
  85. {
  86. Application.RegisterLogCallback(null);
  87. }
  88. #else
  89. void OnEnable()
  90. {
  91. Application.logMessageReceived += LogCallback;
  92. }
  93. void OnDisable()
  94. {
  95. Application.logMessageReceived -= LogCallback;
  96. }
  97. #endif
  98. public void LogCallback(string logString, string stackTrace, LogType type)
  99. {
  100. if(!logString.StartsWith("CPIGNORE"))
  101. {
  102. QueueLog(logString, stackTrace, type);
  103. }
  104. }
  105. void QueueLog(string logString, string stackTrace, LogType type)
  106. {
  107. logs.Add(new QueuedLog() { message = logString, stackTrace = stackTrace, type = type } );
  108. }
  109. void ListenerCallback(IAsyncResult result)
  110. {
  111. HTTPContext context = new HTTPContext(listener.EndGetContext(result));
  112. HandleRequest(context);
  113. listener.BeginGetContext(new AsyncCallback(ListenerCallback), null);
  114. }
  115. void HandleRequest(HTTPContext context)
  116. {
  117. // Debug.LogError("HANDLE " + context.Request.HttpMethod);
  118. // Debug.LogError("HANDLE " + context.path);
  119. bool foundCommand = false;
  120. switch(context.Command)
  121. {
  122. case "/NewLogs":
  123. {
  124. foundCommand = true;
  125. if(logs.Count > 0)
  126. {
  127. string response = "";
  128. // foreach(QueuedLog cLog in logs)
  129. for(int i = 0; i < logs.Count; i++)
  130. {
  131. QueuedLog cLog = logs[i];
  132. response += "::::" + cLog.type;
  133. response += "||||" + cLog.message;
  134. response += ">>>>" + cLog.stackTrace + ">>>>";
  135. }
  136. context.RespondWithString(response);
  137. logs.Clear();
  138. }
  139. else
  140. {
  141. context.RespondWithString("");
  142. }
  143. break;
  144. }
  145. }
  146. if(!foundCommand)
  147. {
  148. context.Response.StatusCode = (int)HttpStatusCode.NotFound;
  149. context.Response.StatusDescription = "Not Found";
  150. }
  151. context.Response.OutputStream.Close();
  152. }
  153. #endif
  154. }
  155. }