void DespatchData(byte[] buff) { if (_callback == null) { throw new InvalidOperationException("No callback to read data to"); } try { int processed = 0; while (processed < buff.Length) { var len = buff.Length - processed; if (len > _callback.Size) { len = _callback.Size; } if (len > 0) { //No point shifting the target buffer as they always read from index 0 Buffer.BlockCopy(buff, processed, _callback.Buffer, _callback.Offset, len); _callback.Callback(_callback.State, len); processed += len; } } } catch (Exception e) { var buffLength = buff == null ? "<null>" : buff.Length.ToString(); var callbackExists = _callback == null ? "<null>" : "yes"; var cbBufferExists = _callback == null || _callback.Buffer == null ? "<null>" : "Nope"; var cbOffset = _callback == null ? "<null>" : _callback.Offset.ToString(); var cbSize = _callback == null ? "<null>" : _callback.Size.ToString(); ProgramLog.Error.Log("Read exception caught."); ProgramLog.Error.Log("Debug Values: {0},{1},{2},{3},{4}", buffLength, callbackExists, cbBufferExists, cbOffset, cbSize); ProgramLog.Error.Log("Error: {0}", e); } }