protected virtual void ReceiveMessage(MessageEnumerator enumerator, TimeSpan timeout, Action <Func <Message> > receiveAction)
        {
            receiveAction(() => enumerator.RemoveCurrent(timeout, MessageQueueTransactionType.None));
            // ADK - End Server PurePath Here
            DynaTraceADKFactory.initialize();
            Tagging adk = DynaTraceADKFactory.createTagging();

            adk.endServerPurePath();
        }
Example #2
0
        public static void Main(string[] args)
        {
            // initialize the dynaTrace ADK
            DynaTraceADKFactory.initialize();

            // get an instance of the Tagging ADK
            tagging = DynaTraceADKFactory.createTagging();

            Char c = '0';

            while (c != 'q')
            {
                Console.WriteLine("Available Options:");
                Console.WriteLine("  1 ... start server");
                Console.WriteLine("  2 ... do remote call (by dynaTrace tags)");
                Console.WriteLine("  3 ... do remote call (by custom tagging)");
                Console.WriteLine("  q ... quit");

                String s = Console.ReadLine();
                if (s.Length < 1)
                {
                    continue;
                }
                c = s[0];

                try {
                    switch (c)
                    {
                    case '1': new Server().run(PORT); break;

                    case '2': new Client().run(HOST, PORT, true); break;

                    case '3': new Client().run(HOST, PORT, false); break;

                    case 'q': break;
                    }
                } catch (Exception e) {
                    Console.WriteLine("exception: " + e.Message);
                }
            }

            // uninitialize the dynaTrace ADK
            DynaTraceADKFactory.uninitialize();
        }
        protected virtual bool EnumerateQueue(Func <Message, Action <Message> > receiver, TimeSpan timeout)
        {
            if (_disposed)
            {
                throw NewDisposedException();
            }

            bool received = false;

            using (MessageEnumerator enumerator = _queue.GetMessageEnumerator2())
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Enumerating endpoint: {0} ({1}ms)", Address, timeout);
                }


                // ADK - Add MessageReadPorpertyFilter to get Extension
                _queue.MessageReadPropertyFilter.Extension = true;

                while (enumerator.MoveNext(timeout))
                {
                    Message current = enumerator.Current;
                    if (current == null)
                    {
                        if (_log.IsDebugEnabled)
                        {
                            _log.DebugFormat("Current message was null while enumerating endpoint");
                        }

                        continue;
                    }
                    // ADK - Get tag here
                    DynaTraceADKFactory.initialize();
                    var adk = DynaTraceADKFactory.createTagging();
                    // Get the tag from current.Extension
                    byte[] data = current.Extension;

                    const int magicInt       = 395394190;
                    const int tracetagSize   = 1 /* Version */ + 1 /* Size */ + 4 /* ServerId */ + 4 * 6;
                    const int traceTagLength = tracetagSize + 4;
                    if (data.Length >= traceTagLength)
                    {
                        var offset    = data.Length - traceTagLength;
                        var tagOffset = offset + tracetagSize;
                        var magic     = data[tagOffset] << 24 | (data[tagOffset + 1] << 16) | (data[tagOffset + 2] << 8) | (data[tagOffset + 3]);
                        if (magic == magicInt)
                        {
                            byte[] original;
                            if (offset == 0)
                            {
                                // we do not set the extension to new byte[0] to prevent an aliasing bug in System.Messaging
                                original = new byte[1];
                            }
                            else
                            {
                                original = new byte[offset];
                                Array.Copy(data, original, offset);
                            }
                            current.Extension = original;
                            byte[] tag = new byte[traceTagLength];
                            Array.Copy(data, offset, tag, 0, traceTagLength);

                            adk.setTag(tag);
                            adk.startServerPurePath();
                        }
                    }


                    Action <Message> receive = receiver(current);
                    if (receive == null)
                    {
                        if (_log.IsDebugEnabled)
                        {
                            _log.DebugFormat("SKIP:{0}:{1}", Address, current.Id);
                        }

                        if (SpecialLoggers.Messages.IsInfoEnabled)
                        {
                            SpecialLoggers.Messages.InfoFormat("SKIP:{0}:{1}", Address, current.Id);
                        }

                        continue;
                    }

                    ReceiveMessage(enumerator, timeout, receiveCurrent =>
                    {
                        using (Message message = receiveCurrent())
                        {
                            if (message == null)
                            {
                                throw new TransportException(Address.Uri, "Unable to remove message from queue: " + current.Id);
                            }

                            if (message.Id != current.Id)
                            {
                                throw new TransportException(Address.Uri,
                                                             string.Format("Received message does not match current message: ({0} != {1})", message.Id, current.Id));
                            }

                            receive(message);

                            received = true;
                        }
                    });
                }
            }

            return(received);
        }