예제 #1
0
		/// <summary>
		/// TODO
		/// </summary>
		/// <param name="id">Unique identifier to be used</param>
		/// <param name="netWorker">The NetWorker(Socket) to write with</param>
		/// <param name="data">Data to send over</param>
		/// <param name="reliableUDP">If this be a reliable UDP</param>
		public static void WriteCustom(string id, NetWorker netWorker, BMSByte data, NetworkingPlayer target, bool reliableUDP = false)
		{
			if (!netWorker.IsServer)
				throw new NetworkException("Currently this overload of WriteCustom is only supported being called on the server.");

			if (netWorker is CrossPlatformUDP)
			{
				netWorker.Write(id, target, new NetworkingStream().Prepare(
					netWorker, NetworkingStream.IdentifierType.Custom, null, data, NetworkReceivers.Others, reliableUDP, id
				), reliableUDP);
			}
			else
			{
				netWorker.Write(target, new NetworkingStream().Prepare(
					netWorker, NetworkingStream.IdentifierType.Custom, null, data, NetworkReceivers.Others, reliableUDP, id
				));
			}
		}
예제 #2
0
		/// <summary>
		/// Write to the TCP given a NetWorker(Socket) with a stream of data
		/// </summary>
		/// <param name="socket">The NetWorker(Socket) to write with</param>
		/// <param name="stream">The stream of data to be written</param>
		public static void WriteTCP(NetWorker socket, NetworkingStream stream)
		{
			socket.Write(stream);
		}
예제 #3
0
		/// <summary>
		/// TODO
		/// </summary>
		/// <param name="id">Unique identifier to be used</param>
		/// <param name="netWorker">The NetWorker(Socket) to write with</param>
		/// <param name="data">Data to send over</param>
		/// <param name="reliableUDP">If this be a reliable UDP</param>
		public static void WriteCustom(string id, NetWorker netWorker, BMSByte data, bool reliableUDP = false, NetworkReceivers recievers = NetworkReceivers.All)
		{
			if (netWorker is CrossPlatformUDP)
			{
				netWorker.Write(id, new NetworkingStream().Prepare(
					netWorker, NetworkingStream.IdentifierType.Custom, null, data, recievers, reliableUDP, id
				), reliableUDP);
			}
			else
			{
				netWorker.Write(new NetworkingStream().Prepare(
					netWorker, NetworkingStream.IdentifierType.Custom, null, data, recievers, reliableUDP, id
				));
			}
		}
예제 #4
0
		/// <summary>
		/// Writes a <see cref="NetworkingStream"/> to a particular <see cref="NetWorker"/> directly to a player (if the port is a server)
		/// </summary>
		/// <param name="socket">NetWorker(Socket) to write with</param>
		/// <param name="player">Player to be written to server</param>
		/// <param name="stream">The stream of data to be written</param>
		/// <exception cref="NetworkException">Thrown when there is not a <see cref="NetWorker"/> on the supplied port</exception>
		/// <exception cref="NetworkException">Thrown when the <see cref="NetWorker"/> on the specified port is not a server</exception>
		public static void Write(NetWorker socket, NetworkingPlayer player, NetworkingStream stream)
		{
			if (!socket.IsServer)
				throw new NetworkException("Writing to particular players can only be done by the server, the NetWorker on the specified port is not a server");

			socket.Write(player, stream);
		}
예제 #5
0
		/// <summary>
		/// Writes a <see cref="NetworkingStream"/> to a particular <see cref="NetWorker"/>
		/// </summary>
		/// <param name="socket">NetWorker(Socket) to write with</param>
		/// <param name="identifier">Unique identifier to be used</param>
		/// <param name="stream">The stream of data to be written</param>
		/// <param name="reliable">If this be a reliable UDP</param>
		public static void WriteUDP(NetWorker socket, string identifier, NetworkingStream stream, bool reliable = false)
		{
			socket.Write(identifier, stream, reliable);
		}
		/// <summary>
		/// Call an Unreliable RPC method on a NetWorker(Socket) with receivers and arguments
		/// </summary>
		/// <param name="methodName">Method(Function) name to call</param>
		/// <param name="socket">The NetWorker(Socket) being used</param>
		/// <param name="receivers">Who shall receive the RPC</param>
		/// <param name="arguments">The RPC function parameters to be passed in</param>
		public void URPC(string methodName, NetWorker socket, NetworkReceivers receivers, params object[] arguments)
		{
			MethodInfo rpc = GetStreamRPC(methodName, receivers, arguments);

			if (socket is CrossPlatformUDP)
				((CrossPlatformUDP)socket).Write("BMS_INTERNAL_Rpc_" + methodName, rpcNetworkingStream, false);
			else
				socket.Write(rpcNetworkingStream);

			if (socket.IsServer && receivers != NetworkReceivers.Others && receivers != NetworkReceivers.OthersBuffered && receivers != NetworkReceivers.OthersProximity)
				rpc.Invoke(this, arguments);
		}
		/// <summary>
		/// Used for the server to call an RPC method on a NetWorker(Socket) on a particular player
		/// </summary>
		/// <param name="methodName">Method(Function) name to call</param>
		/// <param name="socket">The NetWorker(Socket) being used</param>
		/// <param name="player">The NetworkingPlayer who will execute this RPC</param>
		/// <param name="arguments">The RPC function parameters to be passed in</param>
		public void AuthoritativeRPC(string methodName, NetWorker socket, NetworkingPlayer player, bool runOnServer, params object[] arguments)
		{
			MethodInfo rpc = GetStreamRPC(methodName, NetworkReceivers.All, arguments);

			if (socket is CrossPlatformUDP)
				((CrossPlatformUDP)socket).Write("BMS_INTERNAL_Rpc_" + methodName, player, rpcNetworkingStream, true);
			else
				socket.Write(player, rpcNetworkingStream);

			if (socket.IsServer && runOnServer)
			{
				CallOnMainThread(delegate(object[] args)
				{
					rpc.Invoke(this, arguments);
				});
			}
		}
예제 #8
0
		/// <summary>
		/// This allows you to write custom data across the network and is useful for serializing entire classes if needed
		/// </summary>
		/// <param name="id">Unique identifier to be used</param>
		/// <param name="netWorker">The NetWorker(Socket) to write with</param>
		/// <param name="data">Data to send over</param>
		/// <param name="reliableUDP">If this be a reliable UDP</param>
		public static void WriteCustom(uint id, NetWorker netWorker, BMSByte data, bool reliableUDP = false, NetworkReceivers recievers = NetworkReceivers.All)
		{
			NetworkingStream stream = new NetworkingStream(netWorker is CrossPlatformUDP ? ProtocolType.UDP : ProtocolType.TCP).Prepare(netWorker, NetworkingStream.IdentifierType.Custom, null,
				data, recievers, netWorker is CrossPlatformUDP && reliableUDP, id);

			if (netWorker.IsServer)
			{
				switch (recievers)
				{
					case NetworkReceivers.Server:
					case NetworkReceivers.All:
					case NetworkReceivers.AllBuffered:
					case NetworkReceivers.AllProximity:
						BMSByte returnBytes = stream.Bytes;
						netWorker.ExecuteCustomRead(id, netWorker.Me, new NetworkingStream().Consume(netWorker, netWorker.Me, returnBytes));
						break;
				}

				if (recievers == NetworkReceivers.Server || recievers == NetworkReceivers.ServerAndOwner) // If only sending to the server, then just execute it itself.
					return;
			}

			if (netWorker is CrossPlatformUDP)
				netWorker.Write("BMS_INTERNAL_Write_Custom_" + id.ToString(), stream, reliableUDP);
			else
				netWorker.Write(stream);
		}
		/// <summary>
		/// Call an Unreliable RPC method on a NetWorker(Socket) with receivers and arguments
		/// </summary>
		/// <param name="methodName">Method(Function) name to call</param>
		/// <param name="socket">The NetWorker(Socket) being used</param>
		/// <param name="receivers">Who shall receive the RPC</param>
		/// <param name="arguments">The RPC function parameters to be passed in</param>
		public void URPC(string methodName, NetWorker socket, NetworkReceivers receivers, params object[] arguments)
		{
			int rpcId = GetStreamRPC(methodName, receivers, arguments);

			// JM: offline fix
			if (NetworkingManager.IsOnline)
			{
				if (socket is CrossPlatformUDP)
					((CrossPlatformUDP)socket).Write ("BMS_INTERNAL_Rpc_" + methodName, rpcNetworkingStream, false);
				else
					socket.Write (rpcNetworkingStream);
			}

			// JM: added offline check and simular change that was in the reliable RPC
			if ((!NetworkingManager.IsOnline || socket.IsServer) && receivers != NetworkReceivers.Others && receivers != NetworkReceivers.OthersBuffered && receivers != NetworkReceivers.OthersProximity)
			{
				Unity.MainThreadManager.Run(() =>
				{
					bool faildValidate = false;

					foreach (IBRPCIntercept intercept in RPCs[rpcId].Value)
					{
						if (!intercept.ValidateRPC(RPCs[rpcId].Key))
						{
							faildValidate = true;
							break;
						}
					}

					if (faildValidate)
						return;

					List<object> args = new List<object>();
					int argCount = 0;
					foreach (ParameterInfo info in RPCs[rpcId].Key.GetParameters())
					{
						if (info.ParameterType == typeof(MessageInfo))
							args.Add(new MessageInfo(OwningNetWorker.Me.NetworkId, NetworkingManager.Instance.CurrentFrame));
						else
							args.Add(arguments[argCount++]);
					}
				
					CurrentRPCSender = OwningPlayer;
					RPCs[rpcId].Key.Invoke(this, args.ToArray());
					CurrentRPCSender = null;
				});
			}
		}
		private void AuthRPC(string methodName, NetWorker socket, NetworkingPlayer player, bool runOnServer, string uniqueIdentifier, bool reliable, params object[] arguments)
		{
			int rpcId = GetStreamRPC(methodName, NetworkReceivers.All, arguments);

			if (socket is CrossPlatformUDP)
				((CrossPlatformUDP)socket).Write(uniqueIdentifier + methodName, player, rpcNetworkingStream, reliable);
			else
				socket.Write(player, rpcNetworkingStream);

			if (socket.IsServer && runOnServer)
			{
				Unity.MainThreadManager.Run(() =>
				{
					bool failedValidate = false;

					foreach (IBRPCIntercept intercept in RPCs[rpcId].Value)
					{
						if (!intercept.ValidateRPC(RPCs[rpcId].Key))
						{
							failedValidate = true;
							break;
						}
					}

					if (!failedValidate)
						RPCs[rpcId].Key.Invoke(this, arguments);
				});
			}
		}