Beispiel #1
0
        /// <summary>
        /// <inheritdoc cref="Send(INetCommand?, NetworkDestination)"/>
        /// to a specific NetworkConnection, only callable from server.
        /// You can retrieve a <see cref="NetworkConnection"/> from <see cref="NetworkServer.connections"/> or
        /// from a <see cref="NetworkBehaviour.connectionToClient"/> field.
        /// </summary>
        /// <param name="command">Registered command</param>
        /// <param name="target">NetworkConnection the command will be sent to.</param>
        /// <exception cref="ArgumentNullException">Thrown when target is null</exception>
        /// <exception cref="InvalidOperationException">Thrown if not called from server</exception>
        public static void Send(this INetCommand?command, NetworkConnection target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (!NetworkServer.active)
            {
                throw new InvalidOperationException("NetworkServer is not active.");
            }

            if (NetworkClient.active)
            {
                foreach (var networkClient in NetworkClient.allClients)
                {
                    if (networkClient.connection != null && networkClient.connection.connectionId == target.connectionId)
                    {
                        command.OnReceived();
                        return;
                    }
                }
            }

            var header = NetworkDestination.Clients.GetHeader(NetworkingAPI.GetNetworkHash(command.GetType()));

            SendCommand(header, target);
        }
Beispiel #2
0
        /// <summary>
        /// Send the passed command over the network
        /// </summary>
        /// <param name="command">Registered command</param>
        /// <param name="destination">Destination of the command</param>
        public static void Send(this INetCommand?command, NetworkDestination destination)
        {
            if (destination.ShouldRun())
            {
                command.OnReceived();
            }

            if (destination.ShouldSend())
            {
                var header = destination.GetHeader(NetworkingAPI.GetNetworkHash(command.GetType()));

                if (NetworkServer.active)
                {
                    for (int i = 0; i < NetworkServer.connections.Count; ++i)
                    {
                        NetworkConnection conn = NetworkServer.connections[i];
                        if (conn == null)
                        {
                            continue;
                        }

                        if (NetworkServer.localClientActive && NetworkServer.localConnections.Contains(conn))
                        {
                            continue;
                        }

                        SendCommand(header, conn);
                    }
                }
                else if (NetworkClient.active)
                {
                    SendCommand(header, ClientScene.readyConnection);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// <inheritdoc cref="Send{TRequest, TReply}(TRequest, NetworkDestination)"/>
        /// to a specific NetworkConnection, only callable from server.
        /// You can retrieve a <see cref="NetworkConnection"/> from <see cref="NetworkServer.connections"/> or
        /// from a <see cref="NetworkBehaviour.connectionToClient"/> field.
        /// </summary>
        /// <param name="request">Registered request</param>
        /// <param name="target">NetworkConnection the request will be sent to.</param>
        /// <exception cref="ArgumentNullException">Thrown when target is null</exception>
        /// <exception cref="InvalidOperationException">Thrown if not called from server</exception>
        public static void Send <TRequest, TReply>(this TRequest request, NetworkConnection target)
            where TRequest : INetRequest <TRequest, TReply>
            where TReply : INetRequestReply <TRequest, TReply>
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (!NetworkServer.active)
            {
                throw new InvalidOperationException("NetworkServer is not active.");
            }

            if (NetworkClient.active)
            {
                foreach (var networkClient in NetworkClient.allClients)
                {
                    if (networkClient.connection != null && networkClient.connection.connectionId == target.connectionId)
                    {
                        request.OnRequestReceived().OnReplyReceived();
                        return;
                    }
                }
            }

            var header = NetworkDestination.Clients.GetHeader(NetworkingAPI.GetNetworkHash(request.GetType()));

            SendRequest <TRequest, TReply>(request, header, target);
        }
Beispiel #4
0
        public static void Send <TRequest, TReply>(this TRequest request, NetworkDestination destination)
            where TRequest : INetRequest <TRequest, TReply>
            where TReply : INetRequestReply <TRequest, TReply>
        {
            if (destination.ShouldRun())
            {
                request.OnRequestReceived().OnReplyReceived();
            }

            if (destination.ShouldSend())
            {
                var header = destination.GetHeader(NetworkingAPI.GetNetworkHash(request.GetType()));

                if (NetworkServer.active)
                {
                    for (var i = 0; i < NetworkServer.connections.Count; ++i)
                    {
                        NetworkConnection conn = NetworkServer.connections[i];
                        if (conn == null)
                        {
                            continue;
                        }

                        if (NetworkServer.localClientActive && NetworkServer.localConnections.Contains(conn))
                        {
                            continue;
                        }

                        using (Writer netWriter = NetworkingAPI.GetWriter(NetworkingAPI.RequestIndex, conn, QosType.Reliable)) {
                            NetworkWriter writer = netWriter;
                            writer.Write(header);
                            writer.Write(request);
                        }
                    }
                }
                else if (NetworkClient.active)
                {
                    using (Writer netWriter = NetworkingAPI.GetWriter(NetworkingAPI.RequestIndex, ClientScene.readyConnection, QosType.Reliable)) {
                        NetworkWriter writer = netWriter;
                        writer.Write(header);
                        writer.Write(request);
                    }
                }
            }
        }
Beispiel #5
0
        public static void Send(this INetMessage?message, NetworkDestination destination)
        {
            if (destination.ShouldRun())
            {
                message.OnReceived();
            }

            if (destination.ShouldSend())
            {
                var header = destination.GetHeader(NetworkingAPI.GetNetworkHash(message.GetType()));

                if (NetworkServer.active)
                {
                    for (int i = 0; i < NetworkServer.connections.Count; ++i)
                    {
                        NetworkConnection conn = NetworkServer.connections[i];
                        if (conn == null)
                        {
                            continue;
                        }

                        if (NetworkServer.localClientActive && NetworkServer.localConnections.Contains(conn))
                        {
                            continue;
                        }

                        using (Writer netWriter = NetworkingAPI.GetWriter(NetworkingAPI.MessageIndex, conn, QosType.Reliable)) {
                            NetworkWriter writer = netWriter;
                            writer.Write(header);
                            writer.Write(message);
                        }
                    }
                }
                else if (NetworkClient.active)
                {
                    using (Writer netWriter = NetworkingAPI.GetWriter(NetworkingAPI.MessageIndex, ClientScene.readyConnection, QosType.Reliable)) {
                        NetworkWriter writer = netWriter;
                        writer.Write(header);
                        writer.Write(message);
                    }
                }
            }
        }
Beispiel #6
0
        public static void Send(this INetCommand command, NetworkDestination destination)
        {
            if (destination.ShouldRun())
            {
                command.OnReceived();
            }

            if (destination.ShouldSend())
            {
                var header = destination.GetHeader(NetworkingAPI.GetNetworkHash(command.GetType()));

                if (NetworkServer.active)
                {
                    for (int i = 0; i < NetworkServer.connections.Count; ++i)
                    {
                        NetworkConnection conn = NetworkServer.connections[i];
                        if (conn == null)
                        {
                            continue;
                        }

                        using (Writer netWriter = NetworkingAPI.GetWriter(NetworkingAPI.CommandIndex, conn, QosType.Reliable)) {
                            NetworkWriter writer = netWriter;
                            writer.Write(header);
                        }
                    }
                }

                if (NetworkClient.active)
                {
                    using (var netWriter = NetworkingAPI.GetWriter(NetworkingAPI.CommandIndex, ClientScene.readyConnection, QosType.Reliable)) {
                        NetworkWriter writer = netWriter;
                        writer.Write(header);
                    }
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Send the passed request over the network
        /// </summary>
        /// <param name="request">Registered request</param>
        /// <param name="destination">Destination of the request</param>
        public static void Send <TRequest, TReply>(this TRequest request, NetworkDestination destination)
            where TRequest : INetRequest <TRequest, TReply>
            where TReply : INetRequestReply <TRequest, TReply>
        {
            if (destination.ShouldRun())
            {
                request.OnRequestReceived().OnReplyReceived();
            }

            if (destination.ShouldSend())
            {
                var header = destination.GetHeader(NetworkingAPI.GetNetworkHash(request.GetType()));

                if (NetworkServer.active)
                {
                    for (var i = 0; i < NetworkServer.connections.Count; ++i)
                    {
                        NetworkConnection conn = NetworkServer.connections[i];
                        if (conn == null)
                        {
                            continue;
                        }

                        if (NetworkServer.localClientActive && NetworkServer.localConnections.Contains(conn))
                        {
                            continue;
                        }

                        SendRequest <TRequest, TReply>(request, header, conn);
                    }
                }
                else if (NetworkClient.active)
                {
                    SendRequest <TRequest, TReply>(request, header, ClientScene.readyConnection);
                }
            }
        }