/// <summary> /// Registers a callback delegate to handle packets that are received. /// </summary> /// <param name="callback"></param> void IPacketInterface.RegisterReceiveCallback(ReceivePacketDelegate callback) { StopReceiverThread(); _receiver = callback; _receiverThread = new Thread(PacketReceiver); _receiverThread.Start(); }
public void RegisterReceiveCallback(ReceivePacketDelegate callback) { _callback = callback; // Now that we have a callback we can start receiving stuff. Open(true /* promiscuous */, 0); BeginReceive(); }
public void RegisterReceiveCallback(ReceivePacketDelegate callback) { _callback = callback; // Now that we have a callback we can start receiving stuff. Open(false /* not promiscuous */, int.MaxValue); BeginReceive(); }
public void RegisterReceiveCallback(ReceivePacketDelegate callback) { // UDP connection could not be configured, can't receive anything. if (_udpClient == null) { return; } // Set up input _callback = callback; BeginReceive(); }
/// <summary> /// Shuts down the encapsulation provider. /// </summary> void IPacketInterface.Shutdown() { if (_stream != null) { _stream.Close(); } if (_client != null) { _client.Close(); } StopReceiverThread(); _receiver = null; }
private void PacketReceiver() { Byte[] data = new Byte[1024]; try { while (true) { NetworkStream str = _stream; if (str == null) { return; } // wait for next packet from the nethub int byteLen = GetLenPrefix(str); // read the packet content up to the buffer size int pos = 0; while (pos < byteLen && pos < data.Length) { data[pos++] = GetByte(str); } // swallow the exceeding bytes in the packet while (pos < byteLen) { pos++; GetByte(str); } // check if it is for us (to reduce traffic on this machine), also get sending machine for logging ulong dstAddress = ReadAddress(data, 0); ulong srcAddress = ReadAddress(data, 6); if (dstAddress == _localAddress || dstAddress == _broadcastAddress) { // pass it the the ethernet controller ReceivePacketDelegate rcvr = _receiver; if (rcvr != null) { rcvr(new MemoryStream(data, 0, byteLen)); } } } } catch (ThreadInterruptedException) { // ignored } }