Example #1
0
        public static bool AddFileDescriptor(this BPS bps, NetworkStream ns, BPSIO ioEvents, Func <NetworkStream, BPSIO, object, bool> ioHandler, object data = null)
        {
            var socketProp = typeof(NetworkStream).GetProperties(BindingFlags.GetProperty | BindingFlags.NonPublic).First(prop => prop.Name == "Socket");
            var socket     = socketProp.GetValue(ns) as Socket;

            return(bps.AddFileDescriptor(new SafeFileHandle(socket.Handle, false), ns, ioEvents, ioHandler, data)); // Not really the proper way to do this
        }
Example #2
0
 public void Dispose()
 {
     if (!canShutdown)
     {
         throw new InvalidOperationException("Cannot dispose BPS instance");
     }
     if (disposed)
     {
         throw new ObjectDisposedException("BPS");
     }
     if (initBPScount == 0)
     {
         //TODO: should log why the program just exited abnormally
         Environment.Exit(1);
     }
     disposed = true;
     if (initBPScount > 0)
     {
         if (Interlocked.Decrement(ref initBPScount) == 0)
         {
             initBPS = null;
         }
         bps_shutdown();
     }
     // Don't call GC.SuppressFinalize as this works off a reference count-style methodology and we don't use a finalizer anyway
 }
Example #3
0
 public bool PushEvent(BPSEvent ev)
 {
     if (chid == 0)
     {
         throw new ObjectDisposedException("Channel");
     }
     return(BPS.bps_channel_push_event(chid, ev.DangerousGetHandle()) == BPS.BPS_SUCCESS);
 }
Example #4
0
 public Channel()
     : this(0)
 {
     Util.GetBPSOrException();
     if (BPS.bps_channel_create(out chid, 0) != BPS.BPS_SUCCESS)
     {
         Util.ThrowExceptionForLastErrno();
     }
 }
Example #5
0
        protected override bool ReleaseHandle()
        {
            IntPtr hwnd;

            if ((hwnd = Interlocked.Exchange(ref handle, IntPtr.Zero)) != IntPtr.Zero)
            {
                BPS.bps_free(hwnd);
            }
            return(true);
        }
Example #6
0
        public bool PushDelegate(Action <object> exec, object args = null)
        {
            if (chid == 0)
            {
                throw new ObjectDisposedException("Channel");
            }
            var toSerialize = args == null ? (object)exec : new object[] { exec, args };
            var data        = Util.SerializeToPointer(toSerialize);

            if (data == IntPtr.Zero)
            {
                return(false);
            }
            return(BPS.bps_channel_exec(chid, Util.ActionObjFreeHandlerFromFuncZeroReturn, data) == BPS.BPS_SUCCESS);
        }
Example #7
0
        internal static void FreeDataPointer(IntPtr ptr)
        {
            if (ptr == IntPtr.Zero)
            {
                return;
            }
            var payload = new Payload();

            Marshal.PtrToStructure(ptr, payload);
            BPS.UnregisterSerializedPointer(payload.data1);
            BPS.UnregisterSerializedPointer(payload.data2);
            BPS.UnregisterSerializedPointer(payload.data3);
            Util.FreeSerializePointer(payload.data1);
            Util.FreeSerializePointer(payload.data2);
            Util.FreeSerializePointer(payload.data3);
        }
Example #8
0
 private BPS(bool init)
 {
     if (init)
     {
         // init can be called multiple times
         if (bps_initialize() != BPS_SUCCESS)
         {
             Util.ThrowExceptionForLastErrno();
         }
         if (Interlocked.Increment(ref initBPScount) == 1)
         {
             bps_register_channel_destroy_handler(CleanupPointers, IntPtr.Zero);
             initBPS = this;
         }
     }
     canShutdown = init;
     disposed    = false;
 }
Example #9
0
        public void Dispose()
        {
            if (chid == 0)
            {
                throw new ObjectDisposedException("Channel");
            }
            if (BPS.bps_channel_destroy(chid) != BPS.BPS_SUCCESS)
            {
                Util.ThrowExceptionForLastErrno();
            }
            var token = GetTokenSource(chid, false);

            if (token != null)
            {
                RemoveToken(chid);
                token.Cancel();
            }
            chid = 0;
        }
Example #10
0
        internal IntPtr GetDataPointer()
        {
            if (Data1 == null && Data2 == null && Data3 == null)
            {
                return(IntPtr.Zero);
            }

            var payload = new Payload();

#if !ObjectPointersPinnedByDefault
            if (!DataIsAccessibleToNative)
#endif
            {
                payload.data1 = Util.SerializeToPointer(Data1);
                payload.data2 = Util.SerializeToPointer(Data2);
                payload.data3 = Util.SerializeToPointer(Data3);
            }
#if !ObjectPointersPinnedByDefault
            else
            {
                payload.data1 = Util.SerializeToPointer(Data1, GCHandleType.Pinned);
                payload.data2 = Util.SerializeToPointer(Data2, GCHandleType.Pinned);
                payload.data3 = Util.SerializeToPointer(Data3, GCHandleType.Pinned);
            }
#endif

            var result = Stdlib.malloc((ulong)Marshal.SizeOf(payload));
            if (result == IntPtr.Zero)
            {
                Util.FreeSerializePointer(payload.data1);
                Util.FreeSerializePointer(payload.data2);
                Util.FreeSerializePointer(payload.data3);
                return(IntPtr.Zero);
            }
            Marshal.StructureToPtr(payload, result, false);
            BPS.RegisterSerializedPointer(payload.data1);
            BPS.RegisterSerializedPointer(payload.data2);
            BPS.RegisterSerializedPointer(payload.data3);
            return(result);
        }
Example #11
0
 public static bool RemoveFileDescriptor(this BPS bps, MemoryMappedViewStream mmv)
 {
     return(bps.RemoveFileDescriptor(mmv.SafeMemoryMappedViewHandle));
 }
Example #12
0
 public static bool AddFileDescriptor(this BPS bps, PipeStream p, BPSIO ioEvents, Func <PipeStream, BPSIO, object, bool> ioHandler, object data = null)
 {
     return(bps.AddFileDescriptor(p.SafePipeHandle, p, ioEvents, ioHandler, data));
 }
Example #13
0
 public static bool AddFileDescriptor(this BPS bps, MemoryMappedViewStream mmv, BPSIO ioEvents, Func <MemoryMappedViewStream, BPSIO, object, bool> ioHandler, object data = null)
 {
     return(bps.AddFileDescriptor(mmv.SafeMemoryMappedViewHandle, mmv, ioEvents, ioHandler, data));
 }
Example #14
0
 public static bool RemoveFileDescriptor(this BPS bps, FileStream fs)
 {
     return(bps.RemoveFileDescriptor(fs.SafeFileHandle));
 }
Example #15
0
 public static bool RemoveFileDescriptor(this BPS bps, Socket s)
 {
     return(bps.RemoveFileDescriptor(new SafeFileHandle(s.Handle, false))); // Not really the proper way to do this
 }
Example #16
0
 public static bool AddFileDescriptor(this BPS bps, Socket s, BPSIO ioEvents, Func <Socket, BPSIO, object, bool> ioHandler, object data = null)
 {
     return(bps.AddFileDescriptor(new SafeFileHandle(s.Handle, false), s, ioEvents, ioHandler, data)); // Not really the proper way to do this
 }
Example #17
0
 public static bool AddFileDescriptor(this BPS bps, FileStream fs, BPSIO ioEvents, Func <FileStream, BPSIO, object, bool> ioHandler, object data = null)
 {
     return(bps.AddFileDescriptor(fs.SafeFileHandle, fs, ioEvents, ioHandler, data));
 }
Example #18
0
 public static bool RemoveFileDescriptor(this BPS bps, MemoryMappedFile mmf)
 {
     return(bps.RemoveFileDescriptor(mmf.SafeMemoryMappedFileHandle));
 }
Example #19
0
 public static bool RemoveFileDescriptor(this BPS bps, PipeStream p)
 {
     return(bps.RemoveFileDescriptor(p.SafePipeHandle));
 }
Example #20
0
        public static bool RemoveFileDescriptor(this BPS bps, NetworkStream ns)
        {
            var socketProp = typeof(NetworkStream).GetProperties(BindingFlags.GetProperty | BindingFlags.NonPublic).First(prop => prop.Name == "Socket");

            return(bps.RemoveFileDescriptor(socketProp.GetValue(ns) as Socket));
        }