ConsoleProRemoteServer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Uncomment to use in Editor
  2. // #define USECONSOLEPROREMOTESERVERINEDITOR
  3. #if (!UNITY_EDITOR && DEBUG) || (UNITY_EDITOR && USECONSOLEPROREMOTESERVERINEDITOR)
  4. #define USECONSOLEPROREMOTESERVER
  5. #endif
  6. #if (UNITY_WP_8_1 || UNITY_WSA_8_1)
  7. #define UNSUPPORTEDCONSOLEPROREMOTESERVER
  8. #endif
  9. using UnityEngine;
  10. using System;
  11. using System.Collections.Generic;
  12. #if USECONSOLEPROREMOTESERVER
  13. using FlyingWormConsole3.LiteNetLib;
  14. using FlyingWormConsole3.LiteNetLib.Utils;
  15. #endif
  16. namespace FlyingWormConsole3
  17. {
  18. #if USECONSOLEPROREMOTESERVER
  19. public class ConsoleProRemoteServer : MonoBehaviour, INetEventListener
  20. #else
  21. public class ConsoleProRemoteServer : MonoBehaviour
  22. #endif
  23. {
  24. public bool useNATPunch = false;
  25. public int port = 51000;
  26. #if UNITY_EDITOR && !USECONSOLEPROREMOTESERVER
  27. #elif UNSUPPORTEDCONSOLEPROREMOTESERVER
  28. public void Awake()
  29. {
  30. Debug.Log("Console Pro Remote Server is not supported on this platform");
  31. }
  32. #elif !USECONSOLEPROREMOTESERVER
  33. public void Awake()
  34. {
  35. Debug.Log("Console Pro Remote Server is disabled in release mode, please use a Development build or define DEBUG to use it");
  36. }
  37. #else
  38. private NetManager _netServer;
  39. private NetPeer _ourPeer;
  40. private NetDataWriter _dataWriter;
  41. private NetSerializer _serializer;
  42. [System.SerializableAttribute]
  43. public class QueuedLog
  44. {
  45. public string message;
  46. public string stackTrace;
  47. public LogType type;
  48. }
  49. [NonSerializedAttribute]
  50. public List<QueuedLog> logs = new List<QueuedLog>();
  51. private static ConsoleProRemoteServer instance = null;
  52. void Awake()
  53. {
  54. if(instance != null)
  55. {
  56. Destroy(gameObject);
  57. }
  58. instance = this;
  59. DontDestroyOnLoad(gameObject);
  60. Debug.Log("Starting Console Pro Server on port : " + port);
  61. _dataWriter = new NetDataWriter();
  62. _netServer = new NetManager(this, 100, "ConsolePro");
  63. _netServer.Start(port);
  64. _netServer.DiscoveryEnabled = true;
  65. _netServer.UpdateTime = 15;
  66. _netServer.MergeEnabled = true;
  67. _netServer.NatPunchEnabled = useNATPunch;
  68. _serializer = new NetSerializer();
  69. }
  70. void OnDestroy()
  71. {
  72. if(_netServer != null)
  73. {
  74. _netServer.Stop();
  75. }
  76. }
  77. public void OnPeerConnected(NetPeer peer)
  78. {
  79. Debug.Log("Connected to " + peer.EndPoint);
  80. _ourPeer = peer;
  81. }
  82. public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
  83. {
  84. Debug.Log("Disconnected from " + peer.EndPoint + ", info: " + disconnectInfo.Reason);
  85. if (peer == _ourPeer)
  86. {
  87. _ourPeer = null;
  88. }
  89. }
  90. public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
  91. {
  92. }
  93. public void OnPeerDisconnected(NetPeer peer, DisconnectReason reason, int socketErrorCode)
  94. {
  95. }
  96. public void OnNetworkError(NetEndPoint endPoint, int socketErrorCode)
  97. {
  98. }
  99. public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader, UnconnectedMessageType messageType)
  100. {
  101. if (messageType == UnconnectedMessageType.DiscoveryRequest)
  102. {
  103. // Debug.Log("[SERVER] Received discovery request. Send discovery response");
  104. _netServer.SendDiscoveryResponse(new byte[] {1}, remoteEndPoint);
  105. }
  106. }
  107. public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
  108. {
  109. }
  110. #if UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9
  111. void OnEnable()
  112. {
  113. Application.RegisterLogCallback(LogCallback);
  114. }
  115. void Update()
  116. {
  117. Application.RegisterLogCallback(LogCallback);
  118. }
  119. void OnDisable()
  120. {
  121. Application.RegisterLogCallback(null);
  122. }
  123. #else
  124. void OnEnable()
  125. {
  126. Application.logMessageReceived += LogCallback;
  127. }
  128. void OnDisable()
  129. {
  130. Application.logMessageReceived -= LogCallback;
  131. }
  132. #endif
  133. public void LogCallback(string logString, string stackTrace, LogType type)
  134. {
  135. if(!logString.StartsWith("CPIGNORE"))
  136. {
  137. QueueLog(logString, stackTrace, type);
  138. }
  139. }
  140. void QueueLog(string logString, string stackTrace, LogType type)
  141. {
  142. if(logs.Count > 200)
  143. {
  144. while(logs.Count > 200)
  145. {
  146. logs.RemoveAt(0);
  147. }
  148. }
  149. logs.Add(new QueuedLog() { message = logString, stackTrace = stackTrace, type = type } );
  150. }
  151. void LateUpdate()
  152. {
  153. if(_netServer == null)
  154. {
  155. return;
  156. }
  157. _netServer.PollEvents();
  158. if(_ourPeer == null)
  159. {
  160. return;
  161. }
  162. if(logs.Count <= 0)
  163. {
  164. return;
  165. }
  166. string cMessage = "";
  167. for(int i = 0; i < logs.Count; i++)
  168. {
  169. cMessage = "";
  170. QueuedLog cLog = logs[i];
  171. cMessage = "::::" + cLog.type + "::::" + cLog.message + "\n" + cLog.stackTrace;
  172. _dataWriter.Reset();
  173. _dataWriter.Put(cMessage);
  174. _ourPeer.Send(_dataWriter, SendOptions.ReliableOrdered);
  175. }
  176. logs.Clear();
  177. }
  178. #endif
  179. }
  180. }