예제 #1
0
        private void RunRecieve()
        {
            IntPtr ptr = Marshal.AllocHGlobal(256);

            while (Connected)
            {
                // Debug.Log("recieving");
                try
                {
                    Marshal.Copy(ReadBytes(Marshal.SizeOf(typeof(Header))), 0, ptr, Marshal.SizeOf(typeof(Header)));
                    Header header = (Header)Marshal.PtrToStructure(ptr, typeof(Header));

                    if (header.type == 1)
                    {
                        Marshal.Copy(ReadBytes(Marshal.SizeOf(typeof(ControlPacket))), 0, ptr, Marshal.SizeOf(typeof(ControlPacket)));
                        ControlPacket cp = (ControlPacket)Marshal.PtrToStructure(ptr, typeof(ControlPacket));
                        if (cp.ID > lastCPID)
                        {
                            lastCPID = cp.ID;
                            VCList.Enqueue(CPToVC(cp));
                            axisControls = CPToAC(cp);
                        }
                    }
                    else if (header.type == 2)
                    {
                        Marshal.Copy(ReadBytes(Marshal.SizeOf(typeof(ManChangePacket))), 0, ptr, Marshal.SizeOf(typeof(ManChangePacket)));
                        ManChangePacket mcp = (ManChangePacket)Marshal.PtrToStructure(ptr, typeof(ManChangePacket));
                        MCPList.Enqueue(mcp);
                    }
                    else
                    {
                        Debug.Log("Incorrect header");
                    }
                }
                catch (IOException)
                {
                    Debug.Log("Error Recieving");
                    Connected = false;
                }
            }
            Marshal.FreeHGlobal(ptr);
        }
예제 #2
0
        public void Update()
        {
            if (conn != null)
            {
                if (!conn.GetConnected())
                {
                    msg("YARK: client disconneted");
                    conn = null;
                }
                else
                {
                    if (SceneManager.GetActiveScene().buildIndex == 7) //in flight?
                    {
                        currentTime = (UInt32)Planetarium.GetUniversalTime();
                        if (inFlight != 1 || AV.id != FlightGlobals.ActiveVessel.id || lastTime > currentTime)
                        {
                            inFlight = 1;
                            if (AV != null)
                            {
                                AV.OnPostAutopilotUpdate -= AxisInput.Callback;
                            }
                            AV = FlightGlobals.ActiveVessel;
                            AV.OnPostAutopilotUpdate += AxisInput.Callback;

                            //sync inputs on vessel switch
                            ControlPacket cp = new ControlPacket
                            {
                                MainControls      = CalcMainControls(),
                                ActionGroups      = CalcActionGroups(),
                                SASMode           = GetSASMode(true),
                                SpeedMode         = (byte)(FlightGlobals.speedDisplayMode + 1),
                                timeWarpRateIndex = GetTimeWarpIndex()
                            };
                            cp.targetHeading = cp.targetPitch = cp.targetRoll = cp.WheelSteer = cp.WheelThrottle = cp.Throttle = cp.Pitch = cp.Roll = cp.Yaw = cp.TX = cp.TY = cp.TZ = 0;

                            conn.SyncControls(CPToVC(cp));

                            msg("rysync");

                            UpdateSP();
                            conn.SendStatusPacket(SP);
                        }
                        lastTime = currentTime;

                        if (AxisInput.holdTargetVector) //custom SAS vectoring WIP
                        {
                            Quaternion relativeOrientation = Quaternion.identity * Quaternion.Euler((-AxisInput.targetHeading + 90) * new Vector3(1, 0, 0));
                            relativeOrientation = relativeOrientation * Quaternion.Euler((AxisInput.targetPitch) * new Vector3(0, 0, 1));
                            relativeOrientation = relativeOrientation * Quaternion.Euler((AxisInput.targetRoll + 90) * new Vector3(0, 1, 0));

                            Quaternion goalOrientation = Quaternion.LookRotation(north, east) * relativeOrientation;

                            Quaternion currentOrientation = AV.Autopilot.SAS.lockedRotation;
                            float      delta = Quaternion.Angle(goalOrientation, currentOrientation);
                            float      slurp;
                            slurp = (float)(Math.Log(delta + 1) / Math.Log(90) * 0.3);

                            AV.Autopilot.SAS.LockRotation(Quaternion.Slerp(currentOrientation, goalOrientation, slurp));
                        }

                        float time = Time.unscaledTime;
                        VP.deltaTime = time - TimeOFLastSend;
                        if (Config.UpdatesPerSecond == 0 || ((VP.deltaTime) > (1.0f / (float)(Config.UpdatesPerSecond)))) //Limit send rate to config rate
                        {
                            TimeOFLastSend = time;
                            UpdateVP();
                            conn.SendVesselPacket(VP);
                            OrbitPlanSkipRateC++;
                            if ((Config.OrbitPlanSkipRate == 0) || ((OrbitPlanSkipRateC % Config.OrbitPlanSkipRate) == 0))
                            {
                                conn.SendFlightPlanPacket(GenerateRawOrbitPlanData());
                            }
                        }
                        if (conn != null)
                        {
                            while (conn.HaveVCPackets())
                            {
                                VCDifference vcDiff = conn.GetVC();
                                UpdateControls(vcDiff.oldVC, vcDiff.newVC);
                            }
                            while (conn.HaveMCPPackets())
                            {
                                ManChangePacket mcp = conn.GetMCP();
                                switch (mcp.mode)
                                {
                                case 0:     //set
                                    AV.patchedConicSolver.maneuverNodes[mcp.manID].OnGizmoUpdated(new Vector3d(mcp.X, mcp.Y, mcp.Z), mcp.UT);
                                    break;

                                case 1:     //new
                                    AV.patchedConicSolver.AddManeuverNode(mcp.UT);
                                    break;

                                case 2:     //delete
                                    AV.patchedConicSolver.maneuverNodes[mcp.manID].RemoveSelf();
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (inFlight != 0)
                        {
                            msg("switched to NOT FLYING");
                            inFlight = 0;
                            UpdateSP();
                            conn.SendStatusPacket(SP);
                        }
                        if (conn != null)
                        {
                            while (conn.HaveVCPackets())
                            {
                                conn.GetVC(); //ignore results
                            }
                        }
                    }
                }
            }
            else if (server.Pending())
            {
                msg("YARK: Client connected");
                conn = new Connection(server.AcceptTcpClient());

                UpdateSP();
                conn.SendStatusPacket(SP);

                inFlight = SP.status;

                TimeOFLastSend = Time.unscaledTime;
            }
        }