예제 #1
0
        public void TransmitterConnectivity()
        {
            Transmitter server = new Transmitter(MxpConstants.DefaultHubPort);

            Transmitter client = new Transmitter();

            JoinRequestMessage originalMessage = new JoinRequestMessage();

            originalMessage.BubbleId              = Guid.NewGuid();
            originalMessage.LocationName          = "TestLocation";
            originalMessage.IdentityProviderUrl   = "IdentityProviderUrl";
            originalMessage.ParticipantIdentifier = "TestParticipantName";
            originalMessage.ParticipantSecret     = "TestParticipantPassphrase";
            originalMessage.ParticipantRealTime   = 10;

            originalMessage.ProgramName          = "TestProgramName";
            originalMessage.ProgramMajorVersion  = 1;
            originalMessage.ProgramMinorVersion  = 2;
            originalMessage.ProtocolMajorVersion = 3;
            originalMessage.ProtocolMinorVersion = 4;

            Session clientSession = client.OpenSession("127.0.0.1", MxpConstants.DefaultHubPort, originalMessage);

            clientSession.Send(originalMessage);
            client.Send();

            Assert.AreEqual(1, clientSession.GetPacketWaitingAcknowledgeCount());

            Thread.Sleep(100);

            server.Receive();

            server.Send();

            Thread.Sleep(100);

            client.Receive();

            client.HandleControlMessages();

            Assert.AreEqual(0, clientSession.GetPacketWaitingAcknowledgeCount());

            Session serverSession = server.AcceptPendingSession();

            Message decodedMessage = serverSession.Receive();

            decodedMessage.IsAutoRelease = false;

            String originalMessageString = originalMessage.ToString();
            String decodedMessageString  = decodedMessage.ToString();

            Assert.AreEqual(originalMessageString, decodedMessageString);

            server.Shutdown();

            Thread.Sleep(100);
        }
예제 #2
0
        public void ProcessMessages()
        {
            if (m_transmitter.PendingSessionCount > 0)
            {
                Session tmp = m_transmitter.AcceptPendingSession();
                m_sessions.Add(tmp);
                m_sessionsToClient.Add(tmp);
            }

            List <Session> tmpRemove = new List <Session>();

            foreach (Session session in m_sessionsToClient)
            {
                while (session.AvailableMessages > 0)
                {
                    Message message = session.Receive();

                    if (message.GetType() == typeof(JoinRequestMessage))
                    {
                        JoinRequestMessage joinRequestMessage = (JoinRequestMessage)message;

                        m_log.Info("[MXP ClientStack]: Session join request: " + session.SessionId + " (" +
                                   (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" +
                                   session.RemoteEndPoint.Port + ")");

                        try
                        {
                            if (joinRequestMessage.BubbleId == Guid.Empty)
                            {
                                foreach (Scene scene in m_scenes.Values)
                                {
                                    if (scene.RegionInfo.RegionName == joinRequestMessage.BubbleName)
                                    {
                                        m_log.Info("[MXP ClientStack]: Resolved region by name: " + joinRequestMessage.BubbleName + " (" + scene.RegionInfo.RegionID + ")");
                                        joinRequestMessage.BubbleId = scene.RegionInfo.RegionID.Guid;
                                    }
                                }
                            }

                            if (joinRequestMessage.BubbleId == Guid.Empty)
                            {
                                m_log.Warn("[MXP ClientStack]: Failed to resolve region by name: " + joinRequestMessage.BubbleName);
                            }

                            UUID sceneId = new UUID(joinRequestMessage.BubbleId);

                            bool regionExists = true;
                            if (!m_scenes.ContainsKey(sceneId))
                            {
                                m_log.Info("[MXP ClientStack]: No such region: " + sceneId);
                                regionExists = false;
                            }

                            UserProfileData user       = null;
                            UUID            userId     = UUID.Zero;
                            string          firstName  = null;
                            string          lastName   = null;
                            bool            authorized = regionExists ? AuthoriseUser(joinRequestMessage.ParticipantName,
                                                                                      joinRequestMessage.ParticipantPassphrase,
                                                                                      new UUID(joinRequestMessage.BubbleId), out userId, out firstName, out lastName, out user)
                                                            : false;

                            if (authorized)
                            {
                                Scene scene        = m_scenes[sceneId];
                                UUID  mxpSessionID = UUID.Random();

                                string reason;

                                m_log.Debug("[MXP ClientStack]: Session join request success: " + session.SessionId + " (" +
                                            (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" +
                                            session.RemoteEndPoint.Port + ")");

                                m_log.Debug("[MXP ClientStack]: Attaching UserAgent to UserProfile...");
                                AttachUserAgentToUserProfile(session, mxpSessionID, sceneId, user);
                                m_log.Debug("[MXP ClientStack]: Attached UserAgent to UserProfile.");
                                m_log.Debug("[MXP ClientStack]: Preparing Scene to Connection...");
                                if (!PrepareSceneForConnection(mxpSessionID, sceneId, user, out reason))
                                {
                                    m_log.DebugFormat("[MXP ClientStack]: Scene refused connection: {0}", reason);
                                    DeclineConnection(session, joinRequestMessage);
                                    tmpRemove.Add(session);
                                    continue;
                                }
                                m_log.Debug("[MXP ClientStack]: Prepared Scene to Connection.");
                                m_log.Debug("[MXP ClientStack]: Accepting connection...");
                                AcceptConnection(session, joinRequestMessage, mxpSessionID, userId);
                                m_log.Info("[MXP ClientStack]: Accepted connection.");

                                m_log.Debug("[MXP ClientStack]: Creating ClientView....");
                                MXPClientView client = new MXPClientView(session, mxpSessionID, userId, scene, firstName, lastName);
                                m_clients.Add(client);
                                m_log.Debug("[MXP ClientStack]: Created ClientView.");


                                m_log.Debug("[MXP ClientStack]: Adding ClientView to Scene...");
                                scene.ClientManager.Add(client.CircuitCode, client);
                                m_log.Debug("[MXP ClientStack]: Added ClientView to Scene.");


                                client.MXPSendSynchronizationBegin(m_scenes[new UUID(joinRequestMessage.BubbleId)].SceneContents.GetTotalObjectsCount());

                                m_log.Debug("[MXP ClientStack]: Starting ClientView...");
                                try
                                {
                                    client.Start();
                                    m_log.Debug("[MXP ClientStack]: Started ClientView.");
                                }
                                catch (Exception e)
                                {
                                    m_log.Error(e);
                                }

                                m_log.Debug("[MXP ClientStack]: Connected");
                            }
                            else
                            {
                                m_log.Info("[MXP ClientStack]: Session join request failure: " + session.SessionId + " (" +
                                           (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" +
                                           session.RemoteEndPoint.Port + ")");

                                DeclineConnection(session, joinRequestMessage);
                            }
                        }
                        catch (Exception e)
                        {
                            m_log.Error("[MXP ClientStack]: Session join request failure: " + session.SessionId + " (" +
                                        (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" +
                                        session.RemoteEndPoint.Port + "): " + e.ToString() + " :" + e.StackTrace.ToString());
                        }
                        tmpRemove.Add(session);
                    }
                }
            }

            foreach (Session session in tmpRemove)
            {
                m_sessionsToClient.Remove(session);
            }

            foreach (MXPClientView clientView in m_clients)
            {
                int     messagesProcessedCount = 0;
                Session session = clientView.Session;

                while (session.AvailableMessages > 0)
                {
                    Message message = session.Receive();

                    if (message.GetType() == typeof(LeaveRequestMessage))
                    {
                        LeaveResponseMessage leaveResponseMessage = (LeaveResponseMessage)MessageFactory.Current.ReserveMessage(
                            typeof(LeaveResponseMessage));

                        m_log.Debug("[MXP ClientStack]: Session leave request: " + session.SessionId + " (" + (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" + session.RemoteEndPoint.Port + ")");

                        leaveResponseMessage.RequestMessageId = message.MessageId;
                        leaveResponseMessage.FailureCode      = 0;
                        session.Send(leaveResponseMessage);

                        if (session.SessionState != SessionState.Disconnected)
                        {
                            session.SetStateDisconnected();
                        }

                        m_log.Debug("[MXP ClientStack]: Removing Client from Scene");
                        //clientView.Scene.RemoveClient(clientView.AgentId);
                    }
                    if (message.GetType() == typeof(LeaveResponseMessage))
                    {
                        LeaveResponseMessage leaveResponseMessage = (LeaveResponseMessage)message;

                        m_log.Debug("[MXP ClientStack]: Session leave response: " + session.SessionId + " (" + (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" + session.RemoteEndPoint.Port + ")");

                        if (leaveResponseMessage.FailureCode == 0)
                        {
                            session.SetStateDisconnected();
                        }

                        m_log.Debug("[MXP ClientStack]: Removing Client from Scene");
                        //clientView.Scene.RemoveClient(clientView.AgentId);
                    }
                    else
                    {
                        clientView.MXPPRocessMessage(message);
                    }

                    MessageFactory.Current.ReleaseMessage(message);
                    messagesProcessedCount++;
                    if (messagesProcessedCount > 1000)
                    {
                        break;
                    }
                }
            }
        }
예제 #3
0
        public void ProcessMessages()
        {
            if (transmitter.PendingSessionCount > 0)
            {
                sessions.Add(transmitter.AcceptPendingSession());
            }

            for (int i = 0; i < sessions.Count; i++)
            {
                int     messagesProcessedCount = 0;
                Session session = sessions[i];
                while (session.AvailableMessages > 0)
                {
                    Message message = session.Receive();

                    if (message.GetType() == typeof(AttachRequestMessage))
                    {
                        AttachRequestMessage attachRequestMessage = (AttachRequestMessage)message;

                        if (Bubbles.ContainsKey(attachRequestMessage.TargetBubbleId))
                        {
                            session.Bubble = Bubbles[attachRequestMessage.TargetBubbleId];
                        }

                        bool success = session.Bubble != null && session.Bubble.BubbleConnectAuthorize(
                            session, attachRequestMessage);

                        if (success)
                        {
                            AttachResponseMessage attachResponseMessage = (AttachResponseMessage)MessageFactory.Current.ReserveMessage(
                                typeof(AttachResponseMessage));

                            attachResponseMessage.RequestMessageId = attachRequestMessage.MessageId;
                            attachResponseMessage.FailureCode      = 0;

                            attachResponseMessage.TargetBubbleFragment.BubbleId              = session.Bubble.BubbleId;
                            attachResponseMessage.TargetBubbleFragment.BubbleName            = session.Bubble.BubbleName;
                            attachResponseMessage.TargetBubbleFragment.BubbleAssetCacheUrl   = HubAssetCacheUrl;
                            attachResponseMessage.TargetBubbleFragment.BubbleAddress         = HubAddress;
                            attachResponseMessage.TargetBubbleFragment.BubblePort            = (uint)HubPort;
                            attachResponseMessage.TargetBubbleFragment.BubbleCenter.X        = -attachRequestMessage.SourceBubbleFragment.BubbleCenter.X;
                            attachResponseMessage.TargetBubbleFragment.BubbleCenter.Y        = -attachRequestMessage.SourceBubbleFragment.BubbleCenter.Y;
                            attachResponseMessage.TargetBubbleFragment.BubbleCenter.Z        = -attachRequestMessage.SourceBubbleFragment.BubbleCenter.Z;
                            attachResponseMessage.TargetBubbleFragment.BubbleRange           = session.Bubble.BubbleRange;
                            attachResponseMessage.TargetBubbleFragment.BubblePerceptionRange = session.Bubble.BubblePerceptionRange;
                            attachResponseMessage.TargetBubbleFragment.BubbleRealTime        = 0; // Calculate real time here.

                            attachResponseMessage.ProgramName            = ProgramName;
                            attachResponseMessage.ProgramMajorVersion    = ProgramMajorVersion;
                            attachResponseMessage.ProgramMinorVersion    = ProgramMinorVersion;
                            attachResponseMessage.ProtocolMajorVersion   = MxpConstants.ProtocolMajorVersion;
                            attachResponseMessage.ProtocolMinorVersion   = MxpConstants.ProtocolMinorVersion;
                            attachResponseMessage.ProtocolSourceRevision = MxpConstants.ProtocolSourceRevision;

                            session.Send(attachResponseMessage);
                            //MessageFactory.Current.ReleaseMessage(attachResponseMessage);

                            session.SetStateConnected();

                            session.Bubble.BubbleConnected(session, message);
                        }
                        else
                        {
                            AttachResponseMessage attachResponseMessage = (AttachResponseMessage)MessageFactory.Current.ReserveMessage(typeof(AttachResponseMessage));

                            attachResponseMessage.RequestMessageId = attachRequestMessage.MessageId;
                            attachResponseMessage.FailureCode      = 1;


                            if (session.Bubble != null)
                            {
                                attachResponseMessage.TargetBubbleFragment.BubbleId              = session.Bubble.BubbleId;
                                attachResponseMessage.TargetBubbleFragment.BubbleName            = session.Bubble.BubbleName;
                                attachResponseMessage.TargetBubbleFragment.BubbleAssetCacheUrl   = HubAssetCacheUrl;
                                attachResponseMessage.TargetBubbleFragment.BubbleAddress         = HubAddress;
                                attachResponseMessage.TargetBubbleFragment.BubblePort            = (uint)HubPort;
                                attachResponseMessage.TargetBubbleFragment.BubbleCenter.X        = -attachRequestMessage.SourceBubbleFragment.BubbleCenter.X;
                                attachResponseMessage.TargetBubbleFragment.BubbleCenter.Y        = -attachRequestMessage.SourceBubbleFragment.BubbleCenter.Y;
                                attachResponseMessage.TargetBubbleFragment.BubbleCenter.Z        = -attachRequestMessage.SourceBubbleFragment.BubbleCenter.Z;
                                attachResponseMessage.TargetBubbleFragment.BubbleRange           = session.Bubble.BubbleRange;
                                attachResponseMessage.TargetBubbleFragment.BubblePerceptionRange = session.Bubble.BubblePerceptionRange;
                                attachResponseMessage.TargetBubbleFragment.BubbleRealTime        = 0; // Calculate real time here.
                            }
                            else
                            {
                                attachResponseMessage.TargetBubbleFragment.BubbleId              = Guid.Empty;
                                attachResponseMessage.TargetBubbleFragment.BubbleName            = "";
                                attachResponseMessage.TargetBubbleFragment.BubbleAssetCacheUrl   = "";
                                attachResponseMessage.TargetBubbleFragment.BubbleAddress         = "";
                                attachResponseMessage.TargetBubbleFragment.BubblePort            = 0;
                                attachResponseMessage.TargetBubbleFragment.BubbleCenter.X        = 0;
                                attachResponseMessage.TargetBubbleFragment.BubbleCenter.Y        = 0;
                                attachResponseMessage.TargetBubbleFragment.BubbleCenter.Z        = 0;
                                attachResponseMessage.TargetBubbleFragment.BubbleRange           = 0;
                                attachResponseMessage.TargetBubbleFragment.BubblePerceptionRange = 0;
                                attachResponseMessage.TargetBubbleFragment.BubbleRealTime        = 0;
                            }

                            attachResponseMessage.ProgramName            = ProgramName;
                            attachResponseMessage.ProgramMajorVersion    = ProgramMajorVersion;
                            attachResponseMessage.ProgramMinorVersion    = ProgramMinorVersion;
                            attachResponseMessage.ProtocolMajorVersion   = MxpConstants.ProtocolMajorVersion;
                            attachResponseMessage.ProtocolMinorVersion   = MxpConstants.ProtocolMinorVersion;
                            attachResponseMessage.ProtocolSourceRevision = MxpConstants.ProtocolSourceRevision;
                            session.Send(attachResponseMessage);
                            //MessageFactory.Current.ReleaseMessage(attachResponseMessage);

                            session.SetStateDisconnected();

                            if (session.Bubble != null)
                            {
                                session.Bubble.BubbleConnectFailure(session, message);
                            }
                        }
                    }
                    if (message.GetType() == typeof(AttachResponseMessage))
                    {
                        AttachResponseMessage attachResponseMessage = (AttachResponseMessage)message;
                        if (session.Bubble != null)
                        {
                            if (attachResponseMessage.FailureCode == 0)
                            {
                                session.SetStateConnected();
                                session.Bubble.BubbleConnected(session, message);
                            }
                            else
                            {
                                session.SetStateDisconnected();
                                session.Bubble.BubbleConnectFailure(session, message);
                            }
                        }
                    }
                    if (message.GetType() == typeof(DetachRequestMessage))
                    {
                        DetachResponseMessage detachResponseMessage = (DetachResponseMessage)MessageFactory.Current.ReserveMessage(
                            typeof(DetachResponseMessage));

                        detachResponseMessage.RequestMessageId = message.MessageId;
                        detachResponseMessage.FailureCode      = 0;
                        session.Send(detachResponseMessage);
                        //MessageFactory.Current.ReleaseMessage(detachResponseMessage);

                        //session.SetStateDisconnected();
                    }
                    if (message.GetType() == typeof(DetachResponseMessage))
                    {
                        DetachResponseMessage leaveResponseMessage = (DetachResponseMessage)message;

                        if (leaveResponseMessage.FailureCode == 0)
                        {
                            if (session.SessionState == SessionState.Connected || session.SessionState == SessionState.Connecting)
                            {
                                session.SetStateDisconnected();
                            }
                        }
                    }
                    if (message.GetType() == typeof(ListBubblesRequest))
                    {
                        if (session.Bubble != null)
                        {
                            session.Bubble.BubbleListRequested(session, (ListBubblesRequest)message);
                        }
                    }
                    if (message.GetType() == typeof(ListBubblesResponse))
                    {
                        if (session.Bubble != null)
                        {
                            session.Bubble.BubbleListReceived(session, (ListBubblesResponse)message);
                        }
                    }
                    else
                    {
                        if (session.Bubble != null)
                        {
                            session.Bubble.BubbleMessageReceived(session, message);
                        }
                    }

                    MessageFactory.Current.ReleaseMessage(message);

                    messagesProcessedCount++;
                    if (messagesProcessedCount > 5000)
                    {
                        break;
                    }
                }
            }
        }
예제 #4
0
        public void ProcessMessages()
        {
            if (transmitter.PendingSessionCount > 0)
            {
                sessions.Add(transmitter.AcceptPendingSession());
            }

            for (int i = 0; i < sessions.Count; i++)
            {
                int     messagesProcessedCount = 0;
                Session session = sessions[i];
                while (session.AvailableMessages > 0)
                {
                    Message message = session.Receive();

                    if (message.GetType() == typeof(JoinRequestMessage))
                    {
                        JoinRequestMessage joinRequestMessage = (JoinRequestMessage)message;

                        if (Bubbles.ContainsKey(joinRequestMessage.BubbleId))
                        {
                            session.Bubble = Bubbles[joinRequestMessage.BubbleId];
                        }

                        Guid participantId = Guid.Empty;
                        Guid avatarId      = Guid.Empty;
                        bool success       = session.Bubble != null && session.Bubble.ParticipantConnectAuthorize(
                            session, joinRequestMessage, out participantId, out avatarId);

                        if (success)
                        {
                            //LogUtil.Info("Participant connect success: " + joinRequestMessage.ParticipantIdentifier + " (" + session.IncomingSessionId + " " + (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" + session.RemoteEndPoint.Port + ")");

                            JoinResponseMessage joinResponseMessage = (JoinResponseMessage)MessageFactory.Current.ReserveMessage(
                                typeof(JoinResponseMessage));

                            joinResponseMessage.RequestMessageId = joinRequestMessage.MessageId;
                            joinResponseMessage.FailureCode      = 0;

                            joinResponseMessage.ParticipantId          = participantId;
                            joinResponseMessage.AvatarId               = avatarId;
                            joinResponseMessage.BubbleAssetCacheUrl    = assetCacheUrl;
                            joinResponseMessage.BubbleId               = session.Bubble.BubbleId;
                            joinResponseMessage.BubbleName             = session.Bubble.BubbleName;
                            joinResponseMessage.BubbleRealTime         = 0;
                            joinResponseMessage.ProgramName            = programName;
                            joinResponseMessage.ProgramMajorVersion    = programMajorVersion;
                            joinResponseMessage.ProgramMinorVersion    = programMinorVersion;
                            joinResponseMessage.ProtocolMajorVersion   = MxpConstants.ProtocolMajorVersion;
                            joinResponseMessage.ProtocolMinorVersion   = MxpConstants.ProtocolMinorVersion;
                            joinResponseMessage.ProtocolSourceRevision = MxpConstants.ProtocolSourceRevision;

                            session.Send(joinResponseMessage);

                            session.SetStateConnected();

                            session.Bubble.ParticipantConnected(session, joinRequestMessage, participantId, avatarId);
                        }
                        else
                        {
                            //LogUtil.Info("Participant connect failure failure: " + joinRequestMessage.ParticipantIdentifier + " (" + session.IncomingSessionId + " " + (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" + session.RemoteEndPoint.Port + ")");

                            JoinResponseMessage joinResponseMessage = (JoinResponseMessage)MessageFactory.Current.ReserveMessage(typeof(JoinResponseMessage));

                            joinResponseMessage.RequestMessageId = joinRequestMessage.MessageId;
                            joinResponseMessage.FailureCode      = 1;

                            joinResponseMessage.BubbleAssetCacheUrl = assetCacheUrl;

                            if (session.Bubble != null)
                            {
                                joinResponseMessage.BubbleName = session.Bubble.BubbleName;
                                joinResponseMessage.BubbleId   = session.Bubble.BubbleId;
                            }
                            else
                            {
                                joinResponseMessage.BubbleName = "";
                                joinResponseMessage.BubbleId   = Guid.Empty;
                            }

                            joinResponseMessage.BubbleRealTime         = 0;
                            joinResponseMessage.ProgramName            = programName;
                            joinResponseMessage.ProgramMajorVersion    = programMajorVersion;
                            joinResponseMessage.ProgramMinorVersion    = programMinorVersion;
                            joinResponseMessage.ProtocolMajorVersion   = MxpConstants.ProtocolMajorVersion;
                            joinResponseMessage.ProtocolMinorVersion   = MxpConstants.ProtocolMinorVersion;
                            joinResponseMessage.ProtocolSourceRevision = MxpConstants.ProtocolSourceRevision;

                            session.Send(joinResponseMessage);

                            session.SetStateDisconnected();

                            if (session.Bubble != null)
                            {
                                session.Bubble.ParticipantConnectFailure(session, message);
                            }
                        }
                    }
                    if (message.GetType() == typeof(LeaveRequestMessage))
                    {
                        LeaveResponseMessage leaveResponseMessage = (LeaveResponseMessage)MessageFactory.Current.ReserveMessage(
                            typeof(LeaveResponseMessage));

                        //LogUtil.Info("Session leave request: " + session.IncomingSessionId + " (" + (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" + session.RemoteEndPoint.Port + ")");

                        leaveResponseMessage.RequestMessageId = message.MessageId;
                        leaveResponseMessage.FailureCode      = 0;
                        session.Send(leaveResponseMessage);

                        /*if (session.SessionState != SessionState.Disconnected)
                         * {
                         *  session.SetStateDisconnected();
                         * }*/
                    }
                    if (message.GetType() == typeof(LeaveResponseMessage))
                    {
                        LeaveResponseMessage leaveResponseMessage = (LeaveResponseMessage)message;

                        //LogUtil.Info("Session leave response: " + session.IncomingSessionId + " (" + (session.IsIncoming ? "from" : "to") + " " + session.RemoteEndPoint.Address + ":" + session.RemoteEndPoint.Port + ")");

                        if (leaveResponseMessage.FailureCode == 0)
                        {
                            session.SetStateDisconnected();
                        }
                    }
                    else
                    {
                        if (session.Bubble != null)
                        {
                            session.Bubble.ParticipantMessageReceived(session, message);
                        }
                    }

                    MessageFactory.Current.ReleaseMessage(message);
                    messagesProcessedCount++;
                    if (messagesProcessedCount > 1000)
                    {
                        break;
                    }
                }
            }
        }