Ejemplo n.º 1
0
        private void CreateSession(System.Net.Sockets.TcpClient client)
        {
            // Cancel any existing sessions
            _cancel.Cancel();
            _cancel = new CancellationTokenSource();

            _log.InfoFormat("Client {0} connected", client.Client.RemoteEndPoint);
            var reader = new OpcReader(client, _log);
            var token = _cancel.Token;
            Task.Run(async () =>
            {
                try
                {
                    OpcMessage? message;
                    do
                    {
                        message = await reader.ReadMessageAsync(token).ConfigureAwait(false);
                        if (message != null)
                            HandleMessageReceived(message.Value);
                    } while (message != null && !token.IsCancellationRequested);
                }
                catch (Exception err)
                {
                    if(!IsHandled(err))
                        throw;
                }
                finally
                {
                    _log.InfoFormat("Client {0} disconnected", client.Client.RemoteEndPoint);
                    reader.Dispose();
                }

            }, token);
        }
Ejemplo n.º 2
0
        private void HandleMessageReceived(OpcMessage e)
        {
            // Expose the raw message (debugging / tests)
            OnMessageReceived(e);

            // Also expose the system-level command
            switch (e.Command)
            {
            case OpcCommandType.SetPixels:
                _log.VerboseFormat("Dispatch SetPixels(byte[{1}]) to channel {0}", e.Channel, e.Data.Length);
                HandleSetPixels(e.Channel, e.Data);
                break;

            case OpcCommandType.SystemExclusive: {
                // Payload starts with a system ID 0x00 0x01 = FadeCandy
                // It seems to have 2 byte 'command id' for these:
                // 01 - color correction
                // 02 - firmware config
                var systemId = OpcReader.ReadUInt16(e.Data, 0);
                _log.WarnFormat("System-specific command for {0:x4} not handled", systemId);
                break;
            }

            default:
                _log.WarnFormat("Command {0}-{1} unhandled", e.Channel, e.Command);
                break;
            }
            ;
        }
Ejemplo n.º 3
0
        private void CreateSession(System.Net.Sockets.TcpClient client)
        {
            // Cancel any existing sessions
            _cancel.Cancel();
            _cancel = new CancellationTokenSource();

            _log.InfoFormat("Client {0} connected", client.Client.RemoteEndPoint);
            var reader = new OpcReader(client, _log);
            var token  = _cancel.Token;

            Task.Run(async() =>
            {
                try
                {
                    OpcMessage?message;
                    do
                    {
                        message = await reader.ReadMessageAsync(token).ConfigureAwait(false);
                        if (message != null)
                        {
                            HandleMessageReceived(message.Value);
                        }
                    } while (message != null && !token.IsCancellationRequested);
                }
                catch (Exception err)
                {
                    if (!IsHandled(err))
                    {
                        throw;
                    }
                }
                finally
                {
                    _log.InfoFormat("Client {0} disconnected", client.Client.RemoteEndPoint);
                    reader.Dispose();
                }
            }, token);
        }
Ejemplo n.º 4
0
        private static void CreateClient(System.Net.Sockets.TcpClient client)
        {
            var ep = client.Client.RemoteEndPoint;
            Log.InfoFormat("Client {0} connected", ep);
            var session = new OpcReader(client, Log);

            Task.Run(async () =>
            {
                while (true)
                {
                    var message = await session.ReadMessageAsync().ConfigureAwait(false);
                    if (message == null)
                    {
                        Log.InfoFormat("Client {0} disconnected", ep);
                        return;
                    }

                    // We don't need this queuing. That wasn't the problem anyway!
                    Queue.Enqueue(message.Value);
                    //HandleMessageReceived(session, message);
                }
            });
        }
Ejemplo n.º 5
0
        const int defaultPort = 7890; // same as OpenPixelControl reference impl. uses

        #endregion Fields

        #region Methods

        private static void CreateClient(System.Net.Sockets.TcpClient client)
        {
            Console.WriteLine("Handling client connect from {0}", client.Client.RemoteEndPoint);
            var reader = new OpcReader(client);
        }