Ejemplo n.º 1
0
    void InitSendInstance()
    {
        var name        = Marshal.StringToHGlobalAnsi("Test Server");
        var sendOptions = new NDIlib.send_create_t {
            p_ndi_name = name
        };

        _sendInstance = NDIlib.send_create(ref sendOptions);
        Marshal.FreeHGlobal(name);
    }
Ejemplo n.º 2
0
    // private static unsafe void Marshal<T>(string text, T* objT, Tracker state) where T : unmanaged, Enum
    // {
    //     if (Enum.TryParse(text, false, out T value))
    //     {
    //         T result = (T)Enum.Parse(typeof(T), text, false);
    //         state.InplaceEndianToPlatform((uint*)&result);
    //         *objT = result;
    //     }
    // }
    //
    // private static unsafe void Marshal<T>(Value value, T* objT, Tracker state) where T : unmanaged, Enum
    // {
    //     if (value is null)
    //     {
    //         return;
    //     }
    //     Marshal(value.GetText(), objT, state);
    // }

    public static unsafe void Marshal(string text, AnsiString *objT, Tracker state)
    {
        uint length     = (uint)text.Length;
        uint textLength = length;

        state.InplaceEndianToPlatform(&textLength);
        objT->Length = (int)textLength;
        using (Tracker.Context context = state.Push((void **)&objT->Target, 1u, length + 1))
        {
            sbyte *str   = objT->Target;
            IntPtr hText = SMarshal.StringToHGlobalAnsi(text);
            sbyte *pText = (sbyte *)hText;
            int    c;
            do
            {
                *str = *pText;
                ++pText;
                c = *str;
                ++str;
            }while (c != 0);
            SMarshal.FreeHGlobal(hText);
        }
    }
Ejemplo n.º 3
0
        public static void AllSerializeDeserialize <From, To>(From from, bool noTranscoding = false)
            where From : class
            where To : class
        {
            RoundtripMemory <From, To> memoryRoundtrip = (serialize, deserialize) =>
            {
                var data = serialize(from);
                var to   = deserialize(data);
                Assert.IsTrue(from.IsEqual(to));
            };

            RoundtripPointer <From, To> pointerRoundtrip = (serialize, deserialize) =>
            {
                var ptr  = RMarshal.AllocHGlobal(UnsafeBufferSize);
                var data = serialize(from, ptr, UnsafeBufferSize);
                var to   = deserialize(data, UnsafeBufferSize);
                Assert.IsTrue(from.IsEqual(to));
                RMarshal.FreeHGlobal(data);
            };

            RoundtripMemoryPointer <From, To> memoryPointerRoundtrip = (serialize, deserialize) =>
            {
                var data   = serialize(from);
                var pinned = GCHandle.Alloc(data.Array, GCHandleType.Pinned);
                var to     = deserialize(RMarshal.UnsafeAddrOfPinnedArrayElement(data.Array, data.Offset), data.Count);
                Assert.IsTrue(from.IsEqual(to));
                pinned.Free();
            };

            RoundtripStream <From, To> streamRoundtrip = (serialize, deserialize) =>
            {
                var stream = new MemoryStream();

                serialize(from, stream);
                stream.Position = 0;
                var to = deserialize(stream);

                Assert.IsTrue(from.IsEqual(to));
            };

            MarshalStream <From> streamMarshal = serialize => streamRoundtrip(serialize, stream =>
            {
                stream.Position = 0;
                return(Unmarshal <To> .From(new InputStream(stream)));
            });

            MarshalStream <From> streamMarshalSchema = serialize => streamRoundtrip(serialize, stream =>
            {
                stream.Position = 0;
                return(Unmarshal.From(new InputStream(stream), Schema <From> .RuntimeSchema).Deserialize <To>());
            });

            MarshalStream <From> streamMarshalNoSchema = serialize => streamRoundtrip(serialize, stream =>
            {
                stream.Position = 0;
                return(Unmarshal.From(new InputStream(stream)).Deserialize <To>());
            });

            MarshalMemory <From> memoryMarshal = serialize => memoryRoundtrip(serialize, Unmarshal <To> .From);

            TranscodeStream <From, To> streamTranscode = (serialize, transcode, deserialize) =>
                                                         streamRoundtrip((obj, stream) =>
            {
                using (var tmp = new MemoryStream())
                {
                    serialize(obj, tmp);
                    tmp.Position = 0;
                    transcode(tmp, stream);
                }
            }, deserialize);

            if (noTranscoding)
            {
                streamTranscode = (serialize, transcode, deserialize) => { }
            }
            ;

            // Compact Binary
            streamRoundtrip(SerializeCB, DeserializeCB <To>);
            memoryRoundtrip(SerializeUnsafeCB, DeserializeSafeCB <To>);
            memoryRoundtrip(SerializeUnsafeCB, DeserializeUnsafeCB <To>);
            memoryPointerRoundtrip(SerializeUnsafeCB, DeserializePointerCB <To>);
            pointerRoundtrip(SerializePointerCB, DeserializePointerCB <To>);
            memoryRoundtrip(SerializeSafeCB, DeserializeSafeCB <To>);
            memoryRoundtrip(SerializeSafeCB, DeserializeUnsafeCB <To>);
            memoryPointerRoundtrip(SerializeSafeCB, DeserializePointerCB <To>);
            memoryRoundtrip(SerializeSafeCBNoInlining, DeserializeSafeCB <To>);

            streamMarshal(MarshalCB);
            streamMarshal(SerializerMarshalCB);
            streamMarshalSchema(MarshalCB);
            streamMarshalNoSchema(MarshalCB);
            memoryMarshal(MarshalCB);

            streamTranscode(SerializeCB, TranscodeCBCB, DeserializeCB <To>);
            streamTranscode(SerializeCB, TranscodeCBFB, DeserializeFB <To>);

            streamRoundtrip(SerializeCB, stream =>
            {
                var input  = new InputStream(stream);
                var reader = new CompactBinaryReader <InputStream>(input);
                return(DeserializeTagged <To>(reader));
            });

            // Fast Binary
            streamRoundtrip(SerializeFB, DeserializeFB <To>);
            memoryRoundtrip(SerializeFB, DeserializeSafeFB <To>);
            memoryRoundtrip(SerializeFB, DeserializeUnsafeFB <To>);
            memoryPointerRoundtrip(SerializeFB, DeserializePointerFB <To>);

            streamMarshal(MarshalFB);
            streamMarshal(SerializerMarshalFB);
            streamMarshalSchema(MarshalFB);
            streamMarshalNoSchema(MarshalFB);
            memoryMarshal(MarshalFB);

            streamTranscode(SerializeFB, TranscodeFBFB, DeserializeFB <To>);
            streamTranscode(SerializeFB, TranscodeFBCB, DeserializeCB <To>);

            streamRoundtrip(SerializeFB, stream =>
            {
                var input  = new InputStream(stream);
                var reader = new FastBinaryReader <InputStream>(input);
                return(DeserializeTagged <To>(reader));
            });

            // Simple doesn't support omitting fields
            if (typeof(From) != typeof(Nothing) && typeof(From) != typeof(GenericsWithNothing))
            {
                streamRoundtrip(SerializeSP, DeserializeSP <From, To>);
                memoryRoundtrip(SerializeSP, DeserializeSafeSP <From, To>);
                memoryRoundtrip(SerializeSP, DeserializeUnsafeSP <From, To>);
                memoryPointerRoundtrip(SerializeSP, DeserializePointerSP <From, To>);

                streamRoundtrip(SerializeSP2, DeserializeSP2 <From, To>);
                memoryRoundtrip(SerializeSP2, DeserializeSafeSP2 <From, To>);
                memoryRoundtrip(SerializeSP2, DeserializeUnsafeSP2 <From, To>);

                streamTranscode(SerializeCB, TranscodeCBSP <From>, DeserializeSP <From, To>);
                streamTranscode(SerializeFB, TranscodeFBSP <From>, DeserializeSP <From, To>);
                streamTranscode(SerializeSP, TranscodeSPSP <From>, DeserializeSP <From, To>);
                streamTranscode(SerializeSP, TranscodeSPCB <From>, DeserializeCB <To>);
                streamTranscode(SerializeSP, TranscodeSPFB <From>, DeserializeFB <To>);

                // Pull parser doesn't supprot bonded<T>
                if (AnyField <From>(Reflection.IsBonded))
                {
                    streamTranscode(SerializeSP, TranscodeSPXml <From>, DeserializeXml <To>);

                    // NewtonSoft JSON doesn't support uint64
                    if (typeof(From) != typeof(MaxUInt64))
                    {
                        streamTranscode(SerializeSP, TranscodeSPJson <From>, DeserializeJson <To>);
                    }
                }

                streamRoundtrip(SerializeSP, stream =>
                {
                    var input  = new InputStream(stream);
                    var reader = new SimpleBinaryReader <InputStream>(input);
                    return(DeserializeUntagged <From, To>(reader));
                });

                streamMarshalSchema(MarshalSP);
            }

            // Pull parser doesn't supprot bonded<T>
            if (AnyField <From>(Reflection.IsBonded))
            {
                streamRoundtrip(SerializeXml, DeserializeXml <To>);
                streamTranscode(SerializeCB, TranscodeCBXml <From>, DeserializeXml <To>);
                streamTranscode(SerializeFB, TranscodeFBXml <From>, DeserializeXml <To>);

                // NewtonSoft JSON doesn't support uint64
                if (typeof(From) != typeof(MaxUInt64))
                {
                    streamRoundtrip(SerializeJson, DeserializeJson <To>);
                    streamTranscode(SerializeCB, TranscodeCBJson <From>, DeserializeJson <To>);
                    streamTranscode(SerializeFB, TranscodeFBJson <From>, DeserializeJson <To>);
                }
            }
        }

        delegate bool TypePredicate(Type field);
Ejemplo n.º 4
0
        public byte[][] GetServiceRecordsUnparsedWindowsRaw(Guid service)
        {
            WqsOffset.AssertCheckLayout();
            BlobOffsets.AssertCheckLayout();
            //temporary workaround - sockets must be initialised
            Socket s = new Socket(AddressFamily32.Bluetooth, SocketType.Stream, BluetoothProtocolType.RFComm);

            //store variable length collection of records
            System.Collections.ArrayList records = new System.Collections.ArrayList();

            byte[] sdp = null;

            WSAQUERYSET wqs = new WSAQUERYSET();
            wqs.dwSize = WqsOffset.StructLength_60;
            wqs.dwNameSpace = WqsOffset.NsBth_16;

#if NETCF
            CSADDR_INFO sainfo = new CSADDR_INFO(null, this.DeviceAddress , SocketType.Unknown, ProtocolType.Unknown);
            wqs.dwNumberOfCsAddrs = 1;
            
            IntPtr pSaInfo = Marshal32.AllocHGlobal(24);
            IntPtr pBrb = Marshal32.AllocHGlobal(256);
            IntPtr pService = Marshal32.AllocHGlobal(240);

            Marshal.StructureToPtr(sainfo, pSaInfo, false);
            wqs.lpcsaBuffer = pSaInfo;            

            Marshal.WriteInt32(pBrb, 0, (int)SdpQueryType.SearchAttributeRequest);

            //write the service guid
            Marshal.Copy(service.ToByteArray(), 0, new IntPtr(pBrb.ToInt32() + 8), 16);
            Marshal.WriteInt16(pBrb, 24, (short)SdpSpecificType.Uuid128);
            Marshal.WriteInt16(pBrb, 26, 0);

            //write an empty guid to the next position
            Marshal.Copy(Guid.Empty.ToByteArray(), 0, new IntPtr(pBrb.ToInt32() + 28), 16);
            Marshal.WriteInt16(pBrb, 44, 0x0);
            Marshal.WriteInt16(pBrb, 46, 0);

            //number of attribute ranges
            Marshal.WriteInt32(pBrb, 248, 1);
            //min attribute
            Marshal.WriteInt16(pBrb, 252, 0);
            //max attribute
            Marshal.StructureToPtr((ushort)0xffff, (IntPtr)(pBrb.ToInt32() + 254), false);
            //Marshal.WriteInt16(pBrb, 254, 0x800);
            
            BLOB b = new BLOB(256, pBrb);

            IntPtr pb = Marshal32.AllocHGlobal(8);

            Marshal.StructureToPtr(b, pb, false);
            wqs.lpBlob = pb;
            
#endif


#if WinXP
            GCHandle hservice = GCHandle.Alloc(service.ToByteArray(), GCHandleType.Pinned);
            wqs.lpServiceClassId = hservice.AddrOfPinnedObject();
            wqs.lpszContext = "(" + this.DeviceAddress.ToString("C") + ")"; // sb.ToString(); // hContext.AddrOfPinnedObject();
#endif

            IntPtr handle = IntPtr.Zero;

            int lookupresult;

            //start looking for Bluetooth services


#if NETCF
            LookupFlags flagsForBegin; // Move above the #if is changed to use for desktop build too.
            flagsForBegin = 0;
            bool isQueryingLocalhost = false;
            try {
                BluetoothRadio theOne = BluetoothRadio.PrimaryRadio; // Only ever one on CE...
                if (theOne != null) {
                    BluetoothAddress localAddr = theOne.LocalAddress;
                    if (localAddr != null) {
                        if (localAddr == this.DeviceAddress) {
                            isQueryingLocalhost = true;
                        }
                    }
                }
            } catch (Exception ex) {
                System.Diagnostics.Debug.Fail("Exception in hack on CE to check if localhost: " + ex);
            }
            if (isQueryingLocalhost) {
                flagsForBegin |= LookupFlags.ResService;
            }
#endif
#if NETCF
            try
            {
                lookupresult = NativeMethods.WSALookupServiceBegin(ref wqs, flagsForBegin, out handle);
#else
            lookupresult = NativeMethods.WSALookupServiceBegin(ref wqs, LookupFlags.FlushCache | LookupFlags.ReturnName | LookupFlags.ReturnBlob, out handle);
#endif
            SocketBluetoothClient.ThrowSocketExceptionForHR(lookupresult);


#if WinXP
            hservice.Free();
#endif

            while (lookupresult == 0) {
                byte[] sdpBuffer = new byte[6000];
                BitConverter.GetBytes(WqsOffset.StructLength_60).CopyTo(sdpBuffer, WqsOffset.dwSize_0);
                BitConverter.GetBytes(WqsOffset.NsBth_16).CopyTo(sdpBuffer, WqsOffset.dwNameSpace_20);
                int size = sdpBuffer.Length;

#if NETCF
                    lookupresult = NativeMethods.WSALookupServiceNext(handle, (LookupFlags)0, ref size, sdpBuffer);
#else
                lookupresult = NativeMethods.WSALookupServiceNext(handle, LookupFlags.FlushCache | LookupFlags.ReturnBlob, ref size, sdpBuffer);
#endif

                if (lookupresult == -1) {
                    const int WSA_E_NO_MORE = 10110;
                    SocketBluetoothClient.ThrowSocketExceptionForHrExceptFor(lookupresult, WSA_E_NO_MORE);
                } else {
                    IntPtr pBlob = InTheHand.Runtime.InteropServices.Marshal32.ReadIntPtr(sdpBuffer, WqsOffset.lpBlob_56);
                    if (pBlob != IntPtr.Zero) {
                        IntPtr pSdpBlob = InTheHand.Runtime.InteropServices.Marshal32.ReadIntPtr(pBlob, BlobOffsets.Offset_pBlobData_4);
                        int cSdpBlob = Marshal.ReadInt32(pBlob);

                        if (cSdpBlob > 2) {
                            sdp = new byte[cSdpBlob];
                            Marshal.Copy(pSdpBlob, sdp, 0, cSdpBlob);
                            records.Add(sdp);
                        }
                    }

                }

            }

#if NETCF
            }
            finally
            {
                sainfo.Dispose();

                Marshal32.FreeHGlobal(pSaInfo);
                Marshal32.FreeHGlobal(pb);
                Marshal32.FreeHGlobal(pBrb);
            }
#endif

            //stop looking
            lookupresult = NativeMethods.WSALookupServiceEnd(handle);
            SocketBluetoothClient.ThrowSocketExceptionForHR(lookupresult);

            return (byte[][])records.ToArray(typeof(byte[]));
        }