internal void ReturnBlocks(ICollection <byte[]> blocks)
        {
            if (blocks == null)
            {
                throw new ArgumentNullException(nameof(blocks));
            }

            var num = blocks.Count * BlockSize;

            Interlocked.Add(ref smallPoolInUseSize, -num);
            foreach (var block in blocks)
            {
                if (block == null || block.Length != BlockSize)
                {
                    throw new ArgumentException("blocks contains buffers that are not BlockSize in length");
                }
            }
            foreach (byte[] block2 in blocks)
            {
                if (MaximumFreeSmallPoolBytes != 0 && SmallPoolFreeSize >= MaximumFreeSmallPoolBytes)
                {
                    BlockDiscarded?.Invoke();
                    break;
                }

                Interlocked.Add(ref smallPoolFreeSize, BlockSize);
                smallPool.Push(block2);
            }

            UsageReport?.Invoke(smallPoolInUseSize, smallPoolFreeSize, LargePoolInUseSize, LargePoolFreeSize);
        }
        internal void ReturnLargeBuffer(byte[] buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (!IsLargeBufferMultiple(buffer.Length))
            {
                throw new ArgumentException("buffer did not originate from this memory manager. The size is not a multiple of " + LargeBufferMultiple);
            }

            var num = buffer.Length / largeBufferMultiple - 1;

            if (num < largePools.Length)
            {
                if ((largePools[num].Count + 1) * buffer.Length <= MaximumFreeLargePoolBytes || MaximumFreeLargePoolBytes == 0)
                {
                    largePools[num].Push(buffer);
                    Interlocked.Add(ref largeBufferFreeSize[num], buffer.Length);
                }
                else
                {
                    LargeBufferDiscarded?.Invoke(MemoryStreamDiscardReason.EnoughFree);
                }
            }
            else
            {
                num = largeBufferInUseSize.Length - 1;
                LargeBufferDiscarded?.Invoke(MemoryStreamDiscardReason.TooLarge);
            }

            Interlocked.Add(ref largeBufferInUseSize[num], -buffer.Length);
            UsageReport?.Invoke(smallPoolInUseSize, smallPoolFreeSize, LargePoolInUseSize, LargePoolFreeSize);
        }
Beispiel #3
0
 internal void ReportUsageReport(
     long blockPoolInUseBytes, long blockPoolFreeBytes,
     long bufferPoolInUseBytes, long bufferPoolFreeBytes)
 {
     UsageReport?.Invoke(
         blockPoolInUseBytes, blockPoolFreeBytes, bufferPoolInUseBytes, bufferPoolFreeBytes);
 }
Beispiel #4
0
 internal void ReportUsageReport(
     long smallPoolInUseBytes, long smallPoolFreeBytes, long largePoolInUseBytes, long largePoolFreeBytes
     ) =>
 UsageReport?.Invoke(smallPoolInUseBytes, smallPoolFreeBytes, largePoolInUseBytes, largePoolFreeBytes);
Beispiel #5
0
        public void MessageReceiver(object sender, SPPMessage e)
        {
            BaseMessageParser parser = SPPMessageParserFactory.BuildParser(e);

            AnyMessageReceived?.Invoke(this, parser);
            switch (e.Id)
            {
            case SPPMessage.MessageIds.MSG_ID_RESET:
                ResetResponse?.Invoke(this, ((ResetResponseParser)parser).ResultCode);
                break;

            case SPPMessage.MessageIds.MSG_ID_FOTA_DEVICE_INFO_SW_VERSION:
                SwVersionResponse?.Invoke(this, ((SoftwareVersionOTAParser)parser).SoftwareVersion);
                break;

            case SPPMessage.MessageIds.MSG_ID_BATTERY_TYPE:
                BatteryTypeResponse?.Invoke(this, (BatteryTypeParser)parser);
                break;

            case SPPMessage.MessageIds.MSG_ID_AMBIENT_MODE_UPDATED:
                AmbientEnabledUpdateResponse?.Invoke(this, ((AmbientModeUpdateParser)parser).Enabled);
                break;

            case SPPMessage.MessageIds.MSG_ID_DEBUG_BUILD_INFO:
                BuildStringResponse?.Invoke(this, ((DebugBuildInfoParser)parser).BuildString);
                break;

            case SPPMessage.MessageIds.MSG_ID_DEBUG_GET_ALL_DATA:
                GetAllDataResponse?.Invoke(this, (DebugGetAllDataParser)parser);
                break;

            case SPPMessage.MessageIds.MSG_ID_DEBUG_SERIAL_NUMBER:
                SerialNumberResponse?.Invoke(this, (DebugSerialNumberParser)parser);
                break;

            case SPPMessage.MessageIds.MSG_ID_EXTENDED_STATUS_UPDATED:
                ExtendedStatusUpdate?.Invoke(this, (ExtendedStatusUpdateParser)parser);
                break;

            case SPPMessage.MessageIds.MSG_ID_FIND_MY_EARBUDS_STOP:
                FindMyGearStopped?.Invoke(this, null);
                break;

            case SPPMessage.MessageIds.MSG_ID_RESP:
                GenericResponse?.Invoke(this, (GenericResponseParser)parser);
                break;

            case SPPMessage.MessageIds.MSG_ID_SELF_TEST:
                SelfTestResponse?.Invoke(this, (SelfTestParser)parser);
                break;

            case SPPMessage.MessageIds.MSG_ID_SET_TOUCHPAD_OTHER_OPTION:
                OtherOption?.Invoke(this, ((SetOtherOptionParser)parser).OptionType);
                break;

            case SPPMessage.MessageIds.MSG_ID_STATUS_UPDATED:
                StatusUpdate?.Invoke(this, (StatusUpdateParser)parser);
                break;

            case SPPMessage.MessageIds.MSG_ID_USAGE_REPORT:
                UsageReport?.Invoke(this, (UsageReportParser)parser);
                break;

            case SPPMessage.MessageIds.MSG_ID_MUTE_EARBUD_STATUS_UPDATED:
                FindMyGearMuteUpdate?.Invoke(this, (MuteUpdateParser)parser);
                break;

            case SPPMessage.MessageIds.MSG_ID_ANC_STATUS_UPDATED:
                NoiseCancellingUpdated?.Invoke(this, ((NoiseCancellingUpdatedParser)parser).Enabled);
                break;
            }
        }
Beispiel #6
0
        public void DispatchEvent(BaseMessageParser?parser, SPPMessage.MessageIds?ids = null)
        {
            AnyMessageReceived?.Invoke(this, parser);
            switch (ids ?? parser?.HandledType ?? 0)
            {
            case SPPMessage.MessageIds.RESET:
                ResetResponse?.Invoke(this, (parser as ResetResponseParser)?.ResultCode ?? -1);
                break;

            case SPPMessage.MessageIds.FOTA_DEVICE_INFO_SW_VERSION:
                SwVersionResponse?.Invoke(this, (parser as SoftwareVersionOTAParser)?.SoftwareVersion ?? "null");
                break;

            case SPPMessage.MessageIds.BATTERY_TYPE:
                BatteryTypeResponse?.Invoke(this, (parser as BatteryTypeParser) !);
                break;

            case SPPMessage.MessageIds.AMBIENT_MODE_UPDATED:
                AmbientEnabledUpdateResponse?.Invoke(this, (parser as AmbientModeUpdateParser)?.Enabled ?? false);
                break;

            case SPPMessage.MessageIds.DEBUG_BUILD_INFO:
                BuildStringResponse?.Invoke(this, (parser as DebugBuildInfoParser)?.BuildString ?? "null");
                break;

            case SPPMessage.MessageIds.DEBUG_GET_ALL_DATA:
                GetAllDataResponse?.Invoke(this, (parser as DebugGetAllDataParser) !);
                break;

            case SPPMessage.MessageIds.DEBUG_SERIAL_NUMBER:
                SerialNumberResponse?.Invoke(this, (parser as DebugSerialNumberParser) !);
                break;

            case SPPMessage.MessageIds.EXTENDED_STATUS_UPDATED:
                BaseUpdate?.Invoke(this, (parser as IBasicStatusUpdate) !);
                ExtendedStatusUpdate?.Invoke(this, (parser as ExtendedStatusUpdateParser) !);
                break;

            case SPPMessage.MessageIds.FIND_MY_EARBUDS_STOP:
                FindMyGearStopped?.Invoke(this, EventArgs.Empty);
                break;

            case SPPMessage.MessageIds.RESP:
                GenericResponse?.Invoke(this, (parser as GenericResponseParser) !);
                break;

            case SPPMessage.MessageIds.SELF_TEST:
                SelfTestResponse?.Invoke(this, (parser as SelfTestParser) !);
                break;

            case SPPMessage.MessageIds.SET_TOUCHPAD_OTHER_OPTION:
                OtherOption?.Invoke(this, (parser as SetOtherOptionParser) !.OptionType);
                break;

            case SPPMessage.MessageIds.STATUS_UPDATED:
                StatusUpdate?.Invoke(this, (parser as StatusUpdateParser) !);
                BaseUpdate?.Invoke(this, (parser as IBasicStatusUpdate) !);
                break;

            case SPPMessage.MessageIds.USAGE_REPORT:
                UsageReport?.Invoke(this, (parser as UsageReportParser) !);
                break;

            case SPPMessage.MessageIds.MUTE_EARBUD_STATUS_UPDATED:
                FindMyGearMuteUpdate?.Invoke(this, (parser as MuteUpdateParser) !);
                break;

            case SPPMessage.MessageIds.NOISE_REDUCTION_MODE_UPDATE:
                AncEnabledUpdateResponse?.Invoke(this,
                                                 (parser as NoiseReductionModeUpdateParser)?.Enabled ?? false);
                break;

            case SPPMessage.MessageIds.NOISE_CONTROLS_UPDATE:
                NoiseControlUpdateResponse?.Invoke(this,
                                                   (parser as NoiseControlUpdateParser)?.Mode ?? NoiseControlMode.Off);
                break;
            }
        }