示例#1
0
        private async Task                    _sendMessage(SMPPMessage message)
        {
            using (await _sendLock.Enter()) {
                if (!_isRunning)
                {
                    throw new SMPPException("Connection is down.");
                }

                PduWriter writer = new PduWriter();

                if ((message.Command & CommandSet.Response) == 0)
                {
                    lock (this) {
                        message.Sequence = _sequence;
                        _sequence        = (_sequence < 1000000000) ? _sequence + 1 : 1;
                    }
                }

                writer.WriteMessage(message);

                byte[] pduData = writer.PduData();
#if DEBUG
                Debug.WriteLine("SMPPConnection: SendMessage size=" + pduData.Length + " id=" + message.Command + " status=" + message.Status + " seq=" + message.Sequence);
#endif
                try {
                    await _stream.WriteAsync(pduData, 0, pduData.Length);
                }
                catch (Exception err) {
                    throw _setFailed(new SMPPException("Send data to SMPP server failed", err));
                }
            }
        }
        public async Task        LockedTimeout()
        {
            using (var x = new TaskLock()) {
                using (await x.Enter()) {
                    var start = DateTime.UtcNow;

                    try {
                        using (await x.Enter(2000)) {
                            Assert.Fail();
                        }
                    }
                    catch (TimeoutException) {
                        var time = (DateTime.UtcNow - start).Ticks;

                        Assert.IsTrue((TimeSpan.TicksPerMillisecond * 1900) <= time && time <= (TimeSpan.TicksPerMillisecond * 2100));
                    }
                }
            }
        }
        public async Task        LockedCancellation()
        {
            using (var x = new TaskLock()) {
                using (await x.Enter()) {
                    var start = DateTime.UtcNow;

                    try {
                        using (var cts = new CancellationTokenSource(2000)) {
                            using (await x.Enter(cts.Token)) {
                                Assert.Fail();
                            }
                        }
                    }
                    catch (OperationCanceledException) {
                        var time = (DateTime.UtcNow - start).Ticks;
                        Assert.IsTrue((TimeSpan.TicksPerMillisecond * 1900) <= time && time <= (TimeSpan.TicksPerMillisecond * 2100));
                    }
                }
            }
        }
            private async Task            _test()
            {
                using (await _taskLock.Enter()) {
                    lock (_lockObject) {
                        _count++;
                        if (_n++ != 0)
                        {
                            Assert.Fail();
                        }
                    }

                    await Task.Yield();

                    lock (_lockObject) {
                        _n--;
                    }
                }
            }