You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.3 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. namespace Networking.Server
  7. {
  8. [CreateAssetMenu(menuName = "Major Project/Networking/ClientList", order = 150)]
  9. public class ConnectionHandler : ScriptableObject
  10. {
  11. #region Public Variables
  12. /// <summary>
  13. /// All Clients which are Currently Connected;
  14. /// </summary>
  15. public List<ClientData> ConnectedClients;
  16. /// <summary>
  17. /// Clients which connected at one point but no longer are
  18. /// Usefull if a client needs to reconnect
  19. /// </summary>
  20. public List<ClientData> DisconnectedClients;
  21. /// <summary>
  22. /// Called when a client connects or disconnet
  23. /// </summary>
  24. public System.Action<List<ClientData>> OnClientsChange;
  25. #endregion Public Variables
  26. #region Private Variables
  27. public NetworkServerSimple server;
  28. #endregion Private Variables
  29. /// <summary>
  30. /// Clears the lists
  31. /// </summary>
  32. public void Reset()
  33. {
  34. ConnectedClients = new List<ClientData>();
  35. DisconnectedClients = new List<ClientData>();
  36. }
  37. /// <summary>
  38. /// Resets the client list + registers handlers with the server
  39. /// </summary>
  40. /// <param name="serverObject">Server to register handlers with</param>
  41. public void SetUp(ServerObject serverObject)
  42. {
  43. this.server = serverObject.server;
  44. Reset();
  45. server.RegisterHandler(MsgType.Connect, OnClientConnect);
  46. server.RegisterHandler(MsgType.Disconnect, OnClientDisconnect);
  47. server.RegisterHandler(LoginProtocols.SendingLoginDetails, OnClientLogin);
  48. }
  49. public void OnClientConnect(NetworkMessage msg)
  50. {
  51. if (ConnectedClients.Any(p => p.ID == msg.channelId))
  52. {
  53. Debug.LogError("Client[" + msg.channelId + "] already connected");
  54. msg.conn.Send(LoginProtocols.LoginFail, new LoginProtocols.EmptyMsg());
  55. return;
  56. }
  57. msg.conn.Send(LoginProtocols.RequestLoginDetails, new LoginProtocols.EmptyMsg());
  58. }
  59. public void OnClientDisconnect(NetworkMessage msg)
  60. {
  61. if (!ConnectedClients.Any(p => p.ID == msg.conn.connectionId))
  62. {
  63. Debug.LogError("Unknown client disconnect [" + msg.conn.connectionId + "]");
  64. return;
  65. }
  66. ClientData client = ConnectedClients.FirstOrDefault(p => p.ID == msg.conn.connectionId);
  67. ConnectedClients.Remove(client);
  68. DisconnectedClients.Add(client);
  69. Debug.Log("Disconnected: " + client.Name);
  70. OnClientsChange.Invoke(ConnectedClients);
  71. }
  72. public void OnClientLogin(NetworkMessage msg)
  73. {
  74. LoginProtocols.LoginMsg loginMsg;
  75. if (!msg.TryRead(out loginMsg))
  76. {
  77. Debug.Log("Recieved unknown message from client");
  78. msg.conn.Send(LoginProtocols.LoginFail, new LoginProtocols.EmptyMsg());
  79. return;
  80. }
  81. if (ConnectedClients.Any(p => p.ID == msg.channelId))
  82. {
  83. Debug.LogError("Client[" + msg.channelId + "] already connected");
  84. msg.conn.Send(LoginProtocols.LoginFail, new LoginProtocols.EmptyMsg());
  85. return;
  86. }
  87. ClientData newClient = DisconnectedClients.FirstOrDefault(p => p.ID == msg.conn.connectionId);
  88. if (newClient != default)
  89. {
  90. Debug.Log("Reconnection: " + loginMsg.Name + ((loginMsg.Name != newClient.Name) ? (" (Prev. " + newClient.Name +")") : ""));
  91. DisconnectedClients.Remove(newClient);
  92. }
  93. else
  94. {
  95. Debug.Log("new Connection: " + loginMsg.Name);
  96. newClient = new ClientData();
  97. }
  98. newClient.Color = loginMsg.Color;
  99. newClient.Name = loginMsg.Name;
  100. newClient.conn = msg.conn;
  101. ConnectedClients.Add(newClient);
  102. newClient.conn.Send(LoginProtocols.LoginSuccess, new LoginProtocols.LoginMsg(newClient.Name, newClient.Color));
  103. OnClientsChange.Invoke(ConnectedClients);
  104. }
  105. }
  106. [System.Serializable]
  107. public class ClientData
  108. {
  109. /// <summary>
  110. /// Client Name
  111. /// </summary>
  112. public string Name;
  113. /// <summary>
  114. /// Color which represents player, picked by player
  115. /// </summary>
  116. public Color Color;
  117. /// <summary>
  118. /// Clients Current Score
  119. /// </summary>
  120. public int Score;
  121. /// <summary>
  122. /// Clients character animal - needed for instantiation
  123. /// </summary>
  124. public string characterAnimal;
  125. /// <summary>
  126. /// Clients number of collected items - for colelction level
  127. /// </summary>
  128. public int collected;
  129. /// <summary>
  130. /// Network connection ID
  131. /// </summary>
  132. public int ID { get { return (conn != null) ? conn.connectionId : -1; }}
  133. /// <summary>
  134. /// Connection to Client
  135. /// </summary>
  136. public NetworkConnection conn;
  137. }
  138. }