FazeFew

Untitled

Jan 18th, 2020
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. using System.IO;
  2. using System.Runtime.Serialization.Formatters.Binary;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5.  
  6. public class Client : MonoBehaviour
  7. {
  8. public static Client Instance{ private set; get; }
  9.  
  10. private const int MAX_USER = 100;
  11. private const int PORT = 26000;
  12. private const int WEB_PORT = 26001;
  13. private const int BYTE_SIZE = 1024;
  14. private const string SERVER_IP = "127.0.0.1";
  15.  
  16. private byte reliableChannel;
  17. private int connectionId;
  18. private int hostId;
  19. private byte error;
  20.  
  21. private bool isStarted;
  22.  
  23. #region MonoBehaviour
  24. private void Start()
  25. {
  26. Instance = this;
  27. DontDestroyOnLoad(gameObject);
  28. Init();
  29. }
  30. private void Update()
  31. {
  32. UpdateMessagePump();
  33. }
  34. #endregion
  35.  
  36. public void Init()
  37. {
  38. NetworkTransport.Init();
  39.  
  40. ConnectionConfig cc = new ConnectionConfig();
  41. reliableChannel = cc.AddChannel(QosType.Reliable);
  42.  
  43. HostTopology topo = new HostTopology(cc, MAX_USER);
  44.  
  45. // Client only code
  46. hostId = NetworkTransport.AddHost(topo, 0);
  47.  
  48. #if UNITY_WEBGL && !UNITY_EDITOR
  49. // Web Client
  50. connectionId = NetworkTransport.Connect(hostId,SERVER_IP,WEB_PORT, 0, out error);
  51. Debug.Log("Connecting from Web");
  52. #else
  53. // Standalone Client
  54. connectionId = NetworkTransport.Connect(hostId,SERVER_IP,PORT, 0, out error);
  55. Debug.Log("Connecting from standalone");
  56. #endif
  57.  
  58. Debug.Log(string.Format("Attempting to connect on {0}...", SERVER_IP));
  59. isStarted = true;
  60. }
  61. public void Shutdown()
  62. {
  63. isStarted = false;
  64. NetworkTransport.Shutdown();
  65. }
  66.  
  67. public void UpdateMessagePump()
  68. {
  69. if(!isStarted)
  70. return;
  71.  
  72. int recHostId; // Is this from Web? Or standalone
  73. int connectionId; // Which user is sending me this?
  74. int channelId; // Which lane is he sending that message from
  75.  
  76. byte[] recBuffer = new byte[BYTE_SIZE];
  77. int dataSize;
  78.  
  79. NetworkEventType type = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, BYTE_SIZE, out dataSize, out error);
  80. switch (type)
  81. {
  82. case NetworkEventType.Nothing:
  83. break;
  84.  
  85. case NetworkEventType.ConnectEvent:
  86. Debug.Log("We have connected to the server");
  87. break;
  88.  
  89. case NetworkEventType.DisconnectEvent:
  90. Debug.Log("We have been disconnected");
  91. break;
  92.  
  93. case NetworkEventType.DataEvent:
  94. BinaryFormatter formatter = new BinaryFormatter();
  95. MemoryStream ms = new MemoryStream(recBuffer);
  96. NetMsg msg = (NetMsg)formatter.Deserialize(ms);
  97.  
  98. OnData(connectionId, channelId, recHostId, msg);
  99. break;
  100.  
  101. default:
  102. case NetworkEventType.BroadcastEvent:
  103. Debug.Log("Unexpected network event tpye");
  104. break;
  105. }
  106. }
  107.  
  108. #region OnData
  109. private void OnData(int cnnId, int channelId, int recHostId, NetMsg msg)
  110. {
  111. switch (msg.OP)
  112. {
  113. case NetOP.None:
  114. Debug.Log("Unexpected NETOP");
  115. break;
  116.  
  117. case NetOP.OnCreateAccount:
  118. OnCreateAccount((Net_OnCreateAccount)msg);
  119. break;
  120.  
  121. case NetOP.OnLoginRequest:
  122. OnLoginRequest((Net_OnLoginRequest)msg);
  123. break;
  124. }
  125. }
  126.  
  127. private void OnCreateAccount(Net_OnCreateAccount oca)
  128. {
  129. LobbyScene.Instance.EnableInputs();
  130. LobbyScene.Instance.ChangeAuthenticationMessage(oca.Information);
  131. }
  132. private void OnLoginRequest(Net_OnLoginRequest olr)
  133. {
  134. LobbyScene.Instance.ChangeAuthenticationMessage(olr.Information);
  135.  
  136. if(olr.Success != 0)
  137. {
  138. // Unable to login
  139. LobbyScene.Instance.EnableInputs();
  140. }
  141. else
  142. {
  143. // Successful Login
  144. }
  145. }
  146. #endregion
  147.  
  148. #region Send
  149. public void SendServer(NetMsg msg)
  150. {
  151. // This is where we hold our data
  152. byte[] buffer = new byte[BYTE_SIZE];
  153.  
  154. // This is where you would crush your data into a byte[]
  155. BinaryFormatter formatter = new BinaryFormatter();
  156. MemoryStream ms = new MemoryStream(buffer);
  157. formatter.Serialize(ms, msg);
  158.  
  159. NetworkTransport.Send(hostId,connectionId, reliableChannel, buffer, BYTE_SIZE, out error);
  160. }
  161. public void SendCreateAccount(string username,string password,string email)
  162. {
  163. Net_CreateAccount ca = new Net_CreateAccount();
  164.  
  165. ca.Username = username;
  166. ca.Password = password;
  167. ca.Email = email;
  168.  
  169. SendServer(ca);
  170. }
  171. public void SendLoginRequest(string usernameOrEmail, string password)
  172. {
  173. Net_LoginRequest lr = new Net_LoginRequest();
  174.  
  175. lr.UsernameOrEmail = usernameOrEmail;
  176. lr.Password = password;
  177.  
  178. SendServer(lr);
  179. }
  180. #endregion
  181. }
Add Comment
Please, Sign In to add comment