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.

169 lines
5.0 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. #endregion Public Variables
  22. #region Private Variables
  23. public NetworkServerSimple server;
  24. #endregion Private Variables
  25. /// <summary>
  26. /// Clears the lists
  27. /// </summary>
  28. public void Reset()
  29. {
  30. ConnectedClients = new List<ClientData>();
  31. DisconnectedClients = new List<ClientData>();
  32. }
  33. /// <summary>
  34. /// Resets the client list + registers handlers with the server
  35. /// </summary>
  36. /// <param name="serverObject">Server to register handlers with</param>
  37. public void SetUp(ServerObject serverObject)
  38. {
  39. this.server = serverObject.server;
  40. Reset();
  41. server.RegisterHandler(MsgType.Connect, OnClientConnect);
  42. server.RegisterHandler(MsgType.Disconnect, OnClientDisconnect);
  43. server.RegisterHandler(LoginProtocols.SendingLoginDetails, OnClientLogin);
  44. }
  45. public void OnClientConnect(NetworkMessage msg)
  46. {
  47. if (ConnectedClients.Any(p => p.ID == msg.channelId))
  48. {
  49. Debug.LogError("Client[" + msg.channelId + "] already connected");
  50. msg.conn.Send(LoginProtocols.LoginFail, new LoginProtocols.EmptyMsg());
  51. return;
  52. }
  53. msg.conn.Send(LoginProtocols.RequestLoginDetails, new LoginProtocols.EmptyMsg());
  54. }
  55. public void OnClientDisconnect(NetworkMessage msg)
  56. {
  57. if (!ConnectedClients.Any(p => p.ID == msg.conn.connectionId))
  58. {
  59. Debug.LogError("Unknown client disconnect [" + msg.conn.connectionId + "]");
  60. return;
  61. }
  62. ClientData client = ConnectedClients.FirstOrDefault(p => p.ID == msg.conn.connectionId);
  63. ConnectedClients.Remove(client);
  64. DisconnectedClients.Add(client);
  65. Debug.Log("Disconnected: " + client.Name);
  66. }
  67. public void OnClientLogin(NetworkMessage msg)
  68. {
  69. LoginProtocols.LoginMsg loginMsg;
  70. if (!msg.TryRead(out loginMsg))
  71. {
  72. Debug.Log("Recieved unknown message from client");
  73. msg.conn.Send(LoginProtocols.LoginFail, new LoginProtocols.EmptyMsg());
  74. return;
  75. }
  76. if (ConnectedClients.Any(p => p.ID == msg.channelId))
  77. {
  78. Debug.LogError("Client[" + msg.channelId + "] already connected");
  79. msg.conn.Send(LoginProtocols.LoginFail, new LoginProtocols.EmptyMsg());
  80. return;
  81. }
  82. ClientData newClient = DisconnectedClients.FirstOrDefault(p => p.ID == msg.conn.connectionId);
  83. if (newClient != default)
  84. {
  85. Debug.Log("Reconnection: " + loginMsg.Name + ((loginMsg.Name != newClient.Name) ? (" (Prev. " + newClient.Name +")") : ""));
  86. DisconnectedClients.Remove(newClient);
  87. }
  88. else
  89. {
  90. Debug.Log("new Connection: " + loginMsg.Name);
  91. newClient = new ClientData();
  92. }
  93. newClient.Color = loginMsg.Color;
  94. newClient.Name = loginMsg.Name;
  95. newClient.conn = msg.conn;
  96. ConnectedClients.Add(newClient);
  97. newClient.conn.Send(LoginProtocols.LoginSuccess, new LoginProtocols.LoginMsg(newClient.Name, newClient.Color));
  98. }
  99. }
  100. [System.Serializable]
  101. public class ClientData
  102. {
  103. /// <summary>
  104. /// Client Name
  105. /// </summary>
  106. public string Name;
  107. /// <summary>
  108. /// Color which represents player, picked by player
  109. /// </summary>
  110. public Color Color;
  111. /// <summary>
  112. /// Clients Current Score
  113. /// </summary>
  114. public int Score;
  115. /// <summary>
  116. /// Clients character animal - needed for instantiation
  117. /// </summary>
  118. public string characterAnimal;
  119. /// <summary>
  120. /// Clients number of collected items - for colelction level
  121. /// </summary>
  122. public int collected;
  123. /// <summary>
  124. /// Network connection ID
  125. /// </summary>
  126. public int ID { get { return (conn != null) ? conn.connectionId : -1; }}
  127. /// <summary>
  128. /// Connection to Client
  129. /// </summary>
  130. public NetworkConnection conn;
  131. }
  132. }