ResetNetDiag() public static method

public static ResetNetDiag ( ) : void
return void
コード例 #1
0
        public static void ServerLoop(object threadContext)
        {
            Netplay.ResetNetDiag();
            if (Main.rand == null)
            {
                Main.rand = new UnifiedRandom((int)DateTime.Now.Ticks);
            }
            Main.myPlayer      = (int)byte.MaxValue;
            Netplay.ServerIP   = IPAddress.Any;
            Main.menuMode      = 14;
            Main.statusText    = Lang.menu[8].Value;
            Main.netMode       = 2;
            Netplay.disconnect = false;
            for (int index = 0; index < 256; ++index)
            {
                Netplay.Clients[index] = new RemoteClient();
                Netplay.Clients[index].Reset();
                Netplay.Clients[index].Id         = index;
                Netplay.Clients[index].ReadBuffer = new byte[1024];
            }
            Netplay.TcpListener = (ISocket) new TcpSocket();
            if (!Netplay.disconnect)
            {
                if (!Netplay.StartListening())
                {
                    Main.menuMode      = 15;
                    Main.statusText    = Language.GetTextValue("Error.TriedToRunServerTwice");
                    Netplay.disconnect = true;
                }
                Main.statusText = Language.GetTextValue("CLI.ServerStarted");
            }
            if (Netplay.UseUPNP)
            {
                try
                {
                    Netplay.OpenPort();
                }
                catch
                {
                }
            }
            int num1 = 0;

            while (!Netplay.disconnect)
            {
                if (!Netplay.IsListening)
                {
                    int num2 = -1;
                    for (int index = 0; index < Main.maxNetPlayers; ++index)
                    {
                        if (!Netplay.Clients[index].IsConnected())
                        {
                            num2 = index;
                            break;
                        }
                    }
                    if (num2 >= 0)
                    {
                        if (Main.ignoreErrors)
                        {
                            try
                            {
                                Netplay.StartListening();
                                Netplay.IsListening = true;
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            Netplay.StartListening();
                            Netplay.IsListening = true;
                        }
                    }
                }
                int num3 = 0;
                for (int index = 0; index < 256; ++index)
                {
                    if (NetMessage.buffer[index].checkBytes)
                    {
                        NetMessage.CheckBytes(index);
                    }
                    if (Netplay.Clients[index].PendingTermination)
                    {
                        Netplay.Clients[index].Reset();
                        NetMessage.SyncDisconnectedPlayer(index);
                    }
                    else if (Netplay.Clients[index].IsConnected())
                    {
                        if (!Netplay.Clients[index].IsActive)
                        {
                            Netplay.Clients[index].State = 0;
                        }
                        Netplay.Clients[index].IsActive = true;
                        ++num3;
                        if (!Netplay.Clients[index].IsReading)
                        {
                            try
                            {
                                if (Netplay.Clients[index].Socket.IsDataAvailable())
                                {
                                    Netplay.Clients[index].IsReading = true;
                                    Netplay.Clients[index].Socket.AsyncReceive(Netplay.Clients[index].ReadBuffer, 0, Netplay.Clients[index].ReadBuffer.Length, new SocketReceiveCallback(Netplay.Clients[index].ServerReadCallBack), (object)null);
                                }
                            }
                            catch
                            {
                                Netplay.Clients[index].PendingTermination = true;
                            }
                        }
                        if (Netplay.Clients[index].StatusMax > 0 && Netplay.Clients[index].StatusText2 != "")
                        {
                            if (Netplay.Clients[index].StatusCount >= Netplay.Clients[index].StatusMax)
                            {
                                Netplay.Clients[index].StatusText  = Language.GetTextValue("Net.ClientStatusComplete", (object)Netplay.Clients[index].Socket.GetRemoteAddress(), (object)Netplay.Clients[index].Name, (object)Netplay.Clients[index].StatusText2);
                                Netplay.Clients[index].StatusText2 = "";
                                Netplay.Clients[index].StatusMax   = 0;
                                Netplay.Clients[index].StatusCount = 0;
                            }
                            else
                            {
                                Netplay.Clients[index].StatusText = "(" + (object)Netplay.Clients[index].Socket.GetRemoteAddress() + ") " + Netplay.Clients[index].Name + " " + Netplay.Clients[index].StatusText2 + ": " + (object)(int)((double)Netplay.Clients[index].StatusCount / (double)Netplay.Clients[index].StatusMax * 100.0) + "%";
                            }
                        }
                        else if (Netplay.Clients[index].State == 0)
                        {
                            Netplay.Clients[index].StatusText = Language.GetTextValue("Net.ClientConnecting", (object)string.Format("({0}) {1}", (object)Netplay.Clients[index].Socket.GetRemoteAddress(), (object)Netplay.Clients[index].Name));
                        }
                        else if (Netplay.Clients[index].State == 1)
                        {
                            Netplay.Clients[index].StatusText = Language.GetTextValue("Net.ClientSendingData", (object)Netplay.Clients[index].Socket.GetRemoteAddress(), (object)Netplay.Clients[index].Name);
                        }
                        else if (Netplay.Clients[index].State == 2)
                        {
                            Netplay.Clients[index].StatusText = Language.GetTextValue("Net.ClientRequestedWorldInfo", (object)Netplay.Clients[index].Socket.GetRemoteAddress(), (object)Netplay.Clients[index].Name);
                        }
                        else if (Netplay.Clients[index].State != 3)
                        {
                            if (Netplay.Clients[index].State == 10)
                            {
                                try
                                {
                                    Netplay.Clients[index].StatusText = Language.GetTextValue("Net.ClientPlaying", (object)Netplay.Clients[index].Socket.GetRemoteAddress(), (object)Netplay.Clients[index].Name);
                                }
                                catch (Exception ex)
                                {
                                    Netplay.Clients[index].PendingTermination = true;
                                }
                            }
                        }
                    }
                    else if (Netplay.Clients[index].IsActive)
                    {
                        Netplay.Clients[index].PendingTermination = true;
                    }
                    else
                    {
                        Netplay.Clients[index].StatusText2 = "";
                        if (index < (int)byte.MaxValue)
                        {
                            int num2 = Main.player[index].active ? 1 : 0;
                            Main.player[index].active = false;
                            if (num2 != 0)
                            {
                                Player.Hooks.PlayerDisconnect(index);
                            }
                        }
                    }
                }
                ++num1;
                if (num1 > 10)
                {
                    Thread.Sleep(1);
                    num1 = 0;
                }
                else
                {
                    Thread.Sleep(0);
                }
                if (!WorldGen.saveLock && !Main.dedServ)
                {
                    Main.statusText = num3 != 0 ? Language.GetTextValue("Net.ClientsConnected", (object)num3) : Language.GetTextValue("Net.WaitingForClients");
                }
                Netplay.anyClients      = num3 != 0;
                Netplay.IsServerRunning = true;
            }
            Netplay.StopListening();
            try
            {
                Netplay.closePort();
            }
            catch
            {
            }
            for (int index = 0; index < 256; ++index)
            {
                Netplay.Clients[index].Reset();
            }
            if (Main.menuMode != 15)
            {
                Main.netMode  = 0;
                Main.menuMode = 10;
                WorldFile.saveWorld();
                do
                {
                    ;
                }while (WorldGen.saveLock);
                Main.menuMode = 0;
            }
            else
            {
                Main.netMode = 0;
            }
            Main.myPlayer = 0;
        }
コード例 #2
0
ファイル: Netplay.cs プロジェクト: TerrariaJava/ExecutableSRC
        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;
                WorldFile.saveWorld(false);
                while (WorldGen.saveLock)
                {
                }
                Main.menuMode = 0;
            }
            else
            {
                Main.netMode = 0;
            }
            Main.myPlayer = 0;
        }
コード例 #3
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;
            Main.menuMode      = 14;
            Main.statusText    = "Starting server...";
            Main.netMode       = 2;
            Netplay.disconnect = false;
            for (int i = 0; i < 256; i++)
            {
                Netplay.Clients[i] = new RemoteClient();
                Netplay.Clients[i].Reset();
                Netplay.Clients[i].Id         = i;
                Netplay.Clients[i].ReadBuffer = new byte[1024];
            }
            Netplay.TcpListener = new TcpSocket();
            if (!Netplay.disconnect)
            {
                if (!Netplay.StartListening())
                {
                    Main.menuMode      = 15;
                    Main.statusText    = "Tried to run two servers on the same PC";
                    Netplay.disconnect = true;
                }
                Main.statusText = "Server started";
            }
            int num = 0;

            while (!Netplay.disconnect)
            {
                if (!Netplay.IsListening)
                {
                    int num2 = -1;
                    for (int j = 0; j < Main.maxNetPlayers; j++)
                    {
                        if (!Netplay.Clients[j].Socket.IsConnected())
                        {
                            num2 = j;
                            break;
                        }
                    }
                    if (num2 >= 0)
                    {
                        if (Main.ignoreErrors)
                        {
                            try
                            {
                                Netplay.StartListening();
                                Netplay.IsListening = true;
                                goto IL_16A;
                            }
                            catch
                            {
                                goto IL_16A;
                            }
                        }
                        Netplay.StartListening();
                        Netplay.IsListening = true;
                    }
                }
IL_16A:
                int num3 = 0;
                for (int k = 0; k < 256; k++)
                {
                    if (NetMessage.buffer[k].checkBytes)
                    {
                        NetMessage.CheckBytes(k);
                    }
                    if (Netplay.Clients[k].PendingTermination)
                    {
                        ServerApi.Hooks.InvokeServerLeave(Netplay.Clients[k].Id);
                        Netplay.Clients[k].Reset();
                        NetMessage.syncPlayers(sendInventory: false, sendPlayerInfo: false);
                    }
                    else
                    {
                        if (Netplay.Clients[k].Socket.IsConnected())
                        {
                            if (!Netplay.Clients[k].IsActive)
                            {
                                Netplay.Clients[k].State = 0;
                            }
                            Netplay.Clients[k].IsActive = true;
                            num3++;
                            if (!Netplay.Clients[k].IsReading)
                            {
                                try
                                {
                                    if (Netplay.Clients[k].Socket.IsDataAvailable())
                                    {
                                        Netplay.Clients[k].IsReading = true;
                                        Netplay.Clients[k].Socket.AsyncReceive(Netplay.Clients[k].ReadBuffer, 0, Netplay.Clients[k].ReadBuffer.Length, new SocketReceiveCallback(Netplay.Clients[k].ServerReadCallBack), null);
                                    }
                                }
                                catch
                                {
                                    Netplay.Clients[k].PendingTermination = true;
                                }
                            }
                            if (Netplay.Clients[k].StatusMax > 0 && Netplay.Clients[k].StatusText2 != "")
                            {
                                if (Netplay.Clients[k].StatusCount >= Netplay.Clients[k].StatusMax)
                                {
                                    Netplay.Clients[k].StatusText = string.Concat(new object[]
                                    {
                                        "(",
                                        Netplay.Clients[k].Socket.GetRemoteAddress(),
                                        ") ",
                                        Netplay.Clients[k].Name,
                                        " ",
                                        Netplay.Clients[k].StatusText2,
                                        ": Complete!"
                                    });
                                    Netplay.Clients[k].StatusText2 = "";
                                    Netplay.Clients[k].StatusMax   = 0;
                                    Netplay.Clients[k].StatusCount = 0;
                                    continue;
                                }
                                Netplay.Clients[k].StatusText = string.Concat(new object[]
                                {
                                    "(",
                                    Netplay.Clients[k].Socket.GetRemoteAddress(),
                                    ") ",
                                    Netplay.Clients[k].Name,
                                    " ",
                                    Netplay.Clients[k].StatusText2,
                                    ": ",
                                    (int)((float)Netplay.Clients[k].StatusCount / (float)Netplay.Clients[k].StatusMax * 100f),
                                    "%"
                                });
                                continue;
                            }
                            else
                            {
                                if (Netplay.Clients[k].State == 0)
                                {
                                    Netplay.Clients[k].StatusText = string.Concat(new object[]
                                    {
                                        "(",
                                        Netplay.Clients[k].Socket.GetRemoteAddress(),
                                        ") ",
                                        Netplay.Clients[k].Name,
                                        " is connecting..."
                                    });
                                    continue;
                                }
                                if (Netplay.Clients[k].State == 1)
                                {
                                    Netplay.Clients[k].StatusText = string.Concat(new object[]
                                    {
                                        "(",
                                        Netplay.Clients[k].Socket.GetRemoteAddress(),
                                        ") ",
                                        Netplay.Clients[k].Name,
                                        " is sending player data..."
                                    });
                                    continue;
                                }
                                if (Netplay.Clients[k].State == 2)
                                {
                                    Netplay.Clients[k].StatusText = string.Concat(new object[]
                                    {
                                        "(",
                                        Netplay.Clients[k].Socket.GetRemoteAddress(),
                                        ") ",
                                        Netplay.Clients[k].Name,
                                        " requested world information"
                                    });
                                    continue;
                                }
                                if (Netplay.Clients[k].State == 3 || Netplay.Clients[k].State != 10)
                                {
                                    continue;
                                }
                                try
                                {
                                    Netplay.Clients[k].StatusText = string.Concat(new object[]
                                    {
                                        "(",
                                        Netplay.Clients[k].Socket.GetRemoteAddress(),
                                        ") ",
                                        Netplay.Clients[k].Name,
                                        " is playing"
                                    });
                                    continue;
                                }
                                catch
                                {
                                    Netplay.Clients[k].PendingTermination = true;
                                    continue;
                                }
                            }
                        }
                        if (Netplay.Clients[k].IsActive)
                        {
                            Netplay.Clients[k].PendingTermination = true;
                        }
                        else
                        {
                            Netplay.Clients[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.IsServerRunning = true;
            }
            Netplay.StopListening();
            for (int l = 0; l < 256; l++)
            {
                Netplay.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;
        }
コード例 #4
0
ファイル: Netplay.cs プロジェクト: TerrariaJava/ExecutableSRC
        public static void ClientLoop(object threadContext)
        {
            Netplay.ResetNetDiag();
            Main.ServerSideCharacter = false;
            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.player[Main.myPlayer].hostile = false;
            Main.clientPlayer = (Player)Main.player[Main.myPlayer].clientClone();
            for (int i = 0; i < 255; i++)
            {
                if (i != Main.myPlayer)
                {
                    Main.player[i] = new Player();
                }
            }
            Main.menuMode = 10;
            Main.menuMode = 14;
            if (!Main.autoPass)
            {
                Main.statusText = "Connecting to " + Netplay.serverIP;
            }
            Main.netMode       = 1;
            Netplay.disconnect = false;
            Netplay.clientSock = new ClientSock();
            Netplay.clientSock.tcpClient.NoDelay = true;
            Netplay.clientSock.readBuffer        = new byte[1024];
            Netplay.clientSock.writeBuffer       = new byte[1024];
            bool flag = true;

            while (flag)
            {
                flag = false;
                try
                {
                    Netplay.clientSock.tcpClient.Connect(Netplay.serverIP, Netplay.serverPort);
                    Netplay.clientSock.networkStream = Netplay.clientSock.tcpClient.GetStream();
                    flag = false;
                }
                catch
                {
                    if (!Netplay.disconnect && Main.gameMenu)
                    {
                        flag = true;
                    }
                }
            }
            NetMessage.buffer[256].Reset();
            int num = -1;

            while (!Netplay.disconnect)
            {
                if (Netplay.clientSock.tcpClient.Connected)
                {
                    if (NetMessage.buffer[256].checkBytes)
                    {
                        NetMessage.CheckBytes(256);
                    }
                    Netplay.clientSock.active = true;
                    if (Netplay.clientSock.state == 0)
                    {
                        Main.statusText          = "Found server";
                        Netplay.clientSock.state = 1;
                        NetMessage.SendData(1, -1, -1, "", 0, 0f, 0f, 0f, 0);
                    }
                    if (Netplay.clientSock.state == 2 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "Sending player data...";
                    }
                    if (Netplay.clientSock.state == 3 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "Requesting world information";
                    }
                    if (Netplay.clientSock.state == 4)
                    {
                        WorldGen.worldCleared    = false;
                        Netplay.clientSock.state = 5;
                        if (Main.cloudBGActive >= 1f)
                        {
                            Main.cloudBGAlpha = 1f;
                        }
                        else
                        {
                            Main.cloudBGAlpha = 0f;
                        }
                        Main.windSpeed = Main.windSpeedSet;
                        Cloud.resetClouds();
                        Main.cloudAlpha = Main.maxRaining;
                        WorldGen.clearWorld();
                        if (Main.mapEnabled)
                        {
                            Map.loadMap();
                        }
                    }
                    if (Netplay.clientSock.state == 5 && Main.loadMapLock)
                    {
                        float num2 = (float)Main.loadMapLastX / (float)Main.maxTilesX;
                        Main.statusText = string.Concat(new object[]
                        {
                            Lang.gen[68],
                            " ",
                            (int)(num2 * 100f + 1f),
                            "%"
                        });
                    }
                    else
                    {
                        if (Netplay.clientSock.state == 5 && WorldGen.worldCleared)
                        {
                            Netplay.clientSock.state = 6;
                            Main.player[Main.myPlayer].FindSpawn();
                            NetMessage.SendData(8, -1, -1, "", Main.player[Main.myPlayer].SpawnX, (float)Main.player[Main.myPlayer].SpawnY, 0f, 0f, 0);
                        }
                    }
                    if (Netplay.clientSock.state == 6 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "Requesting tile data";
                    }
                    if (!Netplay.clientSock.locked && !Netplay.disconnect && Netplay.clientSock.networkStream.DataAvailable)
                    {
                        Netplay.clientSock.locked = true;
                        Netplay.clientSock.networkStream.BeginRead(Netplay.clientSock.readBuffer, 0, Netplay.clientSock.readBuffer.Length, new AsyncCallback(Netplay.clientSock.ClientReadCallBack), Netplay.clientSock.networkStream);
                    }
                    if (Netplay.clientSock.statusMax > 0 && Netplay.clientSock.statusText != "")
                    {
                        if (Netplay.clientSock.statusCount >= Netplay.clientSock.statusMax)
                        {
                            Main.statusText = Netplay.clientSock.statusText + ": Complete!";
                            Netplay.clientSock.statusText  = "";
                            Netplay.clientSock.statusMax   = 0;
                            Netplay.clientSock.statusCount = 0;
                        }
                        else
                        {
                            Main.statusText = string.Concat(new object[]
                            {
                                Netplay.clientSock.statusText,
                                ": ",
                                (int)((float)Netplay.clientSock.statusCount / (float)Netplay.clientSock.statusMax * 100f),
                                "%"
                            });
                        }
                    }
                    Thread.Sleep(1);
                }
                else
                {
                    if (Netplay.clientSock.active)
                    {
                        Main.statusText    = "Lost connection";
                        Netplay.disconnect = true;
                    }
                }
                num = Netplay.clientSock.state;
            }
            try
            {
                Netplay.clientSock.networkStream.Close();
                if (Netplay.clientSock.tcpClient.Connected)
                {
                    Netplay.clientSock.networkStream = Netplay.clientSock.tcpClient.GetStream();
                }
            }
            catch
            {
            }
            if (!Main.gameMenu)
            {
                Main.netMode = 0;
                Player.SavePlayer(Main.player[Main.myPlayer], Main.playerPathName, false, false);
                Main.gameMenu = true;
                Main.menuMode = 14;
            }
            NetMessage.buffer[256].Reset();
            if (Main.menuMode == 15 && Main.statusText == "Lost connection")
            {
                Main.menuMode = 14;
            }
            if (Netplay.clientSock.statusText != "" && Netplay.clientSock.statusText != null)
            {
                Main.statusText = "Lost connection";
            }
            Netplay.clientSock.statusCount = 0;
            Netplay.clientSock.statusMax   = 0;
            Netplay.clientSock.statusText  = "";
            Main.netMode = 0;
        }
コード例 #5
0
ファイル: Netplay.cs プロジェクト: saniainf/EDTerraria
        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      = (int)byte.MaxValue;
            Netplay.ServerIP   = IPAddress.Any;
            Main.menuMode      = 14;
            Main.statusText    = "Starting server...";
            Main.netMode       = 2;
            Netplay.disconnect = false;
            for (int index = 0; index < 256; ++index)
            {
                Netplay.Clients[index] = new RemoteClient();
                Netplay.Clients[index].Reset();
                Netplay.Clients[index].Id         = index;
                Netplay.Clients[index].ReadBuffer = new byte[1024];
            }
            Netplay.TcpListener = (ISocket) new TcpSocket();
            if (!Netplay.disconnect)
            {
                if (!Netplay.StartListening())
                {
                    Main.menuMode      = 15;
                    Main.statusText    = "Tried to run two servers on the same PC";
                    Netplay.disconnect = true;
                }
                Main.statusText = "Server started";
            }
            if (Netplay.UseUPNP)
            {
                try
                {
                    Netplay.OpenPort();
                }
                catch
                {
                }
            }
            int num1 = 0;

            while (!Netplay.disconnect)
            {
                if (!Netplay.IsListening)
                {
                    int num2 = -1;
                    for (int index = 0; index < Main.maxNetPlayers; ++index)
                    {
                        if (!Netplay.Clients[index].Socket.IsConnected())
                        {
                            num2 = index;
                            break;
                        }
                    }
                    if (num2 >= 0)
                    {
                        if (Main.ignoreErrors)
                        {
                            try
                            {
                                Netplay.StartListening();
                                Netplay.IsListening = true;
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            Netplay.StartListening();
                            Netplay.IsListening = true;
                        }
                    }
                }
                int num3 = 0;
                for (int bufferIndex = 0; bufferIndex < 256; ++bufferIndex)
                {
                    if (NetMessage.buffer[bufferIndex].checkBytes)
                    {
                        NetMessage.CheckBytes(bufferIndex);
                    }
                    if (Netplay.Clients[bufferIndex].PendingTermination)
                    {
                        Netplay.Clients[bufferIndex].Reset();
                        NetMessage.syncPlayers();
                    }
                    else if (Netplay.Clients[bufferIndex].Socket.IsConnected())
                    {
                        if (!Netplay.Clients[bufferIndex].IsActive)
                        {
                            Netplay.Clients[bufferIndex].State = 0;
                        }
                        Netplay.Clients[bufferIndex].IsActive = true;
                        ++num3;
                        if (!Netplay.Clients[bufferIndex].IsReading)
                        {
                            try
                            {
                                if (Netplay.Clients[bufferIndex].Socket.IsDataAvailable())
                                {
                                    Netplay.Clients[bufferIndex].IsReading = true;
                                    Netplay.Clients[bufferIndex].Socket.AsyncReceive(Netplay.Clients[bufferIndex].ReadBuffer, 0, Netplay.Clients[bufferIndex].ReadBuffer.Length, new SocketReceiveCallback(Netplay.Clients[bufferIndex].ServerReadCallBack), (object)null);
                                }
                            }
                            catch
                            {
                                Netplay.Clients[bufferIndex].PendingTermination = true;
                            }
                        }
                        if (Netplay.Clients[bufferIndex].StatusMax > 0 && Netplay.Clients[bufferIndex].StatusText2 != "")
                        {
                            if (Netplay.Clients[bufferIndex].StatusCount >= Netplay.Clients[bufferIndex].StatusMax)
                            {
                                Netplay.Clients[bufferIndex].StatusText  = "(" + (object)Netplay.Clients[bufferIndex].Socket.GetRemoteAddress() + ") " + Netplay.Clients[bufferIndex].Name + " " + Netplay.Clients[bufferIndex].StatusText2 + ": Complete!";
                                Netplay.Clients[bufferIndex].StatusText2 = "";
                                Netplay.Clients[bufferIndex].StatusMax   = 0;
                                Netplay.Clients[bufferIndex].StatusCount = 0;
                            }
                            else
                            {
                                Netplay.Clients[bufferIndex].StatusText = "(" + (object)Netplay.Clients[bufferIndex].Socket.GetRemoteAddress() + ") " + Netplay.Clients[bufferIndex].Name + " " + Netplay.Clients[bufferIndex].StatusText2 + ": " + (string)(object)(int)((double)Netplay.Clients[bufferIndex].StatusCount / (double)Netplay.Clients[bufferIndex].StatusMax * 100.0) + "%";
                            }
                        }
                        else if (Netplay.Clients[bufferIndex].State == 0)
                        {
                            Netplay.Clients[bufferIndex].StatusText = "(" + (object)Netplay.Clients[bufferIndex].Socket.GetRemoteAddress() + ") " + Netplay.Clients[bufferIndex].Name + " is connecting...";
                        }
                        else if (Netplay.Clients[bufferIndex].State == 1)
                        {
                            Netplay.Clients[bufferIndex].StatusText = "(" + (object)Netplay.Clients[bufferIndex].Socket.GetRemoteAddress() + ") " + Netplay.Clients[bufferIndex].Name + " is sending player data...";
                        }
                        else if (Netplay.Clients[bufferIndex].State == 2)
                        {
                            Netplay.Clients[bufferIndex].StatusText = "(" + (object)Netplay.Clients[bufferIndex].Socket.GetRemoteAddress() + ") " + Netplay.Clients[bufferIndex].Name + " requested world information";
                        }
                        else if (Netplay.Clients[bufferIndex].State != 3)
                        {
                            if (Netplay.Clients[bufferIndex].State == 10)
                            {
                                try
                                {
                                    Netplay.Clients[bufferIndex].StatusText = "(" + (object)Netplay.Clients[bufferIndex].Socket.GetRemoteAddress() + ") " + Netplay.Clients[bufferIndex].Name + " is playing";
                                }
                                catch
                                {
                                    Netplay.Clients[bufferIndex].PendingTermination = true;
                                }
                            }
                        }
                    }
                    else if (Netplay.Clients[bufferIndex].IsActive)
                    {
                        Netplay.Clients[bufferIndex].PendingTermination = true;
                    }
                    else
                    {
                        Netplay.Clients[bufferIndex].StatusText2 = "";
                        if (bufferIndex < (int)byte.MaxValue)
                        {
                            Main.player[bufferIndex].active = false;
                        }
                    }
                }
                ++num1;
                if (num1 > 10)
                {
                    Thread.Sleep(1);
                    num1 = 0;
                }
                else
                {
                    Thread.Sleep(0);
                }
                if (!WorldGen.saveLock && !Main.dedServ)
                {
                    Main.statusText = num3 != 0 ? (string)(object)num3 + (object)" clients connected" : "Waiting for clients...";
                }
                Netplay.anyClients      = num3 != 0;
                Netplay.IsServerRunning = true;
            }
            Netplay.StopListening();
            try
            {
                Netplay.closePort();
            }
            catch
            {
            }
            for (int index = 0; index < 256; ++index)
            {
                Netplay.Clients[index].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;
        }
コード例 #6
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;
        }
コード例 #7
0
ファイル: Netplay.cs プロジェクト: menfeng/terraria
        public static void ClientLoop(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.player[Main.myPlayer].hostile = false;
            Main.clientPlayer = (Player)Main.player[Main.myPlayer].clientClone();
            for (int i = 0; i < 255; i++)
            {
                if (i != Main.myPlayer)
                {
                    Main.player[i] = new Player();
                }
            }
            Main.menuMode = 10;
            Main.menuMode = 14;
            if (!Main.autoPass)
            {
                Main.statusText = "正在连接到 " + Netplay.serverIP;
            }
            Main.netMode       = 1;
            Netplay.disconnect = false;
            Netplay.clientSock = new ClientSock();
            Netplay.clientSock.tcpClient.NoDelay = true;
            Netplay.clientSock.readBuffer        = new byte[1024];
            Netplay.clientSock.writeBuffer       = new byte[1024];
            bool flag = true;

            while (flag)
            {
                flag = false;
                try
                {
                    Netplay.clientSock.tcpClient.Connect(Netplay.serverIP, Netplay.serverPort);
                    Netplay.clientSock.networkStream = Netplay.clientSock.tcpClient.GetStream();
                    flag = false;
                }
                catch
                {
                    if (!Netplay.disconnect && Main.gameMenu)
                    {
                        flag = true;
                    }
                }
            }
            NetMessage.buffer[256].Reset();
            int num = -1;

            while (!Netplay.disconnect)
            {
                if (Netplay.clientSock.tcpClient.Connected)
                {
                    if (NetMessage.buffer[256].checkBytes)
                    {
                        NetMessage.CheckBytes(256);
                    }
                    Netplay.clientSock.active = true;
                    if (Netplay.clientSock.state == 0)
                    {
                        Main.statusText          = "连接到服务器";
                        Netplay.clientSock.state = 1;
                        NetMessage.SendData(1, -1, -1, "", 0, 0f, 0f, 0f, 0);
                    }
                    if (Netplay.clientSock.state == 2 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "发送角色数据...";
                    }
                    if (Netplay.clientSock.state == 3 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "向服务器请求世界信息";
                    }
                    if (Netplay.clientSock.state == 4)
                    {
                        WorldGen.worldCleared    = false;
                        Netplay.clientSock.state = 5;
                        WorldGen.clearWorld();
                    }
                    if (Netplay.clientSock.state == 5 && WorldGen.worldCleared)
                    {
                        Netplay.clientSock.state = 6;
                        Main.player[Main.myPlayer].FindSpawn();
                        NetMessage.SendData(8, -1, -1, "", Main.player[Main.myPlayer].SpawnX, (float)Main.player[Main.myPlayer].SpawnY, 0f, 0f, 0);
                    }
                    if (Netplay.clientSock.state == 6 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "向服务器请求物块数据";
                    }
                    if (!Netplay.clientSock.locked && !Netplay.disconnect && Netplay.clientSock.networkStream.DataAvailable)
                    {
                        Netplay.clientSock.locked = true;
                        Netplay.clientSock.networkStream.BeginRead(Netplay.clientSock.readBuffer, 0, Netplay.clientSock.readBuffer.Length, new AsyncCallback(Netplay.clientSock.ClientReadCallBack), Netplay.clientSock.networkStream);
                    }
                    if (Netplay.clientSock.statusMax > 0 && Netplay.clientSock.statusText != "")
                    {
                        if (Netplay.clientSock.statusCount >= Netplay.clientSock.statusMax)
                        {
                            Main.statusText = Netplay.clientSock.statusText + ": 完成!";
                            Netplay.clientSock.statusText  = "";
                            Netplay.clientSock.statusMax   = 0;
                            Netplay.clientSock.statusCount = 0;
                        }
                        else
                        {
                            Main.statusText = string.Concat(new object[]
                            {
                                Netplay.clientSock.statusText,
                                ": ",
                                (int)((float)Netplay.clientSock.statusCount / (float)Netplay.clientSock.statusMax * 100f),
                                "%"
                            });
                        }
                    }
                    Thread.Sleep(1);
                }
                else if (Netplay.clientSock.active)
                {
                    Main.statusText    = "失去连接";
                    Netplay.disconnect = true;
                }
                num = Netplay.clientSock.state;
            }
            try
            {
                Netplay.clientSock.networkStream.Close();
                Netplay.clientSock.networkStream = Netplay.clientSock.tcpClient.GetStream();
            }
            catch
            {
            }
            if (!Main.gameMenu)
            {
                Main.netMode = 0;
                Player.SavePlayer(Main.player[Main.myPlayer], Main.playerPathName);
                Main.gameMenu = true;
                Main.menuMode = 14;
            }
            NetMessage.buffer[256].Reset();
            if (Main.menuMode == 15 && Main.statusText == "失去连接")
            {
                Main.menuMode = 14;
            }
            if (Netplay.clientSock.statusText != "" && Netplay.clientSock.statusText != null)
            {
                Main.statusText = "失去连接";
            }
            Netplay.clientSock.statusCount = 0;
            Netplay.clientSock.statusMax   = 0;
            Netplay.clientSock.statusText  = "";
            Main.netMode = 0;
        }
コード例 #8
0
ファイル: Netplay.cs プロジェクト: windlex/mylualib
        // Token: 0x0600015D RID: 349 RVA: 0x0002AB20 File Offset: 0x00028D20
        public static void ServerLoop(object threadContext)
        {
            Netplay.ResetNetDiag();
            if (Main.rand == null)
            {
                Main.rand = new UnifiedRandom((int)DateTime.Now.Ticks);
            }
            Main.myPlayer      = 255;
            Netplay.ServerIP   = IPAddress.Any;
            Main.menuMode      = 14;
            Main.statusText    = Lang.menu[8].Value;
            Main.netMode       = 2;
            Netplay.disconnect = false;
            for (int i = 0; i < 256; i++)
            {
                Netplay.Clients[i] = new RemoteClient();
                Netplay.Clients[i].Reset();
                Netplay.Clients[i].Id         = i;
                Netplay.Clients[i].ReadBuffer = new byte[1024];
            }
            Netplay.TcpListener = new TcpSocket();
            if (!Netplay.disconnect)
            {
                if (!Netplay.StartListening())
                {
                    Main.menuMode      = 15;
                    Main.statusText    = Language.GetTextValue("Error.TriedToRunServerTwice");
                    Netplay.disconnect = true;
                }
                Main.statusText = Language.GetTextValue("CLI.ServerStarted");
            }
            if (Netplay.UseUPNP)
            {
                try
                {
                    Netplay.OpenPort();
                }
                catch
                {
                }
            }
            int num = 0;

            while (!Netplay.disconnect)
            {
                if (!Netplay.IsListening)
                {
                    int num2 = -1;
                    for (int j = 0; j < Main.maxNetPlayers; j++)
                    {
                        if (!Netplay.Clients[j].IsConnected())
                        {
                            num2 = j;
                            break;
                        }
                    }
                    if (num2 >= 0)
                    {
                        if (Main.ignoreErrors)
                        {
                            try
                            {
                                Netplay.StartListening();
                                Netplay.IsListening = true;
                                goto IL_15E;
                            }
                            catch
                            {
                                goto IL_15E;
                            }
                        }
                        Netplay.StartListening();
                        Netplay.IsListening = true;
                    }
                }
IL_15E:
                int num3 = 0;
                for (int k = 0; k < 256; k++)
                {
                    if (NetMessage.buffer[k].checkBytes)
                    {
                        NetMessage.CheckBytes(k);
                    }
                    if (Netplay.Clients[k].PendingTermination)
                    {
                        Netplay.Clients[k].Reset();
                        NetMessage.SyncDisconnectedPlayer(k);
                    }
                    else
                    {
                        if (Netplay.Clients[k].IsConnected())
                        {
                            if (!Netplay.Clients[k].IsActive)
                            {
                                Netplay.Clients[k].State = 0;
                            }
                            Netplay.Clients[k].IsActive = true;
                            num3++;
                            if (!Netplay.Clients[k].IsReading)
                            {
                                try
                                {
                                    if (Netplay.Clients[k].Socket.IsDataAvailable())
                                    {
                                        Netplay.Clients[k].IsReading = true;
                                        Netplay.Clients[k].Socket.AsyncReceive(Netplay.Clients[k].ReadBuffer, 0, Netplay.Clients[k].ReadBuffer.Length, new SocketReceiveCallback(Netplay.Clients[k].ServerReadCallBack), null);
                                    }
                                }
                                catch
                                {
                                    Netplay.Clients[k].PendingTermination = true;
                                }
                            }
                            if (Netplay.Clients[k].StatusMax > 0 && Netplay.Clients[k].StatusText2 != "")
                            {
                                if (Netplay.Clients[k].StatusCount >= Netplay.Clients[k].StatusMax)
                                {
                                    Netplay.Clients[k].StatusText  = Language.GetTextValue("Net.ClientStatusComplete", Netplay.Clients[k].Socket.GetRemoteAddress(), Netplay.Clients[k].Name, Netplay.Clients[k].StatusText2);
                                    Netplay.Clients[k].StatusText2 = "";
                                    Netplay.Clients[k].StatusMax   = 0;
                                    Netplay.Clients[k].StatusCount = 0;
                                    goto IL_58E;
                                }
                                Netplay.Clients[k].StatusText = string.Concat(new object[]
                                {
                                    "(",
                                    Netplay.Clients[k].Socket.GetRemoteAddress(),
                                    ") ",
                                    Netplay.Clients[k].Name,
                                    " ",
                                    Netplay.Clients[k].StatusText2,
                                    ": ",
                                    (int)((float)Netplay.Clients[k].StatusCount / (float)Netplay.Clients[k].StatusMax * 100f),
                                    "%"
                                });
                                goto IL_58E;
                            }
                            else
                            {
                                if (Netplay.Clients[k].State == 0)
                                {
                                    Netplay.Clients[k].StatusText = Language.GetTextValue("Net.ClientConnecting", string.Format("({0}) {1}", Netplay.Clients[k].Socket.GetRemoteAddress(), Netplay.Clients[k].Name));
                                    goto IL_58E;
                                }
                                if (Netplay.Clients[k].State == 1)
                                {
                                    Netplay.Clients[k].StatusText = Language.GetTextValue("Net.ClientSendingData", Netplay.Clients[k].Socket.GetRemoteAddress(), Netplay.Clients[k].Name);
                                    goto IL_58E;
                                }
                                if (Netplay.Clients[k].State == 2)
                                {
                                    Netplay.Clients[k].StatusText = Language.GetTextValue("Net.ClientRequestedWorldInfo", Netplay.Clients[k].Socket.GetRemoteAddress(), Netplay.Clients[k].Name);
                                    goto IL_58E;
                                }
                                if (Netplay.Clients[k].State == 3 || Netplay.Clients[k].State != 10)
                                {
                                    goto IL_58E;
                                }
                                try
                                {
                                    Netplay.Clients[k].StatusText = Language.GetTextValue("Net.ClientPlaying", Netplay.Clients[k].Socket.GetRemoteAddress(), Netplay.Clients[k].Name);
                                    goto IL_58E;
                                }
                                catch (Exception)
                                {
                                    Netplay.Clients[k].PendingTermination = true;
                                    goto IL_58E;
                                }
                            }
                        }
                        if (Netplay.Clients[k].IsActive)
                        {
                            Netplay.Clients[k].PendingTermination = true;
                        }
                        else
                        {
                            Netplay.Clients[k].StatusText2 = "";
                            if (k < 255)
                            {
                                bool arg_585_0 = Main.player[k].active;
                                Main.player[k].active = false;
                                if (arg_585_0)
                                {
                                    Player.Hooks.PlayerDisconnect(k);
                                }
                            }
                        }
                    }
                    IL_58E :;
                }
                num++;
                if (num > 10)
                {
                    Thread.Sleep(1);
                    num = 0;
                }
                else
                {
                    Thread.Sleep(0);
                }
                if (!WorldGen.saveLock && !Main.dedServ)
                {
                    if (num3 == 0)
                    {
                        Main.statusText = Language.GetTextValue("Net.WaitingForClients");
                    }
                    else
                    {
                        Main.statusText = Language.GetTextValue("Net.ClientsConnected", num3);
                    }
                }
                if (num3 == 0)
                {
                    Netplay.anyClients = false;
                }
                else
                {
                    Netplay.anyClients = true;
                }
                Netplay.IsServerRunning = true;
            }
            Netplay.StopListening();
            try
            {
                Netplay.closePort();
            }
            catch
            {
            }
            for (int l = 0; l < 256; l++)
            {
                Netplay.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;
        }