Exemplo n.º 1
0
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: private void processIncomingJdwpData(Device device, java.nio.channels.SocketChannel monitorSocket, int length) throws java.io.IOException
        private void processIncomingJdwpData(Device device, SocketChannel monitorSocket, int length)
        {
            if (length >= 0)
            {
                // array for the current pids.
                List <int?> pidList = new List <int?>();

                // get the string data if there are any
                if (length > 0)
                {
                    var    buffer = new byte[length];
                    string result = read(monitorSocket, buffer);

                    // split each line in its own list and create an array of integer pid
                    string[] pids = StringHelperClass.StringSplit(result, "\n", true); //$NON-NLS-1$

                    foreach (string pid in pids)
                    {
                        try
                        {
                            pidList.Add(Convert.ToInt32(pid));
                        }
                        catch (SystemException)
                        {
                            // looks like this pid is not really a number. Lets ignore it.
                            continue;
                        }
                    }
                }

                MonitorThread monitorThread = MonitorThread.instance;

                // Now we merge the current list with the old one.
                // this is the same mechanism as the merging of the device list.

                // For each client in the current list, we look for a matching the pid in the new list.
                // * if we find it, we do nothing, except removing the pid from its list,
                //   to mark it as "processed"
                // * if we do not find any match, we remove the client from the current list.
                // Once this is done, the new list contains pids for which we don't have clients yet,
                // so we create clients for them, add them to the list, and start monitoring them.

                IList <Client> clients = device.clientList;

                bool changed = false;

                // because MonitorThread#dropClient acquires first the monitorThread lock and then the
                // Device client list lock (when removing the Client from the list), we have to make
                // sure we acquire the locks in the same order, since another thread (MonitorThread),
                // could call dropClient itself.
                lock (monitorThread)
                {
                    lock (clients)
                    {
                        for (int c = 0; c < clients.Count;)
                        {
                            Client client = clients[c];
                            int    pid    = client.clientData.pid;

                            // look for a matching pid
                            int?match = null;
                            foreach (int?matchingPid in pidList)
                            {
                                if (pid == (int)matchingPid)
                                {
                                    match = matchingPid;
                                    break;
                                }
                            }

                            if (match != null)
                            {
                                pidList.Remove(match);
                                c++; // move on to the next client.
                            }
                            else
                            {
                                // we need to drop the client. the client will remove itself from the
                                // list of its device which is 'clients', so there's no need to
                                // increment c.
                                // We ask the monitor thread to not send notification, as we'll do
                                // it once at the end.
                                monitorThread.dropClient(client, false); // notify
                                changed = true;
                            }
                        }
                    }
                }

                // at this point whatever pid is left in the list needs to be converted into Clients.
                foreach (int newPid in pidList)
                {
                    openClient(device, newPid, nextDebuggerPort, monitorThread);
                    changed = true;
                }

                if (changed)
                {
                    mServer.deviceChanged(device, DeviceConstants.CHANGE_CLIENT_LIST);
                }
            }
        }
Exemplo n.º 2
0
        private void deviceClientMonitorLoop()
        {
            do
            {
                try
                {
                    // This synchronized block stops us from doing the select() if a new
                    // Device is being added.
                    // @see startMonitoringDevice()
                    lock (mDevices)
                    {
                    }

                    int count = mSelector.select();

                    if (mQuit)
                    {
                        return;
                    }

                    lock (mClientsToReopen)
                    {
                        if (mClientsToReopen.Count > 0)
                        {
                            var           clients       = mClientsToReopen.Keys;
                            MonitorThread monitorThread = MonitorThread.instance;

                            foreach (Client client in clients)
                            {
                                Device device = client.deviceImpl;
                                int    pid    = client.clientData.pid;

                                monitorThread.dropClient(client, false); // notify

                                // This is kinda bad, but if we don't wait a bit, the client
                                // will never answer the second handshake!
                                waitABit();

                                int port = mClientsToReopen[client];

                                if (port == DebugPortManager.DebugPortProvider.NO_STATIC_PORT)
                                {
                                    port = nextDebuggerPort;
                                }
                                Log.d("DeviceMonitor", "Reopening " + client);
                                openClient(device, pid, port, monitorThread);
                                device.update(DeviceConstants.CHANGE_CLIENT_LIST);
                            }

                            mClientsToReopen.Clear();
                        }
                    }

                    if (count == 0)
                    {
                        continue;
                    }

                    var keys = mSelector.selectedKeys();
                    foreach (var key in keys)
                    {
                        //SelectionKey key = iter.Current;
                        //iter.remove();

                        if (key.valid && key.readable)
                        {
                            object attachment = key.attachment();

                            if (attachment is Device)
                            {
                                Device device = (Device)attachment;

                                SocketChannel socket = device.clientMonitoringSocket;

                                if (socket != null)
                                {
                                    try
                                    {
                                        int length = readLength(socket, mLengthBuffer2);

                                        processIncomingJdwpData(device, socket, length);
                                    }
                                    catch (IOException ioe)
                                    {
                                        Log.d("DeviceMonitor", "Error reading jdwp list: " + ioe.Message);
                                        socket.close();

                                        // restart the monitoring of that device
                                        lock (mDevices)
                                        {
                                            if (mDevices.Contains(device))
                                            {
                                                Log.d("DeviceMonitor", "Restarting monitoring service for " + device);
                                                startMonitoringDevice(device);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (IOException)
                {
                    if (mQuit == false)
                    {
                    }
                }
            } while (mQuit == false);
        }