Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using UnityEngine;
- using UnityEngine.Networking;
- public class Server : MonoBehaviour
- {
- private const int MAX_USER = 100;
- private const int PORT = 26000;
- private const int WEB_PORT = 26001;
- private const int BYTE_SIZE = 1024;
- private byte reliableChannel;
- private int hostId;
- private int webHostId;
- private bool isStarted;
- private byte error;
- #region MonoBehaviour
- private void Start()
- {
- DontDestroyOnLoad(gameObject);
- Init();
- }
- private void Update()
- {
- UpdateMessagePump();
- }
- #endregion
- public void Init()
- {
- NetworkTransport.Init();
- ConnectionConfig cc = new ConnectionConfig();
- reliableChannel = cc.AddChannel(QosType.Reliable);
- HostTopology topo = new HostTopology(cc, MAX_USER);
- // Server only code
- hostId = NetworkTransport.AddHost(topo, PORT, null);
- webHostId = NetworkTransport.AddWebsocketHost(topo, WEB_PORT, null);
- Debug.Log(string.Format("Opening connection on port {0} and webport {1}",PORT,WEB_PORT));
- isStarted = true;
- }
- public void Shutdown()
- {
- isStarted = false;
- NetworkTransport.Shutdown();
- }
- public void UpdateMessagePump()
- {
- if(!isStarted)
- return;
- int recHostId; // Is this from Web? Or standalone
- int connectionId; // Which user is sending me this?
- int channelId; // Which lane is he sending that message from
- byte[] recBuffer = new byte[BYTE_SIZE];
- int dataSize;
- NetworkEventType type = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, BYTE_SIZE, out dataSize, out error);
- switch (type)
- {
- case NetworkEventType.Nothing:
- break;
- case NetworkEventType.ConnectEvent:
- Debug.Log(string.Format("User {0} has connected has connected through host {1}", connectionId,recHostId));
- break;
- case NetworkEventType.DisconnectEvent:
- Debug.Log(string.Format("User {0} has disconnected :(", connectionId));
- break;
- case NetworkEventType.DataEvent:
- BinaryFormatter formatter = new BinaryFormatter();
- MemoryStream ms = new MemoryStream(recBuffer);
- NetMsg msg = (NetMsg)formatter.Deserialize(ms);
- OnData(connectionId, channelId, recHostId, msg);
- break;
- default:
- case NetworkEventType.BroadcastEvent:
- Debug.Log("Unexpected network event tpye");
- break;
- }
- }
- #region OnData
- private void OnData(int cnnId, int channelId, int recHostId, NetMsg msg)
- {
- switch (msg.OP)
- {
- case NetOP.None:
- Debug.Log("Unexpected NETOP");
- break;
- case NetOp.CreateAccount:
- CreateAccount(cnnId, channelId, recHostId, (Net_CreateAccount)msg);
- break;
- }
- }
- private void CreateAccount(int cnnId, int channelId, int recHostId, Net_CreateAccount ca)
- {
- Debug.Log(string.Format("{0},{1},{2}",ca.Username,ca.Password,ca.Email));
- }
- #endregion
- #region Send
- public void SendClient(int recHost, int cnnId, NetMsg msg)
- {
- // This is where we hold our data
- byte[] buffer = new byte[BYTE_SIZE];
- // This is where you would crush your data into a byte[]
- BinaryFormatter formatter new BinaryFormatter();
- MemoryStream ms = new MemoryStream(buffer);
- formatter.Serialize(ms, msg);
- if(recHost == 0)
- NetworkTransport.Send(hostId,cnnId, reliableChannel, buffer, BYTE_SIZE, out error);
- else
- NetworkTransport.Send(webHostId,cnnId, reliableChannel, buffer, BYTE_SIZE, out error);
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement