Beispiel #1
0
    static void TestEnqueue(CAsyncQueue aq)
    {
        Console.WriteLine("Going to enqueue 1024 messages ......");
        for (int n = 0; n < 1024; ++n)
        {
            string str = n + " Object test";
            ushort idMessage;
            switch (n % 3)
            {
            case 0:
                idMessage = idMessage0;
                break;

            case 1:
                idMessage = idMessage1;
                break;

            default:
                idMessage = idMessage2;
                break;
            }
            //System.Threading.Thread.Sleep(100);
            //enqueue two unicode strings and one int
            if (!aq.Enqueue(TEST_QUEUE_KEY, idMessage, "SampleName", str, n))
            {
                aq.raise(CAsyncQueue.idEnqueue);
            }
        }
    }
Beispiel #2
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");
     }
 }
Beispiel #3
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");
        }
    }
Beispiel #4
0
    static bool TestEnqueue(CAsyncQueue aq)
    {
        bool ok = true;

        Console.WriteLine("Going to enqueue 1024 messages ......");
        for (int n = 0; n < 1024; ++n)
        {
            string str = n + " Object test";
            ushort idMessage;
            switch (n % 3)
            {
            case 0:
                idMessage = idMessage0;
                break;

            case 1:
                idMessage = idMessage1;
                break;

            default:
                idMessage = idMessage2;
                break;
            }
            //enqueue two unicode strings and one int
            ok = aq.Enqueue(TEST_QUEUE_KEY, idMessage, "SampleName", str, n);
            if (!ok)
            {
                break;
            }
        }
        return(ok);
    }
Beispiel #5
0
    static Task <CAsyncQueue.DeqInfo> TestDequeue(CAsyncQueue aq)
    {
        //prepare callback for parsing messages dequeued from server side
        aq.ResultReturned += (sender, idReq, q) =>
        {
            bool processed = true;
            switch (idReq)
            {
            case idMessage0:
            case idMessage1:
            case idMessage2:
                Console.Write("message id={0}", idReq);
                {
                    string name, str;
                    int    index;
                    //parse a dequeued message which should be the same as the above enqueued message (two unicode strings and one int)
                    q.Load(out name).Load(out str).Load(out index);
                    Console.WriteLine(", name={0}, str={1}, index={2}", name, str, index);
                }
                break;

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

        TaskCompletionSource <CAsyncQueue.DeqInfo> tcs = new TaskCompletionSource <CAsyncQueue.DeqInfo>();

        CAsyncQueue.DDiscarded             aborted = CAsyncQueue.get_aborted(tcs, CAsyncQueue.idDequeue);
        CAsyncQueue.DOnExceptionFromServer se      = CAsyncQueue.get_se(tcs);

        //prepare a callback for processing returned result of dequeue request
        CAsyncQueue.DDequeue d = (asyncq, messageCount, fileSize, messages, bytes) =>
        {
            if (messages > 0)
            {
                Console.WriteLine("Total message count={0}, queue file size={1}, messages dequeued={2}, message bytes dequeued={3}", messageCount, fileSize, messages, bytes);
            }
            if (messageCount > 0)
            {
                //there are more messages left at server queue, we re-send a request to dequeue
                asyncq.Dequeue(TEST_QUEUE_KEY, asyncq.LastDequeueCallback, 0, aborted, se);
            }
            else
            {
                tcs.TrySetResult(new CAsyncQueue.DeqInfo(messageCount, fileSize, messages, bytes));
            }
        };

        Console.WriteLine("Going to dequeue messages ......");
        //optionally, add one extra to improve processing concurrency at both client and server sides for better performance and through-output
        if (!(aq.Dequeue(TEST_QUEUE_KEY, d, 0, aborted, se) && aq.Dequeue(TEST_QUEUE_KEY, d, 0, aborted, se)))
        {
            aq.raise(CAsyncQueue.idDequeue);
        }
        return(tcs.Task);
    }
Beispiel #6
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();
        }
    }
Beispiel #7
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");
 }
Beispiel #8
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");
 }
Beispiel #9
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");
    }
Beispiel #10
0
    static void TestDequeue(CAsyncQueue aq)
    {
        //prepare callback for parsing messages dequeued from server side
        aq.ResultReturned += (sender, idReq, q) =>
        {
            bool processed = true;
            switch (idReq)
            {
            case idMessage0:
            case idMessage1:
            case idMessage2:
                Console.Write("message id={0}", idReq);
                {
                    string name, str;
                    int    index;
                    //parse a dequeued message which should be the same as the above enqueued message (two unicode strings and one int)
                    q.Load(out name).Load(out str).Load(out index);
                    Console.WriteLine(", name={0}, str={1}, index={2}", name, str, index);
                }
                break;

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

        //prepare a callback for processing returned result of dequeue request
        CAsyncQueue.DDequeue d = (messageCount, fileSize, messages, bytes) =>
        {
            Console.WriteLine("Total message count={0}, queue file size={1}, messages dequeued={2}, message bytes dequeued={3}", 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, aq.LastDequeueCallback);
            }
        };

        Console.WriteLine("Going to dequeue messages ......");
        bool ok = aq.Dequeue(TEST_QUEUE_KEY, d);

        //optionally, add one extra to improve processing concurrency at both client and server sides for better performance and through-output
        ok = aq.Dequeue(TEST_QUEUE_KEY, d);
    }
Beispiel #11
0
    static void TestDequeue(CAsyncQueue aq)
    {
        //prepare callback for parsing messages dequeued from server side
        aq.ResultReturned += (sender, idReq, q) =>
        {
            bool processed = true;
            switch (idReq)
            {
                case idMessage0:
                case idMessage1:
                case idMessage2:
                    Console.Write("message id={0}", idReq);
                    {
                        string name, str;
                        int index;
                        //parse a dequeued message which should be the same as the above enqueued message (two unicode strings and one int)
                        q.Load(out name).Load(out str).Load(out index);
                        Console.WriteLine(", name={0}, str={1}, index={2}", name, str, index);
                    }
                    break;
                default:
                    processed = false;
                    break;
            }
            return processed;
        };

        //prepare a callback for processing returned result of dequeue request
        CAsyncQueue.DDequeue d = (messageCount, fileSize, messages, bytes) =>
        {
            Console.WriteLine("Total message count={0}, queue file size={1}, messages dequeued={2}, message bytes dequeued={3}", 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, aq.LastDequeueCallback);
            }
        };

        Console.WriteLine("Going to dequeue messages ......");
        bool ok = aq.Dequeue(TEST_QUEUE_KEY, d);

        //optionally, add one extra to improve processing concurrency at both client and server sides for better performance and through-output
        ok = aq.Dequeue(TEST_QUEUE_KEY, d);
    }
Beispiel #12
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>())
        {
            //spAq.QueueName = "qname";
            if (!spAq.StartSocketPool(cc, 1))
            {
                Console.WriteLine("Failed in connecting to remote async queue server");
                Console.WriteLine("Press key ENTER to close the application ......");
                Console.ReadLine();
                return;
            }
            CAsyncQueue aq = spAq.Seek();
            try
            {
                TestEnqueue(aq);
                Console.WriteLine(TestDequeue(aq).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 key ENTER to close the application ......");
            Console.ReadLine();
        }
    }
Beispiel #13
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");
    }
Beispiel #14
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 key ENTER to close the application ......");
                Console.ReadLine();
                return;
            }
            CAsyncQueue aq = spAq.Seek();

            TestEnqueue(aq);
            TestDequeue(aq);

            Console.WriteLine("Press key ENTER to close the application ......");
            Console.ReadLine();
        }
    }
Beispiel #15
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();
        }
    }
Beispiel #16
0
 static bool TestEnqueue(CAsyncQueue aq)
 {
     bool ok = true;
     Console.WriteLine("Going to enqueue 1024 messages ......");
     for (int n = 0; n < 1024; ++n)
     {
         string str = n + " Object test";
         ushort idMessage;
         switch (n % 3)
         {
             case 0:
                 idMessage = idMessage0;
                 break;
             case 1:
                 idMessage = idMessage1;
                 break;
             default:
                 idMessage = idMessage2;
                 break;
         }
         //enqueue two unicode strings and one int
         ok = aq.Enqueue(TEST_QUEUE_KEY, idMessage, "SampleName", str, n);
         if (!ok)
             break;
     }
     return ok;
 }
Beispiel #17
0
    static void Main(string[] args)
    {
        Console.WriteLine("Remote host: ");
        string             host = Console.ReadLine();
        CConnectionContext cc   = new CConnectionContext(host, 20901, "root", "Smash123");

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

            CAsyncQueue sq = spAq.Seek();

            string s4 = "Sock";
            EnqueueToServer(sq, s4, 200000000);
            DequeueFromServer(sq);

            //Manually batching messages improves throughput for high volume of tiny messages
            EnqueueToServerBatch(sq, s4, 200000000, 8 * 1024);
            DequeueFromServer(sq);

            string s32 = "SocketPro is a world-leading pac";
            EnqueueToServer(sq, s32, 200000000);
            DequeueFromServer(sq);

            //Manually batching messages improves throughput for high volume of small messages
            EnqueueToServerBatch(sq, s32, 200000000, 8 * 1024);
            DequeueFromServer(sq);

            //a string having 200 chars
            string s = "SocketPro is a world-leading package of secured communication software components written with request batching, asynchrony and parallel computation in mind. It offers superior performance and scalabi";
            EnqueueToServer(sq, s, 50000000);
            DequeueFromServer(sq);

            //Batching messages improves throughput for high volume of middle size massages
            EnqueueToServerBatch(sq, s, 50000000, 8 * 1024);
            DequeueFromServer(sq);

            string s1024 = "";
            for (int n = 0; n < 6; ++n)
            {
                s1024 += s;
            }
            s1024 = s1024.Substring(0, 1024);
            EnqueueToServer(sq, s1024, 10000000);
            DequeueFromServer(sq);

            string s10240 = "";
            for (int n = 0; n < 10; ++n)
            {
                s10240 += s1024;
            }
            EnqueueToServer(sq, s10240, 1000000);
            DequeueFromServer(sq);

            Console.WriteLine("Press key ENTER to complete the application ......");
            Console.ReadLine();
        }
    }