コード例 #1
0
ファイル: AsyncPing.cs プロジェクト: lyzardiar/Librelancer
 static void RunLinux(IPAddress addr, Action <int> RTT)
 {
     AsyncManager.RunTask(() =>
     {
         try
         {
             var p                    = new ProcessStartInfo("bash", "-c 'LANG=C ping -c 2 -i 0.8 -w 2 \"" + addr + "\"'");
             p.UseShellExecute        = false;
             p.RedirectStandardOutput = true;
             var proc                 = Process.Start(p);
             string s                 = "";
             proc.OutputDataReceived += (sender, e) =>
             {
                 s += "\n" + e.Data;
             };
             proc.BeginOutputReadLine();
             proc.WaitForExit();
             if (proc.ExitCode != 0)
             {
                 return;
             }
             //Parse result (e.g. rtt min/avg/max/mdev = 27.766/28.624/29.482/0.858 ms)
             var split = s.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
             var f     = split[split.Length - 1];
             var avg   = (int)double.Parse(f.Split('/')[4], CultureInfo.InvariantCulture);
             RTT(avg);
         }
         catch { }
     });
 }
コード例 #2
0
 public void Update(ICamera camera)
 {
     vp        = camera.ViewProjection;
     cameraPos = camera.Position;
     _camera   = camera;
     //for (int i = 0; i < field.Cube.Count; i++)
     //field.Cube [i].Drawable.Update (camera, TimeSpan.Zero);
     if (field.Cube.Count > 0 && VectorMath.DistanceSquared(cameraPos, field.Zone.Position) <= renderDistSq)
     {
         _asteroidsCalculated = false;
         cubeCount            = 0;
         AsyncManager.RunTask(_asteroidsCalculation);
     }
 }
コード例 #3
0
ファイル: CommandBuffer.cs プロジェクト: darkzter/Librelancer
 public void DrawOpaque(RenderState state)
 {
     _sorted = false;
     _filled = false;
     AsyncManager.RunTask(_transparentSort);
     for (int i = 0; i < currentCommand; i++)
     {
         Commands[i].Run(state);
         if (!_filled)
         {
             if (_sorted)
             {
                 FillBillboards();
             }
         }
     }
 }
コード例 #4
0
ファイル: GameServer.cs プロジェクト: youndoldman/Librelancer
        void NetThread()
        {
            FLLog.Info("Server", "Loading Game Data...");
            GameData.LoadData();
            FLLog.Info("Server", "Finished Loading Game Data");
            Database = new ServerDatabase(DbConnectionString);
            var netconf = new NetPeerConfiguration(AppIdentifier);

            netconf.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            netconf.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
            netconf.EnableMessageType(NetIncomingMessageType.UnconnectedData);
            netconf.Port = Port;
            netconf.MaximumConnections = 200;
            NetServer = new NetServer(netconf);
            NetServer.Start();
            FLLog.Info("Server", "Listening on port " + Port);
            NetIncomingMessage im;

            while (running)
            {
                while ((im = NetServer.ReadMessage()) != null)
                {
                    switch (im.MessageType)
                    {
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                        FLLog.Info("Lidgren", im.ReadString());
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.ConnectionApproval:
                        //Ban IP?
                        im.SenderConnection.Approve();
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.DiscoveryRequest:
                        NetOutgoingMessage dresp = NetServer.CreateMessage();
                        //Include Server Data
                        dresp.Write(ServerName);
                        dresp.Write(ServerDescription);
                        dresp.Write(GameData.DataVersion);
                        dresp.Write(NetServer.ConnectionsCount);
                        dresp.Write(NetServer.Configuration.MaximumConnections);
                        //Send off
                        NetServer.SendDiscoveryResponse(dresp, im.SenderEndPoint);
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.UnconnectedData:
                        //Respond to pings
                        try
                        {
                            if (im.ReadUInt32() == NetConstants.PING_MAGIC)
                            {
                                var om = NetServer.CreateMessage();
                                om.Write(NetConstants.PING_MAGIC);
                                NetServer.SendUnconnectedMessage(om, im.SenderEndPoint);
                            }
                        }
                        catch (Exception)
                        {
                        }
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();

                        string reason = im.ReadString();
                        FLLog.Info("Lidgren", NetUtility.ToHexString(im.SenderConnection.RemoteUniqueIdentifier) + " " + status + ": " + reason);

                        if (status == NetConnectionStatus.Connected)
                        {
                            FLLog.Info("Lidgren", "Remote hail: " + im.SenderConnection.RemoteHailMessage.ReadString());
                            BeginAuthentication(NetServer, im.SenderConnection);
                        }
                        else if (status == NetConnectionStatus.Disconnected)
                        {
                            FLLog.Info("Lidgren", im.SenderEndPoint.ToString() + " disconnected");
                            if (im.SenderConnection.Tag is NetPlayer)
                            {
                                ((NetPlayer)im.SenderConnection.Tag).Disconnected();
                            }
                        }
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.Data:
                        var pkt = im.ReadPacket();
                        if (im.SenderConnection.Tag == TagConnecting)
                        {
                            if (pkt is AuthenticationReplyPacket)
                            {
                                var auth = (AuthenticationReplyPacket)pkt;

                                //im.SenderConnection.Disconnect("boilerplate reason from server");

                                /*
                                 *                                  var authkind = (AuthenticationKind)im.ReadByte();
                                 *                                  var guid = new Guid(im.ReadBytes(16));
                                 *                                  if (guid == Guid.Empty) im.SenderConnection.Disconnect("Invalid UUID");
                                 *                                  FLLog.Info("Lidgren", "GUID for " + im.SenderEndPoint + " = " + guid.ToString());
                                 *                                  var p = new NetPlayer(im.SenderConnection, this, guid);
                                 *                                  im.SenderConnection.Tag = p;
                                 *                                  AsyncManager.RunTask(() => p.DoAuthSuccess());*/

                                var p = new NetPlayer(im.SenderConnection, this, auth.Guid);
                                im.SenderConnection.Tag = p;
                                AsyncManager.RunTask(() => p.DoAuthSuccess());
                            }
                            else
                            {
                                im.SenderConnection.Disconnect("Invalid Packet");
                            }
                            NetServer.Recycle(im);
                        }
                        else
                        {
                            var player = (NetPlayer)im.SenderConnection.Tag;
                            AsyncManager.RunTask(() => player.ProcessPacket(pkt));
                            NetServer.Recycle(im);
                        }
                        break;
                    }
                }
                Thread.Sleep(0);                 //Reduce CPU load
            }
            NetServer.Shutdown("Shutdown");
            Database.Dispose();
        }
コード例 #5
0
        void NetThread()
        {
            var netconf = new NetPeerConfiguration(AppIdentifier);

            netconf.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            netconf.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
            netconf.EnableMessageType(NetIncomingMessageType.UnconnectedData);
            netconf.DualStack          = true;
            netconf.Port               = Port;
            netconf.MaximumConnections = 200;
            NetServer = new NetServer(netconf);
            NetServer.Start();
            FLLog.Info("Server", "Listening on port " + Port);
            NetIncomingMessage im;

            while (running)
            {
                while ((im = NetServer.ReadMessage()) != null)
                {
                    switch (im.MessageType)
                    {
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                        FLLog.Info("Lidgren", im.ReadString());
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.ConnectionApproval:
                        //Ban IP?
                        im.SenderConnection.Approve();
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.DiscoveryRequest:
                        NetOutgoingMessage dresp = NetServer.CreateMessage();
                        //Include Server Data
                        dresp.Write(game.ServerName);
                        dresp.Write(game.ServerDescription);
                        dresp.Write(game.GameData.DataVersion);
                        dresp.Write(NetServer.ConnectionsCount);
                        dresp.Write(NetServer.Configuration.MaximumConnections);
                        //Send off
                        NetServer.SendDiscoveryResponse(dresp, im.SenderEndPoint);
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.UnconnectedData:
                        //Respond to pings
                        try
                        {
                            if (im.ReadUInt32() == NetConstants.PING_MAGIC)
                            {
                                var om = NetServer.CreateMessage();
                                om.Write(NetConstants.PING_MAGIC);
                                NetServer.SendUnconnectedMessage(om, im.SenderEndPoint);
                            }
                        }
                        finally
                        {
                            NetServer.Recycle(im);
                        }
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();
                        string reason = im.ReadString();
                        FLLog.Info("Lidgren",
                                   NetUtility.ToHexString(im.SenderConnection.RemoteUniqueIdentifier) + " " + status +
                                   ": " + reason);
                        if (status == NetConnectionStatus.Connected)
                        {
                            FLLog.Info("Lidgren",
                                       "Remote hail: " + im.SenderConnection.RemoteHailMessage.ReadString());
                            BeginAuthentication(NetServer, im.SenderConnection);
                        }
                        else if (status == NetConnectionStatus.Disconnected)
                        {
                            FLLog.Info("Lidgren", im.SenderEndPoint.ToString() + " disconnected");
                            if (im.SenderConnection.Tag is Player player)
                            {
                                player.Disconnected();
                                lock (game.ConnectedPlayers)
                                {
                                    game.ConnectedPlayers.Remove(player);
                                }
                            }
                        }
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.Data:
                        IPacket pkt;
                        try
                        {
                            pkt = im.ReadPacket();
                        }
                        catch (Exception)
                        {
                            pkt = null;
                            im.SenderConnection.Disconnect("Malformed Packet");
                            if (im.SenderConnection.Tag is Player)
                            {
                                ((Player)im.SenderConnection.Tag).Disconnected();
                            }
                        }
                        if (pkt != null)
                        {
                            if (im.SenderConnection.Tag == TagConnecting)
                            {
                                if (pkt is AuthenticationReplyPacket)
                                {
                                    var auth = (AuthenticationReplyPacket)pkt;
                                    var p    = new Player(new RemotePacketClient(im.SenderConnection, NetServer),
                                                          game, auth.Guid);
                                    im.SenderConnection.Tag = p;
                                    AsyncManager.RunTask(() => p.DoAuthSuccess());
                                    lock (game.ConnectedPlayers)
                                    {
                                        game.ConnectedPlayers.Add(p);
                                    }
                                }
                                else
                                {
                                    im.SenderConnection.Disconnect("Invalid Packet");
                                }
                                NetServer.Recycle(im);
                            }
                            else
                            {
                                var player = (Player)im.SenderConnection.Tag;
                                AsyncManager.RunTask(() => player.ProcessPacket(pkt));
                                NetServer.Recycle(im);
                            }
                        }
                        break;
                    }
                }
                Thread.Sleep(0); //Reduce CPU load
            }
            NetServer.Shutdown("Shutdown");
        }
コード例 #6
0
ファイル: GameServer.cs プロジェクト: gp-alex/Librelancer
        void NetThread()
        {
            FLLog.Info("Server", "Loading Game Data...");
            GameData.LoadData();
            FLLog.Info("Server", "Finished Loading Game Data");
            Database = new ServerDatabase(DbConnectionString, GameData);
            var netconf = new NetPeerConfiguration(AppIdentifier);

            netconf.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            netconf.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
            netconf.Port = Port;
            netconf.MaximumConnections = 200;
            NetServer = new NetServer(netconf);
            NetServer.Start();
            FLLog.Info("Server", "Listening on port " + Port);
            NetIncomingMessage im;

            while (running)
            {
                while ((im = NetServer.ReadMessage()) != null)
                {
                    switch (im.MessageType)
                    {
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                        FLLog.Info("Lidgren", im.ReadString());
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.ConnectionApproval:
                        //Ban IP?
                        im.SenderConnection.Approve();
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.DiscoveryRequest:
                        NetOutgoingMessage dresp = NetServer.CreateMessage();
                        //Include Server Data
                        dresp.Write(ServerName);
                        dresp.Write(ServerDescription);
                        dresp.Write(NetServer.ConnectionsCount);
                        dresp.Write(NetServer.Configuration.MaximumConnections);
                        //Send off
                        NetServer.SendDiscoveryResponse(dresp, im.SenderEndPoint);
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();

                        string reason = im.ReadString();
                        FLLog.Info("Lidgren", NetUtility.ToHexString(im.SenderConnection.RemoteUniqueIdentifier) + " " + status + ": " + reason);

                        if (status == NetConnectionStatus.Connected)
                        {
                            FLLog.Info("Lidgren", "Remote hail: " + im.SenderConnection.RemoteHailMessage.ReadString());
                            BeginAuthentication(NetServer, im.SenderConnection);
                        }
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.Data:
                        var kind = (PacketKind)im.ReadByte();
                        if (im.SenderConnection.Tag == TagConnecting)
                        {
                            if (kind == PacketKind.Authentication)
                            {
                                var authkind = (AuthenticationKind)im.ReadByte();
                                var guid     = new Guid(im.ReadBytes(16));
                                if (guid == Guid.Empty)
                                {
                                    im.SenderConnection.Disconnect("Invalid UUID");
                                }
                                FLLog.Info("Lidgren", "GUID for " + im.SenderEndPoint + " = " + guid.ToString());
                                var p = new NetPlayer(im.SenderConnection, this, guid);
                                im.SenderConnection.Tag = p;
                                AsyncManager.RunTask(() => p.DoAuthSuccess());
                            }
                            else
                            {
                                im.SenderConnection.Disconnect("Invalid Packet");
                            }
                        }
                        else
                        {
                            var player = (NetPlayer)im.SenderConnection.Tag;
                            AsyncManager.RunTask(() => player.ProcessPacket(im, kind));
                        }
                        break;
                    }
                }
                Thread.Sleep(1);                 //Reduce CPU load
            }
            Database.Dispose();
        }