Esempio n. 1
0
        private bool OnReceived()
        {
            bool closeConnection = false;

            switch (e.LocalEndPoint.Protocol)
            {
            case ServerProtocol.Udp:
            {
                if (readerUdp == null)
                {
                    readerUdp = new SipMessageReader();
                }

                readerUdp.SetDefaultValue();

                int parsed = readerUdp.Parse(e.Buffer, e.Offset, e.BytesTransferred);

                if (readerUdp.IsFinal)
                {
                    if (readerUdp.HasContentLength == false)
                    {
                        readerUdp.ContentLength = e.BytesTransferred - parsed;
                    }

                    if (readerUdp.ContentLength == e.BytesTransferred - parsed)
                    {
                        readerUdp.SetArray(e.Buffer);

                        OnIncomingSipMessage(new IncomingMessage(e, readerUdp,
                                                                 new ArraySegment <byte>(e.Buffer, e.Offset + parsed, readerUdp.ContentLength)));
                    }
                }
            }
            break;


            case ServerProtocol.Tcp:
            case ServerProtocol.Tls:
            {
                for (bool repeat = true; repeat && closeConnection == false;)
                {
                    repeat = connection.Proccess(ref e, out closeConnection);

                    if (connection.IsMessageReady)
                    {
                        switch (connection.Mode)
                        {
                        case Connection.Modes.WebSocket:
                            if (connection.IsSipWebSocket)
                            {
                                goto case Connection.Modes.Sip;
                            }
                            OnIncomingWebSocketMessage(connection, out closeConnection);
                            break;

                        case Connection.Modes.Sip:
                            OnIncomingSipMessage(new IncomingMessage(connection));
                            break;

                        case Connection.Modes.Http:
                            OnIncominHttpRequest(connection);
                            break;

                        case Connection.Modes.Ajax:
                            int id = ajax.ProcessConnection(connection);
                            if (id >= 0 && connection.Reader.IsFinal)
                            {
                                OnIncomingSipMessage(new IncomingMessage(ajax.GetConnectionAddresses(connection, id), connection));
                            }
                            break;

                        default:
                            throw new InvalidProgramException();
                        }

                        connection.ResetState();
                    }
                }
            }
            break;

            default:
                throw new NotImplementedException();
            }

            return(!closeConnection);
        }