Ejemplo n.º 1
0
 static void EnqueueToServerBatch(CAsyncQueue sq, string message, int cycles, uint batchSize = 8 * 1024)
 {
     Console.WriteLine("Going to enqueue " + cycles + " messages ......");
     using (CScopeUQueue sb = new CScopeUQueue()) {
         CUQueue q    = sb.UQueue;
         byte[]  utf8 = Encoding.UTF8.GetBytes(message);
         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
         sw.Start();
         for (int n = 0; n < cycles; ++n)
         {
             CAsyncQueue.BatchMessage(idMessage, utf8, q);
             if (q.GetSize() >= batchSize)
             {
                 sq.EnqueueBatch(TEST_QUEUE_KEY, q);
             }
         }
         if (q.GetSize() > 0)
         {
             sq.EnqueueBatch(TEST_QUEUE_KEY, q);
         }
         sq.WaitAll();
         sw.Stop();
         Console.WriteLine(cycles + " messages sent to server and enqueued within " + sw.ElapsedMilliseconds + " ms");
     }
 }
Ejemplo n.º 2
0
    static void Main(string[] args)
    {
        Console.WriteLine("Remote host: ");
        string             host = Console.ReadLine();
        CConnectionContext cc   = new CConnectionContext(host, 20901, "async_queue_client", "pwd_for_async_queue");

        using (CSocketPool <CAsyncQueue> spAq = new CSocketPool <CAsyncQueue>())
        {
            if (!spAq.StartSocketPool(cc, 1, 1))
            {
                Console.WriteLine("Failed in connecting to remote async queue server");
                Console.WriteLine("Press any key to close the application ......");
                Console.Read();
                return;
            }
            CAsyncQueue aq = spAq.Seek();

            //Optionally, you can enqueue messages with transaction style by calling the methods StartQueueTrans and EndQueueTrans in pair
            aq.StartQueueTrans(TEST_QUEUE_KEY, (errCode) =>
            {
                //error code could be one of CAsyncQueue.QUEUE_OK, CAsyncQueue.QUEUE_TRANS_ALREADY_STARTED, ......
            });
            TestEnqueue(aq);

            //test message batching
            using (CScopeUQueue sb = new CScopeUQueue())
            {
                CUQueue q = sb.UQueue;
                CAsyncQueue.BatchMessage(idMessage3, "Hello", "World", q);
                CAsyncQueue.BatchMessage(idMessage4, true, 234.456, "MyTestWhatever", q);
                aq.EnqueueBatch(TEST_QUEUE_KEY, q, (res) =>
                {
                    System.Diagnostics.Debug.Assert(res == 2);
                });
            }
            aq.EndQueueTrans(false);
            TestDequeue(aq);
            aq.WaitAll();

            //get a queue key two parameters, message count and queue file size by default option oMemoryCached
            aq.FlushQueue(TEST_QUEUE_KEY, (messageCount, fileSize) =>
            {
                Console.WriteLine("Total message count={0}, queue file size={1}", messageCount, fileSize);
            });

            aq.GetKeys((keys) =>
            {
                keys = null;
            });

            aq.CloseQueue(TEST_QUEUE_KEY, (errCode) =>
            {
                //error code could be one of CAsyncQueue.QUEUE_OK, CAsyncQueue.QUEUE_TRANS_ALREADY_STARTED, ......
            });

            Console.WriteLine("Press any key to close the application ......");
            Console.Read();
        }
    }
Ejemplo n.º 3
0
 static void EnqueueToServer(CAsyncQueue sq, string message, int cycles)
 {
     Console.WriteLine("Going to enqueue " + cycles + " messages ......");
     byte[] utf8 = Encoding.UTF8.GetBytes(message);
     System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
     sw.Start();
     for (int n = 0; n < cycles; ++n)
     {
         sq.Enqueue(TEST_QUEUE_KEY, idMessage, utf8);
     }
     sq.WaitAll();
     sw.Stop();
     Console.WriteLine(cycles + " messages sent to server and enqueued within " + sw.ElapsedMilliseconds + " ms");
 }
Ejemplo n.º 4
0
 static void EnqueueToServer(CAsyncQueue sq, string message, int cycles)
 {
     Console.WriteLine("Going to enqueue " + cycles + " messages ......");
     byte[] utf8 = Encoding.UTF8.GetBytes(message);
     System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
     sw.Start();
     for (int n = 0; n < cycles; ++n)
     {
         sq.Enqueue(TEST_QUEUE_KEY, idMessage, utf8);
     }
     sq.WaitAll();
     sw.Stop();
     Console.WriteLine(cycles + " messages sent to server and enqueued within " + sw.ElapsedMilliseconds + " ms");
 }
Ejemplo n.º 5
0
    static void DequeueFromServer(CAsyncQueue sq)
    {
        uint messages_dequeued = 0;

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        CAsyncQueue.DDequeue         d  = (aq, messageCount, fileSize, messages, bytes) => {
            if (messageCount > 0)
            {
                //there are more messages left at server queue, we re-send a request to dequeue
                aq.Dequeue(TEST_QUEUE_KEY, sq.LastDequeueCallback);
            }
            else
            {
                //set dequeue callback to null and stop dequeuing
                aq.LastDequeueCallback = null;
            }
        };
        CAsyncServiceHandler.DOnResultReturned rr = (sender, reqId, q) => {
            bool processed = false;
            switch (reqId)
            {
            case idMessage:
            {
                byte[] utf8 = q.IntenalBuffer;
                string s    = CUQueue.ToString(utf8, (int)q.GetSize());
                ++messages_dequeued;
            }
                processed = true;
                break;

            default:
                break;
            }
            return(processed);
        };

        sq.ResultReturned += rr;
        Console.WriteLine("Going to dequeue message ......");
        sw.Start();
        bool ok = sq.Dequeue(TEST_QUEUE_KEY, d);

        //optionally, add one or two extra to improve processing concurrency at both client and server sides for better performance and throughput
        ok = sq.Dequeue(TEST_QUEUE_KEY, d);
        ok = sq.Dequeue(TEST_QUEUE_KEY, d);
        sq.WaitAll();
        sq.ResultReturned -= rr;
        sw.Stop();
        Console.WriteLine(messages_dequeued + " messages dequeued from server within " + sw.ElapsedMilliseconds + " ms");
    }
Ejemplo n.º 6
0
    static void DequeueFromServer(CAsyncQueue sq)
    {
        uint messages_dequeued = 0;
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        CAsyncQueue.DDequeue d = (messageCount, fileSize, messages, bytes) =>
        {
            if (messageCount > 0)
            {
                //there are more messages left at server queue, we re-send a request to dequeue
                sq.Dequeue(TEST_QUEUE_KEY, sq.LastDequeueCallback);
            }
        };

        sq.ResultReturned += (sender, reqId, q) =>
        {
            bool processed = false;
            switch (reqId)
            {
                case idMessage:
                    {
                        byte[] utf8 = q.IntenalBuffer;
                        string s = CUQueue.ToString(utf8, (int)q.GetSize());
                        ++messages_dequeued;
                    }
                    processed = true;
                    break;
                default:
                    break;
            }
            return processed;
        };
        Console.WriteLine("Going to dequeue message ......");
        sw.Start();
        bool ok = sq.Dequeue(TEST_QUEUE_KEY, d);

        //optionally, add one or two extra to improve processing concurrency at both client and server sides for better performance and through-output
        ok = sq.Dequeue(TEST_QUEUE_KEY, d);
        ok = sq.Dequeue(TEST_QUEUE_KEY, d);
        sq.WaitAll();
        sw.Stop();
        Console.WriteLine(messages_dequeued + " messages dequeued from server within " + sw.ElapsedMilliseconds + " ms");
    }
Ejemplo n.º 7
0
    static void EnqueueToServerBatch(CAsyncQueue sq, string message, int cycles, uint batchSize = 16 * 1024)
    {
        Console.WriteLine("Going to enqueue " + cycles + " messages ......");
        using (CScopeUQueue sb = new CScopeUQueue())
        {
            CUQueue q = sb.UQueue;
            byte[] utf8 = Encoding.UTF8.GetBytes(message);
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            for (int n = 0; n < cycles; ++n)
            {
                CAsyncQueue.BatchMessage(idMessage, utf8, q);
                if (q.GetSize() >= batchSize)
                {
                    sq.EnqueueBatch(TEST_QUEUE_KEY, q, (res) => {

                    });
                }
            }
            if (q.GetSize() > 0)
            {
                sq.EnqueueBatch(TEST_QUEUE_KEY, q, (res) =>
                {

                });
            }
            sq.WaitAll();
            sw.Stop();
            Console.WriteLine(cycles + " messages sent to server and enqueued within " + sw.ElapsedMilliseconds + " ms");
        }
    }
Ejemplo n.º 8
0
    static void Main(string[] args)
    {
        Console.WriteLine("Remote host: ");
        string             host = Console.ReadLine();
        CConnectionContext cc   = new CConnectionContext(host, 20901, "async_queue_client", "pwd_for_async_queue");

        using (CSocketPool <CAsyncQueue> spAq = new CSocketPool <CAsyncQueue>())
        {
            if (!spAq.StartSocketPool(cc, 1))
            {
                Console.WriteLine("Failed in connecting to remote async queue server");
                Console.WriteLine("Press any key to close the application ......");
                Console.Read();
                return;
            }
            CAsyncQueue aq = spAq.Seek();
            try
            {
                //Optionally, you can enqueue messages with transaction style by calling the methods StartQueueTrans and EndQueueTrans in pair
                var fsqt = aq.startQueueTrans(TEST_QUEUE_KEY);
                TestEnqueue(aq);
                //test message batching
                using (CScopeUQueue sb = new CScopeUQueue())
                {
                    CUQueue q = sb.UQueue;
                    CAsyncQueue.BatchMessage(idMessage3, "Hello", "World", q);
                    CAsyncQueue.BatchMessage(idMessage4, true, 234.456, "MyTestWhatever", q);
                    if (!aq.EnqueueBatch(TEST_QUEUE_KEY, q))
                    {
                        throw new CSocketError(CAsyncQueue.SESSION_CLOSED_BEFORE, "Socket already closed before sending the request EnqueueBatch", CAsyncQueue.idEnqueueBatch, true);
                    }
                }
                var feqt = aq.endQueueTrans(false);
                TestDequeue(aq);
                aq.WaitAll();
                //get a queue key two parameters, message count and queue file size by default option oMemoryCached
                var ffq = aq.flushQueue(TEST_QUEUE_KEY);
                var fgk = aq.getKeys();
                var fcq = aq.closeQueue(TEST_QUEUE_KEY);
                Console.WriteLine("StartQueueTrans/res: " + fsqt.Result);
                Console.WriteLine("EndQueueTrans/res: " + feqt.Result);
                Console.WriteLine(ffq.Result);
                int index = 0;
                Console.Write("[");
                string[] keys = fgk.Result;
                foreach (string k in keys)
                {
                    if (index != 0)
                    {
                        Console.Write(",");
                    }
                    Console.Write(k);
                }
                Console.WriteLine("]");
                Console.WriteLine("CloseQueue/res: " + fcq.Result);
            }
            catch (AggregateException ex)
            {
                foreach (Exception e in ex.InnerExceptions)
                {
                    //An exception from server (CServerError), Socket closed after sending a request (CSocketError) or request canceled (CSocketError),
                    Console.WriteLine(e);
                }
            }
            catch (CSocketError ex)
            {
                //Socket is already closed before sending a request
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                //bad operations such as invalid arguments, bad operations and de-serialization errors, and so on
                Console.WriteLine(ex);
            }
            Console.WriteLine("Press any key to close the application ......");
            Console.Read();
        }
    }