Exemple #1
0
        public void RunLogService(IPEndPoint address, Device device, string logName, LogReceiver rcvr)
        {
            using (Socket socket = ExecuteRawSocketCommand(address, device, "log:{0}".With(logName)))
            {
                byte[] data = new byte[16384];
                using (MemoryStream ms = new MemoryStream(data))
                {
                    int offset = 0;

                    while (true)
                    {
                        int count;
                        if (rcvr != null && rcvr.IsCancelled)
                        {
                            break;
                        }
                        byte[] buffer = new byte[4 * 1024];

                        count = socket.Receive(buffer);
                        if (count < 0)
                        {
                            break;
                        }
                        else if (count == 0)
                        {
                            try
                            {
                                Thread.Sleep(WAIT_TIME * 5);
                            }
                            catch (ThreadInterruptedException)
                            {
                            }
                        }
                        else
                        {
                            ms.Write(buffer, offset, count);
                            offset += count;
                            if (rcvr != null)
                            {
                                byte[] d = ms.ToArray();
                                rcvr.ParseNewData(d, 0, d.Length);
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Runs the Event log service on the Device, and provides its output to the LogReceiver.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <param name="device">The device.</param>
        /// <param name="logName">Name of the log.</param>
        /// <param name="rcvr">The RCVR.</param>
        /// <exception cref="AdbException">failed asking for log</exception>
        /// <exception cref="Managed.Adb.Exceptions.AdbCommandRejectedException"></exception>
        public void RunLogService(IPEndPoint address, Device device, String logName, LogReceiver rcvr)
        {
            try {
                using (var adbChan = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
                    adbChan.Connect(address);
                    adbChan.Blocking = true;
                    // 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);
                    if (!Write(adbChan, request))
                    {
                        throw new AdbException("failed asking for log");
                    }

                    var resp = ReadAdbResponse(adbChan, false /* readDiagString */);
                    if (resp.Okay == false)
                    {
                        throw new AdbCommandRejectedException(resp.Message);
                    }

                    byte[] data = new byte[16384];
                    using (var ms = new MemoryStream(data)) {
                        int offset = 0;

                        while (true)
                        {
                            int count;
                            if (rcvr != null && rcvr.IsCancelled)
                            {
                                break;
                            }
                            var buffer = new byte[4 * 1024];

                            count = adbChan.Receive(buffer);
                            if (count < 0)
                            {
                                break;
                            }
                            else if (count == 0)
                            {
                                try {
                                    Thread.Sleep(WAIT_TIME * 5);
                                } catch (ThreadInterruptedException ie) {
                                }
                            }
                            else
                            {
                                ms.Write(buffer, offset, count);
                                offset += count;
                                if (rcvr != null)
                                {
                                    var d = ms.ToArray( );
                                    rcvr.ParseNewData(d, 0, d.Length);
                                }
                            }
                        }
                    }
                }
            } finally {
            }
        }
Exemple #3
0
 public void RunCatLog(IPEndPoint address, IDevice device, string filePath, LogReceiver receiver)
 {
     if (device.IsOnline && (receiver != null))
     {
         int    num    = 0x1400;
         Action action = null;
         Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         try
         {
             socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, 0));
             socket.ReceiveTimeout    = -1;
             socket.ReceiveBufferSize = num + 1;
             socket.Blocking          = true;
             socket.Connect(AndroidDebugBridge.SocketAddress);
             if (action == null)
             {
                 action = delegate
                 {
                     if (socket != null)
                     {
                         socket.Close();
                     }
                 };
             }
             receiver.CancelAction = action;
             this.SetDevice(socket, device);
             //byte[] data = Instance.FormAdbRequest(string.Format("shell:cat {0}", filePath));
             byte[] data = Instance.FormAdbRequest(string.Format("log:{0}", filePath));
             if (!this.Write(socket, data))
             {
                 throw new AdbException("failed submitting shell command");
             }
             AdbResponse response = Instance.ReadAdbResponse(socket, false);
             if (!response.IOSuccess || !response.Okay)
             {
                 throw new AdbException("sad result from adb: " + response.Message);
             }
             byte[] buffer = new byte[num + 1];
             byte   num2   = 0;
             while ((device.IsOnline && (receiver != null)) && !receiver.IsCancelled)
             {
                 int num3 = socket.Receive(buffer);
                 if (num3 > 0)
                 {
                     using (MemoryStream stream = new MemoryStream())
                     {
                         for (int i = 0; i < num3; i++)
                         {
                             if ((((num3 > (i + 1)) && (buffer[i] == 13)) && (buffer[i + 1] == 10)) || ((num2 == 13) && (buffer[i] == 10)))
                             {
                                 stream.WriteByte(10);
                                 i++;
                             }
                             else
                             {
                                 stream.WriteByte(buffer[i]);
                             }
                             if (i == (num3 - 1))
                             {
                                 num2 = buffer[i];
                             }
                         }
                         receiver.ParseNewData(stream.ToArray(), 0, (int)stream.Length);
                         //continue;
                     }
                 }
             }
         }
         catch (SocketException exception)
         {
             if (exception.SocketErrorCode != SocketError.ConnectionAborted)
             {
                 Log.e("Socket error while receiving response", exception);
             }
         }
         finally
         {
             if (socket != null)
             {
                 socket.Dispose();
             }
         }
     }
 }