public static void TeardownServer() { PNetServer.Disconnect(); while (PNetServer.PeerStatus != NetPeerStatus.NotRunning) { Thread.Sleep(10); } PNetServer.Shutdown(); Thread.Sleep(100); }
public void MyTestInitialize() { _client = new TestablePNet(); _client.TestableHook.StartUpdateThread(); _client.OnRoomChange += s => _client.FinishedRoomChange(); PNetServer.OnPlayerConnected += player => player.ChangeRoom(_testRoom); _testRoom = Room.CreateRoom("test room"); _client.Connect(TestablePNet.GetTestConnectionConfig()); Thread.Sleep(250); _player = PNetServer.AllPlayers().First(f => f != null && f != Player.Server); }
static void Main(string[] args) { PNetServer.InitializeServer( Properties.Settings.Default.MaximumPlayers, Properties.Settings.Default.ListenPort); PNetServer.ApproveConnection = ApproveConnection; PNetServer.OnPlayerConnected += OnPlayerConnected; PNetServer.OnPlayerDisconnected += delegate(Player player) { Debug.Log("player {0} disconnected", player.Id); }; 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(); //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. while (true) { var input = Console.ReadLine(); if (input == "quit") { break; } Thread.Sleep(100); } //we're exiting. close the server thread. if (_serverThread.IsAlive) { _serverThread.Abort(); } }
public void TestRoomJoining() { _client.OnRoomChange += s => _client.FinishedRoomChange(); PNetServer.OnPlayerConnected += OnPlayerConnected; _client.Connect(TestablePNet.GetTestConnectionConfig()); Thread.Sleep(250); Assert.AreEqual(NetConnectionStatus.Connected, _client.Status, "Client did not connect in 250 ms"); Thread.Sleep(100); var tPlayer = PNetServer.AllPlayers()[1]; Assert.AreSame(_testRoom, tPlayer.CurrentRoom, "Client did not change to the room within 100 ms"); GameState.InvokeIfRequired(() => { tPlayer.ChangeRoom(_testRoom2); }); Thread.Sleep(100); Assert.AreSame(_testRoom2, tPlayer.CurrentRoom, "Client did not switch to room 2 within 100 ms"); GameState.InvokeIfRequired(() => { tPlayer.ChangeRoom(_testRoom); }); Thread.Sleep(100); Assert.AreSame(_testRoom, tPlayer.CurrentRoom, "Client did not change back to room within 100 ms"); }
public void RapidConnectionsTest() { const int ClientCount = 20; const int ClientConnectionTries = 5; const int ExpectedTotalConnections = ClientCount * ClientConnectionTries; ServerUtils.SetupDefaultServer(); ServerUtils.StartServerOnNewThread(); int cCount = 0; int dcCount = 0; PNetServer.OnPlayerConnected += player => Interlocked.Increment(ref cCount); PNetServer.OnPlayerDisconnected += player => Interlocked.Increment(ref dcCount); PNetServer.ApproveConnection += message => { Thread.Sleep(10); message.SenderConnection.Approve(); }; var clients = new TestablePNet[ClientCount]; for (int i = 0; i < clients.Length; i++) { clients[i] = new TestablePNet(); clients[i].TestableHook.StartUpdateThread(); int i1 = i; clients[i].OnFailedToConnect += s => Assert.Fail("{0} failed to connect: {1}", i1, s); } var connTasks = new List <Task>(clients.Length); Thread.Sleep(100); //wait for clients to start... for (int c = 0; c < clients.Length; c++) { var client = clients[c]; int c1 = c; var task = new Task(() => { int connectCount = 0; var config = TestablePNet.GetTestConnectionConfig(); client.OnFailedToConnect += s => Assert.Fail("{0} connect failed {1}", c1, s); client.OnConnectedToServer += () => { connectCount++; Thread.Sleep(200); client.Disconnect(); }; client.OnDisconnectedFromServer += () => { if (connectCount < ClientConnectionTries) { client.Connect(config); } }; client.Connect(config); while (connectCount < ClientConnectionTries) { Thread.Sleep(1); } }); connTasks.Add(task); } var aTasks = connTasks.ToArray(); foreach (var task in aTasks) { task.Start(); } Task.WaitAll(aTasks); Thread.Sleep(500); Assert.AreEqual(ExpectedTotalConnections, cCount, "Expected " + ExpectedTotalConnections + " connections but got " + cCount); Assert.AreEqual(ExpectedTotalConnections, dcCount, "Expected " + ExpectedTotalConnections + " disconnects but got " + dcCount); var ap = PNetServer.AllPlayers(); foreach (var player in ap) { Assert.IsNull(player); } }
public static void StartServerOnNewThread() { _serverThread = new Thread(() => PNetServer.Start()); _serverThread.Start(); }
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(); } }