diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Communications/RestClient.cs | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/OpenSim/Framework/Communications/RestClient.cs b/OpenSim/Framework/Communications/RestClient.cs index 6f517b6..ff7cb4d 100644 --- a/OpenSim/Framework/Communications/RestClient.cs +++ b/OpenSim/Framework/Communications/RestClient.cs | |||
@@ -519,4 +519,158 @@ namespace OpenSim.Framework.Communications | |||
519 | 519 | ||
520 | #endregion Async Invocation | 520 | #endregion Async Invocation |
521 | } | 521 | } |
522 | |||
523 | internal class SimpleAsyncResult : IAsyncResult | ||
524 | { | ||
525 | private readonly AsyncCallback m_callback; | ||
526 | |||
527 | /// <summary> | ||
528 | /// Is process completed? | ||
529 | /// </summary> | ||
530 | /// <remarks>Should really be boolean, but VolatileRead has no boolean method</remarks> | ||
531 | private byte m_completed; | ||
532 | |||
533 | /// <summary> | ||
534 | /// Did process complete synchronously? | ||
535 | /// </summary> | ||
536 | /// <remarks>I have a hard time imagining a scenario where this is the case, again, same issue about | ||
537 | /// booleans and VolatileRead as m_completed | ||
538 | /// </remarks> | ||
539 | private byte m_completedSynchronously; | ||
540 | |||
541 | private readonly object m_asyncState; | ||
542 | private ManualResetEvent m_waitHandle; | ||
543 | private Exception m_exception; | ||
544 | |||
545 | internal SimpleAsyncResult(AsyncCallback cb, object state) | ||
546 | { | ||
547 | m_callback = cb; | ||
548 | m_asyncState = state; | ||
549 | m_completed = 0; | ||
550 | m_completedSynchronously = 1; | ||
551 | } | ||
552 | |||
553 | #region IAsyncResult Members | ||
554 | |||
555 | public object AsyncState | ||
556 | { | ||
557 | get { return m_asyncState; } | ||
558 | } | ||
559 | |||
560 | public WaitHandle AsyncWaitHandle | ||
561 | { | ||
562 | get | ||
563 | { | ||
564 | if (m_waitHandle == null) | ||
565 | { | ||
566 | bool done = IsCompleted; | ||
567 | ManualResetEvent mre = new ManualResetEvent(done); | ||
568 | if (Interlocked.CompareExchange(ref m_waitHandle, mre, null) != null) | ||
569 | { | ||
570 | mre.Close(); | ||
571 | } | ||
572 | else | ||
573 | { | ||
574 | if (!done && IsCompleted) | ||
575 | { | ||
576 | m_waitHandle.Set(); | ||
577 | } | ||
578 | } | ||
579 | } | ||
580 | |||
581 | return m_waitHandle; | ||
582 | } | ||
583 | } | ||
584 | |||
585 | |||
586 | public bool CompletedSynchronously | ||
587 | { | ||
588 | get { return Thread.VolatileRead(ref m_completedSynchronously) == 1; } | ||
589 | } | ||
590 | |||
591 | |||
592 | public bool IsCompleted | ||
593 | { | ||
594 | get { return Thread.VolatileRead(ref m_completed) == 1; } | ||
595 | } | ||
596 | |||
597 | #endregion | ||
598 | |||
599 | #region class Methods | ||
600 | |||
601 | internal void SetAsCompleted(bool completedSynchronously) | ||
602 | { | ||
603 | m_completed = 1; | ||
604 | if (completedSynchronously) | ||
605 | m_completedSynchronously = 1; | ||
606 | else | ||
607 | m_completedSynchronously = 0; | ||
608 | |||
609 | SignalCompletion(); | ||
610 | } | ||
611 | |||
612 | internal void HandleException(Exception e, bool completedSynchronously) | ||
613 | { | ||
614 | m_completed = 1; | ||
615 | if (completedSynchronously) | ||
616 | m_completedSynchronously = 1; | ||
617 | else | ||
618 | m_completedSynchronously = 0; | ||
619 | m_exception = e; | ||
620 | |||
621 | SignalCompletion(); | ||
622 | } | ||
623 | |||
624 | private void SignalCompletion() | ||
625 | { | ||
626 | if (m_waitHandle != null) m_waitHandle.Set(); | ||
627 | |||
628 | if (m_callback != null) m_callback(this); | ||
629 | } | ||
630 | |||
631 | public void EndInvoke() | ||
632 | { | ||
633 | // This method assumes that only 1 thread calls EndInvoke | ||
634 | if (!IsCompleted) | ||
635 | { | ||
636 | // If the operation isn't done, wait for it | ||
637 | AsyncWaitHandle.WaitOne(); | ||
638 | AsyncWaitHandle.Close(); | ||
639 | m_waitHandle.Close(); | ||
640 | m_waitHandle = null; // Allow early GC | ||
641 | } | ||
642 | |||
643 | // Operation is done: if an exception occured, throw it | ||
644 | if (m_exception != null) throw m_exception; | ||
645 | } | ||
646 | |||
647 | #endregion | ||
648 | } | ||
649 | |||
650 | internal class AsyncResult<T> : SimpleAsyncResult | ||
651 | { | ||
652 | private T m_result = default(T); | ||
653 | |||
654 | public AsyncResult(AsyncCallback asyncCallback, Object state) : | ||
655 | base(asyncCallback, state) | ||
656 | { | ||
657 | } | ||
658 | |||
659 | public void SetAsCompleted(T result, bool completedSynchronously) | ||
660 | { | ||
661 | // Save the asynchronous operation's result | ||
662 | m_result = result; | ||
663 | |||
664 | // Tell the base class that the operation completed | ||
665 | // sucessfully (no exception) | ||
666 | base.SetAsCompleted(completedSynchronously); | ||
667 | } | ||
668 | |||
669 | public new T EndInvoke() | ||
670 | { | ||
671 | base.EndInvoke(); | ||
672 | return m_result; | ||
673 | } | ||
674 | } | ||
675 | |||
522 | } \ No newline at end of file | 676 | } \ No newline at end of file |