示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bits"></param>
        /// <param name="index"></param>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public static GribMessage Create(byte[] bits, int index, GribContext ctx)
        {
            var buff = Marshal.AllocHGlobal(bits.Length);

            Marshal.Copy(bits, 0, buff, bits.Length);
            int   err = 0;
            SizeT sz  = (SizeT)bits.Length;

            GribMessage msg    = null;
            GribHandle  handle = null;

            lock (_fileLock)
            {
                // grib_api moves to the next message in a stream for each new handle
                handle = GribApiProxy.GribHandleNewFromMultiMessage(ctx, out buff, ref sz, out err);
            }

            if (err != 0)
            {
                Marshal.AllocHGlobal(buff);
                throw GribApiException.Create(err);
            }

            if (handle != null)
            {
                msg = new GribMessage(handle, ctx, buff, index);
            }

            return(msg);
        }
示例#2
0
        /// <summary>
        /// Creates a GribMessage instance from a buffer.
        /// </summary>
        /// <param name="bits"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static GribMessage Create(byte[] bits, int index = 0)
        {
            GCHandle h       = GCHandle.Alloc(bits, GCHandleType.Pinned);
            IntPtr   pHandle = h.AddrOfPinnedObject();

            int        err    = 0;
            SizeT      sz     = (SizeT)bits.Length;
            GribHandle handle = GribApiProxy.GribHandleNewFromMultiMessage(GribContext.Default, out pHandle, ref sz, out err);

            if (err != 0)
            {
                h.Free();
                throw GribApiException.Create(err);
            }

            GribMessage msg = null;

            if (handle != null)
            {
                msg = new GribMessage(handle, GribContext.Default, h, index);
            }

            return(msg);
        }