Esempio n. 1
0
 public void Refresh()
 {
     if (AgentId == -1 || PlayerId == -1)
     {
         throw new InvalidOperationException("Can not refresh when not ready.");
     }
     client.Send(new Message()
     {
         Address = -1,
         Content = new Message()
         {
             Address = AgentId,
             Content = new Message()
             {
                 Address = PlayerId,
                 Content = new PingPacket()
                 {
                     Ticks = DateTime.Now.Ticks
                 }
             }
         }
     });
 }
Esempio n. 2
0
        private static int MainInternal(string ep, ushort port, string token, int debugLevel)
        {
            string[] t = ep.Split(':');
            Console.WriteLine("Server endpoint: " + t);
            Server      = new IPEndPoint(IPAddress.Parse(t[0]), int.Parse(t[1]));
            server.Port = port;
            Console.WriteLine("Agent Listen Port: " + server.Port.ToString());
            Console.WriteLine("Client Token: " + (token ?? "<offline>"));

            //init timer
            myTimer.Interval  = Interval;
            myTimer.Elapsed  += TimedUpdate;
            myTimer.Enabled   = true;
            client.OnReceive += delegate(Message message)
            {
                server.Send(message.Content as Message); //向客户端转发Content
            };
            server.InternalQuit += delegate()
            {
                client.Quit();
                server.Resume();
            };
            server.OnReceive += delegate(Message message)
            {
                if (MessageLimit > 0)
                {
                    client.Send(message.Content as Message);
                    MessageLimit--;
                    System.Timers.Timer timer = new System.Timers.Timer(Constants.TimeLimit);
                    timer.AutoReset = false;
                    timer.Elapsed  += TimeCount;
                    timer.Start();
                }
            };

            client.OnDisconnect += delegate()
            {
                if (server.Count == 0)
                {
                    server.Stop(); //主动Disconnect意味着游戏结束,关闭Agent。
                    Environment.Exit(0);
                }
            };

            server.OnAccept += delegate()
            {
                Constants.Debug($"Player Connected: {server.Count}/{Constants.PlayerCount}");
                if (server.Count == Constants.PlayerCount)
                {
                    server.Pause();
                    client.Connect(Server); //客户端满人后再向Server发送连接请求,可以省略GameStart包
                    if (token != "offline")
                    {
                        client.Send(new Message //发送token
                        {
                            Address = -1,
                            Content = new PlayerToken
                            {
                                Token = token
                            }
                        });
                    }
                }
            };

            if (debugLevel == 0)
            {
                Constants.Debug = new Constants.DebugFunc((str) => { });
            }

            server.Start();

            Thread.Sleep(int.MaxValue);
            return(0);
        }
Esempio n. 3
0
        private static int MainInternal(string ep, ushort port, string token, int debugLevel)
        {
            string[] t = ep.Split(':');
            Constants.Debug("Server endpoint: " + t);
            Server      = new IPEndPoint(IPAddress.Parse(t[0]), int.Parse(t[1]));
            server.Port = port;
            Constants.Debug("Agent Listen Port: " + server.Port.ToString());
            Constants.Debug("Client Token: " + (token ?? "<offline>"));
            //for (int i = 0; i < LastSpam.Length; i++)
            //    LastSpam[i] = Environment.TickCount;

            //init timer
            myTimer.Interval  = Interval;
            myTimer.Elapsed  += TimedUpdate;
            myTimer.Enabled   = true;
            client.OnReceive += delegate(Message message)
            {
                Console.Write("S:" + Environment.TickCount % 10000 + ",");
                server.Send(message.Content as Message); //向客户端转发Content
            };
            server.InternalQuit += delegate()
            {
                client.Quit();
                server.Resume();
            };
            server.OnReceive += delegate(Message message)
            {
                //if (!(((message.Content as Message)?.Content as Message)?.Content is PingPacket))
                //{
                //    var now = Environment.TickCount;
                //    lock (LastSpam)
                //    {
                //        //Console.WriteLine(message.Address + " : " + now + " deltaSendTime : " + deltaSendTime[message.Address][0] + ", " + deltaSendTime[message.Address][1]);
                //        int deltaTime = now - (int)LastSpam[message.Address];
                //        if (((double)deltaTime + (double)deltaSendTime[message.Address][0] + (double)deltaSendTime[message.Address][1]) / 3.0 <= Constants.TimeLimit)
                //        {
                //            Console.WriteLine("skip client's message");
                //            return;
                //        }
                //        LastSpam[message.Address] = now;
                //        deltaSendTime[message.Address][0] = deltaSendTime[message.Address][1];
                //        deltaSendTime[message.Address][1] = deltaTime;
                //    }
                //}
                //else
                //    Constants.Debug("Ignoring PingPacket");
                Console.Write("C" + message.Address + ":" + Environment.TickCount % 10000 + ",");
                client.Send(message.Content as Message);
            };

            client.OnDisconnect += delegate()
            {
                if (server.Count == 0)
                {
                    server.Stop(); //主动Disconnect意味着游戏结束,关闭Agent。
                    Environment.Exit(0);
                }
            };

            server.OnAccept += delegate()
            {
                Constants.Debug($"Player Connected: {server.Count}/{Constants.PlayerCount}");
                if (server.Count == Constants.PlayerCount)
                {
                    server.Pause();
                    client.Connect(Server); //客户端满人后再向Server发送连接请求,可以省略GameStart包

                    client.Send(new Message //发送token
                    {
                        Address = -1,
                        Content = new PlayerToken
                        {
                            Token = token ?? "" //空token代表离线
                        }
                    });
                }
            };

            if (debugLevel == 0)
            {
                Constants.Debug = new Constants.DebugFunc((str) => { });
            }

            server.Start();

            Thread.Sleep(int.MaxValue);
            return(0);
        }