/// <summary>
        /// Calls a function for a connection.
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        /// <param name="onConnection">The connection to call the function for.</param>
        /// <param name="callback">The callback for the return value, sent by the called connection.</param>
        /// <param name="flags">Flags for the packet settings.</param>
        /// <param name="deliveryMethod">The way this packet will reach the connection.</param>
        /// <param name="args">Any data associated with the function to be handled by the other application.</param>
        public bool CallFunction(string functionName, NetConnection onConnection, RemoteFunctionCallback callback,
                                 RemoteFlag flags, NetDeliveryMethod deliveryMethod, params object[] args)
        {
            NetBuffer data = new NetBuffer();

            // Write the data for the function
            for (int i = 0; i < args.Length; i++)
            {
                data.WriteDynamic(args[i]);
            }

            return(CallFunc(functionName, onConnection, callback, flags, deliveryMethod, data, (ushort)args.Length));
        }
        bool CallFunc(string functionName, NetConnection onConnection, RemoteFunctionCallback callback,
                      RemoteFlag flags, NetDeliveryMethod deliveryMethod, NetBuffer data, ushort numArgs)
        {
            // Allocate Id
            ushort callbackId = GetNextCallbackId();

            // Create the function packet
            NetOutboundPacket funcPacket = new NetOutboundPacket(deliveryMethod);

            funcPacket.Type = NetPacketType.RemoteFunction;

            // Handle the flags
            funcPacket.Encrypt = flags.HasFlag(RemoteFlag.Encrypt);
            if (flags.HasFlag(RemoteFlag.DontCompress))
            {
                funcPacket.Compression = NetPacketCompression.None;
            }
            funcPacket.SendImmediately = flags.HasFlag(RemoteFlag.SendImmediately);

            // Write the function header
            funcPacket.Write((byte)Type);
            funcPacket.Write(Id);
            funcPacket.Write(functionName);
            funcPacket.Write(callbackId);
            funcPacket.Write(numArgs);

            // Write the data for the function
            funcPacket.WriteBytes(data.data, 0, data.Length);

            // Add the waiting function for the return value callback
            if (WaitingFunctions.TryAdd(callbackId, callback))
            {
                // Send the event packet
                onConnection.SendPacket(funcPacket);
                return(true);
            }
            else
            {
                NetLogger.LogError("Failed to call function {0}, this function is already in the middle of being called!",
                                   functionName);
                return(false);
            }
        }
 /// <summary>
 /// Calls a function for a connection.
 /// </summary>
 /// <param name="functionName">The name of the function.</param>
 /// <param name="onConnection">The connection to call the function for.</param>
 /// <param name="callback">The callback for the return value, sent by the called connection.</param>
 /// <param name="flags">Flags for the packet settings.</param>
 /// <param name="deliveryMethod">The way this packet will reach the connection.</param>
 /// <param name="args">Any data associated with the function to be handled by the other application.</param>
 public bool CallFunction(string functionName, NetConnection onConnection, RemoteFunctionCallback callback,
                          RemoteFlag flags, NetDeliveryMethod deliveryMethod, NetBuffer data)
 {
     return(CallFunc(functionName, onConnection, callback, flags, deliveryMethod, data, 0));
 }
 /// <summary>
 /// Calls a function for a connection.
 /// </summary>
 /// <param name="functionName">The name of the function.</param>
 /// <param name="onConnection">The connection to call the function for.</param>
 /// <param name="callback">The callback for the return value, sent by the called connection.</param>
 /// <param name="args">Any data associated with the function to be handled by the other application.</param>
 public bool CallFunction(string functionName, NetConnection onConnection, RemoteFunctionCallback callback,
                          NetBuffer data)
 {
     return(CallFunction(functionName, onConnection, callback, RemoteFlag.None, NetDeliveryMethod.Reliable, data));
 }
 /// <summary>
 /// Calls a function for a connection.
 /// </summary>
 /// <param name="functionName">The name of the function.</param>
 /// <param name="onConnection">The connection to call the function for.</param>
 /// <param name="callback">The callback for the return value, sent by the called connection.</param>
 /// <param name="flags">Flags for the packet settings.</param>
 /// <param name="args">Any data associated with the function to be handled by the other application.</param>
 public bool CallFunction(string functionName, NetConnection onConnection, RemoteFunctionCallback callback,
                          RemoteFlag flags, params object[] args)
 {
     return(CallFunction(functionName, onConnection, callback, flags, NetDeliveryMethod.Reliable, args));
 }