/// <summary> /// Opens the sync connection. This must be called before any calls to push[File] / pull[File]. </summary> /// <returns> true if the connection opened, false if adb refuse the connection. This can happen /// if the <seealso cref="Device"/> is invalid. </returns> /// <exception cref="TimeoutException"> in case of timeout on the connection. </exception> /// <exception cref="AdbCommandRejectedException"> if adb rejects the command </exception> /// <exception cref="IOException"> If the connection to adb failed. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: boolean openSync() throws TimeoutException, AdbCommandRejectedException, java.io.IOException internal bool openSync() { try { mChannel = SocketChannel.open(mAddress); mChannel.configureBlocking(false); // target a specific device AdbHelper.setDevice(mChannel, mDevice); var request = AdbHelper.formAdbRequest("sync:"); //$NON-NLS-1$ AdbHelper.write(mChannel, request, -1, DdmPreferences.timeOut); AdbHelper.AdbResponse resp = AdbHelper.readAdbResponse(mChannel, false); // readDiagString if (resp.okay == false) { Log.w("ddms", "Got unhappy response from ADB sync req: " + resp.message); mChannel.close(); mChannel = null; return(false); } } catch (TimeoutException e) { if (mChannel != null) { try { mChannel.close(); } catch (IOException) { // we want to throw the original exception, so we ignore this one. } mChannel = null; } throw e; } catch (IOException e) { if (mChannel != null) { try { mChannel.close(); } catch (IOException) { // we want to throw the original exception, so we ignore this one. } mChannel = null; } throw e; } return(true); }
/// <summary> /// Reboot the device. /// </summary> /// <param name="into"> what to reboot into (recovery, bootloader). Or null to just reboot. </param> /// <exception cref="TimeoutException"> in case of timeout on the connection. </exception> /// <exception cref="AdbCommandRejectedException"> if adb rejects the command </exception> /// <exception cref="IOException"> in case of I/O error on the connection. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static void reboot(String into, java.net.InetSocketAddress adbSockAddr, Device device) throws TimeoutException, AdbCommandRejectedException, java.io.IOException public static void reboot(string into, EndPoint adbSockAddr, Device device) { byte[] request; if (into == null) { request = formAdbRequest("reboot:"); //$NON-NLS-1$ } else { request = formAdbRequest("reboot:" + into); //$NON-NLS-1$ } SocketChannel adbChan = null; try { adbChan = SocketChannel.open(adbSockAddr); adbChan.configureBlocking(false); // if the device is not -1, then we first tell adb we're looking to talk // to a specific device setDevice(adbChan, device); write(adbChan, request); } finally { if (adbChan != null) { adbChan.close(); } } }
/// <summary> /// Remove a port forwarding between a local and a remote port. </summary> /// <param name="adbSockAddr"> the socket address to connect to adb </param> /// <param name="device"> the device on which to remove the port fowarding </param> /// <param name="localPort"> the local port of the forward </param> /// <param name="remotePort"> the remote port. </param> /// <exception cref="TimeoutException"> in case of timeout on the connection. </exception> /// <exception cref="AdbCommandRejectedException"> if adb rejects the command </exception> /// <exception cref="IOException"> in case of I/O error on the connection. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static void removeForward(java.net.InetSocketAddress adbSockAddr, Device device, int localPort, int remotePort) throws TimeoutException, AdbCommandRejectedException, java.io.IOException public static void removeForward(EndPoint adbSockAddr, Device device, int localPort, int remotePort) { SocketChannel adbChan = null; try { adbChan = SocketChannel.open(adbSockAddr); adbChan.configureBlocking(false); var request = formAdbRequest(string.Format("host-serial:{0}:killforward:tcp:{1:D};tcp:{2:D}", device.serialNumber, localPort, remotePort)); //$NON-NLS-1$ write(adbChan, request); AdbResponse resp = readAdbResponse(adbChan, false); // readDiagString if (resp.okay == false) { Log.w("remove-forward", "Error creating forward: " + resp.message); throw new AdbCommandRejectedException(resp.message); } } finally { if (adbChan != null) { adbChan.close(); } } }
/// <summary> /// Runs a log service on the <seealso cref="Device"/>, and provides its output to the <seealso cref="LogReceiver"/>. /// <p/>This call is blocking until <seealso cref="LogReceiver#isCancelled()"/> returns true. </summary> /// <param name="adbSockAddr"> the socket address to connect to adb </param> /// <param name="device"> the Device on which to run the service </param> /// <param name="logName"> the name of the log file to output </param> /// <param name="rcvr"> the <seealso cref="LogReceiver"/> to receive the log output </param> /// <exception cref="TimeoutException"> in case of timeout on the connection. </exception> /// <exception cref="AdbCommandRejectedException"> if adb rejects the command </exception> /// <exception cref="IOException"> in case of I/O error on the connection. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static void runLogService(java.net.InetSocketAddress adbSockAddr, Device device, String logName, com.android.ddmlib.log.LogReceiver rcvr) throws TimeoutException, AdbCommandRejectedException, java.io.IOException public static void runLogService(EndPoint adbSockAddr, Device device, string logName, LogReceiver rcvr) { SocketChannel adbChan = null; try { adbChan = SocketChannel.open(adbSockAddr); adbChan.configureBlocking(false); // if the device is not -1, then we first tell adb we're looking to talk // to a specific device setDevice(adbChan, device); var request = formAdbRequest("log:" + logName); write(adbChan, request); AdbResponse resp = readAdbResponse(adbChan, false); // readDiagString if (resp.okay == false) { throw new AdbCommandRejectedException(resp.message); } var data = new byte[16384]; ByteBuffer buf = ByteBuffer.wrap(data); while (true) { int count; if (rcvr != null && rcvr.cancelled) { break; } count = adbChan.read(buf); if (count < 0) { break; } else if (count == 0) { Thread.Sleep(WAIT_TIME * 5); } else { if (rcvr != null) { rcvr.parseNewData(buf.array(), buf.arrayOffset(), buf.position); } buf.rewind(); } } } finally { if (adbChan != null) { adbChan.close(); } } }
/// <summary> /// Close the client socket channel. If there is a debugger associated /// with us, close that too. /// /// Closing a channel automatically unregisters it from the selector. /// However, we have to iterate through the selector loop before it /// actually lets them go and allows the file descriptors to close. /// The caller is expected to manage that. </summary> /// <param name="notify"> Whether or not to notify the listeners of a change. </param> internal virtual void close(bool notify) { Log.d("ddms", "Closing " + this.ToString()); mOutstandingReqs.Clear(); try { if (mChan != null) { mChan.close(); mChan = null; } if (mDebugger != null) { mDebugger.close(); mDebugger = null; } } catch (IOException) { Log.w("ddms", "failed to close " + this); // swallow it -- not much else to do } mDevice.removeClient(this, notify); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void close() throws java.io.IOException public virtual void close() { if (socketChannel != null) { socketChannel.close(); socketChannel = null; } }
/// <summary> /// Create and connect a new pass-through socket, from the host to a port on /// the device. /// </summary> /// <param name="adbSockAddr"> </param> /// <param name="device"> the device to connect to. Can be null in which case the connection will be /// to the first available device. </param> /// <param name="devicePort"> the port we're opening </param> /// <exception cref="TimeoutException"> in case of timeout on the connection. </exception> /// <exception cref="IOException"> in case of I/O error on the connection. </exception> /// <exception cref="AdbCommandRejectedException"> if adb rejects the command </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static java.nio.channels.SocketChannel open(java.net.InetSocketAddress adbSockAddr, Device device, int devicePort) throws java.io.IOException, TimeoutException, AdbCommandRejectedException public static SocketChannel open(EndPoint adbSockAddr, Device device, int devicePort) { SocketChannel adbChan = SocketChannel.open(adbSockAddr); try { adbChan.socket().NoDelay = true; adbChan.configureBlocking(false); // if the device is not -1, then we first tell adb we're looking to // talk to a specific device setDevice(adbChan, device); var req = createAdbForwardRequest(null, devicePort); // Log.hexDump(req); write(adbChan, req); AdbResponse resp = readAdbResponse(adbChan, false); if (resp.okay == false) { throw new AdbCommandRejectedException(resp.message); } adbChan.configureBlocking(true); } catch (TimeoutException e) { adbChan.close(); throw e; } catch (IOException e) { adbChan.close(); throw e; } return(adbChan); }
/// <summary> /// Stops the monitoring. /// </summary> internal void stop() { mQuit = true; // wakeup the main loop thread by closing the main connection to adb. try { if (mMainAdbConnection != null) { mMainAdbConnection.close(); } } catch (IOException) { } // wake up the secondary loop by closing the selector. if (mSelector != null) { mSelector.wakeup(); } }
/* * We have some activity on the "debug selected" port. Handle it. */ private void processDebugSelectedActivity(SelectionKey key) { Debug.Assert(key.acceptable); ServerSocketChannel acceptChan = (ServerSocketChannel)key.channel(); /* * Find the debugger associated with the currently-selected client. */ if (mSelectedClient != null) { Debugger dbg = mSelectedClient.debugger; if (dbg != null) { Log.d("ddms", "Accepting connection on 'debug selected' port"); try { acceptNewDebugger(dbg, acceptChan); } catch (IOException) { // client should be gone, keep going } return; } } Log.w("ddms", "Connection on 'debug selected' port, but none selected"); try { SocketChannel chan = acceptChan.accept(); chan.close(); } catch (IOException) { // not expected; client should be gone, keep going } catch (NotYetBoundException) { displayDebugSelectedBindError(mDebugSelectedPort); } }
private void removeDevice(Device device) { device.clearClientList(); mDevices.Remove(device); SocketChannel channel = device.clientMonitoringSocket; if (channel != null) { try { channel.close(); } catch (IOException) { // doesn't really matter if the close fails. } } }
/// <summary> /// Close the data connection only. /// </summary> /*lock*/ internal void closeData() { try { if (mChannel != null) { mChannel.close(); mChannel = null; mConnState = ST_NOT_CONNECTED; ClientData cd = mClient.clientData; cd.debuggerConnectionStatus = ClientData.DebuggerStatus.DEFAULT; mClient.update(Client.CHANGE_DEBUGGER_STATUS); } } catch (IOException) { Log.w("ddms", "Failed to close data " + this); } }
/// <summary> /// Executes a shell command on the device and retrieve the output. The output is /// handed to <var>rcvr</var> as it arrives. /// </summary> /// <param name="adbSockAddr"> the <seealso cref="InetSocketAddress"/> to adb. </param> /// <param name="command"> the shell command to execute </param> /// <param name="device"> the <seealso cref="IDevice"/> on which to execute the command. </param> /// <param name="rcvr"> the <seealso cref="IShellOutputReceiver"/> that will receives the output of the shell /// command </param> /// <param name="maxTimeToOutputResponse"> max time between command output. If more time passes /// between command output, the method will throw /// <seealso cref="ShellCommandUnresponsiveException"/>. A value of 0 means the method will /// wait forever for command output and never throw. </param> /// <exception cref="TimeoutException"> in case of timeout on the connection when sending the command. </exception> /// <exception cref="AdbCommandRejectedException"> if adb rejects the command </exception> /// <exception cref="ShellCommandUnresponsiveException"> in case the shell command doesn't send any output /// for a period longer than <var>maxTimeToOutputResponse</var>. </exception> /// <exception cref="IOException"> in case of I/O error on the connection. /// </exception> /// <seealso cref= DdmPreferences#getTimeOut() </seealso> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: static void executeRemoteCommand(java.net.InetSocketAddress adbSockAddr, String command, IDevice device, IShellOutputReceiver rcvr, int maxTimeToOutputResponse) throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, java.io.IOException internal static void executeRemoteCommand(EndPoint adbSockAddr, string command, IDevice device, IShellOutputReceiver rcvr, int maxTimeToOutputResponse) { Log.v("ddms", "execute: running " + command); SocketChannel adbChan = null; try { adbChan = SocketChannel.open(adbSockAddr); adbChan.configureBlocking(false); // if the device is not -1, then we first tell adb we're looking to // talk // to a specific device setDevice(adbChan, device); var request = formAdbRequest("shell:" + command); //$NON-NLS-1$ write(adbChan, request); AdbResponse resp = readAdbResponse(adbChan, false); // readDiagString if (resp.okay == false) { Log.e("ddms", "ADB rejected shell command (" + command + "): " + resp.message); throw new AdbCommandRejectedException(resp.message); } var data = new byte[16384]; ByteBuffer buf = ByteBuffer.wrap(data); int timeToResponseCount = 0; while (true) { int count; if (rcvr != null && rcvr.cancelled) { Log.v("ddms", "execute: cancelled"); break; } count = adbChan.read(buf); if (count < 0) { // we're at the end, we flush the output rcvr.flush(); Log.v("ddms", "execute '" + command + "' on '" + device + "' : EOF hit. Read: " + count); break; } else if (count == 0) { int wait = WAIT_TIME * 5; timeToResponseCount += wait; if (maxTimeToOutputResponse > 0 && timeToResponseCount > maxTimeToOutputResponse) { throw new ShellCommandUnresponsiveException(); } Thread.Sleep(wait); } else { // reset timeout timeToResponseCount = 0; // send data to receiver if present if (rcvr != null) { rcvr.addOutput(buf.array(), buf.arrayOffset(), buf.position); } buf.rewind(); } } } finally { if (adbChan != null) { adbChan.close(); } Log.v("ddms", "execute: returning"); } }
/// <summary> /// Retrieve the frame buffer from the device. </summary> /// <exception cref="TimeoutException"> in case of timeout on the connection. </exception> /// <exception cref="AdbCommandRejectedException"> if adb rejects the command </exception> /// <exception cref="IOException"> in case of I/O error on the connection. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: static RawImage getFrameBuffer(java.net.InetSocketAddress adbSockAddr, Device device) throws TimeoutException, AdbCommandRejectedException, java.io.IOException internal static RawImage getFrameBuffer(EndPoint adbSockAddr, Device device) { RawImage imageParams = new RawImage(); var request = formAdbRequest("framebuffer:"); //$NON-NLS-1$ byte[] nudge = { 0 }; byte[] reply; SocketChannel adbChan = null; try { adbChan = SocketChannel.open(adbSockAddr); adbChan.configureBlocking(false); // if the device is not -1, then we first tell adb we're looking to talk // to a specific device setDevice(adbChan, device); write(adbChan, request); AdbResponse resp = readAdbResponse(adbChan, false); // readDiagString if (resp.okay == false) { throw new AdbCommandRejectedException(resp.message); } // first the protocol version. reply = new byte[4]; read(adbChan, reply); ByteBuffer buf = ByteBuffer.wrap(reply); buf.order = ByteOrder.LITTLE_ENDIAN; int version = buf.getInt(); // get the header size (this is a count of int) int headerSize = RawImage.getHeaderSize(version); // read the header reply = new byte[headerSize * 4]; read(adbChan, reply); buf = ByteBuffer.wrap(reply); buf.order = ByteOrder.LITTLE_ENDIAN; // fill the RawImage with the header if (imageParams.readHeader(version, buf) == false) { Log.e("Screenshot", "Unsupported protocol: " + version); return(null); } Log.d("ddms", "image params: bpp=" + imageParams.bpp + ", size=" + imageParams.size + ", width=" + imageParams.width + ", height=" + imageParams.height); write(adbChan, nudge); reply = new byte[imageParams.size]; read(adbChan, reply); imageParams.data = reply; } finally { if (adbChan != null) { adbChan.close(); } } return(imageParams); }
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> /// Starts a monitoring service for a device. </summary> /// <param name="device"> the device to monitor. </param> /// <returns> true if success. </returns> private bool startMonitoringDevice(Device device) { SocketChannel socketChannel = openAdbConnection(); if (socketChannel != null) { try { bool result = sendDeviceMonitoringRequest(socketChannel, device); if (result) { if (mSelector == null) { startDeviceMonitorThread(); } device.clientMonitoringSocket = socketChannel; lock (mDevices) { // always wakeup before doing the register. The synchronized block // ensure that the selector won't select() before the end of this block. // @see deviceClientMonitorLoop mSelector.wakeup(); socketChannel.configureBlocking(false); socketChannel.register(mSelector, SelectionKey.OP_READ, device); } return(true); } } catch (TimeoutException) { try { // attempt to close the socket if needed. socketChannel.close(); } catch (IOException) { // we can ignore that one. It may already have been closed. } Log.d("DeviceMonitor", "Connection Failure when starting to monitor device '" + device + "' : timeout"); } catch (AdbCommandRejectedException e) { try { // attempt to close the socket if needed. socketChannel.close(); } catch (IOException) { // we can ignore that one. It may already have been closed. } Log.d("DeviceMonitor", "Adb refused to start monitoring device '" + device + "' : " + e.Message); } catch (IOException e) { try { // attempt to close the socket if needed. socketChannel.close(); } catch (IOException) { // we can ignore that one. It may already have been closed. } Log.d("DeviceMonitor", "Connection Failure when starting to monitor device '" + device + "' : " + e.Message); } } return(false); }