Example #1
0
        public void SendToAllTest()
        {
            // listen
            NetworkServer.Listen(1);
            Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));

            // add connection
            LocalConnectionToClient connection = new LocalConnectionToClient();

            connection.connectionToServer = new LocalConnectionToServer();
            // set a client handler
            int called = 0;

            connection.connectionToServer.SetHandlers(new Dictionary <ushort, NetworkMessageDelegate>()
            {
                { MessagePacking.GetId <TestMessage1>(), ((conn, reader, channelId) => ++ called) }
            });
            NetworkServer.AddConnection(connection);

            // create a message
            TestMessage1 message = new TestMessage1 {
                IntValue = 1, DoubleValue = 2, StringValue = "3"
            };

            // send it to all
            NetworkServer.SendToAll(message);

            // update local connection once so that the incoming queue is processed
            connection.connectionToServer.Update();

            // was it send to and handled by the connection?
            Assert.That(called, Is.EqualTo(1));
        }
Example #2
0
        public void ShowForConnection()
        {
            // listen
            NetworkServer.Listen(1);
            Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));

            // add connection
            LocalConnectionToClient connection = new LocalConnectionToClient();

            // required for ShowForConnection
            connection.isReady            = true;
            connection.connectionToServer = new LocalConnectionToServer();
            // set a client handler
            int called = 0;

            connection.connectionToServer.SetHandlers(new Dictionary <ushort, NetworkMessageDelegate>()
            {
                { MessagePacking.GetId <SpawnMessage>(), ((conn, reader, channelId) => ++ called) }
            });
            NetworkServer.AddConnection(connection);

            // create a gameobject and networkidentity and some unique values
            NetworkIdentity identity = new GameObject().AddComponent <NetworkIdentity>();

            identity.connectionToClient = connection;

            // call ShowForConnection
            NetworkServer.ShowForConnection(identity, connection);

            // update local connection once so that the incoming queue is processed
            connection.connectionToServer.Update();

            // was it sent to and handled by the connection?
            Assert.That(called, Is.EqualTo(1));

            // it shouldn't send it if connection isn't ready, so try that too
            connection.isReady = false;
            NetworkServer.ShowForConnection(identity, connection);
            connection.connectionToServer.Update();
            // not 2 but 1 like before?
            Assert.That(called, Is.EqualTo(1));

            // clean up
            NetworkServer.Shutdown();
            // destroy GO after shutdown, otherwise isServer is true in OnDestroy and it tries to call
            // GameObject.Destroy (but we need DestroyImmediate in Editor)
            GameObject.DestroyImmediate(identity.gameObject);
        }
Example #3
0
        // unpack a message we received
        public static T UnpackFromByteArray <T>(byte[] data)
            where T : struct, NetworkMessage
        {
            using (NetworkReaderPooled networkReader = NetworkReaderPool.Get(data))
            {
                int msgType = MessagePacking.GetId <T>();

                int id = networkReader.ReadUShort();
                if (id != msgType)
                {
                    throw new FormatException($"Invalid message,  could not unpack {typeof(T).FullName}");
                }

                return(networkReader.Read <T>());
            }
        }
Example #4
0
        public void SendToClientOfPlayer()
        {
            // listen
            NetworkServer.Listen(1);
            Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));

            // add connection
            LocalConnectionToClient connection = new LocalConnectionToClient();

            connection.connectionToServer = new LocalConnectionToServer();
            // set a client handler
            int called = 0;

            connection.connectionToServer.SetHandlers(new Dictionary <ushort, NetworkMessageDelegate>()
            {
                { MessagePacking.GetId <TestMessage1>(), ((conn, reader, channelId) => ++ called) }
            });
            NetworkServer.AddConnection(connection);

            // create a message
            TestMessage1 message = new TestMessage1 {
                IntValue = 1, DoubleValue = 2, StringValue = "3"
            };

            // create a gameobject and networkidentity
            NetworkIdentity identity = new GameObject().AddComponent <NetworkIdentity>();

            identity.connectionToClient = connection;

            // send it to that player
            identity.connectionToClient.Send(message);

            // update local connection once so that the incoming queue is processed
            connection.connectionToServer.Update();

            // was it send to and handled by the connection?
            Assert.That(called, Is.EqualTo(1));

            // clean up
            NetworkServer.Shutdown();
            // destroy GO after shutdown, otherwise isServer is true in OnDestroy and it tries to call
            // GameObject.Destroy (but we need DestroyImmediate in Editor)
            GameObject.DestroyImmediate(identity.gameObject);
        }
Example #5
0
        public void ClientToServerTest()
        {
            Assert.That(connectionToServer.address, Is.EqualTo("localhost"));

            bool invoked = false;

            void Handler(NetworkConnection conn, NetworkReader reader, int channelId)
            {
                invoked = true;
            }

            Dictionary <ushort, NetworkMessageDelegate> handlers = new Dictionary <ushort, NetworkMessageDelegate>();

            handlers.Add(MessagePacking.GetId <TestMessage>(), Handler);

            connectionToServer.SetHandlers(handlers);
            connectionToClient.Send(new TestMessage());

            connectionToServer.Update();

            Assert.True(invoked, "handler should have been invoked");
        }
Example #6
0
 public void GetId()
 {
     // "Mirror.Tests.MessageTests.TestMessage"
     Debug.Log(typeof(TestMessage).FullName);
     Assert.That(MessagePacking.GetId <TestMessage>(), Is.EqualTo(0x8706));
 }