Ejemplo n.º 1
0
        public IAsyncResult BeginSend(MARC.Everest.Interfaces.IGraphable data, IReceiveResult correlate, AsyncCallback callback, object state)
        {
            // Formatter check
            if (!IsOpen())
            {
                throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen);
            }

            // Setup wroker
            Worker w = new Worker();

            // Create a new instance of the formatter
            w.Formatter = (IXmlStructureFormatter)Formatter;
            var cresult = correlate as WcfReceiveResult;

            w.MessageId       = (correlate as WcfReceiveResult).MessageIdentifier;
            w.MessageVersion  = (correlate as WcfReceiveResult).MessageVersion;
            w.InvalidResponse = InvalidResponse;
            w.ResponseHeaders = cresult.ResponseHeaders;

            // Set async result
            IAsyncResult Result = new SendResultAsyncResult(state, new AutoResetEvent(false));

            // Completed delegate
            w.Completed += delegate(object Sender)
            {
                Worker sWorker = Sender as Worker; // Strong type sender
                // Lookup the result in the dictionary
                if (!asyncResults.ContainsKey(Result))
                {
                    lock (asyncResults) { asyncResults.Add(Result, sWorker.SendResult); }
                }
                (Result as SendResultAsyncResult).SetComplete();  // Set completed
                (Result.AsyncWaitHandle as AutoResetEvent).Set(); // send signal
                if (callback != null)
                {
                    callback(Result);                   // callback
                }
            };

            // Add to thread pool
            ThreadPool.QueueUserWorkItem(new WaitCallback(w.WorkSend), data);

            return(Result);
        }
Ejemplo n.º 2
0
        public IAsyncResult BeginSend(MARC.Everest.Interfaces.IGraphable data, AsyncCallback callback, object state)
        {
            // Formatter check
            if (!IsOpen())
            {
                throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen);
            }

            // Create the work that will perform the operations
            Worker w = new Worker();

            w.Formatter = (IXmlStructureFormatter)Formatter;

#if !WINDOWS_PHONE
            w.WcfConfiguration = wcfConfiguration;
#endif
            w.MessageVersion = wcfClient.Endpoint.Binding.MessageVersion;

            // Result
            IAsyncResult result = new SendResultAsyncResult(state, new AutoResetEvent(false));

            // Work
            w.Completed += new WaitCallback(delegate(object sender)
            {
                lock (results)
                    results.Add((sender as Worker).SendResult, wcfClient.ProcessInboundMessage((sender as Worker).SendResult.Message));
                lock (asyncResults)
                    asyncResults.Add(result, (sender as Worker).SendResult);
                (result.AsyncWaitHandle as AutoResetEvent).Set();
                if (callback != null)
                {
                    callback(result);
                }
            });

            ThreadPool.QueueUserWorkItem(w.WorkSend, data);

            // Return the result
            return(result);
        }
        /// <summary>
        /// Starts an asynchronous send to the file system.
        /// </summary>
        /// <param name="data">The <see cref="MARC.Everest.Interfaces.IGraphable"/> data to be sent.</param>
        /// <param name="callback">The callback to execute when the send is complete.</param>
        /// <param name="state">An object representing state.</param>
        /// <returns>An instance of a callback pointer.</returns>
        /// <example>
        /// Sending an instance asynchronously using a wait handle
        /// <code>
        /// IAsyncResult sendResult = conn.BeginSend(instance, null, null);
        /// sendResult.AsyncWaitHandle.WaitOne(); // Wait for send to finish
        /// FileSendResult result = conn.EndSend(sendResult);
        /// if(result.Code != ResultCode.Accepted) // Result was not successful
        ///     foreach(IResultDetail dtl in result.Details)
        ///         Console.WriteLine(dtl.Message);
        /// </code>
        /// </example>
        /// <seealso cref="Send"/>
        public IAsyncResult BeginSend(MARC.Everest.Interfaces.IGraphable data, AsyncCallback callback, object state)
        {
            if (!IsOpen())
            {
                throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen);
            }

            // Setup wroker
            Worker w = new Worker();

            // Create a new instance of the formatter
            w.Formatter  = Formatter.Clone() as IStructureFormatter;
            w.TargetFile = GenerateTargetFile(data);

            // Set async result
            IAsyncResult Result = new SendResultAsyncResult(state, new AutoResetEvent(false));

            // Completed delegate
            w.Completed += delegate(object Sender)
            {
                Worker sWorker = Sender as Worker; // Strong type sender
                // Lookup the result in the dictionary
                if (!asyncResults.ContainsKey(Result))
                {
                    lock (asyncResults) { asyncResults.Add(Result, sWorker.Result); }
                }
                (Result as SendResultAsyncResult).SetComplete();  // Set completed
                (Result.AsyncWaitHandle as AutoResetEvent).Set(); // send signal
                if (callback != null)
                {
                    callback(Result);                   // callback
                }
            };

            // Add to thread pool
            ThreadPool.QueueUserWorkItem(new WaitCallback(w.Work), data);

            return(Result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Asynchronous send operation
        /// </summary>
        /// <param name="data">The data being sent</param>
        /// <param name="callback">A callback to execute when the method is complete</param>
        /// <param name="state">A user state</param>
        /// <returns>An IAsyncResult representing the asynchronous operation</returns>
        public IAsyncResult BeginSend(MARC.Everest.Interfaces.IGraphable data, AsyncCallback callback, object state)
        {
            // Good practice to check if the connector is open
            if (!IsOpen())
            {
                throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen, null);
            }

            // Create a new worker
            Worker w = new Worker();

            // Always clone the current formatter
            w.Formatter = (IStructureFormatter)Formatter.Clone();

            // Create the async result
            IAsyncResult result = new SendResultAsyncResult(state, new AutoResetEvent(false));

            // Set the callback
            w.Completed += new WaitCallback(delegate(object sender)
            {
                Worker sWorker = sender as Worker;

                // Set the result in the dictionary
                lock (this.results)
                    this.results.Add(result, sWorker.Result);

                // Notify any listeners
                (result.AsyncWaitHandle as AutoResetEvent).Set();
                if (callback != null)
                {
                    callback(result);
                }
            });

            // Execute
            ThreadPool.QueueUserWorkItem(w.Work, data);

            return(result);
        }
Ejemplo n.º 5
0
        public IAsyncResult BeginSend(MARC.Everest.Interfaces.IGraphable data, IReceiveResult correlate, AsyncCallback callback, object state)
        {
            // Formatter check
            if (!IsOpen())
                throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen);

            // Setup wroker
            Worker w = new Worker();

            // Create a new instance of the formatter
            w.Formatter = (IXmlStructureFormatter)Formatter;
            var cresult = correlate as WcfReceiveResult;
            w.MessageId = (correlate as WcfReceiveResult).MessageIdentifier;
            w.MessageVersion = (correlate as WcfReceiveResult).MessageVersion;
            w.InvalidResponse = InvalidResponse;
            w.ResponseHeaders = cresult.ResponseHeaders;

            // Set async result
            IAsyncResult Result = new SendResultAsyncResult(state, new AutoResetEvent(false));

            // Completed delegate
            w.Completed += delegate(object Sender)
            {
                Worker sWorker = Sender as Worker; // Strong type sender
                // Lookup the result in the dictionary
                if (!asyncResults.ContainsKey(Result))
                    lock (asyncResults) { asyncResults.Add(Result, sWorker.SendResult); }
                (Result as SendResultAsyncResult).SetComplete(); // Set completed
                (Result.AsyncWaitHandle as AutoResetEvent).Set(); // send signal
                if (callback != null) callback(Result); // callback
            };

            // Add to thread pool
            ThreadPool.QueueUserWorkItem(new WaitCallback(w.WorkSend), data);

            return Result;
        }
Ejemplo n.º 6
0
        public IAsyncResult BeginSend(MARC.Everest.Interfaces.IGraphable data, AsyncCallback callback, object state)
        {
            // Formatter check
            if (!IsOpen())
                throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen);

            // Create the work that will perform the operations
            Worker w = new Worker();
            w.Formatter = (IXmlStructureFormatter)Formatter;

#if !WINDOWS_PHONE
            w.WcfConfiguration = wcfConfiguration;
#endif
            w.MessageVersion = wcfClient.Endpoint.Binding.MessageVersion;

            // Result
            IAsyncResult result = new SendResultAsyncResult(state, new AutoResetEvent(false));

            // Work
            w.Completed += new WaitCallback(delegate(object sender)
                {
                    lock (results)
                        results.Add((sender as Worker).SendResult, wcfClient.ProcessInboundMessage((sender as Worker).SendResult.Message));
                    lock (asyncResults)
                        asyncResults.Add(result, (sender as Worker).SendResult);
                    (result.AsyncWaitHandle as AutoResetEvent).Set();
                    if (callback != null)
                        callback(result);
                });

            ThreadPool.QueueUserWorkItem(w.WorkSend, data);

            // Return the result
            return result;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Starts an asynchronous send to the file system.
        /// </summary>
        /// <param name="data">The <see cref="MARC.Everest.Interfaces.IGraphable"/> data to be sent.</param>
        /// <param name="callback">The callback to execute when the send is complete.</param>
        /// <param name="state">An object representing state.</param>
        /// <returns>An instance of a callback pointer.</returns>
        /// <example>
        /// Sending an instance asynchronously using a wait handle
        /// <code>
        /// IAsyncResult sendResult = conn.BeginSend(instance, null, null);
        /// sendResult.AsyncWaitHandle.WaitOne(); // Wait for send to finish
        /// FileSendResult result = conn.EndSend(sendResult);
        /// if(result.Code != ResultCode.Accepted) // Result was not successful
        ///     foreach(IResultDetail dtl in result.Details)
        ///         Console.WriteLine(dtl.Message);
        /// </code>
        /// </example>
        /// <seealso cref="Send"/>
        public IAsyncResult BeginSend(MARC.Everest.Interfaces.IGraphable data, AsyncCallback callback, object state)
        {
            if (!IsOpen())
                throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen);

            // Setup wroker
            Worker w = new Worker();
            // Create a new instance of the formatter
            w.Formatter = Formatter.Clone() as IStructureFormatter;
            w.TargetFile = GenerateTargetFile(data);
            
            // Set async result
            IAsyncResult Result = new SendResultAsyncResult(state, new AutoResetEvent(false));

            // Completed delegate
            w.Completed += delegate(object Sender)
            {
                Worker sWorker = Sender as Worker; // Strong type sender
                // Lookup the result in the dictionary
                if (!asyncResults.ContainsKey(Result))
                    lock (asyncResults) { asyncResults.Add(Result, sWorker.Result); }
                (Result as SendResultAsyncResult).SetComplete(); // Set completed
                (Result.AsyncWaitHandle as AutoResetEvent).Set(); // send signal
                if (callback != null) callback(Result); // callback
            };

            // Add to thread pool
            ThreadPool.QueueUserWorkItem(new WaitCallback(w.Work), data);

            return Result;
        }