public void AddCommand(Command command)
 {
     lock (this.commandsLock)
     {
         this.commands.Add(command);
     }
 }
 public CommandFrame(uint ticks, Command[] commands)
 {
     Ticks = ticks;
     Commands = commands;
 }
        private void ProcessMessage(Message m)
        {
            var messageId = (DLNSchema.Messages.MessageIds)m.Id;

            switch (messageId)
            {
                case DLNSchema.Messages.MessageIds.AssignSessionId:
                    {
                        var message = (DLNSchema.Messages.AssignSessionId)m.Body;

                        this.sessionId = message.SessionId;

                        this.handler.OnConnect();
                    }
                    break;

                case DLNSchema.Messages.MessageIds.SyncFrame:
                    {
                        if (this.sessionId == 0)
                            throw new InvalidOperationException("SessionId is not assigned yet");

                        var message = (DLNSchema.Messages.SyncFrame)m.Body;
                        for (int i = 0; i < message.FramesLength; i++)
                        {
                            var f = message.GetFrames(i);

                            var commands = new Command[f.CommandsLength];
                            for (int j = 0; j < f.CommandsLength; j++)
                            {
                                var c = f.GetCommands(j);

                                commands[j] = new Command(c.CommandId, c.SessionId);
                            }

                            var frame = new CommandFrame(f.Ticks, commands);

                            lock (this.framesLock)
                            {
                                this.frames.Add(frame);
                            }

                            this.handler.OnSync();
                        }
                    }
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Unexpected message: {0}", messageId));
            }
        }
        public void SendCommand(uint ticks, Command command)
        {
            var frame = new CommandFrame(ticks, new Command[] { command });

            ThreadPool.QueueUserWorkItem(WriteThread, frame);
        }