예제 #1
0
        /// <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);
        }
예제 #2
0
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: private boolean sendDeviceMonitoringRequest(java.nio.channels.SocketChannel socket, Device device) throws TimeoutException, AdbCommandRejectedException, java.io.IOException
        private bool sendDeviceMonitoringRequest(SocketChannel socket, Device device)
        {
            try
            {
                AdbHelper.setDevice(socket, device);

                var request = AdbHelper.formAdbRequest("track-jdwp"); //$NON-NLS-1$

                AdbHelper.write(socket, request);

                AdbHelper.AdbResponse resp = AdbHelper.readAdbResponse(socket, false); // readDiagString

                if (resp.okay == false)
                {
                    // request was refused by adb!
                    Log.e("DeviceMonitor", "adb refused request: " + resp.message);
                }

                return(resp.okay);
            }
            catch (TimeoutException e)
            {
                Log.e("DeviceMonitor", "Sending jdwp tracking request timed out!");
                throw e;
            }
            catch (IOException e)
            {
                Log.e("DeviceMonitor", "Sending jdwp tracking request failed!");
                throw e;
            }
        }
예제 #3
0
        /// <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);
        }
예제 #4
0
        ///
        /// <summary>
        /// @return </summary>
        /// <exception cref="IOException"> </exception>
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: private boolean sendDeviceListMonitoringRequest() throws TimeoutException, java.io.IOException
        private bool sendDeviceListMonitoringRequest()
        {
            var request = AdbHelper.formAdbRequest("host:track-devices"); //$NON-NLS-1$

            try
            {
                AdbHelper.write(mMainAdbConnection, request);

                AdbHelper.AdbResponse resp = AdbHelper.readAdbResponse(mMainAdbConnection, false); // readDiagString

                if (resp.okay == false)
                {
                    // request was refused by adb!
                    Log.e("DeviceMonitor", "adb refused request: " + resp.message);
                }

                return(resp.okay);
            }
            catch (IOException e)
            {
                Log.e("DeviceMonitor", "Sending Tracking request failed!");
                mMainAdbConnection.close();
                throw e;
            }
        }
예제 #5
0
        /// <summary>
        /// Reads an error message from the opened <seealso cref="#mChannel"/>. </summary>
        /// <param name="result"> the current adb result. Must contain both FAIL and the length of the message. </param>
        /// <param name="timeOut">
        /// @return </param>
        /// <exception cref="TimeoutException"> in case of a timeout reading responses from the device. </exception>
        /// <exception cref="IOException"> </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private String readErrorMessage(byte[] result, final int timeOut) throws TimeoutException, java.io.IOException
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET:
        private string readErrorMessage(byte[] result, int timeOut)
        {
            if (checkResult(result, ID_FAIL))
            {
                int len = ArrayHelper.swap32bitFromArray(result, 4);

                if (len > 0)
                {
                    AdbHelper.read(mChannel, mBuffer, len, timeOut);

                    var message = mBuffer.getString(0, len, Encoding.Default.EncodingName);
                    Log.e("ddms", "transfer error: " + message);

                    return(message);
                }
            }

            return(null);
        }
예제 #6
0
        /// <summary>
        /// Returns the mode of the remote file. </summary>
        /// <param name="path"> the remote file </param>
        /// <returns> an Integer containing the mode if all went well or null
        ///      otherwise </returns>
        /// <exception cref="IOException"> </exception>
        /// <exception cref="TimeoutException"> in case of a timeout reading responses from the device. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private Integer readMode(String path) throws TimeoutException, java.io.IOException
        private int?readMode(string path)
        {
            // create the stat request message.
            var msg = createFileReq(ID_STAT, path);

            AdbHelper.write(mChannel, msg, -1, DdmPreferences.timeOut);             // full length

            // read the result, in a byte array containing 4 ints
            // (id, mode, size, time)
            var statResult = new byte[16];

            AdbHelper.read(mChannel, statResult, -1, DdmPreferences.timeOut);             // full length

            // check we have the proper data back
            if (checkResult(statResult, ID_STAT) == false)
            {
                return(null);
            }

            // we return the mode (2nd int in the array)
            return(ArrayHelper.swap32bitFromArray(statResult, 4));
        }
예제 #7
0
		/// <summary>
		/// Sends a command to the emulator console. </summary>
		/// <param name="command"> The command string. <b>MUST BE TERMINATED BY \n</b>. </param>
		/// <returns> true if success </returns>
		private bool sendCommand(string command)
		{
			bool result = false;
			try
			{
				byte[] bCommand;
				try
				{
					bCommand = command.getBytes(DEFAULT_ENCODING);
				}
				catch (ArgumentException)
				{
					// wrong encoding...
					return result;
				}

				// write the command
				AdbHelper.write(mSocketChannel, bCommand, bCommand.Length, DdmPreferences.timeOut);

				result = true;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				if (result == false)
				{
					// FIXME connection failed somehow, we need to disconnect the console.
					RemoveConsole(mPort);
				}
			}

			return result;
		}
예제 #8
0
        /// <summary>
        /// Push a single file </summary>
        /// <param name="localPath"> the local file to push </param>
        /// <param name="remotePath"> the remote file (length max is 1024) </param>
        /// <param name="monitor"> the monitor. The monitor must be started already.
        /// </param>
        /// <exception cref="SyncException"> if file could not be pushed </exception>
        /// <exception cref="IOException"> in case of I/O error on the connection. </exception>
        /// <exception cref="TimeoutException"> in case of a timeout reading responses from the device. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void doPushFile(String localPath, String remotePath, ISyncProgressMonitor monitor) throws SyncException, java.io.IOException, TimeoutException
        private void doPushFile(string localPath, string remotePath, ISyncProgressMonitor monitor)
        {
            byte[] msg;

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int timeOut = DdmPreferences.getTimeOut();
            int timeOut = DdmPreferences.timeOut;

            try
            {
                var remotePathContent = remotePath.getBytes(AdbHelper.DEFAULT_ENCODING);

                if (remotePathContent.Length > REMOTE_PATH_MAX_LENGTH)
                {
                    throw new SyncException(SyncException.SyncError.REMOTE_PATH_LENGTH);
                }

                // create the header for the action
                //JAVA TO C# CONVERTER TODO TASK: Octal literals cannot be represented in C#:
                msg = createSendFileReq(ID_SEND, remotePathContent, 0644);
            }
            catch (ArgumentException e)
            {
                throw new SyncException(SyncException.SyncError.REMOTE_PATH_ENCODING, e);
            }

            // create the stream to read the file
            using (var fis = File.OpenRead(localPath))
            {
                // and send it. We use a custom try/catch block to make the difference between
                // file and network IO exceptions.
                AdbHelper.write(mChannel, msg, -1, timeOut);

                // create the buffer used to read.
                // we read max SYNC_DATA_MAX, but we need 2 4 bytes at the beginning.
                if (mBuffer == null)
                {
                    mBuffer = new byte[SYNC_DATA_MAX + 8];
                }
                Array.Copy(ID_DATA, 0, mBuffer, 0, ID_DATA.Length);

                // look while there is something to read
                while (true)
                {
                    // check if we're canceled
                    if (monitor.canceled == true)
                    {
                        throw new SyncException(SyncException.SyncError.CANCELED);
                    }

                    // read up to SYNC_DATA_MAX
                    int readCount = fis.Read(mBuffer, 8, SYNC_DATA_MAX);

                    if (readCount == -1)
                    {
                        // we reached the end of the file
                        break;
                    }

                    // now send the data to the device
                    // first write the amount read
                    ArrayHelper.swap32bitsToArray(readCount, mBuffer, 4);

                    // now write it
                    AdbHelper.write(mChannel, mBuffer, readCount + 8, timeOut);

                    // and advance the monitor
                    monitor.advance(readCount);
                }
            }

            // create the DONE message
            long time = Environment.TickCount / 1000;

            msg = createReq(ID_DONE, (int)time);

            // and send it.
            AdbHelper.write(mChannel, msg, -1, timeOut);

            // read the result, in a byte array containing 2 ints
            // (id, size)
            var result = new byte[8];

            AdbHelper.read(mChannel, result, -1, timeOut);         // full length

            if (checkResult(result, ID_OKAY) == false)
            {
                throw new SyncException(SyncException.SyncError.TRANSFER_PROTOCOL_ERROR, readErrorMessage(result, timeOut));
            }
        }
예제 #9
0
        /// <summary>
        /// Pulls a remote file </summary>
        /// <param name="remotePath"> the remote file (length max is 1024) </param>
        /// <param name="localPath"> the local destination </param>
        /// <param name="monitor"> the monitor. The monitor must be started already. </param>
        /// <exception cref="SyncException"> if file could not be pushed </exception>
        /// <exception cref="IOException"> in case of I/O error on the connection. </exception>
        /// <exception cref="TimeoutException"> in case of a timeout reading responses from the device. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void doPullFile(String remotePath, String localPath, ISyncProgressMonitor monitor) throws java.io.IOException, SyncException, TimeoutException
        private void doPullFile(string remotePath, string localPath, ISyncProgressMonitor monitor)
        {
            var pullResult = new byte[8];

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int timeOut = DdmPreferences.getTimeOut();
            int timeOut = DdmPreferences.timeOut;

            try
            {
                var remotePathContent = remotePath.getBytes(AdbHelper.DEFAULT_ENCODING);

                if (remotePathContent.Length > REMOTE_PATH_MAX_LENGTH)
                {
                    throw new SyncException(SyncException.SyncError.REMOTE_PATH_LENGTH);
                }

                // create the full request message
                var msg = createFileReq(ID_RECV, remotePathContent);

                // and send it.
                AdbHelper.write(mChannel, msg, -1, timeOut);

                // read the result, in a byte array containing 2 ints
                // (id, size)
                AdbHelper.read(mChannel, pullResult, -1, timeOut);

                // check we have the proper data back
                if (checkResult(pullResult, ID_DATA) == false && checkResult(pullResult, ID_DONE) == false)
                {
                    throw new SyncException(SyncException.SyncError.TRANSFER_PROTOCOL_ERROR, readErrorMessage(pullResult, timeOut));
                }
            }
            catch (ArgumentException e)
            {
                throw new SyncException(SyncException.SyncError.REMOTE_PATH_ENCODING, e);
            }

            // access the destination file
            // create the stream to write in the file. We use a new try/catch block to differentiate
            // between file and network io exceptions.
            FileStream fos = null;

            try
            {
                fos = File.Create(localPath);
            }
            catch (IOException e)
            {
                Log.e("ddms", string.Format("Failed to open local file {0} for writing, Reason: {1}", localPath, e));
                throw new SyncException(SyncException.SyncError.FILE_WRITE_ERROR);
            }

            using (fos)
            {
                // the buffer to read the data
                var data = new byte[SYNC_DATA_MAX];

                // loop to get data until we're done.
                while (true)
                {
                    // check if we're cancelled
                    if (monitor.canceled == true)
                    {
                        throw new SyncException(SyncException.SyncError.CANCELED);
                    }

                    // if we're done, we stop the loop
                    if (checkResult(pullResult, ID_DONE))
                    {
                        break;
                    }
                    if (checkResult(pullResult, ID_DATA) == false)
                    {
                        // hmm there's an error
                        throw new SyncException(SyncException.SyncError.TRANSFER_PROTOCOL_ERROR, readErrorMessage(pullResult, timeOut));
                    }
                    int length = ArrayHelper.swap32bitFromArray(pullResult, 4);
                    if (length > SYNC_DATA_MAX)
                    {
                        // buffer overrun!
                        // error and exit
                        throw new SyncException(SyncException.SyncError.BUFFER_OVERRUN);
                    }

                    // now read the length we received
                    AdbHelper.read(mChannel, data, length, timeOut);

                    // get the header for the next packet.
                    AdbHelper.read(mChannel, pullResult, -1, timeOut);

                    // write the content in the file
                    fos.Write(data, 0, length);

                    monitor.advance(length);
                }

                fos.Flush();
            }
        }
예제 #10
0
        /*
         * (non-Javadoc)
         * @see com.android.ddmlib.IDevice#reboot()
         */
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void reboot(String into) throws TimeoutException, AdbCommandRejectedException, java.io.IOException
        public void reboot(string into)
        {
            AdbHelper.reboot(into, AndroidDebugBridge.socketAddress, this);
        }
예제 #11
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void removeForward(int localPort, int remotePort) throws TimeoutException, AdbCommandRejectedException, java.io.IOException
        public void removeForward(int localPort, int remotePort)
        {
            AdbHelper.removeForward(AndroidDebugBridge.socketAddress, this, localPort, remotePort);
        }
예제 #12
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void runLogService(String logname, com.android.ddmlib.log.LogReceiver receiver) throws TimeoutException, AdbCommandRejectedException, java.io.IOException
        public void runLogService(string logname, LogReceiver receiver)
        {
            AdbHelper.runLogService(AndroidDebugBridge.socketAddress, this, logname, receiver);
        }
예제 #13
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void runEventLogService(com.android.ddmlib.log.LogReceiver receiver) throws TimeoutException, AdbCommandRejectedException, java.io.IOException
        public void runEventLogService(LogReceiver receiver)
        {
            AdbHelper.runEventLogService(AndroidDebugBridge.socketAddress, this, receiver);
        }
예제 #14
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void executeShellCommand(String command, IShellOutputReceiver receiver, int maxTimeToOutputResponse) throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, java.io.IOException
        public void executeShellCommand(string command, IShellOutputReceiver receiver, int maxTimeToOutputResponse)
        {
            AdbHelper.executeRemoteCommand(AndroidDebugBridge.socketAddress, command, this, receiver, maxTimeToOutputResponse);
        }
예제 #15
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void executeShellCommand(String command, IShellOutputReceiver receiver) throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, java.io.IOException
        public void executeShellCommand(string command, IShellOutputReceiver receiver)
        {
            AdbHelper.executeRemoteCommand(AndroidDebugBridge.socketAddress, command, this, receiver, DdmPreferences.timeOut);
        }