Ipop.Ethernet.ReadLoop C# (CSharp) Method

ReadLoop() protected method

The thread acting as the ISource for Ethernet, this is where reading of the TAP is performed. Use Subscribe to receive the packets coming from here.
The same max MTU byte array is always read into. This is then copied to a minimum sized MemBlock and send to the subscriber.
protected ReadLoop ( ) : void
return void
    protected void ReadLoop()
    {
      byte[] read_buffer = new byte[MTU];
      BufferAllocator ba = new BufferAllocator(MTU, 1.1);
      while(_running) {
        int length = -1;
        try {
          length  = _tap.Read(read_buffer);
        } catch(ThreadInterruptedException x) {
          if(_running && ProtocolLog.Exceptions.Enabled) {
            ProtocolLog.Write(ProtocolLog.Exceptions, x.ToString());
          }
        } catch(Exception e) {
          ProtocolLog.WriteIf(ProtocolLog.Exceptions, e.ToString());
        }

        if (length == 0 || length == -1) {
          ProtocolLog.WriteIf(IpopLog.TapLog, "Couldn't read TAP");
          continue;
        }

        Array.Copy(read_buffer, 0, ba.Buffer, ba.Offset, length);
        MemBlock packet = MemBlock.Reference(ba.Buffer, ba.Offset, length);
        ba.AdvanceBuffer(length);

        Subscriber s = _sub;
        if(s != null) {
          try {
            s.Handle(packet, this);
          } catch(Exception e) {
            ProtocolLog.WriteIf(ProtocolLog.Exceptions, e.ToString());
          }
        }
      }
    }