public static void Initialize() { if (IsActive()) { return; } workerStop = false; gameStarted = false; shotLaunchedThisTurn = false; player1 = new Player { IsPlayerOne = true }; player2 = new Player { IsPlayerOne = false }; peer = new Peer(2); peer.Bind(SocketProtocol.IPv4, GamePort); peer.SetMaxIncomingPeers(2); // unfortunately we don't have a proper game loop like a 3D renderer does, so we'll use a thread instead worker = new Thread(ThreadWorker) { IsBackground = true }; worker.Start(); }
public static void Initialize() { if (IsActive()) { return; } Board = new Board(); TrackingBoard = new Board(); workerStop = false; peer = new Peer(1); peer.Bind(SocketProtocol.IPv4, 0); // unfortunately we don't have a proper game loop like a 3D renderer does, so we'll use a thread instead worker = new Thread(ThreadWorker) { IsBackground = true }; worker.Start(); }
/// <summary> /// Start the server /// <para>If <paramref name="localClient"/> is given then will start in host mode</para> /// </summary> /// <param name="config">Config for <see cref="Peer"/></param> /// <param name="localClient">if not null then start the server and client in hostmode</param> // Has to be called "StartServer" to stop unity complaining about "Start" method public void StartServer(NetworkClient localClient = null) { ThrowIfActive(); ThrowIfSocketIsMissing(); Application.quitting += Stop; if (logger.LogEnabled()) { logger.Log($"NetworkServer Created, Mirage version: {Version.Current}"); } logger.Assert(Players.Count == 0, "Player should have been reset since previous session"); logger.Assert(connections.Count == 0, "Connections should have been reset since previous session"); World = new NetworkWorld(); SyncVarSender = new SyncVarSender(); LocalClient = localClient; MessageHandler = new MessageHandler(World, DisconnectOnException); MessageHandler.RegisterHandler <NetworkPingMessage>(World.Time.OnServerPing); ISocket socket = SocketFactory.CreateServerSocket(); var dataHandler = new DataHandler(MessageHandler, connections); Metrics = EnablePeerMetrics ? new Metrics(MetricsSize) : null; Config config = PeerConfig; if (config == null) { config = new Config { // only use MaxConnections if config was null MaxConnections = MaxConnections, }; } NetworkWriterPool.Configure(config.MaxPacketSize); // Only create peer if listening if (Listening) { peer = new Peer(socket, dataHandler, config, LogFactory.GetLogger <Peer>(), Metrics); peer.OnConnected += Peer_OnConnected; peer.OnDisconnected += Peer_OnDisconnected; peer.Bind(SocketFactory.GetBindEndPoint()); } if (logger.LogEnabled()) { logger.Log("Server started listening"); } InitializeAuthEvents(); Active = true; _started?.Invoke(); if (LocalClient != null) { // we should call onStartHost after transport is ready to be used // this allows server methods like NetworkServer.Spawn to be called in there _onStartHost?.Invoke(); localClient.ConnectHost(this, dataHandler); if (logger.LogEnabled()) { logger.Log("NetworkServer StartHost"); } } }
/// <summary> /// Start the server /// <para>If <paramref name="localClient"/> is given then will start in host mode</para> /// </summary> /// <param name="config">Config for <see cref="Peer"/></param> /// <param name="localClient">if not null then start the server and client in hostmode</param> // Has to be called "StartServer" to stop unity complaining about "Start" method public void StartServer(NetworkClient localClient = null) { ThrowIfActive(); ThrowIfSocketIsMissing(); Application.quitting += Stop; if (logger.LogEnabled()) { logger.Log($"NetworkServer created, Mirage version: {Version.Current}"); } logger.Assert(Players.Count == 0, "Player should have been reset since previous session"); logger.Assert(_connections.Count == 0, "Connections should have been reset since previous session"); World = new NetworkWorld(); SyncVarSender = new SyncVarSender(); LocalClient = localClient; MessageHandler = new MessageHandler(World, DisconnectOnException); MessageHandler.RegisterHandler <NetworkPingMessage>(World.Time.OnServerPing); var dataHandler = new DataHandler(MessageHandler, _connections); Metrics = EnablePeerMetrics ? new Metrics(MetricsSize) : null; var config = PeerConfig; if (config == null) { config = new Config { // only use MaxConnections if config was null MaxConnections = MaxConnections, }; } var maxPacketSize = SocketFactory.MaxPacketSize; NetworkWriterPool.Configure(maxPacketSize); // Are we listening for incoming connections? // If yes, set up a socket for incoming connections (we're a multiplayer game). // If not, that's okay. Some games use a non-listening server for their single player game mode (Battlefield, Call of Duty...) if (Listening) { // Create a server specific socket. var socket = SocketFactory.CreateServerSocket(); // Tell the peer to use that newly created socket. _peer = new Peer(socket, maxPacketSize, dataHandler, config, LogFactory.GetLogger <Peer>(), Metrics); _peer.OnConnected += Peer_OnConnected; _peer.OnDisconnected += Peer_OnDisconnected; // Bind it to the endpoint. _peer.Bind(SocketFactory.GetBindEndPoint()); if (logger.LogEnabled()) { logger.Log($"Server started, listening for connections. Using socket {socket.GetType()}"); } if (RunInBackground) { Application.runInBackground = RunInBackground; } } else { // Nicely mention that we're going live, but not listening for connections. if (logger.LogEnabled()) { logger.Log("Server started, but not listening for connections: Attempts to connect to this instance will fail!"); } } InitializeAuthEvents(); Active = true; _started?.Invoke(); if (LocalClient != null) { // we should call onStartHost after transport is ready to be used // this allows server methods like NetworkServer.Spawn to be called in there _onStartHost?.Invoke(); localClient.ConnectHost(this, dataHandler); if (logger.LogEnabled()) { logger.Log("NetworkServer StartHost"); } } }