コード例 #1
0
        private void StartMessageBuffer()
        {
            byte[] buffer = new byte[2];

            Task.Factory.StartNew(async() =>
            {
                while (!CancellationToken.IsCancellationRequested)
                {
                    await _stream.ReadAsync(buffer, 0, buffer.Length, CancellationToken);

                    short size = (short)(buffer[1] | buffer[2] << 8);
                    if (size > 0)
                    {
                        byte[] payLoad = new byte[size];
                        await _stream.ReadAsync(payLoad, 0, payLoad.Length);
                        ResponseBuffer.TryAdd(Response.GetId(payLoad), payLoad);
                    }
                }
            }, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);

            Task.Factory.StartNew(async() =>
            {
                while (!CancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        Command command;
                        if (NoReplyCommandBuffer.TryDequeue(out command))
                        {
                            await _stream.WriteAsync(command.PayLoad, 0, command.PayLoad.Length);
                            _stream.Flush();
                            ResponseBuffer.TryAdd(command.Id, null);
                        }


                        if (CommandBuffer.TryDequeue(out command))
                        {
                            await _stream.WriteAsync(command.PayLoad, 0, command.PayLoad.Length);
                            _stream.Flush();

                            int retry = 0;
                            while (!ResponseBuffer.ContainsKey(command.Id) && retry < 20)
                            {
                                await Task.Delay(50, CancellationToken);
                                retry++;
                            }
                        }

                        if (EventBuffer.TryDequeue(out command))
                        {
                            await _stream.WriteAsync(command.PayLoad, 0, command.PayLoad.Length);
                            _stream.Flush();
                        }
                    }
                    catch (TaskCanceledException) { }
                }
            }, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
        }
コード例 #2
0
        private void StartMessageBuffer()
        {
            Task.Factory.StartNew(async() =>
            {
                while (!CancellationToken.IsCancellationRequested)
                {
                    byte[] payLoad = await Read();
                    if (payLoad != null)
                    {
                        ResponseBuffer.TryAdd(Response.GetId(payLoad), payLoad);
                    }
                }
            }, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);

            Task.Factory.StartNew(async() =>
            {
                while (!CancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        Command command;
                        if (NoReplyCommandBuffer.TryDequeue(out command))
                        {
                            await Write(command);
                            ResponseBuffer.TryAdd(command.Id, null);
                        }


                        if (CommandBuffer.TryDequeue(out command))
                        {
                            await Write(command);

                            int retry = 0;
                            while (!ResponseBuffer.ContainsKey(command.Id) && retry < 20)
                            {
                                await Task.Delay(50, CancellationToken);
                                retry++;
                            }
                        }

                        if (EventBuffer.TryDequeue(out command))
                        {
                            await Write(command);
                        }
                    }
                    catch (TaskCanceledException) { }
                }
            }, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
        }
コード例 #3
0
 protected void Clear()
 {
     ResponseBuffer.Clear();
     while (NoReplyCommandBuffer.Count > 0)
     {
         NoReplyCommandBuffer.TryDequeue(out _);
     }
     while (CommandBuffer.Count > 0)
     {
         CommandBuffer.TryDequeue(out _);
     }
     while (EventBuffer.Count > 0)
     {
         EventBuffer.TryDequeue(out _);
     }
 }
コード例 #4
0
        private void StartMessageBuffer()
        {
            Task.Factory.StartNew(async() =>
            {
                while (!CancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        Command command;

                        if (NoReplyCommandBuffer.TryDequeue(out command))
                        {
                            lock (_serialPort) _serialPort.Write(command.PayLoad, 0, command.PayLoad.Length);
                            ResponseBuffer.TryAdd(command.Id, null);
                        }


                        if (CommandBuffer.TryDequeue(out command))
                        {
                            lock (_serialPort) _serialPort.Write(command.PayLoad, 0, command.PayLoad.Length);
                            int retry = 0;
                            while (!ResponseBuffer.ContainsKey(command.Id) && retry < 20)
                            {
                                await Task.Delay(50, CancellationToken);
                                retry++;
                            }
                        }

                        if (EventBuffer.TryDequeue(out command))
                        {
                            lock (_serialPort) _serialPort.Write(command.PayLoad, 0, command.PayLoad.Length);
                        }
                    }
                    catch (TaskCanceledException) { }
                }
            }, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
        }