コード例 #1
0
        internal bool Verify(CClientSocket cs)
        {
            int    errCode;
            IUcert cert = cs.UCert;
            string res  = cert.Verify(out errCode);

            //do ssl server certificate authentication here
            if (errCode == 0)
            {
                return(true);
            }
            if (m_KeysAllowed != null)
            {
                return(m_KeysAllowed.IndexOf(ToStr(cert.PublicKey)) != -1);
            }
            return(false);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: skyformat99/socketpro
    static void Main(string[] args)
    {
        Console.WriteLine("Input your user id ......");
        CConnectionContext cc = new CConnectionContext("localhost", 20901, Console.ReadLine(), "MyPassword", tagEncryptionMethod.TLSv1);

        //CA file is located at the directory ..\SocketProRoot\bin
        CClientSocket.SSL.SetVerifyLocation("ca.cert.pem");

        //for windows platforms, you can also use windows system store instead
        //CClientSocket.SSL.SetVerifyLocation("my"); //or "root", "my@currentuser", "root@localmachine"

        using (CSocketPool <HelloWorld> spHw = new CSocketPool <HelloWorld>()) //true -- automatic reconnecting
        {
            spHw.DoSslServerAuthentication += (sender, cs) =>
            {
                int    errCode;
                IUcert cert = cs.UCert;
                Console.WriteLine(cert.SessionInfo);
                string res = cert.Verify(out errCode);
                //do ssl server certificate authentication here
                return(errCode == 0);  //true -- user id and password will be sent to server
            };

            //error handling ignored for code clarity
            bool       ok = spHw.StartSocketPool(cc, 1, 1);
            HelloWorld hw = spHw.Seek(); //or HelloWorld hw = spHw.Lock();

            CClientSocket ClientSocket = hw.AttachedClientSocket;
            ClientSocket.Push.OnSubscribe += (cs, messageSender, groups) =>
            {
                Console.WriteLine("Subscribe for " + ToString(groups));
                Console.WriteLine(ToString(messageSender));
                Console.WriteLine();
            };

            ClientSocket.Push.OnUnsubscribe += (cs, messageSender, groups) =>
            {
                Console.WriteLine("Unsubscribe from " + ToString(groups));
                Console.WriteLine(ToString(messageSender));
                Console.WriteLine();
            };

            ClientSocket.Push.OnPublish += (cs, messageSender, groups, msg) =>
            {
                Console.WriteLine("Publish to " + ToString(groups));
                Console.WriteLine(ToString(messageSender));
                Console.WriteLine("message = " + msg);
                Console.WriteLine();
            };

            ClientSocket.Push.OnSendUserMessage += (cs, messageSender, msg) =>
            {
                Console.WriteLine("SendUserMessage");
                Console.WriteLine(ToString(messageSender));
                Console.WriteLine("message = " + msg);
                Console.WriteLine();
            };

            //asynchronously process multiple requests with inline batching for best network efficiency
            ok = hw.SendRequest(hwConst.idSayHelloHelloWorld, "Jack", "Smith", (ar) =>
            {
                string ret;
                ar.Load(out ret);
                Console.WriteLine(ret);
            });

            uint[] chat_ids = { 1, 2 };
            ok = ClientSocket.Push.Publish("We are going to call the method Sleep", chat_ids);
            CAsyncServiceHandler.DAsyncResultHandler arh = null;
            ok = hw.SendRequest(hwConst.idSleepHelloWorld, (int)5000, arh);

            Console.WriteLine("Input a receiver for receiving my message ......");
            Console.WriteLine();
            ok = ClientSocket.Push.SendUserMessage("A message from " + cc.UserId, Console.ReadLine());
            ok = hw.WaitAll();

            Console.WriteLine("Press key ENTER to shutdown the demo application ......");
            Console.ReadLine();
        }
    }
コード例 #3
0
ファイル: Program.cs プロジェクト: warrior6/socketpro
    static void Main(string[] args)
    {
        Console.WriteLine("Remote middle tier host: ");
        string host = Console.ReadLine();

        Console.WriteLine("Sakila.payment filter: ");
        string             filter = Console.ReadLine();
        CConnectionContext cc     = new CConnectionContext(host, 20911, "SomeUserId", "A_Password_For_SomeUserId", tagEncryptionMethod.TLSv1);

#if WIN32_64
#else
        //CA file is located at the directory ../socketpro/bin
        bool ok = CClientSocket.SSL.SetVerifyLocation("ca.cert.pem");
#endif
        using (CMaster master = new CMaster("")) {
            master.DoSslServerAuthentication += (pool, cs) => {
                int    ret;
                IUcert cert = cs.UCert;
                string res  = cert.Verify(out ret);
                return(ret == 0);
            };
            ok = master.StartSocketPool(cc, 1, 1);
            if (!ok)
            {
                Console.WriteLine("Failed in connecting to remote middle tier server, and press any key to close the application ......");
                Console.ReadLine();
                return;
            }

            CDataSet         cache   = master.Cache; //accessing real-time update cache
            CWebAsyncHandler handler = master.Seek();
            ok = handler.GetMasterSlaveConnectedSessions((m, s) => {
                Console.WriteLine("master connections: {0}, slave connections: {1}", m, s);
            });
            ok = handler.QueryPaymentMaxMinAvgs(filter, (mma, res, errMsg) => {
                if (res != 0)
                {
                    Console.WriteLine("QueryPaymentMaxMinAvgs error code: {0}, error message: {1}", res, errMsg);
                }
                else
                {
                    Console.WriteLine("QueryPaymentMaxMinAvgs max: {0}, min: {1}, avg: {2}", mma.Max, mma.Min, mma.Avg);
                }
            });

            CDBVariantArray vData = new CDBVariantArray();
            vData.Add(1); //Google company id
            vData.Add("Ted Cruz");
            vData.Add(DateTime.Now);
            vData.Add(1); //Google company id
            vData.Add("Donald Trump");
            vData.Add(DateTime.Now);
            vData.Add(2); //Microsoft company id
            vData.Add("Hillary Clinton");
            vData.Add(DateTime.Now);

            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
            ok = handler.UploadEmployees(vData, (res, errMsg, vId) => {
                if (res != 0)
                {
                    Console.WriteLine("UploadEmployees Error code = {0}, , error message = {1}", res, errMsg);
                }
                else
                {
                    foreach (object id in vId)
                    {
                        Console.WriteLine("Last id: " + id);
                    }
                }
                tcs.SetResult(true);
            }, (h, canceled) => {
                Console.WriteLine("Socket closed or request cancelled");
                tcs.SetResult(true);
            });
            if (ok)
            {
                if (!tcs.Task.Wait(5000))
                {
                    Console.WriteLine("The above requests are not completed in 5 seconds");
                }
            }
            else
            {
                Console.WriteLine("Socket already closed before sending request");
            }

            Console.WriteLine("Press ENTER key to test requests parallel processing and fault tolerance at server side ......");
            Console.ReadLine();
            ss.CMaxMinAvg sum_mma  = new ss.CMaxMinAvg();
            DateTime      start    = DateTime.Now;
            uint          returned = 0;
            for (uint n = 0; n < 10000; ++n)
            {
                ok = handler.QueryPaymentMaxMinAvgs(filter, (mma, res, errMsg) => {
                    if (res != 0)
                    {
                        Console.WriteLine("QueryPaymentMaxMinAvgs error code: {0}, error message: {1}", res, errMsg);
                    }
                    else
                    {
                        sum_mma.Avg += mma.Avg;
                        sum_mma.Max += mma.Max;
                        sum_mma.Min += mma.Min;
                    }
                    ++returned;
                });
            }
            ok = handler.WaitAll();
            Console.WriteLine("Time required: {0} seconds for {1} requests", (DateTime.Now - start).TotalSeconds, returned);
            Console.WriteLine("QueryPaymentMaxMinAvgs sum_max: {0}, sum_min: {1}, sum_avg: {2}", sum_mma.Max, sum_mma.Min, sum_mma.Avg);

            Console.WriteLine("Press ENTER key to test requests server parallel processing, fault tolerance and sequence returning ......");
            Console.ReadLine();
            long prev_rental_id = 0;
            CWebAsyncHandler.DRentalDateTimes rdt = (dates, res, errMsg) => {
                if (res != 0)
                {
                    Console.WriteLine("GetRentalDateTimes call error code: {0}, error message: {1}", res, errMsg);
                    prev_rental_id = 0;
                }
                else if (dates.rental_id == 0)
                {
                    Console.WriteLine("GetRentalDateTimes call rental_id={0} not available", dates.rental_id);
                    prev_rental_id = 0;
                }
                else
                {
                    if (0 == prev_rental_id || dates.rental_id == prev_rental_id + 1)
                    {
                        Console.WriteLine("GetRentalDateTimes call rental_id={0} and dates ({1}, {2}, {3})", dates.rental_id, dates.Rental, dates.Return, dates.LastUpdate);
                    }
                    else
                    {
                        Console.WriteLine("****** GetRentalDateTimes returned out of order ******");
                    }
                    prev_rental_id = dates.rental_id;
                }
            };
            //all requests should be returned in sequence (max rental_id = 16049)
            for (int n = 0; n < 1000; ++n)
            {
                ok = handler.GetRentalDateTimes(n + 1, rdt);
            }
            handler.WaitAll();
            Console.WriteLine("Press a key to shutdown the demo application ......");
            Console.ReadLine();
        }
    }
コード例 #4
0
ファイル: Program.cs プロジェクト: udaparts/socketpro
    static void Main(string[] args)
    {
        Console.WriteLine("Remote middle tier host: ");
        string host = Console.ReadLine();

        Console.WriteLine("Sakila.payment filter: ");
        //for example: payment_id between 1 and 49
        string             filter = Console.ReadLine();
        CConnectionContext cc     = new CConnectionContext(host, 20911, "SomeUserId", "A_Password_For_SomeUserId", tagEncryptionMethod.TLSv1);

#if WIN32_64
#else
        //CA file is located at the directory ../socketpro/bin
        bool ok = CClientSocket.SSL.SetVerifyLocation("ca.cert.pem");
#endif
        using (CMaster master = new CMaster(""))
        {
            master.DoSslServerAuthentication += (pool, cs) =>
            {
                int    ret;
                IUcert cert = cs.UCert;
                string res  = cert.Verify(out ret);
                return(ret == 0);
            };
            //master.QueueName = "mcqueue";
            ok = master.StartSocketPool(cc, 1);
            if (!ok)
            {
                Console.WriteLine("No connection to remote middle tier server, and press a key to kill the demo ......");
                Console.ReadLine();
                return;
            }
            //accessing real-time update cache
            CDataSet         cache   = master.Cache;
            CWebAsyncHandler handler = master.Seek();
            CDBVariantArray  vData   = new CDBVariantArray();
            vData.Add(1); //Google company id
            vData.Add("Ted Cruz");
            vData.Add(DateTime.Now);
            vData.Add(1); //Google company id
            vData.Add("Donald Trump");
            vData.Add(DateTime.Now);
            vData.Add(2); //Microsoft company id
            vData.Add("Hillary Clinton");
            vData.Add(DateTime.Now);

            try
            {
                var tms  = handler.sendRequest(Consts.idGetMasterSlaveConnectedSessions);
                var tmma = handler.sendRequest(Consts.idQueryMaxMinAvgs, filter);
                var tue  = handler.sendRequest(Consts.idUploadEmployees, vData);
                var sb   = tms.Result;
                Console.WriteLine("master connections: {0}, slave connections: {1}", sb.Load <uint>(), sb.Load <uint>());
                sb = tmma.Result;
                Console.WriteLine("QueryPaymentMaxMinAvgs");
                int    res    = sb.Load <int>();
                string errMsg = sb.Load <string>();
                if (res != 0)
                {
                    Console.WriteLine("\terror code: {0}, error message: {1}", res, errMsg);
                }
                else
                {
                    CMaxMinAvg mma = sb.Load <CMaxMinAvg>();
                    Console.WriteLine("\tmax: {0}, min: {1}, avg: {2}", mma.Max, mma.Min, mma.Avg);
                }
                if (tue.Wait(5000))
                {
                    sb     = tue.Result;
                    res    = sb.Load <int>();
                    errMsg = sb.Load <string>();
                    Console.WriteLine("UploadEmployees");
                    if (res != 0)
                    {
                        Console.WriteLine("\tError code: {0}, message: {1}", res, errMsg);
                    }
                    else
                    {
                        var vId = sb.Load <CInt64Array>();
                        foreach (object id in vId)
                        {
                            Console.WriteLine("\tLast id: " + id);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("The request UploadEmployees not completed in 5 seconds");
                }
                Console.WriteLine("Press ENTER key to test requests server parallel processing ......");
                Console.ReadLine();
                CMaxMinAvg sum_mma = new CMaxMinAvg();
                Queue <Task <CScopeUQueue> > qT = new Queue <Task <CScopeUQueue> >();
                DateTime start = DateTime.Now;
                for (uint n = 0; n < 10000; ++n)
                {
                    qT.Enqueue(handler.sendRequest(Consts.idQueryMaxMinAvgs, filter));
                }

                int count = qT.Count;
                Console.WriteLine("QueryPaymentMaxMinAvgs");
                while (qT.Count > 0)
                {
                    sb     = qT.Dequeue().Result;
                    res    = sb.Load <int>();
                    errMsg = sb.Load <string>();
                    if (res != 0)
                    {
                        Console.WriteLine("error code: {0}, message: {1}", res, errMsg);
                    }
                    else
                    {
                        CMaxMinAvg mma = sb.Load <CMaxMinAvg>();
                        sum_mma.Avg += mma.Avg;
                        sum_mma.Max += mma.Max;
                        sum_mma.Min += mma.Min;
                    }
                    sb.Dispose();
                }
                Console.WriteLine("\tTime required: {0} seconds for {1} requests", (DateTime.Now - start).TotalSeconds, count);
                Console.WriteLine("\tsum_max: {0}, sum_min: {1}, sum_avg: {2}", sum_mma.Max, sum_mma.Min, sum_mma.Avg);

                Console.WriteLine("Press ENTER key to test server parallel processing and sequence returning ......");
                Console.ReadLine();
                for (long n = 0; n < 16000; ++n)
                {
                    qT.Enqueue(handler.sendRequest(Consts.idGetRentalDateTimes, n + 1));
                }
                long prev_rental_id = 0;
                Console.WriteLine("GetRentalDateTimes:");
                while (qT.Count > 0)
                {
                    sb = qT.Dequeue().Result;
                    CRentalDateTimes dates = sb.Load <CRentalDateTimes>();
                    res    = sb.Load <int>();
                    errMsg = sb.Load <string>();
                    if (res != 0)
                    {
                        Console.WriteLine("\terror code: {0}, message: {1}", res, errMsg);
                    }
                    else if (dates.LastUpdate.Ticks == 0 && dates.Rental.Ticks == 0 && dates.Return.Ticks == 0)
                    {
                        Console.WriteLine("\trental_id: {0} not available", dates.rental_id);
                    }
                    else
                    {
                        if (0 == prev_rental_id || dates.rental_id == prev_rental_id + 1)
                        {
                            //Console.WriteLine("rental_id={0} and dates ({1}, {2}, {3})", dates.rental_id, dates.Rental, dates.Return, dates.LastUpdate);
                        }
                        else
                        {
                            Console.WriteLine("\t****** returned out of order ******");
                        }
                    }
                    prev_rental_id = dates.rental_id;
                    sb.Dispose();
                }
            }
            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)
            {
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine("Press a key to kill the demo ......");
            Console.ReadLine();
        }
    }