public void Send(byte[] buffer, byte channel, ENetPacketFlags flags) { Native.ENetPacket *packet; fixed(byte *p = buffer) { packet = LibENet.PacketCreate((IntPtr)p, (UIntPtr)buffer.Length, flags & ~ENetPacketFlags.NoAllocate); } if (LibENet.PeerSend(Unsafe, channel, packet) < 0) { throw new Exception("Failed to send packet to peer."); } }
/// <summary> /// Queues a packet to be sent. /// </summary> /// <param name="channelId">Destination channel Id</param> /// <param name="packet">Packet to be queued</param> /// <remarks> /// This method will destroy the packet if its <see cref="ENetPacket.ReferenceCount"/> is zero /// </remarks> public void Send(byte channelId, ENetPacket packet) { ThrowIfNull(); if (packet.IsNull) { throw new ArgumentNullException(nameof(packet)); } if (LibENet.PeerSend(m_Native, channelId, packet.GetNativePointer()) < 0) { ThrowHelper.ThrowENetPeerSendFailed(); } }
/// <summary> /// Queues a packet to be sent. /// </summary> /// <param name="channelId">Destination channel Id</param> /// <param name="buffer">Buffer containing packet data</param> /// <param name="flags">Packet flags</param> /// <remarks> /// <see cref="ENetPacketFlags.NoAllocate"/> will be ignored. /// </remarks> public void Send(byte channelId, ReadOnlySpan <byte> buffer, ENetPacketFlags flags) { ThrowIfNull(); NativeENetPacket *packet; fixed(byte *p = buffer) { packet = LibENet.PacketCreate((IntPtr)p, unchecked ((UIntPtr)buffer.Length), flags & ~ENetPacketFlags.NoAllocate); } if (LibENet.PeerSend(m_Native, channelId, packet) < 0) { ThrowHelper.ThrowENetPeerSendFailed(); } }
public void Multicast(byte[] buffer, byte channel, ENetPacketFlags flags, IEnumerable <ENetPeer> peers, ENetPeer except) { CheckDispose(); Native.ENetPacket *packet; fixed(byte *p = buffer) { packet = LibENet.PacketCreate((IntPtr)p, (UIntPtr)buffer.Length, flags & ~ENetPacketFlags.NoAllocate); } foreach (var peer in peers) { if (peer == null) { throw new NullReferenceException(); } if (peer.Host != this) { throw new ENetMulticastException("Speicfied peer is not of this host.", peer); } if (peer == except) { continue; } if (peer.Unsafe->State != ENetPeerState.Connected) { continue; } if (LibENet.PeerSend(peer.Unsafe, channel, packet) != 0) { throw new ENetMulticastException("Failed to send packet to speicfied peer.", peer); } } if (packet->ReferenceCount.ToUInt32() == 0) { LibENet.PacketDestroy(packet); } }