public static void ServerLoop(object threadContext) { if (Main.rand == null) { Main.rand = new Random((int)DateTime.Now.Ticks); } if (WorldGen.genRand == null) { WorldGen.genRand = new Random((int)DateTime.Now.Ticks); } Main.myPlayer = 8; serverIP = IPAddress.Any; serverListenIP = serverIP; Main.menuMode = 14; Main.statusText = "Starting server..."; Main.netMode = 2; disconnect = false; for (int i = 0; i < 9; i++) { serverSock[i] = new ServerSock(); serverSock[i].Reset(); serverSock[i].whoAmI = i; serverSock[i].tcpClient = new TcpClient(); serverSock[i].tcpClient.NoDelay = true; serverSock[i].readBuffer = new byte[0x400]; serverSock[i].writeBuffer = new byte[0x400]; } tcpListener = new TcpListener(serverListenIP, serverPort); try { tcpListener.Start(); } catch (Exception exception) { Main.menuMode = 15; Main.statusText = exception.ToString(); disconnect = true; } if (!disconnect) { ThreadPool.QueueUserWorkItem(new WaitCallback(Netplay.ListenForClients), 1); Main.statusText = "Server started"; } while (!disconnect) { if (stopListen) { int num2 = -1; for (int m = 0; m < 8; m++) { if (!serverSock[m].tcpClient.Connected) { num2 = m; break; } } if (num2 >= 0) { tcpListener.Start(); stopListen = false; ThreadPool.QueueUserWorkItem(new WaitCallback(Netplay.ListenForClients), 1); } } int num4 = 0; for (int k = 0; k < 9; k++) { if (NetMessage.buffer[k].checkBytes) { NetMessage.CheckBytes(k); } if (serverSock[k].kill) { serverSock[k].Reset(); NetMessage.syncPlayers(); } else if (serverSock[k].tcpClient.Connected) { if (!serverSock[k].active) { serverSock[k].state = 0; } serverSock[k].active = true; num4++; if (!serverSock[k].locked) { try { serverSock[k].networkStream = serverSock[k].tcpClient.GetStream(); if (serverSock[k].networkStream.DataAvailable) { serverSock[k].locked = true; serverSock[k].networkStream.BeginRead(serverSock[k].readBuffer, 0, serverSock[k].readBuffer.Length, new AsyncCallback(serverSock[k].ServerReadCallBack), serverSock[k].networkStream); } } catch { serverSock[k].kill = true; } } if ((serverSock[k].statusMax > 0) && (serverSock[k].statusText2 != "")) { if (serverSock[k].statusCount >= serverSock[k].statusMax) { serverSock[k].statusText = string.Concat(new object[] { "(", serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", serverSock[k].name, " ", serverSock[k].statusText2, ": Complete!" }); serverSock[k].statusText2 = ""; serverSock[k].statusMax = 0; serverSock[k].statusCount = 0; } else { serverSock[k].statusText = string.Concat(new object[] { "(", serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", serverSock[k].name, " ", serverSock[k].statusText2, ": ", (int)((((float)serverSock[k].statusCount) / ((float)serverSock[k].statusMax)) * 100f), "%" }); } } else if (serverSock[k].state == 0) { serverSock[k].statusText = string.Concat(new object[] { "(", serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", serverSock[k].name, " is connecting..." }); } else if (serverSock[k].state == 1) { serverSock[k].statusText = string.Concat(new object[] { "(", serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", serverSock[k].name, " is sending player data..." }); } else if (serverSock[k].state == 2) { serverSock[k].statusText = string.Concat(new object[] { "(", serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", serverSock[k].name, " requested world information" }); } else if ((serverSock[k].state != 3) && (serverSock[k].state == 10)) { serverSock[k].statusText = string.Concat(new object[] { "(", serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", serverSock[k].name, " is playing" }); } } else if (serverSock[k].active) { serverSock[k].kill = true; } else { serverSock[k].statusText2 = ""; if (k < 8) { Main.player[k].active = false; } } Thread.Sleep(1); } if (!WorldGen.saveLock) { if (num4 == 0) { Main.statusText = "Waiting for clients..."; } else { Main.statusText = num4 + " clients connected"; } } } tcpListener.Stop(); for (int j = 0; j < 9; j++) { serverSock[j].Reset(); } if (Main.menuMode != 15) { Main.netMode = 0; Main.menuMode = 10; WorldGen.saveWorld(false); while (WorldGen.saveLock) { } Main.menuMode = 0; } else { Main.netMode = 0; } Main.myPlayer = 0; }
public static void ServerLoop(object threadContext) { Netplay.ResetNetDiag(); if (Main.rand == null) { Main.rand = new Random((int)DateTime.Now.Ticks); } if (WorldGen.genRand == null) { WorldGen.genRand = new Random((int)DateTime.Now.Ticks); } Main.myPlayer = 255; Netplay.serverIP = IPAddress.Any; Netplay.serverListenIP = Netplay.serverIP; Main.menuMode = 14; Main.statusText = "Starting server..."; Main.netMode = 2; Netplay.disconnect = false; for (int i = 0; i < 256; i++) { Netplay.serverSock[i] = new ServerSock(); Netplay.serverSock[i].Reset(); Netplay.serverSock[i].whoAmI = i; Netplay.serverSock[i].tcpClient = new TcpClient(); Netplay.serverSock[i].tcpClient.NoDelay = true; Netplay.serverSock[i].readBuffer = new byte[1024]; Netplay.serverSock[i].writeBuffer = new byte[1024]; } Netplay.tcpListener = new TcpListener(Netplay.serverListenIP, Netplay.serverPort); try { Netplay.tcpListener.Start(); } catch (Exception ex) { Main.menuMode = 15; Main.statusText = ex.ToString(); Netplay.disconnect = true; } if (!Netplay.disconnect) { ThreadPool.QueueUserWorkItem(new WaitCallback(Netplay.ListenForClients), 1); Main.statusText = "Server started"; } if (Netplay.uPNP) { try { Netplay.openPort(); } catch { } } int num = 0; while (!Netplay.disconnect) { if (Netplay.stopListen) { int num2 = -1; for (int j = 0; j < Main.maxNetPlayers; j++) { if (!Netplay.serverSock[j].tcpClient.Connected) { num2 = j; break; } } if (num2 >= 0) { if (Main.ignoreErrors) { try { Netplay.tcpListener.Start(); Netplay.stopListen = false; ThreadPool.QueueUserWorkItem(new WaitCallback(Netplay.ListenForClients), 1); goto IL_219; } catch { goto IL_219; } } Netplay.tcpListener.Start(); Netplay.stopListen = false; ThreadPool.QueueUserWorkItem(new WaitCallback(Netplay.ListenForClients), 1); } } IL_219: int num3 = 0; for (int k = 0; k < 256; k++) { if (NetMessage.buffer[k].checkBytes) { NetMessage.CheckBytes(k); } if (Netplay.serverSock[k].kill) { Netplay.serverSock[k].Reset(); NetMessage.syncPlayers(); } else { if (Netplay.serverSock[k].tcpClient.Connected) { if (!Netplay.serverSock[k].active) { Netplay.serverSock[k].state = 0; } Netplay.serverSock[k].active = true; num3++; if (!Netplay.serverSock[k].locked) { try { Netplay.serverSock[k].networkStream = Netplay.serverSock[k].tcpClient.GetStream(); if (Netplay.serverSock[k].networkStream.DataAvailable) { Netplay.serverSock[k].locked = true; Netplay.serverSock[k].networkStream.BeginRead(Netplay.serverSock[k].readBuffer, 0, Netplay.serverSock[k].readBuffer.Length, new AsyncCallback(Netplay.serverSock[k].ServerReadCallBack), Netplay.serverSock[k].networkStream); } } catch { Netplay.serverSock[k].kill = true; } } if (Netplay.serverSock[k].statusMax > 0 && Netplay.serverSock[k].statusText2 != "") { if (Netplay.serverSock[k].statusCount >= Netplay.serverSock[k].statusMax) { Netplay.serverSock[k].statusText = string.Concat(new object[] { "(", Netplay.serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", Netplay.serverSock[k].name, " ", Netplay.serverSock[k].statusText2, ": Complete!" }); Netplay.serverSock[k].statusText2 = ""; Netplay.serverSock[k].statusMax = 0; Netplay.serverSock[k].statusCount = 0; } else { Netplay.serverSock[k].statusText = string.Concat(new object[] { "(", Netplay.serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", Netplay.serverSock[k].name, " ", Netplay.serverSock[k].statusText2, ": ", (int)((float)Netplay.serverSock[k].statusCount / (float)Netplay.serverSock[k].statusMax * 100f), "%" }); } } else { if (Netplay.serverSock[k].state == 0) { Netplay.serverSock[k].statusText = string.Concat(new object[] { "(", Netplay.serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", Netplay.serverSock[k].name, " is connecting..." }); } else { if (Netplay.serverSock[k].state == 1) { Netplay.serverSock[k].statusText = string.Concat(new object[] { "(", Netplay.serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", Netplay.serverSock[k].name, " is sending player data..." }); } else { if (Netplay.serverSock[k].state == 2) { Netplay.serverSock[k].statusText = string.Concat(new object[] { "(", Netplay.serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", Netplay.serverSock[k].name, " requested world information" }); } else { if (Netplay.serverSock[k].state != 3 && Netplay.serverSock[k].state == 10) { Netplay.serverSock[k].statusText = string.Concat(new object[] { "(", Netplay.serverSock[k].tcpClient.Client.RemoteEndPoint, ") ", Netplay.serverSock[k].name, " is playing" }); } } } } } } else { if (Netplay.serverSock[k].active) { Netplay.serverSock[k].kill = true; } else { Netplay.serverSock[k].statusText2 = ""; if (k < 255) { Main.player[k].active = false; } } } } } num++; if (num > 10) { Thread.Sleep(1); num = 0; } else { Thread.Sleep(0); } if (!WorldGen.saveLock && !Main.dedServ) { if (num3 == 0) { Main.statusText = "Waiting for clients..."; } else { Main.statusText = num3 + " clients connected"; } } if (num3 == 0) { Netplay.anyClients = false; } else { Netplay.anyClients = true; } Netplay.ServerUp = true; } Netplay.tcpListener.Stop(); try { Netplay.closePort(); } catch { } for (int l = 0; l < 256; l++) { Netplay.serverSock[l].Reset(); } if (Main.menuMode != 15) { Main.netMode = 0; Main.menuMode = 10; WorldGen.saveWorld(false); while (WorldGen.saveLock) { } Main.menuMode = 0; } else { Main.netMode = 0; } Main.myPlayer = 0; }
public static void ServerLoop(object threadContext) { Netplay.ResetNetDiag(); if (Main.rand == null) { Main.rand = new Random((int)DateTime.Now.Ticks); } if (WorldGen.genRand == null) { WorldGen.genRand = new Random((int)DateTime.Now.Ticks); } Main.myPlayer = 255; Netplay.serverIP = IPAddress.Any; //Netplay.serverListenIP = Netplay.serverIP; Main.menuMode = 14; Main.statusText = "Starting server..."; Main.netMode = 2; Netplay.disconnect = false; for (int i = 0; i < 256; i++) { Netplay.serverSock[i] = new ServerSock(); Netplay.serverSock[i].Reset(); Netplay.serverSock[i].whoAmI = i; Netplay.serverSock[i].tcpClient = new TcpClient(); Netplay.serverSock[i].tcpClient.NoDelay = true; Netplay.serverSock[i].readBuffer = new byte[1024]; Netplay.serverSock[i].writeBuffer = new byte[1024]; } Netplay.tcpListener = new TcpListener(Netplay.serverListenIP, Netplay.serverPort); try { Netplay.tcpListener.Start(); } catch (Exception ex) { Main.menuMode = 15; Main.statusText = ex.ToString(); Netplay.disconnect = true; } if (!Netplay.disconnect) { ThreadPool.QueueUserWorkItem(new WaitCallback(Netplay.ListenForClients), 1); Main.statusText = "Server started"; } int num = 0; while (!Netplay.disconnect) { if (Netplay.stopListen) { int num2 = -1; for (int j = 0; j < Main.maxNetPlayers; j++) { if (serverSock[j].tcpClient == null || !Netplay.serverSock[j].tcpClient.Connected) { num2 = j; break; } } if (num2 >= 0) { if (Main.ignoreErrors) { try { Netplay.tcpListener.Start(); Netplay.stopListen = false; ThreadPool.QueueUserWorkItem(new WaitCallback(Netplay.ListenForClients), 1); goto IL_208; } catch { goto IL_208; } } Netplay.tcpListener.Start(); Netplay.stopListen = false; ThreadPool.QueueUserWorkItem(new WaitCallback(Netplay.ListenForClients), 1); } } IL_208: int num3 = 0; for (int k = 0; k < 256; k++) { if (NetMessage.buffer[k].checkBytes) { NetMessage.CheckBytes(k); } if (killInactive && serverSock[k].active && serverSock[k].state == 0 && (DateTime.UtcNow - serverSock[k].connectTime).TotalSeconds > 5) { Netplay.serverSock[k].kill = true; } if (Netplay.serverSock[k].kill) { ServerHooks.OnLeave(Netplay.serverSock[k].whoAmI); Netplay.serverSock[k].Reset(); NetMessage.syncPlayers(); } else if (serverSock[k].tcpClient != null && Netplay.serverSock[k].tcpClient.Connected) { if (!Netplay.serverSock[k].active) { Netplay.serverSock[k].state = 0; } Netplay.serverSock[k].active = true; num3++; if (!Netplay.serverSock[k].locked) { try { Netplay.serverSock[k].networkStream = Netplay.serverSock[k].tcpClient.GetStream(); if (Netplay.serverSock[k].networkStream.DataAvailable) { Netplay.serverSock[k].locked = true; Netplay.serverSock[k].networkStream.BeginRead(Netplay.serverSock[k].readBuffer, 0, Netplay.serverSock[k].readBuffer.Length, new AsyncCallback(Netplay.serverSock[k].ServerReadCallBack), Netplay.serverSock[k].networkStream); } } catch { Netplay.serverSock[k].kill = true; } } if (Netplay.serverSock[k].statusMax > 0 && Netplay.serverSock[k].statusText2 != "") { if (Netplay.serverSock[k].statusCount >= Netplay.serverSock[k].statusMax) { Netplay.serverSock[k].statusText2 = ""; Netplay.serverSock[k].statusMax = 0; Netplay.serverSock[k].statusCount = 0; } else { } } else { } } else if (Netplay.serverSock[k].active) { Netplay.serverSock[k].kill = true; } else { Netplay.serverSock[k].statusText2 = ""; if (k < 255) { Main.player[k].active = false; } } } num++; if (num > 10) { Thread.Sleep(1); num = 0; } else { Thread.Sleep(0); } if (num3 == 0) { Netplay.anyClients = false; } else { Netplay.anyClients = true; } Netplay.ServerUp = true; } Netplay.tcpListener.Stop(); for (int l = 0; l < 256; l++) { Netplay.serverSock[l].Reset(); } if (Main.menuMode != 15) { Main.netMode = 0; Main.menuMode = 10; WorldGen.saveWorld(false); while (WorldGen.saveLock) { } Main.menuMode = 0; } else { Main.netMode = 0; } Main.myPlayer = 0; }