/// <summary> /// Destroys all objects that given connection owns. /// </summary> public static void DestroyAllOwnedObjectsOfConnection(QNetConnection connection) { if (!QNetManager.IsServerActive) { throw new InvalidOperationException("This methods can only be used by server."); } for (var index = 0; index < QNetObjectBehaviour.SpawnedBehaviours.Length; index++) { var obj = QNetObjectBehaviour.SpawnedBehaviours[index]; if (obj.OwnerIdentity == 0 || obj.OwnerIdentity != connection.ConnectionIdentity) { continue; } QNetObjectBehaviour.Destroy(obj); index--; } for (var index = 0; index < QNetObjectBehaviour.PredefinedBehaviours.Length; index++) { var obj = QNetObjectBehaviour.PredefinedBehaviours[index]; if (obj.OwnerIdentity == 0 || obj.OwnerIdentity != connection.ConnectionIdentity) { continue; } QNetObjectBehaviour.Destroy(obj); index--; } }
private QNetPlayer(QNetConnection connection, string nickname, uint token) { Connection = connection; ConnectionIdentity = connection.ConnectionIdentity; Nickname = nickname; Token = token; }
/// <summary> /// Checks if given connection owns this object. /// </summary> public bool CheckConnectionOwner(QNetConnection connection) { if (QNetManager.IsHostActive && (OwnerIdentity == 0 || connection.ConnectionIdentity == QNetManager.Client.ConnectionIdentity || connection.ConnectionIdentity == 0)) { return(true); } return(OwnerIdentity == connection.ConnectionIdentity); }
/// <summary> /// QNet entity outgoing message constructor. /// </summary> public QNetEntityOutgoingMessage(QNetEntity entity, bool isServer, QNetConnection target) { Entity = entity; FromServer = isServer; FromClient = !isServer; Target = target; Writer = isServer ? QNetManager.Server.GenerateOutgoingMessage((ushort)QNetUnityLocalHeader.ENTITY_QUERY) : QNetManager.Client.GenerateOutgoingMessage((ushort)QNetUnityLocalHeader.ENTITY_QUERY); Channel = (byte)QNetUnityLocalChannel.ENTITY_QUERY; Writer.WriteInt16(Entity.ObjectIdentity); }
/// <summary> /// Sends given QNetObject to given connection. /// </summary> public static void SendObjectToConnection(QNetConnection connection, [NotNull] QNetObjectBehaviour qNetObject) { if (!QNetManager.IsServerActive) { throw new InvalidOperationException("This methods can only be used by server."); } if (qNetObject == null) { throw new ArgumentNullException(nameof(qNetObject)); } QNetManager.Server.Send(connection, QNetUnityLocalChannel.OBJECT_QUERY, QNetMessageMethod.ReliableOrdered, ResolveObjectCreateMessage(qNetObject)); }
/// <summary> /// Sends all QNetObjects in map to given connection. /// </summary> public static void SendAllObjectsToConnection(QNetConnection connection) { if (!QNetManager.IsServerActive) { throw new InvalidOperationException("This methods can only be used by server."); } for (var index = 0; index < QNetObjectBehaviour.PredefinedBehaviours.Length; index++) { var qNetObject = QNetObjectBehaviour.PredefinedBehaviours[index]; SendObjectToConnection(connection, qNetObject); } for (var index = 0; index < QNetObjectBehaviour.SpawnedBehaviours.Length; index++) { var qNetObject = QNetObjectBehaviour.SpawnedBehaviours[index]; SendObjectToConnection(connection, qNetObject); } }
/// <summary> /// Prepare new connection. /// Preparing process includes all data that connection need to work with server and base map loading. /// After preparing, connection will receive all QNetObjects that are currently created. /// </summary> public static void PrepareNewConnection(QNetConnection connection) { var qNetPlayer = QNetPlayer.GetQNetPlayer(connection.ConnectionIdentity); if (qNetPlayer == null) { JEMLogger.LogError("Newly received connection don't have his QNetPlayer instance. Disconnecting!"); QNetManager.Server.CloseConnection(connection, "InternalQNetPlayerError"); } else { JEMLogger.Log($"Preparing newly received connection called {qNetPlayer.Nickname}."); var writer = QNetManager.Server.GenerateOutgoingMessage((ushort)QNetUnityLocalHeader.LEVEL_LOADING); writer.WriteString(QNetLevelLoader.LevelName); QNetManager.Server.Send(connection, QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered, writer); } }
/// <summary> /// Sends network message from server to given connection. /// </summary> /// <param name="targetConnection">Target connection of message.</param> /// <param name="index">Index of message.</param> public QNetEntityOutgoingMessage SendNetworkServerDedicatedMessage(QNetConnection targetConnection, byte index) { if (!QNetManager.IsServerActive) { throw new InvalidOperationException("QNetEntity network server message can only be send from server."); } if (targetConnection.Equals(default(QNetConnection))) { throw new InvalidOperationException("Default target connection received in server dedicated message."); } var outgoingMessage = new QNetEntityOutgoingMessage(this, true, targetConnection); if (IsServer) { // write server frame outgoingMessage.Writer.WriteUInt32(QNetTime.ServerFrame); } outgoingMessage.Writer.WriteByte(index); return(outgoingMessage); }
/// <summary> /// Apply data and send to target peer. /// </summary> public void ApplyAndSend(QNetConnection exclude, QNetMessageMethod sendMethod = QNetMessageMethod.Unreliable) { if (FromServer) { if (Target.Equals(default(QNetConnection))) { QNetManager.Server.SendToAll(exclude, Channel, sendMethod, Writer); } else { QNetManager.Server.Send(Target, Channel, sendMethod, Writer); } } else if (FromClient) { QNetManager.Client.Send(Channel, sendMethod, Writer); } else { throw new InvalidOperationException(); } }
/// <summary> /// Spawns new object in world by QNet. /// </summary> /// <param name="prefab">Prefab of object to spawn.</param> /// <param name="position">Position of object to spawn.</param> /// <param name="rotation">Rotation of object to spawn.</param> /// <param name="ownerConnection">Connection of owner.</param> /// <param name="skipObjectBroadcast">If true, system will skip SendObjectToAllConnection method.</param> public static QNetObjectBehaviour SpawnWithOwner(QNetObjectPrefab prefab, Vector3 position, Quaternion rotation, QNetConnection ownerConnection, bool skipObjectBroadcast = false) { return(SpawnWithOwner(prefab, position, rotation, ownerConnection.ConnectionIdentity, skipObjectBroadcast)); }
/// <summary> /// Gets QNetPlayer by it's connection identity. /// </summary> public static QNetPlayer GetQNetPlayer(QNetConnection connection) { return(GetQNetPlayer(connection.ConnectionIdentity)); }
/// <summary> /// Sends message to given connection. /// </summary> /// <param name="connection">The connection.</param> /// <param name="channel">Channel of message.</param> /// <param name="method">Method of message sending.</param> /// <param name="writer">Message to send.</param> public static void Send(this QNetConnection connection, QNetUnityLocalChannel channel, QNetMessageMethod method, QNetMessageWriter writer) { connection.Send((byte)channel, method, writer); }
/// <summary> /// Sends message to given connection. /// </summary> /// <param name="connection">The connection.</param> /// <param name="channel">Channel of message.</param> /// <param name="method">Method of message sending.</param> /// <param name="header"></param> public static void Send(this QNetConnection connection, QNetUnityLocalChannel channel, QNetMessageMethod method, QNetUnityLocalHeader header) { connection.Send((byte)channel, method, (ushort)header); }
/// <summary> /// Sends message to given connection. /// </summary> /// <param name="server">Our Server.</param> /// <param name="except">Message will be send to all connections except this.</param> /// <param name="channel">Channel of message.</param> /// <param name="method">Method of message sending.</param> /// <param name="writer">Message to send.</param> public static void SendToAll(this QNetServer server, QNetConnection except, QNetLocalHeader channel, QNetMessageMethod method, QNetMessageWriter writer) { server.SendToAll(except, (byte)channel, method, writer); }
/// <summary> /// Sends message to given connection. /// </summary> /// <param name="server">Our Server.</param> /// <param name="connection">Target connection of message.</param> /// <param name="channel">Channel of message.</param> /// <param name="method">Method of message sending.</param> /// <param name="writer">Message to send.</param> public static void Send(this QNetServer server, QNetConnection connection, QNetLocalChannel channel, QNetMessageMethod method, QNetMessageWriter writer) { server.Send(connection, (byte)channel, method, writer); }
/// <summary> /// Sends message to given connection. /// </summary> /// <param name="server">Our Server.</param> /// <param name="connection">Target connection of message.</param> /// <param name="channel">Channel of message.</param> /// <param name="method">Method of message sending.</param> /// <param name="header"></param> /// <param name="args">Additional parameters.</param> public static void Send(this QNetServer server, QNetConnection connection, QNetLocalChannel channel, QNetMessageMethod method, QNetUnityLocalHeader header, params object[] args) { server.Send(connection, (byte)channel, method, (ushort)header, args); }