Advertisement
FazeFew

Untitled

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