Пример #1
0
    private JSONObject[] monitorReadObject(bool expectedReturn)
    {
        JSONObject[] obj = JSONObject.Decode(monitorRead());

        //is it an event?
        if (obj.Length != 0 && obj[0]["event"] != null)
        {
            handleEvent(obj[0]);

            //re-call this function to get the data
            //the caller expects.
            if (!expectedReturn)
            {
                return(null);
            }
            return(monitorReadObject(true));
        }
        return(obj);
    }
Пример #2
0
    public void Start(int memory, StandardOutputCallback standardOutput)
    {
        //already running?
        if (Running)
        {
            return;
        }

        #region Invoke Qemu
        string qemuArguments = "-qmp tcp:127.0.0.1:4444,server,nowait " +
                               "-cdrom \"" + new FileInfo(p_DiskImage).FullName + "\" " +
                               "-m " + memory;

        //create a processlink process so that if this process is killed
        //it automatically kills the emulator as well.
        standardOutput("Launching process linker...");
        if (!File.Exists("processlink.exe"))
        {
            standardOutput("  Error! ProcessLink.exe is not found!");
            return;
        }
        p_Process = new Process()
        {
            StartInfo = new ProcessStartInfo {
                FileName  = "processLink.exe",
                Arguments = "-id " + Process.GetCurrentProcess().Id + " " +
                            "-f \"" + new FileInfo("emulators/qemu/qemu.exe").FullName + "\" " +
                            "-a " + qemuArguments,
                UseShellExecute        = false,
                CreateNoWindow         = true,
                RedirectStandardOutput = true
            }
        };

        //get the qemu process id from the spawn process
        int qemuPID = -1;
        p_Process.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) {
            if (e.Data == null || qemuPID != -1)
            {
                return;
            }
            qemuPID = Convert.ToInt32(e.Data);
        };

        //launch the process
        standardOutput("Starting Qemu...");
        p_Process.Start();
        standardOutput("   Process linker PID=" + p_Process.Id);
        p_Process.BeginOutputReadLine();

        //wait until the spawner has launched Qemu
        while (qemuPID == -1)
        {
            ;
        }
        p_QemuProcess = Process.GetProcessById(qemuPID);
        standardOutput("   Started, PID=" + qemuPID);

        #endregion

        //connect to the Qemu Monitor
        p_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
        int connectStart = Environment.TickCount;
        standardOutput("Connecting to Qemu's QMP service...");
        while (Environment.TickCount < connectStart + 1000)
        {
            try {
                p_Socket.Connect("localhost", 4444);
                p_Stream = new NetworkStream(p_Socket, true);
                break;
            }
            catch { }
        }

        //timed out?
        if (!p_Socket.Connected)
        {
            standardOutput("Error! Unable to connect to Qemu's QMP service");
            Stop();
            return;
        }

        //get the initial response from the server
        standardOutput("Performing handshake with QMP Server");
        JSONObject handshake = JSONObject.Decode("", monitorRead())[0];
        handshake = (JSONObject)handshake["QMP"];

        //get the version of the qemu emulator
        JSONObject version = (JSONObject)handshake["version"];
        version = (JSONObject)version["qemu"];
        int versionMajor = Convert.ToInt32(version["major"]);
        int versionMinor = Convert.ToInt32(version["minor"]);

        //enter command mode
        monitorWrite("{ \"execute\": \"qmp_capabilities\" }");
        JSONObject result = JSONObject.Decode(monitorRead())[0];
        result = (JSONObject)result["return"];
        if (result.ChildValues[0].ChildCount > 0)
        {
            standardOutput("Unable to enter into QMP Command mode!");
            Stop();
            return;
        }

        //create an event listener
        standardOutput("Starting event listener...");
        Thread eventListener = new Thread(new ThreadStart(delegate {
            while (Running)
            {
                //send a blank signal to the server so we don't interfere
                //with current messages.
                Monitor.Enter(p_SyncLock);
                monitorExecute("", null);
                monitorReadObject(false);
                Monitor.Exit(p_SyncLock);

                Thread.Sleep(100);
            }
        }));
        eventListener.Start();
        standardOutput("  Listening...");

        //success
        standardOutput("Qemu Emulator version " + versionMajor + "." + versionMinor);
        standardOutput("Qemu Emulator ready...");

        return;

        EmulationProcessor ps = GetProcessors()[0];
        while (true)
        {
            int time = Environment.TickCount;
            GetRegisters(ps);
            Console.WriteLine((Environment.TickCount - time) + "ms");
        }

        new DebugRegisters(this).Show();
    }
 public static MyRotationFilter FromJSON(string jsonString)
 {
     return(FromJSON(JSONObject.Decode(jsonString)));
 }