FazeFew

Untitled

Jan 18th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 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 Server : MonoBehaviour
  7. {
  8. private const int MAX_USER = 100;
  9. private const int PORT = 26000;
  10. private const int WEB_PORT = 26001;
  11. private const int BYTE_SIZE = 1024;
  12.  
  13. private byte reliableChannel;
  14. private int hostId;
  15. private int webHostId;
  16.  
  17. private bool isStarted;
  18. private byte error;
  19.  
  20. private Mongo db;
  21.  
  22. #region MonoBehaviour
  23. private void Start()
  24. {
  25. DontDestroyOnLoad(gameObject);
  26. Init();
  27. }
  28. private void Update()
  29. {
  30. UpdateMessagePump();
  31. }
  32. #endregion
  33.  
  34. public void Init()
  35. {
  36. db = new Mongo();
  37. db.Init();
  38.  
  39. NetworkTransport.Init();
  40.  
  41. ConnectionConfig cc = new ConnectionConfig();
  42. reliableChannel = cc.AddChannel(QosType.Reliable);
  43.  
  44. HostTopology topo = new HostTopology(cc, MAX_USER);
  45.  
  46. // Server only code
  47. hostId = NetworkTransport.AddHost(topo, PORT, null);
  48. webHostId = NetworkTransport.AddWebsocketHost(topo, WEB_PORT, null);
  49.  
  50. Debug.Log(string.Format("Opening connection on port {0} and webport {1}",PORT,WEB_PORT));
  51. isStarted = true;
  52.  
  53. // $$ TEST
  54. db.InsertAccount("Bamboozled", "Magnificent", "[email protected]");
  55. }
  56. public void Shutdown()
  57. {
  58. isStarted = false;
  59. NetworkTransport.Shutdown();
  60. }
  61.  
  62. public void UpdateMessagePump()
  63. {
  64. if(!isStarted)
  65. return;
  66.  
  67. int recHostId; // Is this from Web? Or standalone
  68. int connectionId; // Which user is sending me this?
  69. int channelId; // Which lane is he sending that message from
  70.  
  71. byte[] recBuffer = new byte[BYTE_SIZE];
  72. int dataSize;
  73.  
  74. NetworkEventType type = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, BYTE_SIZE, out dataSize, out error);
  75. switch (type)
  76. {
  77. case NetworkEventType.Nothing:
  78. break;
  79.  
  80. case NetworkEventType.ConnectEvent:
  81. Debug.Log(string.Format("User {0} has connected has connected through host {1}", connectionId,recHostId));
  82. break;
  83.  
  84. case NetworkEventType.DisconnectEvent:
  85. Debug.Log(string.Format("User {0} has disconnected :(", connectionId));
  86. break;
  87.  
  88. case NetworkEventType.DataEvent:
  89. BinaryFormatter formatter = new BinaryFormatter();
  90. MemoryStream ms = new MemoryStream(recBuffer);
  91. NetMsg msg = (NetMsg)formatter.Deserialize(ms);
  92.  
  93. OnData(connectionId, channelId, recHostId, msg);
  94. break;
  95.  
  96. default:
  97. case NetworkEventType.BroadcastEvent:
  98. Debug.Log("Unexpected network event tpye");
  99. break;
  100. }
  101. }
  102.  
  103. #region OnData
  104. private void OnData(int cnnId, int channelId, int recHostId, NetMsg msg)
  105. {
  106. switch (msg.OP)
  107. {
  108. case NetOP.None:
  109. Debug.Log("Unexpected NETOP");
  110. break;
  111.  
  112. case NetOP.CreateAccount:
  113. CreateAccount(cnnId, channelId, recHostId, (Net_CreateAccount)msg);
  114. break;
  115.  
  116. case NetOP.LoginRequest:
  117. LoginRequest(cnnId, channelId, recHostId, (Net_LoginRequest)msg);
  118. break;
  119. }
  120. }
  121.  
  122. private void CreateAccount(int cnnId, int channelId, int recHostId, Net_CreateAccount ca)
  123. {
  124. Debug.Log(string.Format("{0},{1},{2}",ca.Username,ca.Password,ca.Email));
  125.  
  126. Net_OnCreateAccount oca = new Net_OnCreateAccount();
  127. oca.Success = 0;
  128. oca.Information = "Account was created!";
  129.  
  130. SendClient(recHostId, cnnId, oca);
  131. }
  132. private void LoginRequest(int cnnId, int channelId, int recHostId, Net_LoginRequest lr)
  133. {
  134. Debug.Log(string.Format("{0},{1}", lr.UsernameOrEmail, lr.Password));
  135.  
  136. Net_OnLoginRequest olr = new Net_OnLoginRequest();
  137. olr.Success = 0;
  138. olr.Information = "Everthing is good";
  139. olr.Username = "n3rkmind";
  140. olr.Discriminator = "0000";
  141. olr.Token = "TOKEN";
  142.  
  143. SendClient(recHostId, cnnId, olr);
  144. }
  145. #endregion
  146.  
  147. #region Send
  148. public void SendClient(int recHost, int cnnId, NetMsg msg)
  149. {
  150. // This is where we hold our data
  151. byte[] buffer = new byte[BYTE_SIZE];
  152.  
  153. // This is where you would crush your data into a byte[]
  154. BinaryFormatter formatter = new BinaryFormatter();
  155. MemoryStream ms = new MemoryStream(buffer);
  156. formatter.Serialize(ms, msg);
  157.  
  158. if(recHost == 0)
  159. NetworkTransport.Send(hostId,cnnId, reliableChannel, buffer, BYTE_SIZE, out error);
  160. else
  161. NetworkTransport.Send(webHostId,cnnId, reliableChannel, buffer, BYTE_SIZE, out error);
  162. }
  163. #endregion
  164. }
Advertisement
Add Comment
Please, Sign In to add comment