/// <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; }); } }
static ServerReceivers() { NetworkReceivers.RegisterServerReceiver("say", (sender, packet) => { Server.NotifyAll(sender.name + ": " + packet.ReadString()); }); NetworkReceivers.RegisterServerReceiver("kick", (sender, packet) => { if (!sender.admin) { Server.Notify(sender, "Command denied"); return; } Client target = Server.GetClient(packet.ReadString()); if (target == null) { Server.Notify(sender, "User not found"); return; } target.Disconnect(); Server.NotifyAll(target.name + " has been kicked from the server"); }); NetworkReceivers.RegisterServerReceiver("stopserver", (sender, packet) => { if (!sender.admin) { Server.Notify(sender, "Command denied"); return; } Server.Stop(); }); NetworkReceivers.RegisterServerReceiver("clientJoinInfo", (sender, packet) => { sender.name = packet.ReadString(); Server.NotifyAll(sender.name + " with ip " + sender.GetIp() + " joined the server"); Server.NotifyAll("admin = " + sender.admin); }); NetworkReceivers.RegisterServerReceiver("name", (sender, packet) => { string name = sender.name; sender.name = packet.ReadString(); Server.NotifyAll(name + " changed name to " + sender.name); }); }
private void SendMessage(MessageType messageType, NetworkReceivers receivers, byte[] data) { if (GameNetworker != null) { bufferBites.Clear(); bufferBites.Append(data); Networking.WriteCustom( (uint)messageType, GameNetworker, bufferBites, true, receivers); } }
/// <summary> /// Sends the forge transport object /// </summary> /// <param name="receivers">who will receive the transport object</param> /// <param name="reliable">send the packet reliably/unreliably</param> /// <remarks> /// Serializes the forge transport object, then sends it to all clients specified by the receivers. /// Subscribe a method to ForgeTransportObject.transportObject.transportFinished to decide what method should /// be executed when the object is received. /// </remarks> public void Send(NetworkReceivers receivers = NetworkReceivers.Others, bool reliable = true) { lock (serializerMutex) { serializer.Clear(); ObjectMapper.MapBytes(serializer, id); foreach (FieldInfo field in fields) { ObjectMapper.MapBytes(serializer, field.GetValue(this)); } Networking.WriteCustom(WriteCustomMapping.TRANSPORT_OBJECT, Networking.PrimarySocket, serializer, reliable, receivers); } }
/// <summary> /// Creates a network stream for the method with the specified string name and returns the method info /// </summary> /// <param name="methodName">The name of the method to call from this class</param> /// <param name="receivers">The players on the network that will be receiving RPC</param> /// <param name="arguments">The list of arguments that will be sent for the RPC</param> /// <returns></returns> private int GetStreamRPC(string methodName, NetworkReceivers receivers, params object[] arguments) { foreach (KeyValuePair <int, KeyValuePair <MethodInfo, List <IBRPCIntercept> > > rpc in RPCs) { if (rpc.Value.Key.Name == methodName) { if (!NetworkingManager.IsOnline) { return(rpc.Key); } getStreamBuffer.Clear(); ObjectMapper.MapBytes(getStreamBuffer, rpc.Key); #if UNITY_EDITOR int argCount = 0; foreach (var param in rpc.Value.Key.GetParameters()) { if (param.ParameterType != typeof(MessageInfo)) { argCount++; } } if (arguments.Length != argCount) { throw new NetworkException("The number of arguments [" + arguments.Length + "] provided for the " + methodName + " RPC call do not match the method signature argument count [" + rpc.Value.Key.GetParameters().Length + "]"); } #endif if (arguments != null && arguments.Length > 0) { ObjectMapper.MapBytes(getStreamBuffer, arguments); } bool buffered = receivers == NetworkReceivers.AllBuffered || receivers == NetworkReceivers.OthersBuffered; rpcNetworkingStream.SetProtocolType(OwningNetWorker is CrossPlatformUDP ? Networking.ProtocolType.UDP : Networking.ProtocolType.TCP); rpcNetworkingStream.Prepare(OwningNetWorker, NetworkingStream.IdentifierType.RPC, this, getStreamBuffer, receivers, buffered); return(rpc.Key); } } throw new NetworkException(14, "No method marked with [BRPC] was found by the name " + methodName); }
/// <summary> /// This is the main instantiate method that is proxyed through the Networking.Instantiate method /// </summary> /// <param name="receivers">The receivers that will get this instantiate command</param> /// <param name="obj">The name of the object that is to be instantiated</param> /// <param name="position">The position where the object will be instantiated at</param> /// <param name="rotation">The rotation that the object will have when instantiated</param> /// <param name="callbackCounter">The id for the callback that is to be called when this object is instantiated</param> public static void Instantiate(NetworkReceivers receivers, string obj, Vector3 position, Quaternion rotation, int callbackCounter) { ulong uniqueId = 0; if (!TryPullIdFromObject(obj, ref uniqueId)) { return; } if (Instance != null && Instance.OwningNetWorker != null) { Instance.RPC("NetworkInstantiate", receivers, Instance.OwningNetWorker.Uniqueidentifier, Instance.OwningNetWorker.IsServer ? uniqueId : 0, obj, position, rotation, callbackCounter); } else { setupActions.Add(() => { Instance.RPC("NetworkInstantiate", receivers, Instance.OwningNetWorker.Uniqueidentifier, Instance.OwningNetWorker.IsServer ? uniqueId : 0, obj, position, rotation, callbackCounter); }); } }
public static void Instantiate(NetworkReceivers receivers, string obj, Vector3 position, Quaternion rotation, int callbackCounter) { if (NetworkingManager.Instance.OwningNetWorker != null) NetworkingManager.Instance.RPC("NetworkInstantiate", receivers, NetworkingManager.Instance.OwningNetWorker.Uniqueidentifier, NetworkingManager.Instance.OwningNetWorker.IsServer ? SimpleNetworkedMonoBehavior.GenerateUniqueId() : 0, obj, position, rotation, callbackCounter); else { setupActions.Add(() => { NetworkingManager.Instance.RPC("NetworkInstantiate", receivers, NetworkingManager.Instance.OwningNetWorker.Uniqueidentifier, NetworkingManager.Instance.OwningNetWorker.IsServer ? SimpleNetworkedMonoBehavior.GenerateUniqueId() : 0, obj, position, rotation, callbackCounter); }); } }
/// <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 )); } }
/// <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; }); } }
/// <summary> /// Instantiate an object on the network /// </summary> /// <param name="obj">Object to be instantiated by object name</param> /// <param name="receivers">Recipients will receive this instantiate call (Default: All)</param> public static void Instantiate(GameObject obj, NetworkReceivers receivers = NetworkReceivers.All, Action<GameObject> callback = null) { CallInstantiate(obj.name, receivers, callback: callback); }
/// <summary> /// Instantiate an object on the network from the resources folder /// </summary> /// <param name="resourcePath">Location of the resource</param> /// <param name="position">Position of instantiated object</param> /// <param name="rotation">Rotation of instantiated object</param> /// <param name="receivers">Recipients will receive this instantiate call (Default: All)</param> public static void InstantiateFromResources(string resourcePath, Vector3 position, Quaternion rotation, NetworkReceivers receivers = NetworkReceivers.All, Action<GameObject> callback = null) { GameObject obj = Resources.Load<GameObject>(resourcePath); CallInstantiate(obj.name, position, rotation, receivers, callback); }
/// <summary> /// Prepare the NetworkingStream to be used /// </summary> /// <param name="socket">The NetWorker socket to be used</param> /// <param name="identifierType">The type of Identifier it is going to prepare</param> /// <param name="networkedBehavior">NetworkedBehavior to use</param> /// <param name="extra">Extra parameters to prepare</param> /// <param name="receivers">Who shall be receiving this NetworkingStream</param> /// <param name="bufferedRPC">To know if this is a Buffered RPC</param> /// <param name="customidentifier">A custom Identifier to be passed through</param> /// <returns></returns> public NetworkingStream Prepare(NetWorker socket, IdentifierType identifierType, ulong networkBehaviorId, BMSByte extra = null, NetworkReceivers receivers = NetworkReceivers.All, bool bufferedRPC = false, uint customidentifier = 0, ulong senderId = 0, bool noBehavior = false) { if (noBehavior) { NetworkedBehavior = null; } else { lock (networkedObjectMutex) { NetworkedBehavior = SimpleNetworkedMonoBehavior.Locate(networkBehaviorId); } } if (ReferenceEquals(NetworkedBehavior, null) && (extra == null || extra.Size == 0)) { throw new NetworkException(9, "Prepare was called but nothing was sent to write"); } NetworkedBehaviorId = networkBehaviorId; return(PrepareFinal(socket, identifierType, networkBehaviorId, extra, receivers, bufferedRPC, customidentifier, senderId)); }
/// <summary> /// Instantiate an object on the network from the resources folder /// </summary> /// <param name="resourcePath">Location of the resource</param> /// <param name="receivers">Recipients will receive this instantiate call (Default: All)</param> public static void InstantiateFromResources(string resourcePath, NetworkReceivers receivers = NetworkReceivers.All, Action<GameObject> callback = null) { GameObject obj = Resources.Load<GameObject>(resourcePath); if (NetworkingManager.Instance == null) { NetworkingManager.setupActions.Add(() => { CallInstantiate(obj.name, obj.transform.position, obj.transform.rotation, receivers, callback); }); } else CallInstantiate(obj.name, obj.transform.position, obj.transform.rotation, receivers, callback); }
private static void CallInstantiate(string obj, NetworkReceivers receivers, Action<SimpleNetworkedMonoBehavior> callback = null) { SimpleNetworkedMonoBehavior netBehavior; if (ValidateNetworkedObject(obj, out netBehavior)) { if (callback != null) { instantiateCallbacks.Add(callbackCounter, callback); NetworkingManager.Instantiate(receivers, obj, netBehavior.transform.position, netBehavior.transform.rotation, callbackCounter); callbackCounter++; if (callbackCounter == 0) callbackCounter++; } else { NetworkingManager.Instantiate(receivers, obj, netBehavior.transform.position, netBehavior.transform.rotation, 0); } } }
/// <summary> /// Call an Unreliable RPC method with a receiver and arguments /// </summary> /// <param name="methodName">Method(Function) name to call</param> /// <param name="rpcMode">Who shall receive the RPC</param> /// <param name="arguments">Extra parameters passed in</param> public void URPC(string methodName, NetworkReceivers rpcMode, params object[] arguments) { URPC(methodName, OwningNetWorker, rpcMode, arguments); }
private void SendMessage (MessageType messageType, NetworkReceivers receivers, byte[] data) { if (GameNetworker != null) { bufferBites.Clear (); bufferBites.Append (data); Networking.WriteCustom ( (uint)messageType, GameNetworker, bufferBites, true, receivers); } }
/// <summary> /// Instantiate an object on the network /// </summary> /// <param name="obj">Object to be instantiated by object name</param> /// <param name="receivers">Recipients will receive this instantiate call</param> public static void Instantiate(string obj, NetworkReceivers receivers, Action<GameObject> callback = null) { if (NetworkingManager.Instance == null) { NetworkingManager.setupActions.Add(() => { CallInstantiate(obj, receivers, callback: callback); }); } else CallInstantiate(obj, receivers, callback: callback); }
/// <summary> /// This is the main instantiate method that is proxyed through the Networking.Instantiate method /// </summary> /// <param name="receivers">The receivers that will get this instantiate command</param> /// <param name="obj">The name of the object that is to be instantiated</param> /// <param name="position">The position where the object will be instantiated at</param> /// <param name="rotation">The rotation that the object will have when instantiated</param> /// <param name="callbackCounter">The id for the callback that is to be called when this object is instantiated</param> public static void Instantiate(NetworkReceivers receivers, string obj, Vector3 position, Quaternion rotation, int callbackCounter) { ulong uniqueId = 0; if (!TryPullIdFromObject(obj, ref uniqueId)) return; if (Instance != null && Instance.OwningNetWorker != null) Instance.RPC("NetworkInstantiate", receivers, Instance.OwningNetWorker.Uniqueidentifier, Instance.OwningNetWorker.IsServer ? uniqueId : 0, obj, position, rotation, callbackCounter); else { setupActions.Add(() => { Instance.RPC("NetworkInstantiate", receivers, Instance.OwningNetWorker.Uniqueidentifier, Instance.OwningNetWorker.IsServer ? uniqueId : 0, obj, position, rotation, callbackCounter); }); } }
public void Send(NetworkReceivers targets) { cachedData.Clone(Serialize()); Networking.WriteCustom(UID, Networking.PrimarySocket, cachedData, true, targets); }
private static void CallInstantiate(string obj, Vector3 position, Quaternion rotation, NetworkReceivers receivers, Action<GameObject> callback = null) { SimpleNetworkedMonoBehavior netBehavior; if (ValidateNetworkedObject(obj, out netBehavior)) { if (callback != null) { instantiateCallbacks.Add(callbackCounter, callback); NetworkingManager.Instantiate(receivers, obj, position, rotation, callbackCounter); callbackCounter++; if (callbackCounter == 0) callbackCounter++; } else NetworkingManager.Instantiate(receivers, obj, position, rotation, 0); } }
/// <summary> /// Instantiate an object on the network /// </summary> /// <param name="obj">Object to be instantiated by object name</param> /// <param name="position">Position of instantiated object</param> /// <param name="rotation">Rotation of instantiated object</param> /// <param name="receivers">Recipients will receive this instantiate call</param> public static void Instantiate(string obj, Vector3 position, Quaternion rotation, NetworkReceivers receivers, Action<SimpleNetworkedMonoBehavior> callback = null) { if (NetworkingManager.Instance == null || !NetworkingManager.Instance.IsSetup) { NetworkingManager.setupActions.Add(() => { CallInstantiate(obj, position, rotation, receivers, callback); }); } else CallInstantiate(obj, position, rotation, receivers, callback); }
/// <summary> /// Instantiate an object on the network /// </summary> /// <param name="obj">Object to be instantiated by object name</param> /// <param name="position">Position of instantiated object</param> /// <param name="rotation">Rotation of instantiated object</param> /// <param name="receivers">Recipients will receive this instantiate call (Default: All)</param> public static void Instantiate(GameObject obj, Vector3 position, Quaternion rotation, NetworkReceivers receivers = NetworkReceivers.All, Action<GameObject> callback = null) { if (NetworkingManager.Instance == null) { NetworkingManager.setupActions.Add(() => { CallInstantiate(obj.name, position, rotation, receivers, callback); }); } else CallInstantiate(obj.name, position, rotation, receivers, callback); }
/// <summary> /// Instantiate an object on the network /// </summary> /// <param name="obj">Object to be instantiated by object name</param> /// <param name="position">Position of instantiated object</param> /// <param name="rotation">Rotation of instantiated object</param> /// <param name="receivers">Recipients will receive this instantiate call (Default: All)</param> public static void Instantiate(GameObject obj, Vector3 position, Quaternion rotation, NetworkReceivers receivers = NetworkReceivers.All, Action<SimpleNetworkedMonoBehavior> callback = null) { if (!NetworkingManager.IsOnline) { // JM: offline fixes SimpleNetworkedMonoBehavior snmb = (GameObject.Instantiate (obj, position, rotation) as GameObject).GetComponent<SimpleNetworkedMonoBehavior> (); snmb.OfflineStart (); callback(snmb); return; } if (NetworkingManager.Instance == null || !NetworkingManager.Instance.IsSetup) { NetworkingManager.setupActions.Add(() => { Instantiate(obj, position, rotation, receivers, callback); }); } else CallInstantiate(obj.name, position, rotation, receivers, callback); }
/// <summary> /// Instantiate an object on the network from the resources folder /// </summary> /// <param name="resourcePath">Location of the resource</param> /// <param name="position">Position of instantiated object</param> /// <param name="rotation">Rotation of instantiated object</param> /// <param name="receivers">Recipients will receive this instantiate call (Default: All)</param> public static void InstantiateFromResources(string resourcePath, Vector3 position, Quaternion rotation, NetworkReceivers receivers = NetworkReceivers.All, Action<SimpleNetworkedMonoBehavior> callback = null) { GameObject obj = Resources.Load<GameObject>(resourcePath); if (NetworkingManager.Instance == null || !NetworkingManager.Instance.IsSetup) { NetworkingManager.setupActions.Add(() => { CallInstantiate(obj.name, position, rotation, receivers, callback); }); } else CallInstantiate(obj.name, position, rotation, receivers, callback); }
/// <summary> /// The final steps for preparing the NetworkingStream /// </summary> /// <param name="socket">The NetWorker socket to be used</param> /// <param name="identifierType">The type of Identifier it is going to prepare</param> /// <param name="behaviorNetworkId">NetworkedBehavior to use</param> /// <param name="extra">Extra parameters to prepare</param> /// <param name="receivers">Who shall be receiving this NetworkingStream</param> /// <param name="bufferedRPC">To know if this is a Buffered RPC</param> /// <param name="customidentifier">A custom Identifier to be passed through</param> /// <returns></returns> public NetworkingStream PrepareFinal(NetWorker socket, IdentifierType identifierType, ulong behaviorNetworkId, BMSByte extra = null, NetworkReceivers receivers = NetworkReceivers.All, bool bufferedRPC = false, uint customidentifier = 0, ulong senderId = 0) { lock (networkedObjectMutex) { if (senderId == 0) { senderId = socket.Me != null ? socket.Me.NetworkId : 0; } NetworkedBehaviorId = behaviorNetworkId; RealSenderId = senderId; Receivers = receivers; Customidentifier = customidentifier; BufferedRPC = Receivers == NetworkReceivers.AllBuffered || Receivers == NetworkReceivers.OthersBuffered; Bytes.Clear(); ObjectMapper.MapBytes(bytes, (int)ProtocolType); ObjectMapper.MapBytes(bytes, (int)receivers); ObjectMapper.MapBytes(bytes, socket.Uniqueidentifier); if (ProtocolType != Networking.ProtocolType.HTTP && ProtocolType != Networking.ProtocolType.QuickUDP && ProtocolType != Networking.ProtocolType.QuickTCP) { this.identifierType = identifierType; if (identifierType == IdentifierType.None) { Bytes.BlockCopy <byte>(((byte)identifier_NONE), 1); } else if (identifierType == IdentifierType.RPC) { Bytes.BlockCopy <byte>(((byte)identifier_RPC), 1); Bytes.BlockCopy <byte>(((byte)(bufferedRPC ? 1 : 0)), 1); } else if (identifierType == IdentifierType.Player) { Bytes.BlockCopy <byte>(((byte)identifier_PLAYER), 1); } else if (identifierType == IdentifierType.NetworkedBehavior) { Bytes.BlockCopy <byte>(((byte)identifier_NETWORKED_BEHAVIOR), 1); } else if (identifierType == IdentifierType.Disconnect) { Bytes.BlockCopy <byte>(((byte)identifier_DISCONNECT), 1); } else if (identifierType == IdentifierType.Custom) { Bytes.BlockCopy <byte>(((byte)identifier_CUSTOM), 1); } ObjectMapper.MapBytes(bytes, behaviorNetworkId); } if (identifierType == IdentifierType.Custom) { ObjectMapper.MapBytes(bytes, Customidentifier); } if (extra != null) { Bytes.BlockCopy(extra.byteArr, extra.StartIndex(), extra.Size); } if (!ReferenceEquals(NetworkingManager.Instance, null)) { ObjectMapper.MapBytes(Bytes, NetworkingManager.Instance.CurrentFrame); } else { ObjectMapper.MapBytes(Bytes, (byte)0); } if (ProtocolType == Networking.ProtocolType.TCP) { List <byte> head = new List <byte>(BitConverter.GetBytes(Bytes.Size + 1)); head.Add(0); Bytes.InsertRange(0, head.ToArray()); } Ready = true; return(this); } }
/// <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> /// Instantiate an object on the network /// </summary> /// <param name="obj">Object to be instantiated by object name</param> /// <param name="receivers">Recipients will receive this instantiate call</param> public static void Instantiate(string obj, NetworkReceivers receivers, Action<GameObject> callback = null) { CallInstantiate(obj, receivers, callback: callback); }
/// <summary> /// Creates a network stream for the method with the specified string name and returns the method info /// </summary> /// <param name="methodName">The name of the method to call from this class</param> /// <param name="receivers">The players on the network that will be receiving RPC</param> /// <param name="arguments">The list of arguments that will be sent for the RPC</param> /// <returns></returns> private MethodInfo GetStreamRPC(string methodName, NetworkReceivers receivers, params object[] arguments) { foreach (KeyValuePair<int, MethodInfo> rpc in RPCs) { if (rpc.Value.Name == methodName) { getStreamBuffer.Clear(); ObjectMapper.MapBytes(getStreamBuffer, rpc.Key); if (arguments != null && arguments.Length > 0) ObjectMapper.MapBytes(getStreamBuffer, arguments); bool buffered = receivers == NetworkReceivers.AllBuffered || receivers == NetworkReceivers.OthersBuffered; rpcNetworkingStream.SetProtocolType(OwningNetWorker is CrossPlatformUDP ? Networking.ProtocolType.UDP : Networking.ProtocolType.TCP); rpcNetworkingStream.Prepare(OwningNetWorker, NetworkingStream.IdentifierType.RPC, this, getStreamBuffer, receivers, buffered); return rpc.Value; } } throw new NetworkException(14, "No method marked with [BRPC] was found by the name " + methodName); }
/// <summary> /// Instantiate an object on the network /// </summary> /// <param name="obj">Object to be instantiated by object name</param> /// <param name="position">Position of instantiated object</param> /// <param name="rotation">Rotation of instantiated object</param> /// <param name="receivers">Recipients will receive this instantiate call (Default: All)</param> public static void Instantiate(GameObject obj, Vector3 position, Quaternion rotation, NetworkReceivers receivers = NetworkReceivers.All, Action<GameObject> callback = null) { CallInstantiate(obj.name, position, rotation, receivers, callback); }
/// <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> /// TODO /// </summary> /// <param name="id">Unique identifier to be used</param> /// <param name="port">Port to be written to</param> /// <param name="data">Data to send over</param> public static void WriteCustom(string id, ushort port, BMSByte data, NetworkReceivers recievers = NetworkReceivers.All) { WriteCustom(id, Sockets[port], data, false, recievers); }
/// <summary> /// Used to serialize manual properties across the network for this object /// </summary> /// <param name="reliable">Determines if these properties should be reliably sent</param> /// <param name="receivers">The receivers for this data</param> public void SerializeManualProperties(bool reliable = false, NetworkReceivers receivers = NetworkReceivers.All) { if (ManualProperties == null || ManualProperties.Count == 0 || !IsSetup) return; manualPropertyBytes.Clear(); ObjectMapper.MapBytes(manualPropertyBytes, NetworkedId); foreach (NetRef<object> obj in ManualProperties) { ObjectMapper.MapBytes(manualPropertyBytes, obj.Value); obj.Callback(this); } Networking.WriteCustom(WriteCustomMapping.NETWORKED_MONO_BEHAVIOR_MANUAL_PROPERTIES, OwningNetWorker, manualPropertyBytes, reliable, receivers); }
/// <summary> /// Creates a network stream for the method with the specified string name and returns the method info /// </summary> /// <param name="methodName">The name of the method to call from this class</param> /// <param name="receivers">The players on the network that will be receiving RPC</param> /// <param name="arguments">The list of arguments that will be sent for the RPC</param> /// <returns></returns> private int GetStreamRPC(string methodName, NetworkReceivers receivers, params object[] arguments) { foreach (KeyValuePair<int, KeyValuePair<MethodInfo, List<IBRPCIntercept>>> rpc in RPCs) { if (rpc.Value.Key.Name == methodName) { if (!NetworkingManager.IsOnline) return rpc.Key; getStreamBuffer.Clear(); ObjectMapper.MapBytes(getStreamBuffer, rpc.Key); #if UNITY_EDITOR int argCount = 0; foreach (var param in rpc.Value.Key.GetParameters()) { if (param.ParameterType != typeof(MessageInfo)) argCount++; } if (arguments.Length != argCount) throw new NetworkException("The number of arguments [" + arguments.Length + "] provided for the " + methodName + " RPC call do not match the method signature argument count [" + rpc.Value.Key.GetParameters().Length + "]"); #endif if (arguments != null && arguments.Length > 0) ObjectMapper.MapBytes(getStreamBuffer, arguments); bool buffered = receivers == NetworkReceivers.AllBuffered || receivers == NetworkReceivers.OthersBuffered; rpcNetworkingStream.SetProtocolType(OwningNetWorker is CrossPlatformUDP ? Networking.ProtocolType.UDP : Networking.ProtocolType.TCP); rpcNetworkingStream.Prepare(OwningNetWorker, NetworkingStream.IdentifierType.RPC, this, getStreamBuffer, receivers, buffered); return rpc.Key; } } throw new NetworkException(14, "No method marked with [BRPC] was found by the name " + methodName); }