Esempio n. 1
0
 private static bool ReadStream(string file, StreamCallback callback)
 {
     try
     {
         using var fs = new FileStream(file, FileMode.Open, FileAccess.Read);
         var b4 = new byte[4];
         if (fs.Read(b4, 0, 4) != 4)
         {
             return(false);
         }
         var pos = BitConverter.ToInt32(b4, 0);
         if (pos > fs.Length || pos < 0)
         {
             return(false);
         }
         fs.Seek(pos, SeekOrigin.Begin);
         if (fs.Read(b4, 0, 4) != 4)
         {
             return(false);
         }
         if (b4[0] == marker[0] && b4[1] == marker[1] && b4[2] == marker[2] && b4[3] == marker[3])
         {
             fs.Seek(4, SeekOrigin.Begin);
             callback(fs);
             return(true);
         }
     }
     catch (Exception ex)
     {
         Log.Debug(ex, "TransactedWriter.Write");
     }
     return(false);
 }
 internal unsafe PaStreamCallback ToPaStreamCallback(StreamCallback src, bool isOutput)
 {
     return((input, output, frameCount, timeInfo, statusFlags, userData) => {
         var ptr = timeInfo != IntPtr.Zero ? new CppInstancePtr(timeInfo) : default(CppInstancePtr);
         try {
             byte [] buf = buffer != null ? (byte[])buffer.Target : null;
             var byteCount = FramesToBytes((uint)frameCount);
             if (buf == null || buf.Length < byteCount)
             {
                 buf = new byte [byteCount];
                 buffer = new WeakReference(buf);
             }
             var ret = src(
                 buf,
                 0,
                 byteCount,
                 timeInfo != IntPtr.Zero ? Factory.Create <PaStreamCallbackTimeInfo> (ptr) : default(PaStreamCallbackTimeInfo),
                 (PaStreamCallbackFlags)statusFlags,
                 userData
                 );
             Marshal.Copy(buf, 0, isOutput ? output : input, byteCount);
             return ret;
         } finally {
             ptr.Dispose();
         }
     });
 }
Esempio n. 3
0
        public static bool WriteStream(string fileName, string folder, StreamCallback callback)
        {
            try
            {
                var name1 = Path.Combine(folder, $"{fileName}.1");
                var name2 = Path.Combine(folder, $"{fileName}.2");
                var name3 = Path.Combine(folder, $"{fileName}.3.{Guid.NewGuid()}");

                if (!File.Exists(name1))
                {
                    WriteStream(name1, callback);
                    return(true);
                }
                else
                {
                    WriteStream(name2, callback);
                    File.Move(name1, name3);
                    File.Move(name2, name1);
                    File.Move(name3, name2);
                }
                return(false);
            }
            catch (Exception ex)
            {
                Log.Debug(ex, "TransactedWriter.Write");
            }
            return(false);
        }
Esempio n. 4
0
 public IOStream(IntPtr handle, bool forReading, StreamCallback function) : base(cBufferSize)
 {
     toStop   = false;
     toRead   = forReading;
     callback = function;
     // initializes an Async IO Stream for the handle
     stream = new FileStream(new Microsoft.Win32.SafeHandles.SafeFileHandle(handle, false),
                             (toRead == true)? FileAccess.Read : FileAccess.Write, 128, true);
 }
Esempio n. 5
0
        private static void WriteStream(string file, StreamCallback callback)
        {
            using var fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            fs.Seek(4, SeekOrigin.Begin);
            callback(fs);
            var pos = fs.Position;

            fs.Write(marker, 0, 4);
            fs.Seek(0, SeekOrigin.Begin);
            fs.Write(BitConverter.GetBytes((int)pos), 0, 4);
            fs.Close();
        }
 public PortAudioInputStream(int numInputChannels, PaSampleFormat sampleFormat, double sampleRate, uint framesPerBuffer, StreamCallback streamCallback, IntPtr userData)
     : base(sampleFormat, numInputChannels)
 {
     HandleError (PortAudioInterop.Pa_OpenDefaultStream (
         out handle,
         numInputChannels,
         0,
         (IntPtr) sampleFormat,
         sampleRate,
         (IntPtr) framesPerBuffer,
         ToPaStreamCallback (streamCallback, false),
         userData
     ));
 }
 public PortAudioInputStream(PaStreamParameters inputParameters, double sampleRate, uint framesPerBuffer, PaStreamFlags streamFlags, StreamCallback streamCallback, IntPtr userData)
     : base(inputParameters.sampleFormat, inputParameters.channelCount)
 {
     using (var input = Factory.ToNative<PaStreamParameters> (inputParameters))
         HandleError (PortAudioInterop.Pa_OpenStream (
             out handle,
             input.Native,
             IntPtr.Zero,
             sampleRate,
             framesPerBuffer,
             streamFlags,
             ToPaStreamCallback (streamCallback, false),
             userData
         ));
 }
 public PortAudioOutputStream(int numOutputChannels, PaSampleFormat sampleFormat, double sampleRate, uint framesPerBuffer, StreamCallback streamCallback, object userData)
     : base(sampleFormat, numOutputChannels)
 {
     var gch = userData == null ? default (GCHandle) : GCHandle.Alloc (userData, GCHandleType.Pinned);
     try {
         HandleError (PortAudioInterop.Pa_OpenDefaultStream (
             out handle,
             0,
             numOutputChannels,
             (IntPtr) sampleFormat,
             sampleRate,
             (IntPtr) framesPerBuffer,
             ToPaStreamCallback (streamCallback, true),
             userData != null ? gch.AddrOfPinnedObject () : IntPtr.Zero));
     } finally {
         if (userData != null)
             gch.Free ();
     }
 }
Esempio n. 9
0
        /// <summary>
        /// Adds a callback to be called on the host after all currently enqueued items
        /// in the stream have completed
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="userData">Not supported</param>
        public void EnqueueHostCallback(StreamCallback callback, object userData = null)
        {
            ThrowIfDisposed();
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            if (callbackHandle.IsAllocated)
            {
                callbackHandle.Free();
            }
            if (userDataHandle.IsAllocated)
            {
                userDataHandle.Free();
            }

            IntPtr userDataPtr = IntPtr.Zero;

            if (userData != null)
            {
                userDataHandle = GCHandle.Alloc(userData);
                userDataPtr    = GCHandle.ToIntPtr(userDataHandle);
            }

            callbackInternal = new StreamCallbackInternal(
                delegate(IntPtr rawStream, int status, IntPtr rawUserData)
            {
                var streamObj = new Stream(rawStream)
                {
                    IsEnabledDispose = false
                };
                var userDataObj = GCHandle.FromIntPtr(rawUserData).Target;
                callback(streamObj, status, userDataObj);
            });
            callbackHandle = GCHandle.Alloc(callbackInternal);
            IntPtr callbackPtr = Marshal.GetFunctionPointerForDelegate(callbackInternal);

            NativeMethods.cuda_Stream_enqueueHostCallback(
                ptr, callbackPtr, userDataPtr);
            GC.KeepAlive(this);
        }
Esempio n. 10
0
 public PortAudioOutputStream(PaStreamParameters outputParameters, double sampleRate, uint framesPerBuffer, PaStreamFlags streamFlags, StreamCallback streamCallback, object userData)
     : base(outputParameters.sampleFormat, outputParameters.channelCount)
 {
     var gch = userData == null ? default (GCHandle) : GCHandle.Alloc (userData, GCHandleType.Pinned);
     try {
         using (var output = Factory.ToNative<PaStreamParameters> (outputParameters))
             HandleError (PortAudioInterop.Pa_OpenStream (
                 out handle,
                 IntPtr.Zero,
                 output.Native,
                 sampleRate,
                 framesPerBuffer,
                 streamFlags,
                 ToPaStreamCallback (streamCallback, true),
                 userData != null ? gch.AddrOfPinnedObject () : IntPtr.Zero));
     } finally {
         if (userData != null)
             gch.Free ();
     }
 }
Esempio n. 11
0
        public static bool ReadStream(string fileName, string folder, StreamCallback callback)
        {
            var name1 = Path.Combine(folder, $"{fileName}.1");
            var name2 = Path.Combine(folder, $"{fileName}.2");

            //var name3 = Path.Combine(folder, $"{fileName}.3");

            if (File.Exists(name1) && ReadStream(name1, callback))
            {
                return(true);
            }
            if (File.Exists(name2) && ReadStream(name2, callback))
            {
                return(true);
            }
            //if (File.Exists(name3) && ReadStream(name3, callback))
            //{
            //    return true;
            //}
            return(false);
        }
Esempio n. 12
0
    /// <summary>
    /// Adds reqired composites to this <see cref="Assembler"/> so that XML serialization of configuration composites would be possible.
    /// This method should be invoked to same module as where <see cref="E_Qi4CSConfiguration.AddSupportForConfigurationManager"/> method has been called.
    /// </summary>
    /// <param name="assembler">This <see cref="Assembler"/>.</param>
    /// <param name="streamCallback">The optional callback to get streams. If this is <c>null</c>, the default callback will be used which will interpret <see cref="XMLConfigurationResource.DocumentResource"/> as path to file.</param>
    /// <param name="customSerializers">The custom serializers.</param>
    /// <seealso cref="XMLConfigurationSerializerHelper"/>
    public static void AddXMLSerializationSupport(this Assembler assembler, StreamCallback streamCallback, IList <XMLConfigurationSerializerHelper> customSerializers)
    {
        PlainCompositeAssemblyDeclaration decl;

        if (assembler
            .ForNewOrExistingPlainComposite(new Type[] { typeof(XMLConfigurationSerializer) }, out decl))
        {
            decl.OfTypes(typeof(XMLConfigurationSerializer));
        }
        var serializers = decl.Get <List <XMLConfigurationSerializerHelper> >();

        if (serializers == null)
        {
            serializers = new List <XMLConfigurationSerializerHelper>();
        }
        if (customSerializers != null)
        {
            serializers.AddRange(customSerializers);
        }

        decl.Use(streamCallback ?? new DotNETStreamHelper(), serializers);
    }
Esempio n. 13
0
            public IAsyncResult StartUserStream(
                FollowingCallback followingCallback = null,
                FollowersCallback followersCallback = null,
                PostsCallback postsCallback         = null,
                MentionsCallback mentionsCallback   = null,
                StreamCallback streamCallback       = null,
                UnifiedCallback unifiedCallback     = null,
                ChannelsCallback channelsCallback   = null,
                ChannelSubscribersCallback channelSubscribersCallback = null,
                ChannelMessagesCallback channelMessagesCallback       = null,
                FilesCallback filesCallback                 = null,
                RawJsonCallback rawJsonCallback             = null,
                StreamStoppedCallback streamStoppedCallback = null)
            {
                if (this.request != null)
                {
                    throw new InvalidOperationException("Stream is already open");
                }

                this.followingCallback          = followingCallback;
                this.followersCallback          = followersCallback;
                this.postsCallback              = postsCallback;
                this.mentionsCallback           = mentionsCallback;
                this.streamCallback             = streamCallback;
                this.unifiedCallback            = unifiedCallback;
                this.channelsCallback           = channelsCallback;
                this.channelSubscribersCallback = channelSubscribersCallback;
                this.channelMessagesCallback    = channelMessagesCallback;
                this.filesCallback              = filesCallback;
                this.rawJsonCallback            = rawJsonCallback;
                this.streamStoppedCallback      = streamStoppedCallback;

                subscribed_endpoints       = new Dictionary <string, StreamingEndpoint>();
                to_be_subscribed_endpoints = new List <StreamingEndpoint>();

                Dictionary <string, string> headers = new Dictionary <string, string>();

                headers.Add("Authorization", "Bearer " + access_token);


                string url = "https://pnut.io/stream/user?ct=AppNet.Net";

                if (streaming_options != null)
                {
                    if (!string.IsNullOrEmpty(streaming_options.query_string()))
                    {
                        url += "&" + streaming_options.query_string();
                    }
                }

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

                request.Method            = "GET";
                request.AllowAutoRedirect = true;
                request.Accept            = "*/*";
                request.Timeout           = 10000;
                request.KeepAlive         = true;

                request.UserAgent = "AppNet.Net (http://www.nymphicusapp.com/windows/chapper)";

                foreach (KeyValuePair <string, string> additonalHeader in headers)
                {
                    request.Headers.Add(additonalHeader.Key, additonalHeader.Value);
                }

                try
                {
                    return(request.BeginGetResponse(StreamCallback, request));
                }
                catch (WebException e)
                {
                    Console.WriteLine(e.Message);
                    return(null);
                }
            }
Esempio n. 14
0
File: API.cs Progetto: xlgwr/move
 public static extern int TMCC_RegisterStreamCallBack(IntPtr ptr, StreamCallback back, IntPtr context);
 /// <inheritdoc />
 public Task SetStreamsDefaultHandlerAsync(StreamCallback streamHandler,
                                           object userContext) =>
 throw new NotSupportedException("Device client does not support streams");
Esempio n. 16
0
        public PortAudioOutputStream(PaStreamParameters outputParameters, double sampleRate, uint framesPerBuffer, PaStreamFlags streamFlags, StreamCallback streamCallback, object userData)
            : base(outputParameters.sampleFormat, outputParameters.channelCount)
        {
            var gch = userData == null ? default(GCHandle) : GCHandle.Alloc(userData, GCHandleType.Pinned);

            try {
                using (var output = Factory.ToNative <PaStreamParameters> (outputParameters))
                    HandleError(PortAudioInterop.Pa_OpenStream(
                                    out handle,
                                    IntPtr.Zero,
                                    output.Native,
                                    sampleRate,
                                    framesPerBuffer,
                                    streamFlags,
                                    ToPaStreamCallback(streamCallback, true),
                                    userData != null ? gch.AddrOfPinnedObject() : IntPtr.Zero));
            } finally {
                if (userData != null)
                {
                    gch.Free();
                }
            }
        }
Esempio n. 17
0
        public PortAudioOutputStream(int numOutputChannels, PaSampleFormat sampleFormat, double sampleRate, uint framesPerBuffer, StreamCallback streamCallback, object userData)
            : base(sampleFormat, numOutputChannels)
        {
            var gch = userData == null ? default(GCHandle) : GCHandle.Alloc(userData, GCHandleType.Pinned);

            try {
                HandleError(PortAudioInterop.Pa_OpenDefaultStream(
                                out handle,
                                0,
                                numOutputChannels,
                                (IntPtr)sampleFormat,
                                sampleRate,
                                (IntPtr)framesPerBuffer,
                                ToPaStreamCallback(streamCallback, true),
                                userData != null ? gch.AddrOfPinnedObject() : IntPtr.Zero));
            } finally {
                if (userData != null)
                {
                    gch.Free();
                }
            }
        }
Esempio n. 18
0
 public PortAudioInputStream(PaStreamParameters inputParameters, double sampleRate, uint framesPerBuffer, PaStreamFlags streamFlags, StreamCallback streamCallback, IntPtr userData)
     : base(inputParameters.sampleFormat, inputParameters.channelCount)
 {
     using (var input = Factory.ToNative <PaStreamParameters> (inputParameters))
         HandleError(PortAudioInterop.Pa_OpenStream(
                         out handle,
                         input.Native,
                         IntPtr.Zero,
                         sampleRate,
                         framesPerBuffer,
                         streamFlags,
                         ToPaStreamCallback(streamCallback, false),
                         userData
                         ));
 }
Esempio n. 19
0
 internal unsafe PaStreamCallback ToPaStreamCallback(StreamCallback src, bool isOutput)
 {
     return (input, output, frameCount, timeInfo, statusFlags, userData) => {
         var ptr = timeInfo != IntPtr.Zero ? new CppInstancePtr (timeInfo) : default (CppInstancePtr);
         try {
             byte [] buf = buffer != null ? (byte[]) buffer.Target : null;
             var byteCount = FramesToBytes ((uint) frameCount);
             if (buf == null || buf.Length < byteCount) {
                 buf = new byte [byteCount];
                 buffer = new WeakReference (buf);
             }
             var ret = src (
                 buf,
                 0,
                 byteCount,
                 timeInfo != IntPtr.Zero ? Factory.Create<PaStreamCallbackTimeInfo> (ptr) : default (PaStreamCallbackTimeInfo),
                 (PaStreamCallbackFlags) statusFlags,
                 userData
             );
             Marshal.Copy (buf, 0, isOutput ? output : input, byteCount);
             return ret;
         } finally {
             ptr.Dispose ();
         }
     };
 }
Esempio n. 20
0
        /// <summary>
        /// Adds a callback to be called on the host after all currently enqueued items 
        /// in the stream have completed
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="userData">Not supported</param>
        public void EnqueueHostCallback(StreamCallback callback, object userData = null)
        {
            ThrowIfDisposed();
            if (callback == null)
                throw new ArgumentNullException("callback");

            if (callbackHandle.IsAllocated)
                callbackHandle.Free();
            if (userDataHandle.IsAllocated)
                userDataHandle.Free();

            IntPtr userDataPtr = IntPtr.Zero;
            if (userData != null)
            {
                userDataHandle = GCHandle.Alloc(userData);
                userDataPtr = GCHandle.ToIntPtr(userDataHandle);
            }

            callbackInternal = new StreamCallbackInternal(
                delegate(IntPtr rawStream, int status, IntPtr rawUserData)
                {
                    var streamObj = new Stream(rawStream) {IsEnabledDispose = false};
                    var userDataObj = GCHandle.FromIntPtr(rawUserData).Target;
                    callback(streamObj, status, userDataObj);
                });
            callbackHandle = GCHandle.Alloc(callbackInternal);
            IntPtr callbackPtr = Marshal.GetFunctionPointerForDelegate(callbackInternal);

            NativeMethods.cuda_Stream_enqueueHostCallback(
                ptr, callbackPtr, userDataPtr);
        }
Esempio n. 21
0
 public static extern int Attach(int nIndex, char[] sServerIP, int nServerPort, int nServerChannel, StreamCallback pFunc);
 /// <inheritdoc />
 public Task SetStreamHandlerAsync(string streamName, StreamCallback
                                   streamHandler, object userContext)
 {
     throw new NotSupportedException("Test client does not support streams yet");
 }
Esempio n. 23
0
 public PortAudioInputStream(int numInputChannels, PaSampleFormat sampleFormat, double sampleRate, uint framesPerBuffer, StreamCallback streamCallback, IntPtr userData)
     : base(sampleFormat, numInputChannels)
 {
     HandleError(PortAudioInterop.Pa_OpenDefaultStream(
                     out handle,
                     numInputChannels,
                     0,
                     (IntPtr)sampleFormat,
                     sampleRate,
                     (IntPtr)framesPerBuffer,
                     ToPaStreamCallback(streamCallback, false),
                     userData
                     ));
 }