Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 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. #region MonoBehaviour
  21. private void Start()
  22. {
  23. DontDestroyOnLoad(gameObject);
  24. Init();
  25. }
  26. private void Update()
  27. {
  28. UpdateMessagePump();
  29. }
  30. #endregion
  31.  
  32. public void Init()
  33. {
  34. NetworkTransport.Init();
  35.  
  36. ConnectionConfig cc = new ConnectionConfig();
  37. reliableChannel = cc.AddChannel(QosType.Reliable);
  38.  
  39. HostTopology topo = new HostTopology(cc, MAX_USER);
  40.  
  41. // Server only code
  42. hostId = NetworkTransport.AddHost(topo, PORT, null);
  43. webHostId = NetworkTransport.AddWebsocketHost(topo, WEB_PORT, null);
  44.  
  45. Debug.Log(string.Format("Opening connection on port {0} and webport {1}",PORT,WEB_PORT));
  46. isStarted = true;
  47. }
  48. public void Shutdown()
  49. {
  50. isStarted = false;
  51. NetworkTransport.Shutdown();
  52. }
  53.  
  54. public void UpdateMessagePump()
  55. {
  56. if(!isStarted)
  57. return;
  58.  
  59. int recHostId; // Is this from Web? Or standalone
  60. int connectionId; // Which user is sending me this?
  61. int channelId; // Which lane is he sending that message from
  62.  
  63. byte[] recBuffer = new byte[BYTE_SIZE];
  64. int dataSize;
  65.  
  66. NetworkEventType type = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, BYTE_SIZE, out dataSize, out error);
  67. switch (type)
  68. {
  69. case NetworkEventType.Nothing:
  70. break;
  71.  
  72. case NetworkEventType.ConnectEvent:
  73. Debug.Log(string.Format("User {0} has connected has connected through host {1}", connectionId,recHostId));
  74. break;
  75.  
  76. case NetworkEventType.DisconnectEvent:
  77. Debug.Log(string.Format("User {0} has disconnected :(", connectionId));
  78. break;
  79.  
  80. case NetworkEventType.DataEvent:
  81. BinaryFormatter formatter = new BinaryFormatter();
  82. MemoryStream ms = new MemoryStream(recBuffer);
  83. NetMsg msg = (NetMsg)formatter.Deserialize(ms);
  84.  
  85. OnData(connectionId, channelId, recHostId, msg);
  86. break;
  87.  
  88. default:
  89. case NetworkEventType.BroadcastEvent:
  90. Debug.Log("Unexpected network event tpye");
  91. break;
  92. }
  93. }
  94.  
  95. #region OnData
  96. private void OnData(int cnnId, int channelId, int recHostId, NetMsg msg)
  97. {
  98. switch (msg.OP)
  99. {
  100. case NetOP.None:
  101. Debug.Log("Unexpected NETOP");
  102. break;
  103.  
  104. case NetOp.CreateAccount:
  105. CreateAccount(cnnId, channelId, recHostId, (Net_CreateAccount)msg);
  106. break;
  107. }
  108. }
  109.  
  110. private void CreateAccount(int cnnId, int channelId, int recHostId, Net_CreateAccount ca)
  111. {
  112. Debug.Log(string.Format("{0},{1},{2}",ca.Username,ca.Password,ca.Email));
  113. }
  114. #endregion
  115.  
  116. #region Send
  117. public void SendClient(int recHost, int cnnId, NetMsg msg)
  118. {
  119. // This is where we hold our data
  120. byte[] buffer = new byte[BYTE_SIZE];
  121.  
  122. // This is where you would crush your data into a byte[]
  123. BinaryFormatter formatter new BinaryFormatter();
  124. MemoryStream ms = new MemoryStream(buffer);
  125. formatter.Serialize(ms, msg);
  126.  
  127. if(recHost == 0)
  128. NetworkTransport.Send(hostId,cnnId, reliableChannel, buffer, BYTE_SIZE, out error);
  129. else
  130. NetworkTransport.Send(webHostId,cnnId, reliableChannel, buffer, BYTE_SIZE, out error);
  131. }
  132. #endregion
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement