Пример #1
0
        /// <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();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Reads from the socket until the array is filled, the optional length
        /// is reached, or no more data is coming (because the socket closed or the
        /// timeout expired). After "timeout" milliseconds since the
        /// previous successful read, this will return whether or not new data has
        /// been found.
        /// </summary>
        /// <param name="chan"> the opened socket to read from. It must be in non-blocking
        ///      mode for timeouts to work </param>
        /// <param name="data"> the buffer to store the read data into. </param>
        /// <param name="length"> the length to read or -1 to fill the data buffer completely </param>
        /// <param name="timeout"> The timeout value. A timeout of zero means "wait forever". </param>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: static void read(java.nio.channels.SocketChannel chan, byte[] data, int length, int timeout) throws TimeoutException, java.io.IOException
        internal static void read(SocketChannel chan, byte[] data, int length, int timeout)
        {
            ByteBuffer buf      = ByteBuffer.wrap(data, 0, length != -1 ? length : data.Length);
            int        numWaits = 0;

            while (buf.position != buf.limit)
            {
                int count;

                count = chan.read(buf);
                if (count < 0)
                {
                    Log.d("ddms", "read: channel EOF");
                    throw new IOException("EOF");
                }
                else if (count == 0)
                {
                    // TODO: need more accurate timeout?
                    if (timeout != 0 && numWaits * WAIT_TIME > timeout)
                    {
                        Log.d("ddms", "read: timeout");
                        throw new TimeoutException();
                    }
                    // non-blocking spin
                    Thread.Sleep(WAIT_TIME);
                    numWaits++;
                }
                else
                {
                    numWaits = 0;
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Fills a buffer from a socket. </summary>
        /// <param name="socket"> </param>
        /// <param name="buffer"> </param>
        /// <returns> the content of the buffer as a string, or null if it failed to convert the buffer. </returns>
        /// <exception cref="IOException"> </exception>
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: private String read(java.nio.channels.SocketChannel socket, byte[] buffer) throws java.io.IOException
        private string read(SocketChannel socket, byte[] buffer)
        {
            ByteBuffer buf = ByteBuffer.wrap(buffer, 0, buffer.Length);

            while (buf.position != buf.limit)
            {
                int count;

                count = socket.read(buf);
                if (count < 0)
                {
                    throw new IOException("EOF");
                }
            }

            try
            {
                return(buffer.getString(0, buf.position, AdbHelper.DEFAULT_ENCODING));
            }
            catch (ArgumentException)
            {
                // we'll return null below.
            }

            return(null);
        }
Пример #4
0
        // TODO: ?? add a finalizer that verifies the channel was closed

        /// <summary>
        /// Read data from our channel.
        ///
        /// This is called when data is known to be available, and we don't yet
        /// have a full packet in the buffer.  If the buffer is at capacity,
        /// expand it.
        /// </summary>
        internal void read()
        {
            int count;

            if (mReadBuffer.position == mReadBuffer.capacity)
            {
                if (mReadBuffer.capacity * 2 > MAX_BUF_SIZE)
                {
                    throw new OverflowException();
                }
                Log.d("ddms", "Expanding read buffer to " + mReadBuffer.capacity * 2);

                ByteBuffer newBuffer = ByteBuffer.allocate(mReadBuffer.capacity * 2);
                mReadBuffer.position = 0;
                newBuffer.put(mReadBuffer);                 // leaves "position" at end

                mReadBuffer = newBuffer;
            }

            count = mChannel.read(mReadBuffer);
            Log.v("ddms", "Read " + count + " bytes from " + this);
            if (count < 0)
            {
                throw new IOException("read failed");
            }
        }
Пример #5
0
        /// <summary>
        /// Read data from our channel.
        ///
        /// This is called when data is known to be available, and we don't yet
        /// have a full packet in the buffer.  If the buffer is at capacity,
        /// expand it.
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: void read() throws java.io.IOException, java.nio.BufferOverflowException
        internal virtual void read()
        {
            int count;

            if (mReadBuffer.position == mReadBuffer.capacity)
            {
                if (mReadBuffer.capacity * 2 > MAX_BUF_SIZE)
                {
                    Log.e("ddms", "Exceeded MAX_BUF_SIZE!");
                    throw new OverflowException();
                }
                Log.d("ddms", "Expanding read buffer to " + mReadBuffer.capacity * 2);

                ByteBuffer newBuffer = ByteBuffer.allocate(mReadBuffer.capacity * 2);

                // copy entire buffer to new buffer
                mReadBuffer.position = (0);
                newBuffer.put(mReadBuffer);                 // leaves "position" at end of copied

                mReadBuffer = newBuffer;
            }

            count = mChan.read(mReadBuffer);
            if (count < 0)
            {
                throw new IOException("read failed");
            }

            if (Log.Config.LOGV)
            {
                Log.v("ddms", "Read " + count + " bytes from " + this);
            }
            //Log.hexDump("ddms", Log.DEBUG, mReadBuffer.array(),
            //    mReadBuffer.arrayOffset(), mReadBuffer.position());
        }
Пример #6
0
		/// <summary>
		/// Reads line from the console socket. This call is blocking until we read the lines:
		/// <ul>
		/// <li>OK\r\n</li>
		/// <li>KO<msg>\r\n</li>
		/// </ul> </summary>
		/// <returns> the array of strings read from the emulator. </returns>
		private string[] readLines()
		{
			try
			{
				ByteBuffer buf = ByteBuffer.wrap(mBuffer, 0, mBuffer.Length);
				int numWaits = 0;
				bool stop = false;

				while (buf.position != buf.limit && stop == false)
				{
					int count;

					count = mSocketChannel.read(buf);
					if (count < 0)
					{
						return null;
					}
					else if (count == 0)
					{
						if (numWaits * WAIT_TIME > STD_TIMEOUT)
						{
							return null;
						}
						// non-blocking spin
						try
						{
							Thread.Sleep(WAIT_TIME);
						}
						catch (ThreadInterruptedException)
						{
						}
						numWaits++;
					}
					else
					{
						numWaits = 0;
					}

					// check the last few char aren't OK. For a valid message to test
					// we need at least 4 bytes (OK/KO + \r\n)
					if (buf.position >= 4)
					{
						int pos = buf.position;
						if (endsWithOK(pos) || lastLineIsKO(pos))
						{
							stop = true;
						}
					}
				}

				string msg = mBuffer.getString(0, buf.position, DEFAULT_ENCODING);
				return StringHelperClass.StringSplit(msg, "\r\n", true); //$NON-NLS-1$
			}
			catch (IOException)
			{
				return null;
			}
		}
Пример #7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public byte[] read() throws java.io.IOException
            public virtual sbyte[] read()
            {
                sbyte[] buffer = new sbyte[BUFFER_SIZE];
                int     Length = socketChannel.read(ByteBuffer.wrap(buffer));

                if (Length <= 0)
                {
                    return(null);
                }

                sbyte[] readBuffer = new sbyte[Length];
                Array.Copy(buffer, 0, readBuffer, 0, Length);

                return(readBuffer);
            }
Пример #8
0
        /// <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");
            }
        }