Example #1
0
        /// <summary>
        /// 接收Socket消息,主要为接收设备返回的指令结果,大部分为Plist,所以函数内将会自行作转换
        /// </summary>
        /// <param name="sock"></param>
        /// <returns></returns>
        public object ReceiveMessageFromSocket(int sock)
        {
            if (sock < 0)
            {
                return(null);
            }

            int    recvCount;
            uint   buffer     = 0;
            uint   reviceSize = 0;
            uint   dataSize   = 0;
            IntPtr zero       = IntPtr.Zero;

            if (DeviceSecureIOContext != IntPtr.Zero && SocketContext != IntPtr.Zero)
            {
                if (MobileDevice.AMDServiceConnectionReceive(SocketContext, ref buffer, 4) == 4)
                {
                    dataSize = MobileDevice.ntohl(buffer); //获取数据总长度
                    if (dataSize <= 0)
                    {
                        Console.WriteLine("receive size error, dataSize:" + dataSize);
                        return(null);
                    }

                    zero = Marshal.AllocCoTaskMem((int)dataSize);
                    if (zero == IntPtr.Zero)
                    {
                        Console.WriteLine("Could not allocate message buffer.");
                        return(null);
                    }

                    var tempPtr = zero;
                    while (reviceSize < dataSize)
                    {
                        recvCount = MobileDevice.AMDServiceConnectionReceive_1(SocketContext, tempPtr, (int)(dataSize - reviceSize));
                        if (recvCount <= -1)
                        {
                            Console.WriteLine("Could not receive secure message: " + recvCount);
                            reviceSize = dataSize + 1;
                        }
                        else if (recvCount == 0)
                        {
                            Console.WriteLine("receive size is zero. ");
                            break;
                        }

                        tempPtr     = new IntPtr(tempPtr.ToInt64() + recvCount);
                        reviceSize += (uint)recvCount;
                    }
                }
            }
            else if (MobileDevice.recv_UInt(sock, ref buffer, 4, 0) == 4)
            {
                dataSize = MobileDevice.ntohl(buffer); //获取数据总长度
                if (dataSize <= 0)
                {
                    Console.WriteLine("receive size error, dataSize:" + dataSize);
                    return(null);
                }

                zero = Marshal.AllocCoTaskMem((int)dataSize);
                if (zero == IntPtr.Zero)
                {
                    Console.WriteLine("Could not allocate message buffer.");
                    return(null);
                }

                var tempPtr = zero;
                while (reviceSize < dataSize)
                {
                    recvCount = MobileDevice.recv(sock, tempPtr, (int)(dataSize - reviceSize), 0);
                    if (recvCount <= -1)
                    {
                        Console.WriteLine("Could not receive secure message: " + recvCount);
                        reviceSize = dataSize + 1;
                    }
                    else if (recvCount == 0)
                    {
                        Console.WriteLine("receive size is zero. ");
                        break;
                    }

                    tempPtr     = new IntPtr(tempPtr.ToInt64() + recvCount);
                    reviceSize += (uint)recvCount;
                }
            }

            var datas  = IntPtr.Zero;
            var srcRef = IntPtr.Zero;

            if (reviceSize == dataSize)
            {
                datas = CoreFoundation.CFDataCreate(CoreFoundation.kCFAllocatorDefault, zero, (int)dataSize);
                if (datas == IntPtr.Zero)
                {
                    Console.WriteLine("Could not create CFData for message");
                }
                else
                {
                    var errorString = IntPtr.Zero;
                    srcRef = CoreFoundation.CFPropertyListCreateFromXMLData(CoreFoundation.kCFAllocatorDefault, datas,
                                                                            CFPropertyListMutabilityOptions.kCFPropertyListImmutable, ref errorString);
                    if (srcRef == IntPtr.Zero)
                    {
                        Console.WriteLine("Could not convert raw xml into a dictionary: " + Convert.ToString(CoreFoundation.ManagedTypeFromCFType(ref errorString)));
                        return(null);
                    }
                }
            }

            if (datas != IntPtr.Zero)
            {
                try
                {
                    CoreFoundation.CFRelease(datas);
                }
                catch
                {
                }
            }

            if (zero != IntPtr.Zero)
            {
                Marshal.FreeCoTaskMem(zero);
            }

            var result = CoreFoundation.ManagedTypeFromCFType(ref srcRef);

            if (srcRef != IntPtr.Zero)
            {
                CoreFoundation.CFRelease(srcRef);
            }

            return(result);
        }