Exemplo n.º 1
0
        /// <summary>
        /// 发送soap消息 并返回结果
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public IDataContainer SendSoapMessage(SoapMessage msg)
        {
            IDataContainer result = DataContainer.CreateNullDataContainer();

            //序列化消息
            ISerialize <SoapMessage>   iSendMessageSerializer = null;
            ISerialize <DataContainer> iReturnDataSerializer  = new NTCPMessage.Serialize.JsonSerializer <DataContainer>();

            iSendMessageSerializer = new NTCPMessage.Serialize.JsonSerializer <SoapMessage>();


            //初始化tcp client
            SingleConnectionCable client = new SingleConnectionCable(new IPEndPoint(IPAddress.Parse(_IPAddress), _Port), 7);

            client.ReceiveEventHandler += new EventHandler <ReceiveEventArgs>(ReceiveEventHandler);
            client.ErrorEventHandler   += new EventHandler <ErrorEventArgs>(ErrorEventHandler);
            client.RemoteDisconnected  += new EventHandler <DisconnectEventArgs>(DisconnectEventHandler);

            try
            {
                client.Connect();

                result = client.SyncSend(
                    (UInt32)MessageType.Json,
                    msg,
                    20000,
                    iSendMessageSerializer);
                //sw.Stop();
                //Console.WriteLine("Finished. Elapse : {0} ms", sw.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                client.Close();
            }
            return(result);
        }
Exemplo n.º 2
0
        static void TestASyncMessage(int count)
        {
            Console.Write("Please input serialize type(Default 3:json)   : (3:json, 0:none)");
            string strSerializeType = Console.ReadLine();

            if (string.IsNullOrEmpty(strSerializeType))
            {
                strSerializeType = "3";
            }
            int serializeType = 0;

            int.TryParse(strSerializeType, out serializeType);

            ISerialize <SoapMessage>   iSendMessageSerializer = null;
            ISerialize <DataContainer> iReturnDataSerializer  = new NTCPMessage.Serialize.JsonSerializer <DataContainer>();

            switch (serializeType)
            {
            case 0:
                strSerializeType = "none";
                break;

            case 3:
                strSerializeType       = "json";
                iSendMessageSerializer = new NTCPMessage.Serialize.JsonSerializer <SoapMessage>();
                break;

            default:
                serializeType    = 0;
                strSerializeType = "none";
                break;
            }

            Console.WriteLine("Serialize type is {0}", strSerializeType);

            SingleConnectionCable client = new SingleConnectionCable(new IPEndPoint(IPAddress.Parse(_IPAddress), _Port), 7);

            client.ReceiveEventHandler += new EventHandler <ReceiveEventArgs>(ReceiveEventHandler);
            client.ErrorEventHandler   += new EventHandler <ErrorEventArgs>(ErrorEventHandler);
            client.RemoteDisconnected  += new EventHandler <DisconnectEventArgs>(DisconnectEventHandler);

            try
            {
                client.Connect();

                Stopwatch sw = new Stopwatch();

                Console.WriteLine("Test send message begin:");

                if (serializeType == 0)
                {
                    sw.Start();

                    //---------基本类型 字符串明文消息发送-----------
                    try
                    {
                        for (int i = 0; i < count; i++)
                        {
                            var buffer      = Encoding.UTF8.GetBytes("ping");
                            var resultBytes = client.SyncSend((UInt32)MessageType.None, buffer);

                            var str = Encoding.UTF8.GetString(resultBytes);

                            Console.WriteLine(str);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    sw.Stop();
                    Console.WriteLine("Finished. Elapse : {0} ms", sw.ElapsedMilliseconds);
                }
                else
                {
                    ///标准soap消息发送
                    //var paras = new NTCPMessage.EntityPackage.Arguments.ETaoFetchWebPageArgument
                    //{
                    //    KeyWord = "洗面奶女"
                    //};
                    var paras = new YouhuiquanFetchWebPageArgument
                    {
                        ArgumentsForExistsList = new List <YouhuiquanFetchWebPageArgument.QuanArgument>
                        {
                            new YouhuiquanFetchWebPageArgument.QuanArgument
                            {
                                SellerId = 748501705,
                                ItemId   = 540267461790
                            },
                            new YouhuiquanFetchWebPageArgument.QuanArgument
                            {
                                SellerId = 1690420968,
                                ItemId   = 38002640105
                            },
                        },
                        ArgumentsForQuanDetails = new YouhuiquanFetchWebPageArgument.QuanArgument
                        {
                            SellerId = 1690420968,
                            ItemId   = 38002640105
                        }
                    };
                    string      msg         = JsonConvert.SerializeObject(paras);
                    SoapMessage testMessage = new SoapMessage()
                    {
                        Head = CommandConstants.CMD_FetchquanDetails,//CommandConstants.CMD_FetchquanExistsList,
                        Body = msg
                    };



                    sw.Start();


                    try
                    {
                        //模拟并发

                        for (int i = 0; i < count; i++)
                        {
                            var repResult = client.SyncSend((UInt32)MessageType.Json,
                                                            testMessage, 1800000,
                                                            iSendMessageSerializer);


                            if (null != repResult)
                            {
                                // string content = LZString.DecompressFromBase64(repResult.Result);
                                //Console.Write(content);
                                Console.WriteLine("from server response :{0}", repResult.Result);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }


                    sw.Stop();
                    Console.WriteLine("Finished. Elapse : {0} ms", sw.ElapsedMilliseconds);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                client.Close();
            }
        }
        static void TestASyncMessage(int count)
        {
            Console.Write("Please input serialize type:(0:none, 1:bin, 2:xml, 3:json, 4: simplebin, 5: customer)");
            string strSerializeType = Console.ReadLine();
            int    serializeType    = 0;

            int.TryParse(strSerializeType, out serializeType);

            ISerialize <SoapMessage>   iSendMessageSerializer = null;
            ISerialize <DataContainer> iReturnDataSerializer  = new NTCPMessage.Serialize.JsonSerializer <DataContainer>();

            switch (serializeType)
            {
            case 0:
                strSerializeType = "none";
                break;

            case 1:
                strSerializeType       = "bin";
                iSendMessageSerializer = new BinSerializer <SoapMessage>();

                break;

            case 2:
                strSerializeType       = "xml";
                iSendMessageSerializer = new XMLSerializer <SoapMessage>();
                break;

            case 3:
                strSerializeType       = "json";
                iSendMessageSerializer = new NTCPMessage.Serialize.JsonSerializer <SoapMessage>();
                break;

            case 4:
                iSendMessageSerializer = new SimpleBinSerializer <SoapMessage>();
                strSerializeType       = "simplebin";
                break;

            case 5:
                iSendMessageSerializer = new SoapMessageSerializer();
                strSerializeType       = "customer";
                break;

            default:
                serializeType    = 0;
                strSerializeType = "none";
                break;
            }

            Console.WriteLine("Serialize type is {0}", strSerializeType);

            SingleConnectionCable client = new SingleConnectionCable(new IPEndPoint(IPAddress.Parse(_IPAddress), 2500), 7);

            client.ReceiveEventHandler += new EventHandler <ReceiveEventArgs>(ReceiveEventHandler);
            client.ErrorEventHandler   += new EventHandler <ErrorEventArgs>(ErrorEventHandler);
            client.RemoteDisconnected  += new EventHandler <DisconnectEventArgs>(DisconnectEventHandler);

            try
            {
                client.Connect();

                Stopwatch sw = new Stopwatch();

                Console.WriteLine("Test async message");

                if (serializeType == 0)
                {
                    sw.Start();

                    //---------基本类型 字符串明文消息发送-----------
                    try
                    {
                        for (int i = 0; i < count; i++)
                        {
                            var buffer      = Encoding.UTF8.GetBytes(DateTime.Now.ToString());
                            var resultBytes = client.SyncSend(10, buffer);

                            var str = Encoding.UTF8.GetString(resultBytes);

                            Console.WriteLine(str);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    sw.Stop();
                    Console.WriteLine("Finished. Elapse : {0} ms", sw.ElapsedMilliseconds);
                }
                else
                {
                    ///标准soap消息发送
                    var         obj         = new { spid = 1, ValueType = 9999 };
                    string      msg         = JsonConvert.SerializeObject(obj);
                    SoapMessage testMessage = new SoapMessage()
                    {
                        Head = "student",
                        Body = msg
                    };



                    sw.Start();


                    try
                    {
                        //模拟并发
                        Task[] tks = new Task[count];

                        for (int i = 0; i < count; i++)
                        {
                            //client.AsyncSend((UInt32)(20 + serializeType),
                            //    ref testMessage,
                            //    iSendMessageSerializer);

                            var t = Task <object> .Factory.StartNew(() =>
                            {
                                var repResult = client.SyncSend((UInt32)(20 + serializeType),
                                                                testMessage, 1000,
                                                                iSendMessageSerializer);
                                return(repResult);
                            });

                            tks[i] = t;



                            //if (null != repResult)
                            //{
                            //    Console.WriteLine("from server response :{0}", repResult.Status);
                            //}
                        }

                        Task.WaitAll(tks);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    //else
                    //{
                    //    try
                    //    {
                    //        for (int i = 0; i < count; i++)
                    //        {
                    //            //client.AsyncSend((UInt32)(20 + serializeType), testMessage, iSerializer);
                    //            var repResult = client.SyncSend((UInt32)(20 + serializeType),ref testMessage, 88888888, iSendMessageSerializer, iReturnDataSerializer);

                    //            if (null != repResult)
                    //            {
                    //                Console.WriteLine("from server response :{0}", repResult.Status);
                    //            }
                    //        }
                    //    }
                    //    catch (Exception e)
                    //    {
                    //        Console.WriteLine(e);
                    //    }
                    //}
                    sw.Stop();
                    Console.WriteLine("Finished. Elapse : {0} ms", sw.ElapsedMilliseconds);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                client.Close();
            }
        }