Beispiel #1
0
    internal static TResponse Call <TRequest, TResponse>(TRequest request, NativeMethod nativeMethod)
        where TRequest : IMessage
        where TResponse : IMessage, new()
    {
        using var memory = new UnmanagedMemory();

        var code = nativeMethod(memory.ToByteBuffer(request.ToByteArray()), out var response, out var error);

        memory.ThrowOnError(error);

        var res = new TResponse();

        res.MergeFrom(memory.ToArray(response));
        return(res);
    }
        private unsafe static UnmanagedMemory BuildForPointer(UnmanagedMemory source, string invokeID)
        {
            UnmanagedMemory content = ZlibHelper.ZibCompress(source.ToArray());

            source.Dispose();
            int             contentLength = PacketConstants.InvokeIdLength + content.Length;
            int             packetLength  = PacketConstants.HeadLength + contentLength;
            UnmanagedMemory packet        = new UnmanagedMemory(packetLength);

            byte[] contentLengthBytes = contentLength.ToCustomerBytes();
            packet.Handle[PacketConstants.SettingIndex]       = PacketFirstHeadByteValue.IsPlainString;
            packet.Handle[PacketConstants.SessionLengthIndex] = 0;
            int offset = PacketConstants.ContentLengthIndex;

            Marshal.Copy(contentLengthBytes, 0, (IntPtr)(packet.Handle + offset), contentLengthBytes.Length);
            offset = PacketConstants.HeadLength;
            byte[] invokeIDBytes = PacketConstants.ClientInvokeIDEncoding.GetBytes(invokeID);
            Marshal.Copy(invokeIDBytes, 0, (IntPtr)(packet.Handle + offset), invokeIDBytes.Length);
            offset += invokeIDBytes.Length;
            CopyMemory((IntPtr)(packet.Handle + offset), (IntPtr)content.Handle, (uint)content.Length);
            content.Dispose();
            return(packet);
        }