Пример #1
0
        static void Main(string[] args)
        {
            int  listenPort      = 12345;// int.Parse(System.Configuration.ConfigurationManager.AppSettings["listen_port"]);
            int  playercount     = 2;
            bool useNatTraversal = true;

            try
            {
                CooperateRim.TickManagerPatch.syncRoundLength = int.Parse(System.Configuration.ConfigurationManager.AppSettings["round_length_ticks"]);
                playercount     = int.Parse(System.Configuration.ConfigurationManager.AppSettings["player_count"]);
                useNatTraversal = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["sockopt_nat_traversal"]);
                NetDemo.SetDesiredPlayerCount(playercount);
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.ToString());
                Console.WriteLine("Error reading config file. Press any key to exit.");
                Console.ReadKey();
                return;
            }
            //UPnP.NAT.Discover();
            //UPnP.NAT.ForwardPort(12345, ProtocolType.Tcp, "MyApp (TCP)");
            //CRand.set_state(900 << 32);
            int workers;
            int completions;

            ThreadPool.GetMinThreads(out workers, out completions);
            bool        res = ThreadPool.SetMinThreads(4, 4);
            TcpListener lst = new TcpListener(IPAddress.Any, listenPort);

            lst.Server.NoDelay = true;
            if (useNatTraversal)
            {
                lst.AllowNatTraversal(useNatTraversal);
            }
            lst.Server.ExclusiveAddressUse = true;
            lst.Start();
            AsyncCallback acceptor = null;

            Console.WriteLine("Player count set to " + playercount);
            Console.WriteLine("Listen port is " + listenPort);

            AssemblyName updatedAssemblyName = AssemblyName.GetAssemblyName("CooperateRim.dll");

            Console.WriteLine("RimAlong mod version is " + updatedAssemblyName.Version);
            Console.WriteLine("RimAlong server version 0.0.0.3, codename \"crubmle tree\", ready to rumble!");
            CooperateRim.SyncTickData.cliendID = 0;


            lst.BeginAcceptTcpClient(acceptor = u =>
            {
                Console.WriteLine("new client");
                TcpClient tcc    = (u.AsyncState as TcpListener).EndAcceptTcpClient(u);
                NetworkStream ns = tcc.GetStream();
                int clid         = CooperateRim.SyncTickData.cliendID;

                (u.AsyncState as TcpListener).BeginAcceptTcpClient(acceptor, u.AsyncState);

                if (clid < NetDemo.desiredPlayerCount)
                {
                    NetDemo.allClients.AddLast(ns);


                    for (; tcc.Connected;)
                    {
                        Thread.Sleep(10);
                        if (ns.DataAvailable)
                        {
                            PirateRPC.PirateRPC.ReceiveInvocation(ns);
                        }
                    }
                    ;
                }
                else
                {
                    for (; !ns.DataAvailable;)
                    {
                        Thread.Sleep(10);
                        if (ns.DataAvailable)
                        {
                            PirateRPC.PirateRPC.ReceiveInvocation(ns);
                            break;
                        }
                    }
                    ;
                }

                Console.WriteLine("client lost");
            }, lst);

            for (; ;)
            {
                Thread.Sleep(10);
            }
        }