Example #1
0
        /// <summary>
        /// 发送消息通过Socket,大部分用途为发送plist文件(指令)给设备
        /// </summary>
        /// <param name="sock"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool SendMessageToSocket(int sock, IntPtr message)
        {
            if (sock < 1 || message == IntPtr.Zero)
            {
                return(false);
            }

            var flag   = false;
            var stream = CoreFoundation.CFWriteStreamCreateWithAllocatedBuffers(IntPtr.Zero, IntPtr.Zero);

            if (stream != IntPtr.Zero)
            {
                if (!CoreFoundation.CFWriteStreamOpen(stream))
                {
                    return(false);
                }

                var zero = IntPtr.Zero;
                if (CoreFoundation.CFPropertyListWriteToStream(message, stream, CFPropertyListFormat.kCFPropertyListBinaryFormat_v1_0, ref zero) > 0)
                {
                    IntPtr srcRef        = CoreFoundation.CFWriteStreamCopyProperty(stream, CoreFoundation.kCFStreamPropertyDataWritten);
                    IntPtr buffer        = CoreFoundation.CFDataGetBytePtr(srcRef);
                    var    bufferlen     = CoreFoundation.CFDataGetLength(srcRef);
                    var    structure     = MobileDevice.htonl((uint)bufferlen);
                    var    structureSize = Marshal.SizeOf(structure);
                    if (DeviceSecureIOContext == IntPtr.Zero || SocketContext == IntPtr.Zero)
                    {
                        if (MobileDevice.send_UInt32(sock, ref structure, structureSize, 0) != structureSize)
                        {
                            Console.WriteLine("could not send message size");
                        }
                        else if (MobileDevice.send(sock, buffer, bufferlen, 0) != bufferlen)
                        {
                            Console.WriteLine("Could not send message.");
                        }
                        else
                        {
                            flag = true;
                        }
                    }
                    else if (MobileDevice.AMDServiceConnectionSend_UInt32(SocketContext, ref structure, structureSize) != structureSize)
                    {
                        Console.WriteLine("could not send message size with socket");
                    }
                    else if (MobileDevice.AMDServiceConnectionSend(SocketContext, buffer, bufferlen) != bufferlen)
                    {
                        Console.WriteLine("could not send message size");
                    }
                    else
                    {
                        flag = true;
                    }

                    CoreFoundation.CFRelease(srcRef);
                }

                CoreFoundation.CFWriteStreamClose(stream);
            }

            return(flag);
        }