/// <summary> /// Register for the packets we expect to get from the client. /// </summary> public static void register(MonitorThread mt) { mt.registerChunkHandler(CHUNK_NHGT, mInst); mt.registerChunkHandler(CHUNK_NHSG, mInst); mt.registerChunkHandler(CHUNK_NHST, mInst); mt.registerChunkHandler(CHUNK_NHEN, mInst); }
/// <summary> /// Opens and creates a new client. /// @return /// </summary> private void openClient(Device device, int pid, int port, MonitorThread monitorThread) { SocketChannel clientSocket; try { clientSocket = AdbHelper.createPassThroughConnection(AndroidDebugBridge.socketAddress, device, pid); // required for Selector clientSocket.configureBlocking(false); } catch (ArgumentException) { Log.d("DeviceMonitor", "Unknown Jdwp pid: " + pid); return; } catch (TimeoutException) { Log.w("DeviceMonitor", "Failed to connect to client '" + pid + "': timeout"); return; } catch (AdbCommandRejectedException e) { Log.w("DeviceMonitor", "Adb rejected connection to client '" + pid + "': " + e.Message); return; } catch (Exception ioe) { Log.w("DeviceMonitor", "Failed to connect to client '" + pid + "': " + ioe.Message); return; } createClient(device, pid, clientSocket, port, monitorThread); }
/// <summary> /// Register for the packets we expect to get from the client. /// </summary> public static void register(MonitorThread mt) { mt.registerChunkHandler(CHUNK_THCR, mInst); mt.registerChunkHandler(CHUNK_THDE, mInst); mt.registerChunkHandler(CHUNK_THST, mInst); mt.registerChunkHandler(CHUNK_THNM, mInst); mt.registerChunkHandler(CHUNK_STKL, mInst); }
/// <summary> /// Sets the client to accept debugger connection on the "selected debugger port". /// </summary> /// <seealso cref= AndroidDebugBridge#setSelectedClient(Client) </seealso> /// <seealso cref= DdmPreferences#setSelectedDebugPort(int) </seealso> public virtual void setAsSelectedClient() { MonitorThread monitorThread = MonitorThread.instance; if (monitorThread != null) { monitorThread.selectedClient = this; } }
/// <summary> /// Register for the packets we expect to get from the client. /// </summary> public static void register(MonitorThread mt) { mt.registerChunkHandler(CHUNK_HPIF, mInst); mt.registerChunkHandler(CHUNK_HPST, mInst); mt.registerChunkHandler(CHUNK_HPEN, mInst); mt.registerChunkHandler(CHUNK_HPSG, mInst); mt.registerChunkHandler(CHUNK_HPDS, mInst); mt.registerChunkHandler(CHUNK_REAQ, mInst); mt.registerChunkHandler(CHUNK_REAL, mInst); }
/// <summary> /// Tell the thread to stop. Called from UI thread. /// </summary> internal void quit() { lock (this) { mQuit = true; wakeup(); Log.d("ddms", "Waiting for Monitor thread"); try { thread.Join(); // since we're quitting, lets drop all the client and disconnect // the DebugSelectedPort lock (mClientList) { foreach (Client c in mClientList) { c.close(false); // notify broadcast(CLIENT_DISCONNECTED, c); } mClientList.Clear(); } if (mDebugSelectedChan != null) { mDebugSelectedChan.close(); mDebugSelectedChan.socket().Close(); mDebugSelectedChan = null; } mSelector.close(); } catch (ThreadInterruptedException ie) { Console.WriteLine(ie.ToString()); Console.Write(ie.StackTrace); } catch (IOException e) { // TODO Auto-generated catch block Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } mInstance = null; } }
/// <summary> /// Creates and return the singleton instance of the client monitor thread. /// </summary> internal static MonitorThread createInstance() { return(mInstance = new MonitorThread()); }
/// <summary> /// Register for the packets we expect to get from the client. /// </summary> public static void register(MonitorThread mt) { mt.registerChunkHandler(CHUNK_MPRE, mInst); mt.registerChunkHandler(CHUNK_MPSE, mInst); mt.registerChunkHandler(CHUNK_MPRQ, mInst); }
/// <summary> /// Register for the packets we expect to get from the client. /// </summary> public static void register(MonitorThread mt) { }
/// <summary> /// Creates a client and register it to the monitor thread </summary> /// <param name="device"> </param> /// <param name="pid"> </param> /// <param name="socket"> </param> /// <param name="debuggerPort"> the debugger port. </param> /// <param name="monitorThread"> the <seealso cref="MonitorThread"/> object. </param> private void createClient(Device device, int pid, SocketChannel socket, int debuggerPort, MonitorThread monitorThread) { /* * Successfully connected to something. Create a Client object, add * it to the list, and initiate the JDWP handshake. */ Client client = new Client(device, socket, pid); if (client.sendHandshake()) { try { if (AndroidDebugBridge.clientSupport) { client.listenForDebugger(debuggerPort); } } catch (IOException) { client.clientData.debuggerConnectionStatus = ClientData.DebuggerStatus.ERROR; Log.e("ddms", "Can't bind to local " + debuggerPort + " for debugger"); // oh well } client.requestAllocationStatus(); } else { Log.e("ddms", "Handshake with " + client + " failed!"); /* * The handshake send failed. We could remove it now, but if the * failure is "permanent" we'll just keep banging on it and * getting the same result. Keep it in the list with its "error" * state so we don't try to reopen it. */ } if (client.valid) { device.addClient(client); monitorThread.addClient(client); } else { client = null; } }
//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); } } }
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); }
/// <summary> /// Register for the packets we expect to get from the client. /// </summary> public static void register(MonitorThread mt) { mt.registerChunkHandler(CHUNK_HELO, mInst); }
/// <summary> /// Creates and return the singleton instance of the client monitor thread. /// </summary> internal static MonitorThread createInstance() { return mInstance = new MonitorThread(); }
/// <summary> /// Register for the packets we expect to get from the client. /// </summary> public static void register(MonitorThread mt) { mt.registerChunkHandler(CHUNK_APNM, mInst); }