public void TestUnpackIdMismatch()
        {
            // Unpack<T> has a id != msgType case that throws a FormatException.
            // let's try to trigger it.

            SceneMessage message = new SceneMessage()
            {
                sceneName      = "Hello world",
                sceneOperation = SceneOperation.LoadAdditive
            };

            byte[] data = MessagePacker.Pack(message);

            // overwrite the id
            data[0] = 0x01;
            data[1] = 0x02;

            try
            {
                SceneMessage unpacked = MessagePacker.Unpack <SceneMessage>(data);
                // BAD: IF WE GET HERE THEN NO EXCEPTION WAS THROWN
                Assert.Fail();
            }
            catch (FormatException)
            {
                // GOOD
            }
        }
示例#2
0
 public void AddMessage(SceneMessage message)
 {
     lock (messageQueue)
     {
         messageQueue.Enqueue(message);
     }
 }
示例#3
0
        public void SetUp()
        {
            data = new SceneMessage();
            mockTransportConnection = Substitute.For <IConnection>();

            void ParsePacket(ArraySegment <byte> data)
            {
                var reader = new NetworkReader(data);

                _        = MessagePacker.UnpackId(reader);
                lastSent = reader.ReadNotifyPacket();

                lastSerializedPacket = new byte[data.Count];
                Array.Copy(data.Array, data.Offset, lastSerializedPacket, 0, data.Count);
            }

            mockTransportConnection.Send(
                Arg.Do <ArraySegment <byte> >(ParsePacket), Channel.Unreliable);

            connection = new NetworkPlayer(mockTransportConnection);

            serializedMessage = MessagePacker.Pack(new ReadyMessage());
            connection.RegisterHandler <ReadyMessage>(message => { });

            delivered = Substitute.For <Action <INetworkPlayer, object> >();
            lost      = Substitute.For <Action <INetworkPlayer, object> >();

            connection.NotifyDelivered += delivered;
            connection.NotifyLost      += lost;
        }
示例#4
0
    public static byte[] ToByteArray(SceneMessage sceneMessage)
    {
        MemoryStream memStream = new MemoryStream();

        Serializer.Serialize(memStream, sceneMessage);
        byte[] bytes = memStream.ToArray();
        return(bytes);
    }
示例#5
0
    public static SceneMessage FromByteArray(byte[] bytes)
    {
        MemoryStream outMemStream = new MemoryStream(bytes, 0, bytes.Length);
        SceneMessage objProto     = (SceneMessage)Serializer.Deserialize <SceneMessage>(outMemStream);

        outMemStream.Close();
        //Scene scene = ByteArrayToScene(objProto.sceneBytes, objProto.method);
        return(objProto);
    }
示例#6
0
        public SceneMessage GenerateSceneMessage()
        {
            SceneMessage msg = new SceneMessage
            {
                sceneName      = selectedMap,
                sceneOperation = SceneOperation.LoadAdditive
            };

            return(msg);
        }
示例#7
0
        public void LoadLevel(string sceneToLoad, string sceneToUnload = null, bool allowSceneActivation = true)
        {
            var msg = new SceneMessage();

            msg.sceneToLoad          = sceneToLoad;
            msg.sceneToUnload        = sceneToUnload;
            msg.allowSceneActivation = allowSceneActivation;
            NetworkServer.SendToAll(CustomMsgType.LoadLevel, msg);
            StartCoroutine(ServerLoadLevel(sceneToLoad, sceneToUnload, allowSceneActivation));
        }
示例#8
0
        private void waitingProcess(NetMessageHandler handler, string sessionId, SceneMessage sceneMessage,
                                    MessageQueue <DispetcherMessage> dispatcherHandler)
        {
            DispatcherExitCondition           exitCondition = new DispatcherExitCondition(handler, sessionId, isDisconnected);
            QueueListener <DispetcherMessage> queueListener = new QueueListener <DispetcherMessage>(dispatcherHandler, m_log);

            queueListener.AddResponse("MergerFinishMessage", mergerFinishMessageEvent);
            queueListener.AddResponse("MergerUpdateMessage", mergerUpdateMessageEvent);
            queueListener.AddResponse("MergerUpdateFailedMessage", mergerUpdateFailedMessageEvent);
            queueListener.Run(exitCondition);
        }
        void OnTriggerExit(Collider other)
        {
            // Debug.Log($"Unloading {subScene}");

            NetworkIdentity networkIdentity = other.gameObject.GetComponent <NetworkIdentity>();
            SceneMessage    message         = new SceneMessage {
                sceneName = subScene, sceneOperation = SceneOperation.UnloadAdditive
            };

            networkIdentity.connectionToClient.Send(message);
        }
示例#10
0
        // Unlock round start when the map is loaded, also sends the client the scene to load for some reason
        // I'll move it to another place
        // TODO: move the scene message to LoadMapScene()
        public void UnlockRoundStart()
        {
            // UI stuff
            loadSceneButtonText.text      = "scene loaded";
            startRoundButton.interactable = true;

            // creates a message for the clients that tell them to load the selected scene
            SceneMessage msg = GenerateSceneMessage();

            // sends said message to all clients
            NetworkServer.SendToAll(msg);
        }
示例#11
0
 private void SceneLoader_E_OnServerReady(NetworkConnection conn)
 {
     if (!string.IsNullOrEmpty(activeScene))
     {
         var message = new SceneMessage()
         {
             sceneOperation = SceneOperation.LoadAdditive,
             sceneName      = activeScene,
         };
         conn.Send(message);
     }
 }
        public void TestPacking()
        {
            SceneMessage message = new SceneMessage()
            {
                value = "Hello world"
            };

            byte[] data = MessagePacker.Pack(message);

            SceneMessage unpacked = MessagePacker.Unpack <SceneMessage>(data);

            Assert.That(unpacked.value, Is.EqualTo("Hello world"));
        }
示例#13
0
        public void UnlockRoundStart()
        {
            loadSceneButtonText.text      = "scene loaded";
            startRoundButton.interactable = true;

            SceneMessage msg = new SceneMessage
            {
                sceneName      = selectedMap,
                sceneOperation = SceneOperation.LoadAdditive
            };

            NetworkServer.SendToAll(msg);
        }
示例#14
0
        public void TestPacking()
        {
            SceneMessage message = new SceneMessage()
            {
                sceneName      = "Hello world",
                sceneOperation = SceneOperation.LoadAdditive
            };

            byte[] data = PackToByteArray(message);

            SceneMessage unpacked = UnpackFromByteArray <SceneMessage>(data);

            Assert.That(unpacked.sceneName, Is.EqualTo("Hello world"));
            Assert.That(unpacked.sceneOperation, Is.EqualTo(SceneOperation.LoadAdditive));
        }
示例#15
0
        public void TestPacking()
        {
            var message = new SceneMessage
            {
                MainActivateScene = "Hello world",
                SceneOperation    = SceneOperation.LoadAdditive
            };

            var data = MessagePacker.Pack(message);

            var unpacked = MessagePacker.Unpack <SceneMessage>(data);

            Assert.That(unpacked.MainActivateScene, Is.EqualTo("Hello world"));
            Assert.That(unpacked.SceneOperation, Is.EqualTo(SceneOperation.LoadAdditive));
        }
示例#16
0
        public void TestPacking()
        {
            var message = new SceneMessage
            {
                scenePath      = "Hello world",
                sceneOperation = SceneOperation.LoadAdditive
            };

            byte[] data = MessagePacker.Pack(message);

            SceneMessage unpacked = MessagePacker.Unpack <SceneMessage>(data);

            Assert.That(unpacked.scenePath, Is.EqualTo("Hello world"));
            Assert.That(unpacked.sceneOperation, Is.EqualTo(SceneOperation.LoadAdditive));
        }
示例#17
0
    public void LoadScene(string name)
    {
        UnloadScene();
        activeScene = name;
        SceneManager.LoadSceneAsync(name, LoadSceneMode.Additive);
        var message = new SceneMessage()
        {
            sceneOperation = SceneOperation.LoadAdditive,
            sceneName      = activeScene,
        };

        foreach (var connection in NetworkServer.connections)
        {
            connection.Value.Send(message);
        }
    }
示例#18
0
 void UnloadScene()
 {
     if (!string.IsNullOrEmpty(activeScene))
     {
         SceneManager.UnloadSceneAsync(activeScene);
         var message = new SceneMessage()
         {
             sceneOperation = SceneOperation.UnloadAdditive,
             sceneName      = activeScene,
         };
         foreach (var connection in NetworkServer.connections)
         {
             connection.Value.Send(message);
         }
     }
 }
示例#19
0
        // Unlock round start when the map is loaded, also sends the client the scene to load for some reason
        // I'll move it to another place
        // TODO: move the scene message to LoadMapScene()
        public void UnlockRoundStart()
        {
            // UI stuff
            loadSceneButtonText.text      = "scene loaded";
            startRoundButton.interactable = true;

            // creates a message for the clients that tell them to load the selected scene
            SceneMessage msg = new SceneMessage
            {
                sceneName      = selectedMap,
                sceneOperation = SceneOperation.LoadAdditive
            };

            // sends said message to all clients
            NetworkServer.SendToAll(msg);
        }
示例#20
0
        void OnTriggerExit(Collider other)
        {
            if (!NetworkServer.active)
            {
                return;
            }

            // Debug.LogFormat(LogType.Log, "Unloading {0}", subScene);

            NetworkIdentity networkIdentity = other.gameObject.GetComponent <NetworkIdentity>();
            SceneMessage    message         = new SceneMessage {
                sceneName = subScene, sceneOperation = SceneOperation.UnloadAdditive
            };

            networkIdentity.connectionToClient.Send(message);
        }
示例#21
0
        public void TestUnpackMessageNonGeneric()
        {
            // try a regular message
            var message = new SceneMessage
            {
                scenePath      = "Hello world",
                sceneOperation = SceneOperation.LoadAdditive
            };

            byte[] data   = MessagePacker.Pack(message);
            var    reader = new NetworkReader(data);

            int msgType = MessagePacker.UnpackId(reader);

            Assert.That(msgType, Is.EqualTo(BitConverter.ToUInt16(data, 0)));
        }
示例#22
0
        public void TestUnpackMessageNonGeneric()
        {
            // try a regular message
            var message = new SceneMessage
            {
                MainActivateScene = "Hello world",
                SceneOperation    = SceneOperation.LoadAdditive
            };

            var data = MessagePacker.Pack(message);

            reader.Reset(data);

            var msgType = MessagePacker.UnpackId(reader);

            Assert.That(msgType, Is.EqualTo(BitConverter.ToUInt16(data, 0)));
        }
示例#23
0
        public void TestUnpackMessageNonGeneric()
        {
            // try a regular message
            SceneMessage message = new SceneMessage()
            {
                sceneName      = "Hello world",
                sceneOperation = SceneOperation.LoadAdditive
            };

            byte[]        data   = MessagePacker.Pack(message);
            NetworkReader reader = new NetworkReader(data);

            bool result = MessagePacker.UnpackMessage(reader, out int msgType);

            Assert.That(result, Is.EqualTo(true));
            Assert.That(msgType, Is.EqualTo(BitConverter.ToUInt16(data, 0)));
        }
示例#24
0
        public void UnloadSelectedMap()
        {
            SceneManager.UnloadSceneAsync(selectedMap);

            // just in case (Restarts for example)
            loadSceneButtonText.text      = "load map";
            loadSceneButton.interactable  = true;
            startRoundButton.interactable = false;

            SceneMessage msg = new SceneMessage
            {
                sceneName      = selectedMap,
                sceneOperation = SceneOperation.UnloadAdditive
            };

            NetworkServer.SendToAll(msg);
        }
示例#25
0
    public static void TransitionTo(string sceneName, SceneMessage message, bool waitForNextSceneSetup = false)
    {
        if (isInTransition)
        {
            throw new System.Exception("Cannot transition to " + sceneName + ". We are already transitioning.");
        }

        LoadingScreen.waitForNextSceneSetup = waitForNextSceneSetup;
        isInTransition = true;

        wish = new Wish()
        {
            message   = message,
            sceneName = sceneName
        };
        Scenes.LoadAsync(SCENENAME, LoadSceneMode.Additive);
    }
示例#26
0
        // Unloads the current selected map
        public void UnloadSelectedMap()
        {
            // Unloads the map
            SceneManager.UnloadSceneAsync(selectedMap);

            // just in case (Restarts for example)
            loadSceneButtonText.text      = "load map";
            loadSceneButton.interactable  = true;
            startRoundButton.interactable = false;

            // Creates a message to tell clients what scene to unload
            SceneMessage msg = new SceneMessage
            {
                sceneName      = selectedMap,
                sceneOperation = SceneOperation.UnloadAdditive
            };

            // Sends the client the message
            NetworkServer.SendToAll(msg);
        }
示例#27
0
        public void TestUnpackIdMismatch()
        {
            // Unpack<T> has a id != msgType case that throws a FormatException.
            // let's try to trigger it.

            SceneMessage message = new SceneMessage()
            {
                sceneName      = "Hello world",
                sceneOperation = SceneOperation.LoadAdditive
            };

            byte[] data = MessagePacker.Pack(message);

            // overwrite the id
            data[0] = 0x01;
            data[1] = 0x02;

            Assert.Throws <FormatException>(() => {
                SceneMessage unpacked = MessagePacker.Unpack <SceneMessage>(data);
            });
        }
示例#28
0
        public void TestUnpackIdMismatch()
        {
            // Unpack<T> has a id != msgType case that throws a FormatException.
            // let's try to trigger it.

            var message = new SceneMessage
            {
                MainActivateScene = "Hello world",
                SceneOperation    = SceneOperation.LoadAdditive
            };

            var data = MessagePacker.Pack(message);

            // overwrite the id
            data[0] = 0x01;
            data[1] = 0x02;

            Assert.Throws <FormatException>(delegate
            {
                _ = MessagePacker.Unpack <SceneMessage>(data);
            });
        }
示例#29
0
        private void processingScene(NetMessageHandler handler, SceneMessage message, byte[] sceneFile, string sessionGuid,
                                     MessageQueue <DispetcherMessage> dispatcherHandler)
        {
            string sceneName      = null;
            int    threadID       = m_threadsID[sessionGuid];
            string uniqueBlobName = string.Format("{0}_{1}", BlobName.UNIQUE_BLOB + threadID.ToString(), sessionGuid);

            try
            {
                lock (m_BlobLock)
                {
                    CloudBlockBlob blob = renderStorage.Get().CreateBlob(BlobName.SCENE_BLOB, uniqueBlobName);//sceneContainer.GetBlockBlobReference(uniqueBlobName);
                    Utils.UpploadBlobByteArray(blob, sceneFile);
                    sceneName = blob.Uri.ToString();
                }
            }
            catch (Exception e)
            {
                m_log.Error("Error saving scene to blob: " + e.Message);
                m_instanceManager.IncreaseFreeMergerCount(); //if no blob created than mark requested instances as free
                                                             //and kill thread
                m_instanceManager.IncreaseFreeWorkerCount(message.InstanceCnt);
                Thread.CurrentThread.Abort();
            }

            StartMergeMessage mergerMessage = new StartMergeMessage(message.InstanceCnt, message.TotalTime,
                                                                    message.UpdateTime, sessionGuid, threadID, message.RequiredSpp);

            m_mergerHandler.AddMessage(mergerMessage);
            m_log.Info("IsMergerReady message sent");
            WaitForWorkerAction action = new WaitForWorkerAction(sendClientMessage, handler);

            lock (m_waitMergerLock)
            {
                //wait for merger allocating
                InstanceManager.Get(m_log).WaitForMerger(dispatcherHandler, m_threadsID[sessionGuid], action);
            }
            lock (m_waitWorkerLock)
            {
                //check if workers has been already allocated, if no wait for it
                InstanceManager.Get(m_log).WaitForWorkers(message.InstanceCnt, dispatcherHandler, sessionGuid, threadID, action);
            }
            sendClientMessage(handler, StatusMessage.StatusEnum.Allocated);

            m_log.Info("StartMergeMessage sent to merger. ID: " + threadID);
            m_log.Info("Scene GUID: " + sessionGuid + "Scene ID: " + threadID);
            m_renderAbortHandlers.Add(threadID, new List <MessageQueue <RenderMessage> >()); //each m_renderAbortHandlers contains list of worker queues
            MessageQueue <RenderMessage> toRenderQueue = new MessageQueue <RenderMessage>(threadID.ToString());
            double sppPerOneWorker = 0;                                                      //how much spp is required from each worker

            for (int i = 0; i < message.InstanceCnt; i++)
            {
                //adding new renderqueue for list that correspond to ID of scene which is connected with m_threadsID[sessionGuid]
                try
                {
                    MessageQueue <RenderMessage> newAbortQueue = new MessageQueue <RenderMessage>(threadID.ToString() + m_renderAbortHandlers[threadID].Count.ToString());
                    newAbortQueue.Clear();
                    m_renderAbortHandlers[threadID].Add(newAbortQueue);
                }
                catch (Exception e)
                {
                    m_log.Error("Error creating queue: " + e.Message + ". ID: " + threadID);
                }
                sppPerOneWorker = message.RequiredSpp / message.InstanceCnt;
                ToRenderMessage msg = new ToRenderMessage(sceneName, message.TotalTime,
                                                          message.UpdateTime, threadID, threadID.ToString() +
                                                          (m_renderAbortHandlers[threadID].Count - 1).ToString(), //scene ID + number of worker that is rendering this scene
                                                          sppPerOneWorker);
                m_log.Info("Abort queue name suffix: " + (threadID.ToString() + (m_renderAbortHandlers[threadID].Count - 1)).ToString());
                toRenderQueue.AddMessage(msg);
                m_log.Info("ToRenderMessage sent to worker: " + i);
            }
            m_log.Info("ToRenderMessage sent to workers. ID: " + threadID);
        }