OpenPort() private static method

private static OpenPort ( ) : void
return void
示例#1
0
 private static void InitializeServer()
 {
     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)
     {
         return;
     }
     try
     {
         Netplay.OpenPort(Netplay.ListenPort);
     }
     catch (Exception ex)
     {
     }
 }
示例#2
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)
                        {
                            bool active = Main.player[index].active;
                            Main.player[index].active = false;
                            if (active)
                            {
                                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;
        }
示例#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      = (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;
        }
示例#4
0
        // 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;
        }