public Packet(PacketCommand cmd, [CanBeNull] byte[] payload) { // to pass the buffer to unmanaged code, we need to memcpy it to an unmanaged buffer var length = payload?.Length ?? 0; var buffer = IntPtr.Zero; try { // payload may be null, in which case we pass nullptr to C++ if (payload != null) { buffer = Marshal.AllocHGlobal(length); Marshal.Copy(payload, 0, buffer, length); } m_handle = NativeMethods.wirefox_packet_create((byte)cmd, buffer, new UIntPtr((uint)length)); m_cmd = cmd; m_sender = PeerID.Invalid; m_payload = payload; } finally { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } } }
internal Packet(IntPtr handle) { m_handle = handle; m_cmd = (PacketCommand)NativeMethods.wirefox_packet_get_cmd(handle); m_sender = new PeerID(NativeMethods.wirefox_packet_get_sender(handle)); var dataptr = NativeMethods.wirefox_packet_get_data(handle); if (dataptr == IntPtr.Zero) { // nullptr payload is allowed m_payload = new byte[0]; } else { // copy payload of correct length over to managed land m_payload = new byte[NativeMethods.wirefox_packet_get_length(handle).ToUInt32()]; Marshal.Copy(NativeMethods.wirefox_packet_get_data(handle), m_payload, 0, m_payload.Length); } }
public void DisconnectImmediate(PeerID who) { NativeMethods.wirefox_peer_disconnect_immediate(m_handle, who); }
public uint Send(Packet packet, PeerID recipient, PacketOptions options, PacketPriority priority = PacketPriority.MEDIUM) { return(NativeMethods.wirefox_peer_send(m_handle, packet.GetHandle(), recipient, options, priority, 0)); }
public void Disconnect(PeerID who, TimeSpan linger) { NativeMethods.wirefox_peer_disconnect(m_handle, who, (uint)linger.TotalMilliseconds); }
public void Disconnect(PeerID who) { Disconnect(who, TimeSpan.FromMilliseconds(200)); }
public ulong GetStat(PeerID who, PeerStatID stat) { return((ulong)NativeMethods.wirefox_peer_get_stat(m_handle, who, stat)); }
public bool GetPingAvailable(PeerID who) { return(NativeMethods.wirefox_peer_get_ping_available(m_handle, who)); }
public int GetPing(PeerID who) { return((int)NativeMethods.wirefox_peer_get_ping(m_handle, who)); }