static HookResult OnPreReset(Terraria.RemoteClient remoteClient) { if (!Netplay.Disconnect) { if (remoteClient.IsActive) { _hookManager.InvokeServerLeave(remoteClient.Id); } _hookManager.InvokeServerSocketReset(remoteClient); } return(HookResult.Continue); }
private static void InitializeServer() { ResetNetDiag(); if (Main.rand == null) { Main.rand = new UnifiedRandom((int)DateTime.Now.Ticks); } Main.myPlayer = 255; ServerIP = IPAddress.Any; Main.menuMode = 14; Main.statusText = Lang.menu[8].Value; Main.netMode = 2; Disconnect = false; for (int i = 0; i < 256; i++) { Clients[i] = new RemoteClient(); Clients[i].Reset(); Clients[i].Id = i; Clients[i].ReadBuffer = new byte[1024]; } TcpListener = new TcpSocket(); if (!Disconnect) { if (!StartListening()) { Main.menuMode = 15; Main.statusText = Language.GetTextValue("Error.TriedToRunServerTwice"); Disconnect = true; } Main.statusText = Language.GetTextValue("CLI.ServerStarted"); } if (UseUPNP) { try { OpenPort(ListenPort); } catch (Exception) { } } }
public static void CheckSection(int playerIndex, Vector2 position, int fluff = 1) { int num = playerIndex; int sectionX = Netplay.GetSectionX((int)(position.X / 16f)); int sectionY = Netplay.GetSectionY((int)(position.Y / 16f)); int num1 = 0; for (int i = sectionX - fluff; i < sectionX + fluff + 1; i++) { for (int j = sectionY - fluff; j < sectionY + fluff + 1; j++) { if (i >= 0 && i < Main.maxSectionsX && j >= 0 && j < Main.maxSectionsY && !Netplay.Clients[num].TileSections[i, j]) { num1++; } } } if (num1 > 0) { int num2 = num1; Netplay.Clients[num].StatusText2 = "is receiving tile data"; RemoteClient clients = Netplay.Clients[num]; clients.StatusMax = clients.StatusMax + num2; NetMessage.SendData(9, num, -1, Lang.inter[44], num2, 0f, 0f, 0f, 0, 0, 0); for (int k = sectionX - fluff; k < sectionX + fluff + 1; k++) { for (int l = sectionY - fluff; l < sectionY + fluff + 1; l++) { if (k >= 0 && k < Main.maxSectionsX && l >= 0 && l < Main.maxSectionsY && !Netplay.Clients[num].TileSections[k, l]) { NetMessage.SendSection(num, k, l, false); NetMessage.SendData(11, num, -1, "", k, (float)l, (float)k, (float)l, 0, 0, 0); } } } } }
internal void InvokeServerSocketReset(RemoteClient socket) { SocketResetEventArgs args = new SocketResetEventArgs { Socket = socket }; this.ServerSocketReset.Invoke(args); }
internal bool InvokeNetSendBytes(RemoteClient socket, byte[] buffer, int offset, int count) { SendBytesEventArgs args = new SendBytesEventArgs { Socket = socket, Buffer = buffer, Offset = offset, Count = count }; this.NetSendBytes.Invoke(args); return args.Handled; }
public static string RemoteAddress(this Terraria.RemoteClient sock) { return((sock as ServerSlot).remoteAddress); }
public static bool CanSendWater(this Terraria.RemoteClient sock) { return((sock as ServerSlot).state >= SlotState.SENDING_TILES && (sock as ServerSlot).Connected); }
public static bool IsPlaying(this Terraria.RemoteClient sock) { return((sock as ServerSlot).state == SlotState.PLAYING); }
public static void SetState(this Terraria.RemoteClient sock, SlotState state) { (sock as ServerSlot).state = state; }
public static SlotState State(this Terraria.RemoteClient sock) { return((sock as ServerSlot).state); }
public static void Kick(this Terraria.RemoteClient sock, string reason) { (sock as ServerSlot).Kick(reason); }
public static void ServerLoop(object threadContext) { 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 = 16; ServerIP = IPAddress.Any; Main.menuMode = 14; Main.statusText = Lang.menu[8].Value; Main.netMode = 2; disconnect = false; for (int i = 0; i < 17; i++) { Clients[i] = new RemoteClient(); Clients[i].Reset(); Clients[i].Id = i; Clients[i].ReadBuffer = new byte[1024]; } TcpListener = new TcpSocket(); if (!disconnect) { if (!StartListening()) { Main.menuMode = 15; Main.statusText = Language.GetTextValue("Net.TriedToRunServerTwice"); disconnect = true; } Main.statusText = Language.GetTextValue("CLI.ServerStarted"); } if (UseUPNP) { try { OpenPort(); } catch { } } StartBroadCasting(); int num = 0; while (!disconnect) { if (!IsListening) { int num2 = -1; for (int j = 0; j < Main.maxNetPlayers; j++) { if (!Clients[j].IsConnected()) { num2 = j; break; } } if (num2 >= 0) { if (Main.ignoreErrors) { try { StartListening(); IsListening = true; } catch { } } else { StartListening(); IsListening = true; } } } int num3 = 0; for (int k = 0; k < 17; k++) { if (NetMessage.buffer[k].checkBytes) { NetMessage.CheckBytes(k); } if (Clients[k].PendingTermination) { Clients[k].Reset(); NetMessage.syncPlayers(); } else if (Clients[k].IsConnected()) { if (!Clients[k].IsActive) { Clients[k].State = 0; } Clients[k].IsActive = true; num3++; if (!Clients[k].IsReading) { try { if (Clients[k].Socket.IsDataAvailable()) { Clients[k].IsReading = true; Clients[k].Socket.AsyncReceive(Clients[k].ReadBuffer, 0, Clients[k].ReadBuffer.Length, Clients[k].ServerReadCallBack); } } catch { Clients[k].PendingTermination = true; } } if (Clients[k].StatusMax > 0 && Clients[k].StatusText2 != "") { if (Clients[k].StatusCount >= Clients[k].StatusMax) { Clients[k].StatusText = string.Concat("(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " ", Clients[k].StatusText2, ": Complete!"); Clients[k].StatusText2 = ""; Clients[k].StatusMax = 0; Clients[k].StatusCount = 0; } else { Clients[k].StatusText = string.Concat("(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " ", Clients[k].StatusText2, ": ", (int)((float)Clients[k].StatusCount / (float)Clients[k].StatusMax * 100f), "%"); } } else if (Clients[k].State == 0) { Clients[k].StatusText = string.Concat("(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " is connecting..."); } else if (Clients[k].State == 1) { Clients[k].StatusText = string.Concat("(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " is sending player data..."); } else if (Clients[k].State == 2) { Clients[k].StatusText = string.Concat("(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " requested world information"); } else if (Clients[k].State != 3 && Clients[k].State == 10) { try { Clients[k].StatusText = string.Concat("(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " is playing"); } catch (Exception) { Clients[k].PendingTermination = true; } } } else if (Clients[k].IsActive) { Clients[k].PendingTermination = true; } else { Clients[k].StatusText2 = ""; if (k < 16) { 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) { anyClients = false; } else { anyClients = true; } IsServerRunning = true; } StopBroadCasting(); StopListening(); try { closePort(); } catch { } for (int l = 0; l < 17; l++) { Clients[l].Reset(); } if (Main.menuMode != 15) { Main.netMode = 0; Main.menuMode = 10; WorldFile.saveWorld(); while (WorldGen.saveLock) { } Main.menuMode = 0; } else { Main.netMode = 0; } Main.myPlayer = 0; }
public static void ServerLoop(object threadContext) { 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; Main.menuMode = 14; Main.statusText = "Starting server..."; Main.netMode = 2; disconnect = false; for (int i = 0; i < 256; i++) { Clients[i] = new RemoteClient(); Clients[i].Reset(); Clients[i].Id = i; Clients[i].ReadBuffer = new byte[1024]; } TcpListener = new TcpSocket(); if (!disconnect) { if (!StartListening()) { Main.menuMode = 15; Main.statusText = "Tried to run two servers on the same PC"; disconnect = true; } Main.statusText = "Server started"; } int num = 0; while (!disconnect) { if (!IsListening) { int num2 = -1; for (int j = 0; j < Main.maxNetPlayers; j++) { if (!Clients[j].Socket.IsConnected()) { num2 = j; break; } } if (num2 >= 0) { if (Main.ignoreErrors) { try { StartListening(); IsListening = true; goto IL_16A; } catch { goto IL_16A; } } StartListening(); IsListening = true; } } IL_16A: int num3 = 0; for (int k = 0; k < 256; k++) { if (NetMessage.buffer[k].checkBytes) { NetMessage.CheckBytes(k); } if (Clients[k].PendingTermination) { ServerApi.Hooks.InvokeServerLeave(Clients[k].Id); Clients[k].Reset(); NetMessage.SyncDisconnectedPlayer(k); } else { if (Clients[k].Socket.IsConnected()) { if (!Clients[k].IsActive) { Clients[k].State = 0; } Clients[k].IsActive = true; num3++; if (!Clients[k].IsReading) { try { if (Clients[k].Socket.IsDataAvailable()) { Clients[k].IsReading = true; Clients[k].Socket.AsyncReceive(Clients[k].ReadBuffer, 0, Clients[k].ReadBuffer.Length, new SocketReceiveCallback(Clients[k].ServerReadCallBack), null); } } catch { Clients[k].PendingTermination = true; } } if (Clients[k].StatusMax > 0 && Clients[k].StatusText2 != "") { if (Clients[k].StatusCount >= Clients[k].StatusMax) { Clients[k].StatusText = string.Concat(new object[] { "(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " ", Clients[k].StatusText2, ": Complete!" }); Clients[k].StatusText2 = ""; Clients[k].StatusMax = 0; Clients[k].StatusCount = 0; continue; } Clients[k].StatusText = string.Concat(new object[] { "(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " ", Clients[k].StatusText2, ": ", (int)((float)Clients[k].StatusCount / (float)Clients[k].StatusMax * 100f), "%" }); continue; } else { if (Clients[k].State == 0) { Clients[k].StatusText = string.Concat(new object[] { "(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " is connecting..." }); continue; } if (Clients[k].State == 1) { Clients[k].StatusText = string.Concat(new object[] { "(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " is sending player data..." }); continue; } if (Clients[k].State == 2) { Clients[k].StatusText = string.Concat(new object[] { "(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " requested world information" }); continue; } if (Clients[k].State == 3 || Clients[k].State != 10) { continue; } try { Clients[k].StatusText = string.Concat(new object[] { "(", Clients[k].Socket.GetRemoteAddress(), ") ", Clients[k].Name, " is playing" }); continue; } catch { Clients[k].PendingTermination = true; continue; } } } if (Clients[k].IsActive) { Clients[k].PendingTermination = true; } else { Clients[k].StatusText2 = ""; if (k < 255) { bool active = Main.player[k].active; Main.player[k].active = false; if (active) { Player.Hooks.PlayerDisconnect(k); } } } } } 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) { anyClients = false; } else { anyClients = true; } IsServerRunning = true; } StopListening(); for (int l = 0; l < 256; l++) { Clients[l].Reset(); } if (Main.menuMode != 15) { Main.netMode = 0; Main.menuMode = 10; WorldFile.saveWorld(); //blocks until world saves? while (WorldGen.saveLock) { } Main.menuMode = 0; } else { Main.netMode = 0; } Main.myPlayer = 0; }