protected override T GetTransaction(Transaction.States state, bool isTransportUnreliable)
        {
            var e = GetServerEventArgs(isTransportUnreliable);

            var transaction = new T();

            if (Transaction.States.Calling != Transaction.States.Trying)
            {
                throw new InvalidProgramException();
            }

            switch (state)
            {
            case Transaction.States.Created:
                break;

            case Transaction.States.Trying:
                //case Transaction.States.Calling:
                transaction.ProccessTransactionUser(false, e);
                break;

            case Transaction.States.Proceeding:
                transaction.ProccessTransactionUser(false, e);
                transaction.ProccessTransport(100);
                break;

            case Transaction.States.Completed:
                if (isTransportUnreliable == false)
                {
                    throw new InvalidProgramException(@"States.Completed state accessable only for Unreable transport");
                }
                transaction.ProccessTransactionUser(false, e);
                transaction.ProccessTransport(300);
                break;

            case Transaction.States.Terminated:
                transaction.ProccessTransactionUser(false, e);
                transaction.ProccessTransportError();
                break;
            }

            if (state != transaction.State)
            {
                throw new InvalidProgramException();
            }

            EventArgsManager.Put(e);

            return(transaction);
        }
Esempio n. 2
0
        public void n8_It_should_start_timer_H_for_Completed_and_timer_G_for_unreliable()
        {
            foreach (var state in GetAllStatesFor(Transaction.States.Proceeding))
            {
                var e           = GetServerEventArgs(state.IsTransportUnreliable);
                var transaction = GetTransaction(state.State, state.IsTransportUnreliable);

                int action = transaction.ProccessTransactionUser(300, e);
                Assert.AreEqual(Transaction.Action.StartTimerH, action & Transaction.Action.StartTimerH);
                Assert.AreEqual(state.IsTransportUnreliable ? Transaction.Action.StartTimerG1 : 0, action & Transaction.Action.StartTimerG1);

                EventArgsManager.Put(e);
            }
        }
 public void nF_It_should_send_initial_Request()
 {
     {
         var e      = GetServerEventArgs(false);
         int action = new InviteClientTransaction().ProccessTransactionUser(false, e);
         Assert.AreEqual(Transaction.Action.SendOutgoingMessage, action & Transaction.Action.SendOutgoingMessage);
         EventArgsManager.Put(e);
     }
     {
         var e      = GetServerEventArgs(true);
         int action = new InviteClientTransaction().ProccessTransactionUser(false, e);
         Assert.AreEqual(Transaction.Action.SendOutgoingMessage, action & Transaction.Action.SendOutgoingMessage);
         EventArgsManager.Put(e);
     }
 }
Esempio n. 4
0
        public void nD_It_should_NOT_send_response()
        {
            for (int i = 100; i <= 699; i++)
            {
                foreach (var state in GetAllStatesExcept(Transaction.States.Proceeding))
                {
                    var e           = GetServerEventArgs(state.IsTransportUnreliable);
                    var transaction = GetTransaction(state.State, state.IsTransportUnreliable);

                    int action = transaction.ProccessTransactionUser(i, e);
                    Assert.AreEqual(Transaction.Action.SendOutgoingMessage, action & Transaction.Action.SendOutgoingMessage);

                    EventArgsManager.Put(e);
                }
            }
        }
Esempio n. 5
0
        protected override InviteServerTransaction GetTransaction(Transaction.States state, bool isTransportUnreliable)
        {
            var e = GetServerEventArgs(isTransportUnreliable);

            var transaction = new InviteServerTransaction();

            switch (state)
            {
            case Transaction.States.Created:
                break;

            case Transaction.States.Proceeding:
                transaction.ProccessTransport(false);
                break;

            case Transaction.States.Completed:
                transaction.ProccessTransport(false);
                transaction.ProccessTransactionUser(300, e);
                break;

            case Transaction.States.Confirmed:
                if (isTransportUnreliable == false)
                {
                    throw new InvalidProgramException(@"States.Completed state accessable only for Unreable transport");
                }
                transaction.ProccessTransport(false);
                transaction.ProccessTransactionUser(300, e);
                transaction.ProccessTransport(true);
                break;

            case Transaction.States.Terminated:
                transaction.ProccessTransport(false);
                transaction.ProccessTransportError();
                break;
            }

            if (state != transaction.State)
            {
                throw new InvalidProgramException("GetTransaction can not goto desired state");
            }

            EventArgsManager.Put(e);

            return(transaction);
        }
        public void n6_It_should_request_timer_A_for_unreliable_transport_and_timer_B_for_any()
        {
            var reliable = new InviteClientTransaction();
            var e1       = GetServerEventArgs(false);
            int action1  = reliable.ProccessTransactionUser(false, e1);

            Assert.AreEqual(Transaction.Action.StartTimerB, action1 & Transaction.Action.StartTimerB);
            Assert.AreEqual(0, action1 & Transaction.Action.StartTimerA1);
            EventArgsManager.Put(ref e1);

            var unreliable = new InviteClientTransaction();
            var e2         = GetServerEventArgs(true);
            int action2    = unreliable.ProccessTransactionUser(false, e2);

            Assert.AreEqual(Transaction.Action.StartTimerB, action2 & Transaction.Action.StartTimerB);
            Assert.AreEqual(Transaction.Action.StartTimerA1, action2 & Transaction.Action.StartTimerA1);
            EventArgsManager.Put(ref e2);
        }
Esempio n. 7
0
        public void n6_It_should_request_timer_E_for_unreliable_transport_and_timer_F_for_any()
        {
            int timers = Transaction.Action.StartTimerF | Transaction.Action.StartTimerE1;

            var reliable = new NonInviteClientTransaction();
            var e1       = GetServerEventArgs(false);
            int action1  = reliable.ProccessTransactionUser(false, e1);

            Assert.AreEqual(Transaction.Action.StartTimerF, action1 & timers);
            EventArgsManager.Put(ref e1);

            var unreliable = new NonInviteClientTransaction();
            var e2         = GetServerEventArgs(true);
            int action2    = unreliable.ProccessTransactionUser(false, e2);

            Assert.AreEqual(timers, action2 & timers);
            EventArgsManager.Put(ref e2);
        }
Esempio n. 8
0
        private void ValidateWebsocket(IEnumerable <int> splitSizes, int messagesCount)
        {
            var ping1   = new byte[] { 1, 2, 3, 4, 5, 6, 7, };
            var ping2   = new byte[] { 8, 9, 10 };
            var ping3   = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18 };
            var message = Encoding.UTF8.GetBytes("REGISTER sip:officesip.local SIP/2.0\r\n\r\n");

            var stream = CreateStream(
                messagesCount,
                PrepareForWebsocket(ping1, Opcodes.Ping), ping1,
                PrepareForWebsocket(ping2, Opcodes.Ping), ping2,
                PrepareForWebsocket(ping3, Opcodes.Ping), ping3,
                PrepareForWebsocket(message, Opcodes.Binary), message
                );

            Unmask(ping1);
            Unmask(ping2);
            Unmask(ping3);
            Unmask(message);

            ServerAsyncEventArgs e = null;

            using (var connection = new Connection())
            {
                connection.UpgradeToWebsocket();

                foreach (int splitSize in splitSizes)
                {
                    int realMessageCount = 0;

                    for (int offset = 0; offset < stream.Length; offset += splitSize)
                    {
                        var info = "Split by: " + splitSize + "; Message #: " + realMessageCount;

                        if (e == null)
                        {
                            e = EventArgsManager.Get();
                        }

                        e.BytesTransferred = Math.Min(splitSize, stream.Length - offset);
                        Buffer.BlockCopy(stream, offset, e.Buffer, e.Offset, e.BytesTransferred);

                        bool closeConnection;
                        for (bool repeat = true; repeat;)
                        {
                            repeat = connection.Proccess(ref e, out closeConnection);

                            Assert.IsFalse(closeConnection, info);

                            if (connection.IsMessageReady)
                            {
                                switch (realMessageCount % 4)
                                {
                                case 0:
                                    Assert.AreEqual(Opcodes.Ping, connection.WebSocketHeader.Opcode);
                                    AreEqual(ping1, connection.Content);
                                    break;

                                case 1:
                                    Assert.AreEqual(Opcodes.Ping, connection.WebSocketHeader.Opcode);
                                    AreEqual(ping2, connection.Content);
                                    break;

                                case 2:
                                    Assert.AreEqual(Opcodes.Ping, connection.WebSocketHeader.Opcode);
                                    AreEqual(ping3, connection.Content);
                                    break;

                                case 3:
                                    Assert.AreEqual(Opcodes.Binary, connection.WebSocketHeader.Opcode);
                                    AreEqual(message, connection.Header);
                                    break;
                                }

                                connection.ResetState();
                                realMessageCount++;
                            }
                        }
                    }

                    EventArgsManager.Put(ref e);

                    Assert.AreEqual(messagesCount * 4, realMessageCount);
                }
            }
        }
Esempio n. 9
0
            public ValidateClass(string httpMessage, string sipMessage, int messagesCount, IEnumerable <int> splitSizes)
            {
                if (httpMessage != null)
                {
                    ParseHtppMessage(httpMessage);
                }
                if (sipMessage != null)
                {
                    ParseSipMessage(sipMessage);
                }

                var stream = CreateStream(messagesCount, httpMessage, sipMessage);

                int oldUsedBuffers = EventArgsManager.Created - EventArgsManager.Queued;

                ServerAsyncEventArgs e = null;

                using (var connection = new Connection())
                {
                    foreach (int splitSize in splitSizes)
                    {
                        int realMessageCount = 0;

                        for (int offset = 0; offset < stream.Length; offset += splitSize)
                        {
                            var details = string.Format("Split by: {0}; Message #: {1}", splitSize, realMessageCount);

                            if (e == null)
                            {
                                e = EventArgsManager.Get();
                            }

                            e.BytesTransferred = Math.Min(splitSize, stream.Length - offset);
                            Buffer.BlockCopy(stream, offset, e.Buffer, e.Offset, e.BytesTransferred);

                            bool closeConnection;
                            for (bool repeat = true; repeat;)
                            {
                                repeat = connection.Proccess(ref e, out closeConnection);

                                Assert.IsFalse(closeConnection, details);

                                if (connection.IsMessageReady)
                                {
                                    if (httpMessage != null)
                                    {
                                        ValidateHttp(connection, details);
                                    }
                                    if (sipMessage != null)
                                    {
                                        ValidateSip(connection);
                                    }

                                    connection.ResetState();
                                    realMessageCount++;
                                }
                            }
                        }

                        EventArgsManager.Put(ref e);

                        Assert.AreEqual(messagesCount, realMessageCount);
                    }
                }

                Assert.AreEqual(oldUsedBuffers, EventArgsManager.Created - EventArgsManager.Queued);
            }
Esempio n. 10
0
        private void Validate(string messageText, bool addKeepAlive, int messagesCount, IEnumerable <int> splitSizes, bool detachBuffer, bool websocket)
        {
            var message = Encoding.UTF8.GetBytes(messageText);

            var expected = new SipMessageReader();

            expected.SetDefaultValue();
            int parsed = expected.Parse(message, 0, message.Length);

            expected.SetArray(message);
            if (expected.ContentLength < 0)
            {
                expected.ContentLength = 0;
            }
            var expectedUri = expected.RequestUri.Value.ToString();
            var content     = Encoding.UTF8.GetString(message, parsed, message.Length - parsed);

            if (addKeepAlive)
            {
                message = Encoding.UTF8.GetBytes("\r\n\r\n" + messageText + "\r\n\r\n\r\n\r\n\r\n\r\n");
            }

            var extra = new byte[0];

            if (websocket)
            {
                extra = PrepareForWebsocket(message);
            }

            var stream        = CreateStream(messagesCount, extra, message);
            int headersLength = messageText.IndexOf("\r\n\r\n") + 4;
            var headersText   = messageText.Substring(0, headersLength);

            int oldUsedBuffers = EventArgsManager.Created - EventArgsManager.Queued;

            ServerAsyncEventArgs e = null;

            using (var connection = new Connection())
                using (var threads = new Threads(connection))
                {
                    if (websocket)
                    {
                        connection.UpgradeToWebsocket();
                    }

                    foreach (int splitSize in splitSizes)
                    {
                        int realMessageCount = 0;

                        for (int offset = 0; offset < stream.Length; offset += splitSize)
                        {
                            var info = "Split by: " + splitSize + "; Message #: " + realMessageCount +
                                       "\r\nMessage Sizes: " + message.Length + " " + (message.Length - content.Length) + " " + content.Length;

                            if (e == null)
                            {
                                e = EventArgsManager.Get();
                            }

                            e.BytesTransferred = Math.Min(splitSize, stream.Length - offset);
                            Buffer.BlockCopy(stream, offset, e.Buffer, e.Offset, e.BytesTransferred);

                            bool closeConnection;
                            for (bool repeat = true; repeat;)
                            {
                                //repeat = connection.Proccess(ref e, out closeConnection);
                                repeat = threads.Proccess(ref e, out closeConnection);

                                Assert.IsFalse(closeConnection, info);

                                if (connection.IsMessageReady)
                                {
                                    var actualHeaders = Encoding.UTF8.GetString(connection.Header.Array, connection.Header.Offset, connection.Header.Count);

                                    Assert.AreEqual(expected.Method, connection.Reader.Method, info);
                                    Assert.AreEqual(expectedUri, connection.Reader.RequestUri.Value.ToString(), info);

                                    Assert.AreEqual(headersLength, connection.Header.Count);
                                    Assert.AreEqual(headersText, actualHeaders);

                                    Assert.AreEqual(expected.ContentLength, connection.Content.Count);
                                    if (expected.ContentLength > 0)
                                    {
                                        Assert.AreEqual(content, Encoding.UTF8.GetString(connection.Content.Array, connection.Content.Offset, connection.Content.Count), info);
                                    }

                                    BufferHandle handle = new BufferHandle();
                                    if (detachBuffer)
                                    {
                                        handle = connection.Dettach(ref e);
                                    }

                                    connection.ResetState();
                                    realMessageCount++;

                                    if (detachBuffer)
                                    {
                                        handle.Free();
                                    }
                                }
                            }
                        }

                        EventArgsManager.Put(ref e);

                        Assert.AreEqual(messagesCount, realMessageCount);
                    }
                }

            Assert.AreEqual(oldUsedBuffers, EventArgsManager.Created - EventArgsManager.Queued);
        }