private EP2PSend NetworkDeliveryToEP2PSend(NetworkDelivery type) { EP2PSend options = EP2PSend.k_EP2PSendReliableWithBuffering; switch (type) { case NetworkDelivery.Unreliable: options = EP2PSend.k_EP2PSendUnreliable; break; case NetworkDelivery.UnreliableSequenced: options = EP2PSend.k_EP2PSendUnreliable; break; case NetworkDelivery.Reliable: options = EP2PSend.k_EP2PSendReliableWithBuffering; break; case NetworkDelivery.ReliableSequenced: options = EP2PSend.k_EP2PSendReliableWithBuffering; break; case NetworkDelivery.ReliableFragmentedSequenced: options = EP2PSend.k_EP2PSendReliableWithBuffering; break; default: options = EP2PSend.k_EP2PSendReliableWithBuffering; break; } return(options); }
private int AddChannel(ChannelType type) { EP2PSend options = EP2PSend.k_EP2PSendReliableWithBuffering; switch (type) { case ChannelType.Unreliable: options = EP2PSend.k_EP2PSendUnreliable; break; case ChannelType.UnreliableSequenced: options = EP2PSend.k_EP2PSendUnreliable; break; case ChannelType.Reliable: options = EP2PSend.k_EP2PSendReliableWithBuffering; break; case ChannelType.ReliableSequenced: options = EP2PSend.k_EP2PSendReliableWithBuffering; break; case ChannelType.ReliableFragmentedSequenced: options = EP2PSend.k_EP2PSendReliableWithBuffering; break; default: options = EP2PSend.k_EP2PSendReliableWithBuffering; break; } channelSendTypes.Add(channelCounter, options); channelCounter++; return(channelCounter - 1); }
public override int GetMaxPacketSize(int channelId) { if (channelId >= channels.Length) { channelId = 0; } EP2PSend sendMethod = channels[channelId]; switch (sendMethod) { case EP2PSend.k_EP2PSendUnreliable: return(1200); //UDP like - MTU size. case EP2PSend.k_EP2PSendUnreliableNoDelay: return(1200); //UDP like - MTU size. case EP2PSend.k_EP2PSendReliable: return(1048576); //Reliable message send. Can send up to 1MB of data in a single message. case EP2PSend.k_EP2PSendReliableWithBuffering: return(1048576); //Reliable message send. Can send up to 1MB of data in a single message. default: return(1200); //UDP like - MTU size. } }
private byte[] getByteArrayFromMessage(Message message, EP2PSend packetType, out uint length) { packetSingle.message = message; packetSingle.sendType = packetType; MemoryStream memoryStream = new MemoryStream(); binaryFormatter.Serialize(memoryStream, packetSingle); length = (uint)memoryStream.Length; ulong count = 0; if (Multiplayer._instance.displayPing) { if (messageCounterTypes.ContainsKey(message.type)) { count = messageCounterTypes[message.type]; count += length; messageCounterTypes[message.type] = count; messageCounter += length; } else { messageCounterTypes.Add(message.type, length); messageCounter += length; } } return(memoryStream.ToArray()); }
public OutgoingNetworkPacketContainer(CSteamID receiver, object message, EP2PSend packetType, OutgoingReceivers outgoingReceivers) { this.outgoingReceivers = outgoingReceivers; SteamId = receiver; Message = message; PacketType = packetType; }
public override bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error) { if (steamId.m_SteamID == SteamUser.GetSteamID().m_SteamID) { // sending to self. short circuit TransportReceive(bytes, numBytes, channelId); error = 0; return(true); } EP2PSend eP2PSendType = EP2PSend.k_EP2PSendReliable; QosType qos = SteamNetworkManager.hostTopology.DefaultConfig.Channels[channelId].QOS; if (qos == QosType.Unreliable || qos == QosType.UnreliableFragmented || qos == QosType.UnreliableSequenced) { eP2PSendType = EP2PSend.k_EP2PSendUnreliable; } // Send packet to peer through Steam if (SteamNetworking.SendP2PPacket(steamId, bytes, (uint)numBytes, eP2PSendType, channelId)) { error = 0; return(true); } else { error = 1; return(false); } }
public void SendPacketAsHostToAllClients(Packet existingPacket, EP2PSend packetType) { OutgoingNetworkPacketContainer packet = new OutgoingNetworkPacketContainer(existingPacket, packetType); messageQueue.Enqueue(packet); waitHandle.Set(); }
public override void Send(ulong clientId, ArraySegment <byte> data, string channelName) { if (!channelNameToId.ContainsKey(channelName)) { if (LogHelper.CurrentLogLevel <= LogLevel.Error) { LogHelper.LogError("SteamP2PTransport - Can't Send to client, channel with channelName: " + channelName + " is not present"); } return; } int channelId = channelNameToId[channelName]; EP2PSend sendType = channelSendTypes[channelId]; if (clientId == ServerClientId) { SteamNetworking.SendP2PPacket(serverUser.SteamId, data.Array, (uint)data.Count, sendType, channelId); } else { if (connectedUsers.ContainsKey(clientId)) { SteamNetworking.SendP2PPacket(connectedUsers[clientId].SteamId, data.Array, (uint)data.Count, sendType, channelId); } else { if (LogHelper.CurrentLogLevel <= LogLevel.Error) { LogHelper.LogError("SteamP2PTransport - Can't Send to client, client not connected, clientId: " + clientId); } } } }
public override void Send(ulong clientId, ArraySegment <byte> data, NetworkDelivery delivery) { EP2PSend sendType = NetworkDeliveryToEP2PSend(delivery); if (clientId == ServerClientId) { #if UNITY_SERVER SteamGameServerNetworking.SendP2PPacket(serverUser.SteamId, data.Array, (uint)data.Count, sendType, (int)InternalChannelType.NetcodeData); #else SteamNetworking.SendP2PPacket(serverUser.SteamId, data.Array, (uint)data.Count, sendType, (int)InternalChannelType.NetcodeData); #endif } else { if (connectedUsers.ContainsKey(clientId)) { #if UNITY_SERVER SteamGameServerNetworking.SendP2PPacket(connectedUsers[clientId].SteamId, data.Array, (uint)data.Count, sendType, (int)InternalChannelType.NetcodeData); #else SteamNetworking.SendP2PPacket(connectedUsers[clientId].SteamId, data.Array, (uint)data.Count, sendType, (int)InternalChannelType.NetcodeData); #endif } else { if (NetworkManager.Singleton.LogLevel <= LogLevel.Error) { NetworkLog.LogErrorServer(nameof(SteamNetworkingTransport) + " - Can't Send to client, client not connected, clientId: " + clientId); } } } }
public override void Send(ulong clientId, ArraySegment <byte> data, string channelName) { if (!channelNameToId.ContainsKey(channelName)) { if (NetworkingManager.Singleton.LogLevel <= LogLevel.Error) { UnityEngine.Debug.Log("SteamP2PTransport - Can't Send to client, channel with channelName: " + channelName + " is not present"); } return; } int channelId = channelNameToId[channelName]; EP2PSend sendType = channelSendTypes[channelId]; bool test = true; if (clientId == ServerClientId && !test) { SteamNetworking.SendP2PPacket(serverUser.SteamId, data.Array, (uint)data.Count, sendType, channelId); } else { if (connectedUsers.ContainsKey(clientId)) { SteamNetworking.SendP2PPacket(connectedUsers[clientId].SteamId, data.Array, (uint)data.Count, sendType, channelId); } else { if (NetworkingManager.Singleton.LogLevel <= LogLevel.Error) { UnityEngine.Debug.Log("SteamP2PTransport - Can't Send to client, client not connected, clientId: " + clientId); } } } }
public void SendEntityData <T>(short packetId, T data, Entity sender, EP2PSend m = EP2PSend.k_EP2PSendReliable, bool sendToHost = false) where T : new() { var msg = new byte[1024]; var pos = 0; msg = msg.Write(ref pos, packetId); msg = msg.Write(ref pos, sender.World.GetID()); msg = msg.Write(ref pos, sender.Id); msg = msg.Write(ref pos, data.GetType().Name.ToSnakeCase()); //var b = TypeHelper.WriteProperties(data); //msg = msg.Append(pos, b); //pos += b.Length; if (sendToHost) { SteamNetworking.SendP2PPacket(Host, msg, (uint)msg.Length, m); } else { foreach (var steamUser in RemoteUsers) { if (steamUser != User) { SteamNetworking.SendP2PPacket(steamUser, msg, (uint)msg.Length, m); } } } }
void SendPacket(CSteamID id, byte[] msg, uint size, EP2PSend reliability = EP2PSend.k_EP2PSendReliable) { if (!SteamNetworking.SendP2PPacket(id, msg, size, reliability)) { Debug.LogAssertion("Failed to send Steam packet!"); } }
public void SendPacket(DataPacket packet, CSteamID user, EP2PSend sendType = EP2PSend.k_EP2PSendUnreliableNoDelay) { if (DEBUG) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < packet.data.Length; i++) { sb.Append(packet.data[i] + "|"); } //if (this.channelIndex != 2) // Log("Sending packet to player " + SteamFriends.GetFriendPersonaName(user) + " with data " + sb.ToString() + " in channel " + channelName); //Log(Environment.StackTrace); } if (user.m_SteamID == NetworkGameManager.playerID) { //Process packet immediately if we sent it to ourself. ProcessPacket(packet); } else { //Send it off to another player SteamNetworking.SendP2PPacket(user, packet.data, (uint)packet.data.Length, sendType, channelIndex); } }
public void SendPacketToSpecificPlayer(CSteamID receiver, Packet existingPacket, EP2PSend packetType) { OutgoingNetworkPacketContainer packet = new OutgoingNetworkPacketContainer(receiver, existingPacket, packetType, OutgoingNetworkPacketContainer.OutgoingReceivers.ToSinglePeer); messageQueue.Enqueue(packet); waitHandle.Set(); }
protected override void SendImpl(int connectionId, byte[] buffer, int count, NetworkReliablity reliability) { ValidateSteamInitalized(); EP2PSend ep2p = reliability == NetworkReliablity.Reliable ? EP2PSend.k_EP2PSendReliable : EP2PSend.k_EP2PSendUnreliable; SendPacket(connectionUsers[connectionId], buffer, (uint)count, ep2p); }
public void SendP2PPacket(SteamID steamID, byte[] data, EP2PSend sendType, int channel = 0) { IntPtr num = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, num, data.Length); Networking.SteamUnityAPI_SteamNetworking_SendP2PPacket(this._networking, steamID.ToUInt64(), num, (uint)data.Length, (byte)sendType, channel); Marshal.FreeHGlobal(num); }
private bool SendP2PPacket(bool isServer, CSteamID id, byte[] data, int length, EP2PSend mode) { if (isServer) { return(SteamGameServerNetworking.SendP2PPacket(id, data, (uint)length, mode, 0)); } return(SteamNetworking.SendP2PPacket(id, data, (uint)length, mode, 0)); }
protected void Send(CSteamID host, byte[] msgBuffer, EP2PSend sendType) { if (!SteamManager.Initialized) { throw new ObjectDisposedException("Steamworks"); } SteamNetworking.SendP2PPacket(host, msgBuffer, (uint)msgBuffer.Length, sendType, (int)SteamChannels.SEND_DATA); }
/// <summary> /// Send the transform given to game. /// </summary> public static void SendTransformToInGameUsers(Transform transform, EP2PSend sendType, bool sendToSender = true) { instance.WriteTransformOnPacketData(transform, true); byte[] packet = new byte[instance.Packet.CurrentSeek]; Array.Copy(instance.Packet.Data, 0, packet, 0, packet.Length); Client.SendPacketToInGameUsers(packet, PacketType.ServerTransform, sendType, sendToSender); }
/// <summary> /// Send the transform given to the lobby. /// </summary> public static void SendTransformToLobby(Transform transform, PacketType command, EP2PSend sendType, bool sendToSender = true) { instance.WriteTransformOnPacketData(transform, true); byte[] packet = new byte[instance.Packet.CurrentSeek]; Array.Copy(instance.Packet.Data, 0, packet, 0, packet.Length); Client.SendPacketToLobby(packet, command, sendType, sendToSender); }
/*public int Send(byte[] dgram, int bytes) { CheckDisposed(); if (dgram == null) throw new ArgumentNullException("dgram"); if (!active) throw new InvalidOperationException("Operation not allowed on " + "non-connected Sockets."); return (DoSend(dgram, bytes, null)); }*/ public int Send(byte[] dgram, int bytes, CSteamID steamId, EP2PSend type = EP2PSend.k_EP2PSendUnreliable) { CheckDisposed(); if (dgram == null) throw new ArgumentNullException("dgram is null"); return (DoSend(dgram, bytes, steamId, type)); }
public static void SendP2PMessage(CSteamID clientID, Message msg, EP2PSend sendType) { byte[] msg_array = Converter.ObjectToByteArray(msg); uint size = (uint)msg_array.Length; // Another way to calculate size, dont know if it works // SteamGameServerNetworking.SendP2PPacket(clientID, msg_array, (uint)Marshal.SizeOf(msg_array), sendType); SteamNetworking.SendP2PPacket(clientID, msg_array, size, sendType); }
public ConnectionStream(PacketDistributor packetDistributor, CSteamID remote, int channelID, EP2PSend reliability) { Remote = remote; ChannelID = channelID; Reliability = reliability; this.packetDistributor = packetDistributor; packetDistributor.PacketReceived += OnPacketReceived; }
private void InputManager_Update(On.InputManager.orig_Update orig, InputManager self) { KeyCode key = (KeyCode)Enum.Parse(typeof(KeyCode), (mod.settings as PingSettings).Key); if (Input.GetKeyDown(key)) { // First, find the mouse position as a unity vector // Next, spawn something there that lasts for x seconds // Finally, remove it after those seconds pass IGameCameraService gameCameraManager = Services.GetService <IGameCameraService>(); GameNetworkManager net = GameObject.FindObjectOfType <GameNetworkManager>(); IMessageBox msgBox = new DynData <GameNetworkManager>(net).Get <IMessageBox>("messageBox"); RaycastHit[] array = Physics.RaycastAll(gameCameraManager.ScreenPointToRay(Input.mousePosition), float.PositiveInfinity); Array.Sort <RaycastHit>(array, (RaycastHit hitInfo1, RaycastHit hitInfo2) => hitInfo1.distance.CompareTo(hitInfo2.distance)); // In theory the first raycast hit should be the best? // Not sure if this is the case... Vector3 mousePos = array[0].point; mod.Log("Raycast hits:"); foreach (RaycastHit ra in array) { mod.Log("Dist: " + ra.distance + " with pos: " + ra.point); } mod.Log("Mouse pos: " + mousePos.ToString()); bool assert = (bool)typeof(GameNetworkManager).GetMethod("AssertMessageBoxIsSet", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(net, new object[0]); if (assert && msgBox != null && net != null) { PingMessage pm = new PingMessage(); pm.SetPos(mousePos); Message m = pm as Message; mod.Log("Assertion: " + assert); mod.Log("Net: " + net.name); mod.Log("MSG BOX: " + msgBox.ToString()); EP2PSend temp = msgBox.SteamNetworkingSendMode; msgBox.SteamNetworkingSendMode = EP2PSend.k_EP2PSendReliable; msgBox.SendMessage(ref m, net.GetLobbyPlayerIDs()); msgBox.SteamNetworkingSendMode = temp; mod.Log("Attempting to display!"); } else { GameObject obj = new GameObject("PING OBJECT"); PingScript p = obj.AddComponent <PingScript>(); p.pos = mousePos; mod.Log("It seems the game is not in multiplayer, attempting to create a local ping!"); } } orig(self); }
public static void SendExcludeP2P(CSteamID excludeID, Message message, EP2PSend sendType) { for (int i = 0; i < players.Count; i++) { if (players[i] != excludeID) { SendP2P(players[i], message, sendType); } } }
void SendToHost(byte[] data, PacketType command, CSteamID sender, EP2PSend sendType) { if (lobby.Owner != (CSteamID)0) { Send(data, command, sender, lobby.Owner, sendType); } else if (Server.Host != (CSteamID)0) { Send(data, command, sender, Server.Host, sendType); } }
public static void SendPackets(P2PPackage data, EP2PSend send, bool IncludeSelf = true) { //SendUnreliable – 小包,可以丢失,不需要依次发送,但要快 //SendUnreliableNoDelay – 跟上面一样,但是不做链接检查,因为这样,它可能被丢失,但是这种方式是最快的传送方式。 //SendReliable – 可靠的信息,大包,依次收发。 //SendReliableWithBuffering – 跟上面一样,但是在发送前会缓冲数据,如果你发送大量的小包,它不会那么及时。(可能会延迟200ms) var ms = new MemoryStream(); new BinaryFormatter().Serialize(ms, data); SendPacketsToAll(ms.GetBuffer(), send, IncludeSelf); }
/// <summary> /// Send the transform given to game. /// </summary> /// /// <param name="data">buffer to include in the final packet in addition to the transform. Data will be written before the transform.</param> public static void SendTransformToInGameUsers(byte[] data, Transform transform, EP2PSend sendType, bool sendToSender = true) { instance.Packet.CurrentSeek = 0; instance.Packet.CurrentLength = 0; instance.Packet.WriteByteData(data, 0, data.Length); instance.WriteTransformOnPacketData(transform, false); Client.SendPacketToInGameUsers(instance.Packet.Data, 0, instance.Packet.CurrentLength, PacketType.PlayerDataServer, sendType, sendToSender); }
/// <summary> /// Send the transform given to the lobby. /// </summary> /// /// <param name="data">buffer to include in the final packet in addition to the transform. Data will be written before the transform.</param> public static void SendTransformToLobby(byte[] data, Transform transform, EP2PSend sendType, bool sendToSender = true) { instance.Packet.CurrentSeek = 0; instance.Packet.WriteByteData(data, 0, data.Length); instance.WriteTransformOnPacketData(transform, false); byte[] packet = new byte[instance.Packet.CurrentSeek]; Array.Copy(instance.Packet.Data, 0, packet, 0, packet.Length); Client.SendPacketToLobby(packet, PacketType.ServerTransform, sendType, sendToSender); }
protected void Send(CSteamID host, byte[] msgBuffer, EP2PSend sendType, int channel) { if (!SteamManager.Initialized) { throw new ObjectDisposedException("Steamworks"); } if (channel >= channels.Length) { channel = 0; } SteamNetworking.SendP2PPacket(host, msgBuffer, (uint)msgBuffer.Length, sendType, channel); }
public void SendP2PPacket(SteamID steamID, byte[] data, EP2PSend sendType, int channel = 0) { IntPtr num = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, num, data.Length); Networking.SteamUnityAPI_SteamNetworking_SendP2PPacket(this._networking, steamID.ToUInt64(), num, (uint) data.Length, (byte) sendType, channel); Marshal.FreeHGlobal(num); }
/// <summary> /// Flushes all unflushed data, then sends the packet using a specified send type, and channel. /// See: /// <see cref="SteamGameServerNetworking.SendP2PPacket"/> /// <seealso cref="SteamNetworking.SendP2PPacket"/> /// </summary> /// <param name="sendType">The type of send for Steam to use.</param> /// <param name="nChannel">The channel for steam to pass data through, might want to make sure you add this channel to the client channel list.</param> /// <param name="recipients">Who to send the packet to, leave empty for everyone on the server on the send call.</param> public bool Send(EP2PSend sendType, int nChannel, params CSteamID[] recipients) { Flush(); byte[] bytes = Bytes; bool sent = false; if(bytes.Length <= 0) { throw new InvalidOperationException("Tried to send a packet with no data."); } if (_Server != null) { if (recipients.Length > 0) { foreach (CSteamID player in recipients) { sent = SteamGameServerNetworking.SendP2PPacket(player, bytes, (uint)bytes.Length, sendType, nChannel); } } else { foreach (CSteamID player in _Server.Players.Keys) { sent = SteamGameServerNetworking.SendP2PPacket(player, bytes, (uint)bytes.Length, sendType, nChannel); } } } else { sent = SteamNetworking.SendP2PPacket(_Client.ConnectedTo, bytes, (uint)bytes.Length, sendType, nChannel); } // Dispose unnessary objects. Dispose(); return sent; }
/// <summary> /// <para>//////////////////////////////////////////////////////////////////////////////////////////</para> /// <para> Session-less connection functions</para> /// <para> automatically establishes NAT-traversing or Relay server connections</para> /// <para> Sends a P2P packet to the specified user</para> /// <para> UDP-like, unreliable and a max packet size of 1200 bytes</para> /// <para> the first packet send may be delayed as the NAT-traversal code runs</para> /// <para> if we can't get through to the user, an error will be posted via the callback P2PSessionConnectFail_t</para> /// <para> see EP2PSend enum above for the descriptions of the different ways of sending packets</para> /// <para> nChannel is a routing number you can use to help route message to different systems - you'll have to call ReadP2PPacket()</para> /// <para> with the same channel number in order to retrieve the data on the other end</para> /// <para> using different channels to talk to the same user will still use the same underlying p2p connection, saving on resources</para> /// </summary> public static bool SendP2PPacket(CSteamID steamIDRemote, byte[] pubData, uint cubData, EP2PSend eP2PSendType, int nChannel = 0) { InteropHelp.TestIfAvailableGameServer(); return NativeMethods.ISteamGameServerNetworking_SendP2PPacket(steamIDRemote, pubData, cubData, eP2PSendType, nChannel); }
public static extern bool ISteamGameServerNetworking_SendP2PPacket(CSteamID steamIDRemote, [In, Out] byte[] pubData, uint cubData, EP2PSend eP2PSendType, int nChannel);
public void SendP2PPacket(SteamID steamID, string data, EP2PSend sendType, int channel = 0) { Networking.SteamUnityAPI_SteamNetworking_SendP2PPacket(this._networking, steamID.ToUInt64(), Marshal.StringToHGlobalAnsi(data), (uint) data.Length, (byte) sendType, channel); }