/// <summary> /// Attempts to get the next packet on the specified channel /// </summary> public bool GetNextPacket(out P2Packet?packet, P2PChannel channel = P2PChannel.RELIABLE) { packet = null; if (IsPacketAvailable(channel)) { packet = ReadPacket(channel); return(true); } return(false); }
/// <summary> /// Asynchronously attempts to get the next packet on the specified channel /// </summary> public async Task <P2Packet?> GetNextPacketAsync(P2PChannel channel = P2PChannel.RELIABLE) { DateTime timeoutStart = DateTime.Now; P2Packet?packet; while (!GetNextPacket(out packet, channel)) { TimeSpan timeoutChk = DateTime.Now - timeoutStart; if (timeoutChk.TotalMilliseconds >= MP_TIMEOUT) { return(null); } await Task.Delay(TimeSpan.FromMilliseconds(TickRate)); } return(packet); }
/// <summary> /// Sends a packet via Steamworks; Returns whether or not the packed could be sent /// </summary> public bool SendPacket(SteamId id, byte[] data, int len = -1, P2PChannel channel = P2PChannel.RELIABLE) { if (len > MP_PACKET_MAX || data.Length > MP_PACKET_MAX) { Output.LogWarning($"{Name}: Attempted to send a packet which exceeded MP_MAX_SIZE!"); return(false); } if (channel < 0 || channel >= P2PChannel.NUM_CHANNELS) { Output.LogWarning($"{Name}: Invalid send channel specified, defaulting to Reliable"); return(SteamNetworking.SendP2PPacket(id, data, len, (int)P2PChannel.RELIABLE, P2PSend.Reliable)); } // Cast channel to send type P2PSend send = (P2PSend)channel; return(SteamNetworking.SendP2PPacket(id, data, len, (int)channel, send)); }
/// <summary> /// Returns null if there is no packet available to be read /// </summary> private P2Packet?ReadPacket(P2PChannel channel) { return(SteamNetworking.ReadP2PPacket((int)channel)); }
/// <summary> /// Returns whether or not a packet is available on the specified channel /// </summary> public bool IsPacketAvailable(P2PChannel channel = P2PChannel.RELIABLE) { return(SteamNetworking.IsP2PPacketAvailable((int)channel)); }