Exemplo n.º 1
0
        /// <summary>
        /// Queues a sequence of bytes, from the specified data source, onto the stream for parsing.
        /// </summary>
        /// <param name="source">Identifier of the data source.</param>
        /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the queue.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
        /// <param name="count">The number of bytes to be written to the current stream.</param>
        public override void Parse(SourceChannel source, byte[] buffer, int offset, int count)
        {
            // When SEL Fast Message is transmitted over Ethernet it is embedded in a Telnet stream. As a result
            // any 0xFF will be encoded for Telnet compliance as a duplicate, i.e., 0xFF 0xFF. We remove these
            // duplications when encountered to make sure check-sums and parsing work as expected.
            int doubleFFPosition = buffer.IndexOfSequence(new byte[] { 0xFF, 0xFF }, offset, count);

            while (doubleFFPosition > -1)
            {
                using (BlockAllocatedMemoryStream newBuffer = new BlockAllocatedMemoryStream())
                {
                    // Write buffer before repeated byte
                    newBuffer.Write(buffer, offset, doubleFFPosition - offset + 1);

                    int nextByte = doubleFFPosition + 2;

                    // Write buffer after repeated byte, if any
                    if (nextByte < offset + count)
                    {
                        newBuffer.Write(buffer, nextByte, offset + count - nextByte);
                    }

                    buffer = newBuffer.ToArray();
                }

                offset = 0;
                count  = buffer.Length;

                // Find next 0xFF 0xFF sequence
                doubleFFPosition = buffer.IndexOfSequence(new byte[] { 0xFF, 0xFF }, offset, count);
            }

            base.Parse(source, buffer, offset, count);
        }
Exemplo n.º 2
0
            private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                var startTime  = DateTime.Now;
                var lockObject = SourceChannel.LockObject;

                lock (lockObject)
                {
                    var masterStopWatch = new Stopwatch();
                    var requestBytes    = new byte[] { };

                    TimedThreadBlocker.Wait(requestDelay);

                    requestBytes = new byte[SerialPort.BytesToRead];

                    if (requestBytes.Length > 0)
                    {
                        Read(ref requestBytes, 0, requestBytes.Length, 0);
                        SourceChannel.Write(ref requestBytes, 0, requestBytes.Length);
                        TimedThreadBlocker.Wait(responseDelay);
                        masterStopWatch.Start();

                        var sequence = 0;
                        while (SourceChannel.NumberOfBytesAvailable > 0 && responseTimeout > masterStopWatch.ElapsedMilliseconds)
                        {
                            var responseBytes = new byte[SourceChannel.NumberOfBytesAvailable];

                            SourceChannel.Read(ref responseBytes, 0, responseBytes.Length, sequence++);
                            Write(ref responseBytes, 0, responseBytes.Length);
                        }

                        masterStopWatch.Stop();
                    }
                }
            }
Exemplo n.º 3
0
 public DiscordCrosspostWebhook()
 {
     OnClientUpdated += (s, e) =>
     {
         SourceGuild.SetClient(Client);
         SourceChannel.SetClient(Client);
     };
 }
Exemplo n.º 4
0
        /////////////////////////////////////////

        //Component_Skeleton.SkinningModeEnum GetSkinningMode( Component_Skeleton skeleton )
        //{
        //	var _override = OverrideSkinningMode.Value;
        //	if( _override != Component_Skeleton.SkinningModeEnum.Auto )
        //		return _override;

        //	var selected = skeleton.SkinningMode.Value;
        //	if( selected != Component_Skeleton.SkinningModeEnum.Auto )
        //		return selected;

        //	if( !hasScale )
        //		return Component_Skeleton.SkinningModeEnum.DualQuaternion;
        //	else
        //		return Component_Skeleton.SkinningModeEnum.Linear;
        //}

        protected virtual void CalculateCPU(Component_Skeleton skeleton, Component_Mesh originalMesh, Component_Mesh modifiableMesh)
        {
            bool dualQuaternion = false;            // GetSkinningMode( skeleton ) == Component_Skeleton.SkinningModeEnum.DualQuaternion;

            for (int nOper = 0; nOper < modifiableMesh.Result.MeshData.RenderOperations.Count; nOper++)
            {
                var sourceOper = originalMesh.Result.MeshData.RenderOperations[nOper];
                var destOper   = modifiableMesh.Result.MeshData.RenderOperations[nOper];

                var position = new ChannelFloat3(sourceOper, destOper, VertexElementSemantic.Position);
                if (position.Exists)
                {
                    var normal  = new ChannelFloat3(sourceOper, destOper, VertexElementSemantic.Normal);
                    var tangent = new ChannelFloat4(sourceOper, destOper, VertexElementSemantic.Tangent);

                    var blendIndices = new SourceChannel <Vector4I>(sourceOper, VertexElementSemantic.BlendIndices, VertexElementType.Integer4);
                    var blendWeights = new SourceChannel <Vector4F>(sourceOper, VertexElementSemantic.BlendWeights, VertexElementType.Float4);

                    if (!blendIndices.Exists || !blendWeights.Exists)
                    {
                        continue;
                    }

                    if (normal.Exists)
                    {
                        if (tangent.Exists)
                        {
                            TransformVertices(
                                dualQuaternion,
                                position.SourceData, normal.SourceData, tangent.SourceData, blendIndices.SourceData, blendWeights.SourceData,
                                position.DestData, normal.DestData, tangent.DestData
                                );
                        }
                        else
                        {
                            TransformVertices(
                                dualQuaternion,
                                position.SourceData, normal.SourceData, blendIndices.SourceData, blendWeights.SourceData,
                                position.DestData, normal.DestData
                                );
                        }
                    }
                    else
                    {
                        TransformVertices(
                            dualQuaternion,
                            position.SourceData, blendIndices.SourceData, blendWeights.SourceData,
                            position.DestData
                            );
                    }

                    position.WriteChannel();
                    normal.WriteChannel();
                    tangent.WriteChannel();
                }
            }
        }
Exemplo n.º 5
0
 public void SetChannel(int channel, SourceChannel ch)
 {
     lock (_channels) {
         var c = GetChannel(channel, true);
         if (c != null)
         {
             c.channel = ch;
         }
     }
 }
Exemplo n.º 6
0
 public Task <StopReason> Run()
 {
     if (SourceChannel != null)
     {
         SourceChannel.AddContentSink(this);
         return(taskSource.Task.ContinueWith(prev => {
             SourceChannel.RemoveContentSink(this);
             StoppedReason = prev.Result;
             return prev.Result;
         }));
     }
     else
     {
         StoppedReason = StopReason.NoHost;
         taskSource.TrySetResult(StopReason.NoHost);
         return(taskSource.Task);
     }
 }
Exemplo n.º 7
0
        private List <SourceChannel> ParseFile(string filePath)
        {
            var channelsList = new List <SourceChannel>();

            try
            {
                var rawFileContentByLines = CleanupSourceFile(File.ReadAllLines(filePath));


                for (int i = 0; i < rawFileContentByLines.Length; i++)
                {
                    var line = rawFileContentByLines[i];
                    if (!line.StartsWith("#EXTINF", StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    var nextLine = rawFileContentByLines.Length > i + 1 ? rawFileContentByLines[i + 1] : null;
                    if (string.IsNullOrEmpty(nextLine) || !nextLine.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    var channel = new SourceChannel()
                    {
                        Name = line.Split(',')[1].Trim(),
                        Link = nextLine.Trim()
                    };

                    channelsList.Add(channel);
                    i++;
                }
            }
            catch (Exception ex) { Logger.Exception(ex); }

            Logger.Info($"parsed { channelsList.Count } channels in { Path.GetFileName(filePath) }");

            return(channelsList);
        }
Exemplo n.º 8
0
        private void TcpClientBeginReadCallback(IAsyncResult asyncResult)
        {
            var tcpClient = asyncResult.AsyncState as TcpClient;

            try
            {
                if (asyncResult.IsCompleted)
                {
                    if (tcpClient != null)
                    {
                        try
                        {
                            var startTime = DateTime.Now;
                            var sourceChannelLockObject = SourceChannel.LockObject;
                            lock (sourceChannelLockObject)
                            {
                                lock (lockObject)
                                {
                                    var readBuffer      = new byte[1500];
                                    var masterStopWatch = new Stopwatch();
                                    var requestBytes    = new byte[] { };
                                    var networkStream   = tcpClient.GetStream();

                                    this.tcpClient = tcpClient;

                                    TimedThreadBlocker.Wait(requestDelay);

                                    requestBytes = new byte[tcpClient.Available];

                                    if (requestBytes.Length > 0)
                                    {
                                        Read(ref requestBytes, 0, requestBytes.Length, 0);
                                        SourceChannel.Write(ref requestBytes, 0, requestBytes.Length);
                                        TimedThreadBlocker.Wait(responseDelay);
                                        masterStopWatch.Start();

                                        var sequence = 0;
                                        while (SourceChannel.NumberOfBytesAvailable > 0 && responseTimeout > masterStopWatch.ElapsedMilliseconds)
                                        {
                                            var responseBytes = new byte[SourceChannel.NumberOfBytesAvailable];

                                            SourceChannel.Read(ref responseBytes, 0, responseBytes.Length, sequence++);
                                            Write(ref responseBytes, 0, responseBytes.Length);
                                        }

                                        masterStopWatch.Stop();
                                    }

                                    networkStream.BeginRead(readBuffer, 0, 0, new AsyncCallback(TcpClientBeginReadCallback), tcpClient);

                                    return;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError($"Error reading newtwork stream: {ex.Message}");
                        }
                    }
                }

                try
                {
                    tcpClient.Dispose();
                }
                catch (Exception ex)
                {
                    Trace.TraceError($"Error disposing TCP Client: {ex.Message}");
                }

                try
                {
                    tcpClients.Remove(tcpClient);
                }
                catch (Exception ex)
                {
                    Trace.TraceError($"Error removing TCP Client from list: {ex.Message}");
                }

                Interlocked.Decrement(ref numberOfTcpClients);
            }
            catch (Exception ex)
            {
                Trace.TraceError($"Error disposing TCP Client: {ex.Message}");
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Writes a sequence of bytes onto the stream for parsing.
        /// </summary>
        /// <param name="source">Defines the source channel for the data.</param>
        /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
        /// <param name="count">The number of bytes to be written to the current stream.</param>
        public override void Parse(SourceChannel source, byte[] buffer, int offset, int count)
        {
            // Since the Macrodyne implementation supports both 0xAA and 0xBB as sync-bytes, we must manually check for both during stream initialization,
            // base class handles this only then there is a consistently defined set of sync-bytes, not variable.

            if (Enabled)
            {
                // See if there are any 0xAA 0xAA sequences - these must be removed
                int syncBytePosition = buffer.IndexOfSequence(new byte[] { 0xAA, 0xAA }, offset, count);

                while (syncBytePosition > -1)
                {
                    using (BlockAllocatedMemoryStream newBuffer = new BlockAllocatedMemoryStream())
                    {
                        // Write buffer before repeated byte
                        newBuffer.Write(buffer, offset, syncBytePosition - offset + 1);

                        int nextByte = syncBytePosition + 2;

                        // Write buffer after repeated byte, if any
                        if (nextByte < offset + count)
                        {
                            newBuffer.Write(buffer, nextByte, offset + count - nextByte);
                        }

                        buffer = newBuffer.ToArray();
                    }

                    offset = 0;
                    count  = buffer.Length;

                    // Find next 0xAA 0xAA sequence
                    syncBytePosition = buffer.IndexOfSequence(new byte[] { 0xAA, 0xAA }, offset, count);
                }

                if (StreamInitialized)
                {
                    base.Parse(source, buffer, offset, count);
                }
                else
                {
                    // Initial stream may be anywhere in the middle of a frame, so we attempt to locate sync-bytes to "line-up" data stream,
                    // First we look for data frame sync-byte:
                    syncBytePosition = buffer.IndexOfSequence(new byte[] { 0xAA }, offset, count);

                    if (syncBytePosition > -1)
                    {
                        StreamInitialized = true;
                        base.Parse(source, buffer, syncBytePosition, count - (syncBytePosition - offset));
                    }
                    else
                    {
                        // Second we look for command frame response sync-byte:
                        syncBytePosition = buffer.IndexOfSequence(new byte[] { 0xBB }, offset, count);

                        if (syncBytePosition > -1)
                        {
                            StreamInitialized = true;
                            base.Parse(source, buffer, syncBytePosition, count - (syncBytePosition - offset));
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
 void IFrameParser.Parse(SourceChannel source, byte[] buffer, int offset, int count)
 {
     Parse(source, buffer, offset, count);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Writes a sequence of bytes onto the <see cref="IBinaryImageParser"/> stream for parsing.
        /// </summary>
        /// <param name="source">Defines the source channel for the data.</param>
        /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
        /// <param name="count">The number of bytes to be written to the current stream.</param>
        internal void Parse(SourceChannel source, byte[] buffer, int offset, int count)
        {
            // Pass data from communications client into protocol specific frame parser
            m_frameParser.Parse(source, buffer, offset, count);

            m_byteRateTotal += count;

            if (m_initiatingDataStream)
                m_initialBytesReceived += count;
        }
Exemplo n.º 12
0
    public int ReadAudio(float[] data, int dstIdx, int readlength, int targetChannels, SourceChannel channelType, bool modulate = true)
    {
        int length           = 0;
        int DataLength       = readlength;
        int srcChannelsCount = 2;

        int channelIndex = 0;
        int stepSize     = 1;

        while (length < DataLength && startIndex < samples.Length)
        {
            srcChannelsCount = channels;
            if (channels == 2 && channelType != SourceChannel.Both)
            {
                srcChannelsCount = 1;
                stepSize         = 2;
                channelIndex     = (channelType == SourceChannel.Left ? 0 : 1);
            }

            //calculate the left amount of data in this packet
            int sz = samples.Length - startIndex;
            //determine the amount of data we going to use of this packet
            int count = (int)Mathf.Clamp(sz, 0,
                                         readlength - length);//Remaining data to be filled

            GstNetworkAudioPlayer.ProcessAudioPackets(samples, startIndex, channelIndex, count, stepSize, srcChannelsCount, data, (dstIdx + length), channels, modulate);

            startIndex += count;
            length     += count;
        }
        return(length);
    }