示例#1
0
 private static void RegisterEvents(MyRakNetServer server)
 {
     server.OnChatMessage    += server_OnChatMessage;
     server.OnClientJoined   += server_OnClientJoined;
     server.OnClientLeft     += server_OnClientLeft;
     server.OnClientReady    += server_OnClientReady;
     server.OnConnectionLost += server_OnConnectionLost;
     server.OnRequestWorld   += server_OnRequestWorld;
 }
示例#2
0
        static void Main(string[] args)
        {
            MyPlugins.Load();

            var asyncInput = ReadConsoleAsync();

            Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs eventArgs)
            {
                eventArgs.Cancel = true;
                IsRunning        = false;
            };

            m_server = new MyRakNetServer(0);

            m_server.Startup(32, 27025, null);

            new MyRakNetSyncLayer().LoadData(m_server, typeof(Program).Assembly);

            //MySyncedClass mySyncedObject = new MySyncedClass();
            //MySyncedFloatSNorm F = new MySyncedFloatSNorm();
            //mySyncedObject.Add(F);
            //MySyncedInt I = new MySyncedInt();
            //mySyncedObject.Add(I);

            //MySyncedClass myInnerSyncedObject = new MySyncedClass();
            //MySyncedVector3 V3 = new MySyncedVector3();
            //mySyncedObject.Add(V3);
            //mySyncedObject.Add(myInnerSyncedObject);
            //MyRakNetSyncLayer.RegisterSynced(mySyncedObject);

            RegisterEvents(m_server);

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            Foo foo = null;

            bool sin = false;
            bool str = false;

            while (IsRunning)
            {
                if (asyncInput.IsCompleted)
                {
                    var cmd = asyncInput.Result;
                    if (!String.IsNullOrEmpty(cmd))
                    {
                        if (cmd == "quit")
                        {
                            IsRunning = false;
                        }
                        else if (cmd == "+")
                        {
                            foo          = new Foo();
                            foo.EntityID = 1;
                            MyRakNetSyncLayer.Replicate(foo, foo.EntityID);
                        }
                        else if (cmd == "-")
                        {
                            if (foo != null)
                            {
                                MyRakNetSyncLayer.Destroy(foo.EntityID);
                                foo = null;
                            }
                        }
                        else if (cmd == "sin")
                        {
                            if (foo != null)
                            {
                                sin = !sin;
                            }
                        }
                        else if (cmd == "str")
                        {
                            if (foo != null)
                            {
                                str = !str;
                            }
                        }
                        else
                        {
                            m_server.SendChatMessage(cmd);
                        }
                    }
                    asyncInput = ReadConsoleAsync();
                }

                if (foo != null)
                {
                    if (sin)
                    {
                        foo.Position.Set(new Vector3D(Math.Sin((double)stopWatch.ElapsedMilliseconds / 1000.0)));
                    }
                    if (str)
                    {
                        char c = (char)('a' + (byte)(stopWatch.ElapsedMilliseconds / 1000.0) % 26);
                        foo.Name.Set(String.Concat(Enumerable.Repeat(c, 100)));
                    }
                }

                //I.Set((int)(stopWatch.ElapsedMilliseconds / 1000.0));

                //F.Set((float)Math.Sin((double)stopWatch.ElapsedMilliseconds / 1000.0));
                //I.Set((int)(stopWatch.ElapsedMilliseconds/10));
                //V3.Set(new Vector3(F, F, F));

                MyRakNetSyncLayer.Static.Update();
                m_server.Update();

                //Console.Out.WriteLine(m_server.GetStatsToString());

                Thread.Sleep(16);
            }
            m_server.Dispose();
            MyRakNetSyncLayer.Static.UnloadData();
            MyPlugins.Unload();
        }