| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 
 | //------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Channels
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime;
    using System.Threading;
    partial class PeerNodeImplementation
    {
        // A simple state manager for the PeerNode. Unlike the state managers used for channels and other
        // classes, a PeerNode's Open/Close is counted, a PeerNode is re-openable, and Abort only
        // takes effect if the outstanding number of Opens is 1.
        // The PeerNode defers to this object for all state related operations.
        //
        // Whenever a call is made that may change the state of the object (openCount transitions between 0 and 1),
        // an operation is queued. When an operation is removed from the queue, if the target state is still the
        // same as the operation (e.g. openCount > 0 and operation == Open) and the object is not already in that
        // state, the operation is performed by calling back into the PeerNode
        //
        // Because each operation is pulled form the queue one at a time, the open and close of the
        // PeerNode is serialized
        class SimpleStateManager
        {
            internal enum State { NotOpened, Opening, Opened, Closing };
            State currentState = State.NotOpened;
            object thisLock = new object();
            Queue<IOperation> queue = new Queue<IOperation>();
            bool queueRunning;
            int openCount;
            PeerNodeImplementation peerNode;
            public SimpleStateManager(PeerNodeImplementation peerNode)
            {
                this.peerNode = peerNode;
            }
            object ThisLock
            {
                get { return thisLock; }
            }
            public void Abort()
            {
                lock (ThisLock)
                {
                    bool runAbort = false;
                    if (openCount <= 1 && currentState != State.NotOpened)
                    {
                        runAbort = true;
                    }
                    if (openCount > 0)
                    {
                        --openCount;
                    }
                    if (runAbort)
                    {
                        try
                        {
                            peerNode.OnAbort();
                        }
                        finally
                        {
                            currentState = State.NotOpened;
                        }
                    }
                }
            }
            public IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state)
            {
                CloseOperation op = null;
                lock (ThisLock)
                {
                    if (openCount > 0)
                    {
                        --openCount;
                    }
                    if (openCount > 0)
                    {
                        return new CompletedAsyncResult(callback, state);
                    }
                    else
                    {
                        op = new CloseOperation(this, peerNode, timeout, callback, state);
                        queue.Enqueue(op);
                        RunQueue();
                    }
                }
                return op;
            }
            public IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state, bool waitForOnline)
            {
                bool completedSynchronously = false;
                OpenOperation op = null;
                lock (ThisLock)
                {
                    openCount++;
                    if (openCount > 1 && currentState == State.Opened)
                    {
                        completedSynchronously = true;
                    }
                    else
                    {
                        op = new OpenOperation(this, peerNode, timeout, callback, state, waitForOnline);
                        queue.Enqueue(op);
                        RunQueue();
                    }
                }
                if (completedSynchronously)
                {
                    return new CompletedAsyncResult(callback, state);
                }
                return op;
            }
            public void Close(TimeSpan timeout)
            {
                EndClose(BeginClose(timeout, null, null));
            }
            public static void EndOpen(IAsyncResult result)
            {
                // result can be either an OpenOperation or a CompletedAsyncResult
                if (result is CompletedAsyncResult)
                    CompletedAsyncResult.End(result);
                else
                    OpenOperation.End(result);
            }
            public static void EndClose(IAsyncResult result)
            {
                // result can be either an CloseOperation or a CompletedAsyncResult
                if (result is CompletedAsyncResult)
                    CompletedAsyncResult.End(result);
                else
                    CloseOperation.End(result);
            }
            // Process IP Address change event from IP helper
            public void OnIPAddressesChanged(object sender, EventArgs e)
            {
                IPAddressChangeOperation op = null;
                lock (ThisLock)
                {
                    op = new IPAddressChangeOperation(peerNode);
                    queue.Enqueue(op);
                    RunQueue();
                }
            }
            public void Open(TimeSpan timeout, bool waitForOnline)
            {
                EndOpen(BeginOpen(timeout, null, null, waitForOnline));
            }
            // Start running operations from the queue (must be called within lock)
            void RunQueue()
            {
                if (queueRunning)
                    return;
                queueRunning = true;
                ActionItem.Schedule(new Action<object>(RunQueueCallback), null);
            }
            void RunQueueCallback(object state)
            {
                IOperation op;
                // remove an operation from the queue
                lock (ThisLock)
                {
                    Fx.Assert(queue.Count > 0, "queue should not be empty");
                    op = queue.Dequeue();
                }
                try
                {
                    // execute the operation
                    op.Run();
                }
                finally
                {
                    lock (ThisLock)
                    {
                        // if there are still pending operations, schedule another thread
                        if (queue.Count > 0)
                        {
                            try
                            {
                                ActionItem.Schedule(new Action<object>(RunQueueCallback), null);
                            }
                            catch (Exception e)
                            {
                                if (Fx.IsFatal(e)) throw;
                                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                            }
                        }
                        else
                        {
                            queueRunning = false;
                        }
                    }
                }
            }
            interface IOperation
            {
                void Run();
            }
            class CloseOperation : OperationBase
            {
                PeerNodeImplementation peerNode;
                public CloseOperation(SimpleStateManager stateManager,
                    PeerNodeImplementation peerNode, TimeSpan timeout, AsyncCallback callback, object state)
                    : base(stateManager, timeout, callback, state)
                {
                    this.peerNode = peerNode;
                }
                protected override void Run()
                {
                    Exception lclException = null;
                    try
                    {
                        lock (ThisLock)
                        {
                            if (stateManager.openCount > 0)
                            {
                                // the current target state is no longer Closed
                                invokeOperation = false;
                            }
                            else if (stateManager.currentState == State.NotOpened)
                            {
                                // the state is already Closed
                                invokeOperation = false;
                            }
                            else if (timeoutHelper.RemainingTime() <= TimeSpan.Zero)
                            {
                                // Time out has already happened complete will be taken care of in the 
                                // OperationBase class
                                invokeOperation = false;
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException());
                            }
                            else
                            {
                                // the PeerNode needs to be closed
                                if (!(stateManager.currentState != State.Opening && stateManager.currentState != State.Closing))
                                {
                                    throw Fx.AssertAndThrow("Open and close are serialized by queue We should not be either in Closing or Opening state at this point");
                                }
                                if (stateManager.currentState != State.NotOpened)
                                {
                                    stateManager.currentState = State.Closing;
                                    invokeOperation = true;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        lclException = e;
                    }
                    if (invokeOperation)
                    {
                        try
                        {
                            peerNode.OnClose(timeoutHelper.RemainingTime());
                        }
                        catch (Exception e)
                        {
                            if (Fx.IsFatal(e)) throw;
                            DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                            lclException = e;
                        }
                        lock (ThisLock)
                        {
                            stateManager.currentState = State.NotOpened;
                        }
                    }
                    Complete(lclException);
                }
            }
            class OpenOperation : OperationBase
            {
                PeerNodeImplementation peerNode;
                bool waitForOnline;
                public OpenOperation(SimpleStateManager stateManager, PeerNodeImplementation peerNode, TimeSpan timeout,
                    AsyncCallback callback, object state, bool waitForOnline)
                    : base(stateManager, timeout, callback, state)
                {
                    this.peerNode = peerNode;
                    this.waitForOnline = waitForOnline;
                }
                protected override void Run()
                {
                    Exception lclException = null;
                    try
                    {
                        lock (ThisLock)
                        {
                            if (stateManager.openCount < 1)
                            {
                                // the current target state is no longer Opened
                                invokeOperation = false;
                            }
                            else if (stateManager.currentState == State.Opened)
                            {
                                // the state is already Opened
                                invokeOperation = false;
                            }
                            else if (timeoutHelper.RemainingTime() <= TimeSpan.Zero)
                            {
                                // Time out has already happened complete will be taken care of in the 
                                // OperationBase class
                                invokeOperation = false;
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException());
                            }
                            else
                            {
                                // the PeerNode needs to be opened
                                if (!(stateManager.currentState != State.Opening && stateManager.currentState != State.Closing))
                                {
                                    throw Fx.AssertAndThrow("Open and close are serialized by queue We should not be either in Closing or Opening state at this point");
                                }
                                if (stateManager.currentState != State.Opened)
                                {
                                    stateManager.currentState = State.Opening;
                                    invokeOperation = true;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        lclException = e;
                    }
                    if (invokeOperation)
                    {
                        try
                        {
                            peerNode.OnOpen(timeoutHelper.RemainingTime(), waitForOnline);
                            lock (ThisLock)
                            {
                                stateManager.currentState = State.Opened;
                            }
                        }
                        catch (Exception e)
                        {
                            if (Fx.IsFatal(e)) throw;
                            lock (ThisLock)
                            {
                                stateManager.currentState = State.NotOpened;
                                // since Open is throwing, we roll back the openCount because a matching Close is not
                                // expected
                                stateManager.openCount--;
                            }
                            lclException = e;
                            DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        }
                    }
                    Complete(lclException);
                }
            }
            // Base class for Open and Cose
            abstract class OperationBase : AsyncResult, IOperation
            {
                protected SimpleStateManager stateManager;
                protected TimeoutHelper timeoutHelper;
                AsyncCallback callback;
                protected bool invokeOperation;
                
                // Double-checked locking pattern requires volatile for read/write synchronization
                volatile bool completed;
                public OperationBase(SimpleStateManager stateManager, TimeSpan timeout,
                    AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.stateManager = stateManager;
                    timeoutHelper = new TimeoutHelper(timeout);
                    this.callback = callback;
                    invokeOperation = false;
                    completed = false;
                }
                void AsyncComplete(object o)
                {
                    try
                    {
                        base.Complete(false, (Exception)o);
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(SR.GetString(SR.AsyncCallbackException), e);
                    }
                }
                protected abstract void Run();
                void IOperation.Run()
                {
                    Run();
                }
                protected void Complete(Exception exception)
                {
                    if (completed)
                    {
                        return;
                    }
                    lock (ThisLock)
                    {
                        if (completed)
                        {
                            return;
                        }
                        completed = true;
                    }
                    try
                    {
                        if (callback != null)
                        {
                            // complete the AsyncResult on a separate thread so that the queue can progress.
                            // this prevents a deadlock when the callback attempts to call Close.
                            // this may cause the callbacks to be called in a differnet order in which they completed, but that
                            // is ok because each callback is associated with a different object (channel or listener factory)
                            ActionItem.Schedule(new Action<object>(AsyncComplete), exception);
                        }
                        else
                        {
                            AsyncComplete(exception);
                        }
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(SR.GetString(SR.MessagePropagationException), e);
                    }
                }
                protected object ThisLock
                {
                    get { return stateManager.thisLock; }
                }
                static public void End(IAsyncResult result)
                {
                    AsyncResult.End<OperationBase>(result);
                }
            }
            // To serialize IP address change processing
            class IPAddressChangeOperation : IOperation
            {
                PeerNodeImplementation peerNode;
                public IPAddressChangeOperation(PeerNodeImplementation peerNode)
                {
                    this.peerNode = peerNode;
                }
                void IOperation.Run()
                {
                    peerNode.OnIPAddressChange();
                }
            }
        }
    }
}
 |