Exemplo n.º 1
0
 public static ISegmentBuildDefinition WithOptions(this ISegmentBuildDefinition builder, SegmentionOptions options)
 {
     if (!(builder is SegmentBuildDefinition def))
     {
         throw new NotSupportedException($"{builder.GetType()} is not supported");
     }
     def.Options = options;
     return(builder);
 }
Exemplo n.º 2
0
 public static ISegmentBuildDefinition WithMaxLength(this ISegmentBuildDefinition builder, long maxLength)
 {
     if (!(builder is SegmentBuildDefinition def))
     {
         throw new NotSupportedException($"{builder.GetType()} is not supported");
     }
     if (def.Length.HasValue)
     {
         throw new NotSupportedException("May not set end byte if using length");
     }
     def.MaxLength = maxLength == 0 ? (long?)null : maxLength;
     return(builder);
 }
Exemplo n.º 3
0
 public static ISegmentBuildDefinition AndEndsWith(this ISegmentBuildDefinition builder, byte end)
 {
     if (!(builder is SegmentBuildDefinition def))
     {
         throw new NotSupportedException($"{builder.GetType()} is not supported");
     }
     if (def.Length.HasValue)
     {
         throw new NotSupportedException("May not set end byte if using length");
     }
     def.EndsWith = end;
     return(builder);
 }
Exemplo n.º 4
0
        public StreamDevice(
            IDeviceAdapter adapter,
            IDeviceDefinition device,
            CancellationToken token     = default,
            int minimumTrasmissionDelay = 1000 //TODO should this default be overideable from the devicedefinition or it's attributes?
            )
        {
            _tokenSource = CancellationTokenSource.CreateLinkedTokenSource(token);
            _token       = _tokenSource.Token;

            _adapter = adapter;
            _device  = device;
            _minimumTrasmissionDelay = minimumTrasmissionDelay;

            Task?messageReceiver    = null;
            Task?messageTransmitter = null;
            Task?deviceInitializer  = null;

            var mre = new AsyncManualResetEvent();

            if (_device is IDeviceDefinitionInitialize)
            {
                mre.Reset();
                deviceInitializer = Initializer(mre);
            }
            else
            {
                //Assumed to start in set state but just be sure anyway
                mre.Set();
            }

            if (_device is IDeviceDefinitionReceiver <TMessage> receiver)
            {
                _decoder          = receiver.Decoder;
                _segmentDefintion = receiver.SegmentDefintion;
                messageReceiver   = Receiver(mre);
            }
            if (_device is IDeviceDefinitionTransmitter <TMessage> transmitter)
            {
                _encoder           = transmitter.Encoder;
                messageTransmitter = Transmitter(mre);
            }

            Runner = Task.WhenAll(
                deviceInitializer ?? Task.FromResult(0),
                messageReceiver ?? Task.FromResult(0),
                messageTransmitter ?? Task.FromResult(0)
                );
        }
Exemplo n.º 5
0
        public static ISegmentBuildDefinition ExtendedWithLengthAt <TOfType>(this ISegmentBuildDefinition builder, long position, Endianness endianness)
            where TOfType : unmanaged
        {
            if (!(builder is SegmentBuildDefinition def))
            {
                throw new NotSupportedException($"{builder.GetType()} is not supported");
            }
            if (!def.Length.HasValue)
            {
                throw new NotSupportedException("Must start with fixed length");
            }

            unsafe
            {
                def.ExtensionDefinition = new SegmentExtensionDefinition(type: typeof(TOfType), length: sizeof(TOfType), postion: position, endianness: endianness);
            }
            return(builder);
        }
Exemplo n.º 6
0
        public static ISegmenter ThenDo(this ISegmentBuildDefinition builder, OnSegmentReceived onSegmentReceived)
        {
            if (!(builder is SegmentBuildDefinition def))
            {
                throw new NotSupportedException($"{builder.GetType()} is not supported");
            }

            ISegmenter?built =
                def.EndsWith.HasValue ? (ISegmenter) new BetweenSegmenter(onSegmentReceived, def.StartsWith, def.EndsWith.Value, def.MaxLength, def.Options) :
                def.Length.HasValue ? new StartAndFixLengthSegmenter(onSegmentReceived, def.StartsWith, def.Length.Value, def.Options, def.ExtensionDefinition) :
                null;

            if (built == null)
            {
                throw new NotSupportedException("Unable to Build Segmenter");
            }

            return(built);
        }
Exemplo n.º 7
0
 public static ISegmentBuildDefinition AndEndsWith(this ISegmentBuildDefinition builder, ControlCharacters end)
 {
     return(AndEndsWith(builder, (byte)end));
 }
Exemplo n.º 8
0
 public static ISegmenter ThenDo(this ISegmentBuildDefinition builder, OnSegmentReceived onSegmentReceived) =>
 builder switch
 {