Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var config = new ServerConfiguration(Properties.Settings.Default.MaximumPlayers,
                                                 Properties.Settings.Default.ListenPort);
            PNetServer.InitializeServer(config);

            PNetServer.ApproveConnection = ApproveConnection;
            PNetServer.OnPlayerConnected += OnPlayerConnected;
            PNetServer.OnPlayerDisconnected += delegate(Player player) { Debug.Log("player {0} disconnected", player.Id); };
            
            //If you want a global 'update' function, assign it here
            GameState.update += Update;

            Debug.logger = new DefaultConsoleLogger();

            //TODO: make some Room child classes, and load them into the _rooms dictionary
            Room newRoom = Room.CreateRoom("basic room");
            _room = newRoom.AddBehaviour<BasicRoom>();
            //loading of other data as well

            //Finish starting the server. Started in a new thread so that the console can sit open and still accept input
            _serverThread = new Thread(() => PNetServer.Start(Properties.Settings.Default.FrameTime));
            _serverThread.Start();

            Console.WriteLine("Server is running");
            //let the console sit open, waiting for a quit
            //this will throw errors if the program isn't running as a console app, like on unix as a background process
            //recommend including Mono.Unix.Native, and separately handling unix signals if this is running on unix.
            //you could also write a service, and run things that way. (Might also work on Unix better)
            while(true)
            {
                //This will throw errors on linux if not attached to a terminal
                var input = Console.ReadLine();

                if (input == "quit")
                    break;

                //if you wanted, you could also process other commands here to pass to the server/rooms.

                Thread.Sleep(100);
            }
            
            //shut down lidgren
            PNetServer.Disconnect();
            //shut down server. Will actually cause the server thread to finish running.
            PNetServer.Shutdown();
            //and give plenty of time for the server thread to close nicely
            Thread.Sleep(50);
            
            //we're exiting. make sure the server thread is closed
            if (_serverThread.IsAlive)
            {
                Console.WriteLine("Should not have had to abort thread. This could be a bug\nPress any key to continue shutting down.");
                Console.ReadKey();
                _serverThread.Abort();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Set up the server, bind to a socket. Use Start to fully start the server after running this
        /// </summary>
        /// <param name="configuration"></param>
        public static void InitializeServer(ServerConfiguration configuration)
        {
            Configuration = configuration;

            if (peer != null && peer.Status != NetPeerStatus.NotRunning)
            {
                Debug.LogError("cannot start server while already running");
                return;
            }

            _netPeerConfiguration = new NetPeerConfiguration(Configuration.AppIdentifier);
            _netPeerConfiguration.Port = Configuration.ListenPort;
            _netPeerConfiguration.MaximumConnections = Configuration.MaximumConnections;
            connections = new IntDictionary<NetConnection>(Configuration.MaximumConnections);

            _netPeerConfiguration.SetMessageTypeEnabled(NetIncomingMessageType.ConnectionApproval, true);

            peer = new NetServer(_netPeerConfiguration);

            peer.Start();

            var serverId = connections.Add(null);
            var serverPlayer = new Player();
            serverPlayer.Id = (ushort)serverId;
            Player.Server = serverPlayer;

            GameState.update += Update;
        }
Ejemplo n.º 3
0
 public static void InitializeServer(int maxConnections, int listenPort, int tickRate = 66)
 {
     Configuration = new ServerConfiguration(maxConnections, listenPort, tickRate);
 }
Ejemplo n.º 4
0
 public static void InitializeServer(int maxConnections, int listenPort, int tickRate = 66)
 {
     Configuration = new ServerConfiguration(maxConnections, listenPort, tickRate);
 }