static void TestSyncMessage(object state) { SingleConnectionCable client = (SingleConnectionCable)state; int count = SyncTestCount; Stopwatch sw = new Stopwatch(); Console.WriteLine("Test sync message"); sw.Start(); try { for (int i = 0; i < count; i++) { client.SyncSend(11, buf, 60000); } } catch (Exception e) { Console.WriteLine(e); } sw.Stop(); Console.WriteLine("Finished. Elapse : {0} ms", sw.ElapsedMilliseconds); }
static void TestASyncMessage(int count) { Console.Write("Please input serialize type:(0:none, 1:bin, 2:xml, 3:json, 4: simplebin, 5: struct, 6: customer)"); string strSerializeType = Console.ReadLine(); int serializeType = 0; int.TryParse(strSerializeType, out serializeType); ISerialize iSerializer = null; ISerialize <StructMessage> iStructMessageSerializer = null; ISerialize <TestMessage> iTestMessageSerializer = null; switch (serializeType) { case 0: strSerializeType = "none"; break; case 1: strSerializeType = "bin"; iSerializer = new BinSerializer(); break; case 2: strSerializeType = "xml"; iSerializer = new XMLSerializer(typeof(TestMessage)); break; case 3: strSerializeType = "json"; iSerializer = new JsonSerializer(typeof(TestMessage)); break; case 4: iSerializer = new SimpleBinSerializer(typeof(TestMessage)); strSerializeType = "simplebin"; break; case 5: iStructMessageSerializer = new StructSerializer <StructMessage>(); strSerializeType = "struct"; break; case 6: iTestMessageSerializer = new TestMessageSerializer(); 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++) { client.AsyncSend(10, buf); } } catch (Exception e) { Console.WriteLine(e); } sw.Stop(); Console.WriteLine("Finished. Elapse : {0} ms", sw.ElapsedMilliseconds); } else { TestMessage testMessage = new TestMessage() { Id = 1001, Name = "0123456789", Data = new byte[buf.Length] }; StructMessage structMessage = new StructMessage() { Id = 1001, Name = "0123456789", //Url = "http://www.google.com", //Site = "google.com", Data = new byte[4] }; for (int i = 0; i < testMessage.Data.Length; i++) { testMessage.Data[i] = (byte)i; } for (int i = 0; i < structMessage.Data.Length; i++) { structMessage.Data[i] = (byte)i; } sw.Start(); if (serializeType == 5) { try { for (int i = 0; i < count; i++) { client.AsyncSend <StructMessage>((UInt32)(20 + serializeType), ref structMessage, iStructMessageSerializer); } } catch (Exception e) { Console.WriteLine(e); } } else if (serializeType == 6) { try { for (int i = 0; i < count; i++) { client.AsyncSend <TestMessage>((UInt32)(20 + serializeType), ref testMessage, iTestMessageSerializer); } } catch (Exception e) { Console.WriteLine(e); } } else { try { for (int i = 0; i < count; i++) { client.AsyncSend((UInt32)(20 + serializeType), testMessage, iSerializer); } } 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(); } }
public static void Test(string[] args) { Console.WriteLine("Start to test SigleConnectionCable"); Console.Write("Please input package size:"); string strSize = Console.ReadLine(); Console.Write("Test Sync message? y /n :"); string testSyncMessage = Console.ReadLine().Trim().ToLower(); Console.Write("Please input server IP Address:"); _IPAddress = Console.ReadLine().Trim().ToLower(); if (_IPAddress == "") { _IPAddress = "127.0.0.1"; } int packageSize; if (!int.TryParse(strSize, out packageSize)) { packageSize = 64; } if (packageSize < 0) { packageSize = 0; } Console.WriteLine("IPAddress = {0}", _IPAddress); Console.WriteLine("Package size = {0}", packageSize); buf = new byte[packageSize]; int count = AsyncTestCount; //int count = 10000; for (int i = 0; i < buf.Length; i++) { buf[i] = (byte)i; } try { if (testSyncMessage == "y") { Console.Write("Please input test thread number:"); string strThreadNumber = Console.ReadLine(); int threadNumber; if (!int.TryParse(strThreadNumber, out threadNumber)) { threadNumber = 1; } Console.WriteLine("Actual test thread number = {0}", threadNumber); SingleConnectionCable client = new SingleConnectionCable(new IPEndPoint(IPAddress.Parse(_IPAddress), 2500), 1); client.ReceiveEventHandler += new EventHandler <ReceiveEventArgs>(ReceiveEventHandler); client.ErrorEventHandler += new EventHandler <ErrorEventArgs>(ErrorEventHandler); client.RemoteDisconnected += new EventHandler <DisconnectEventArgs>(DisconnectEventHandler); client.Connect(); for (int i = 0; i < threadNumber; i++) { System.Threading.Thread thread = new System.Threading.Thread(TestSyncMessage); thread.IsBackground = true; thread.Start(client); } } else { TestASyncMessage(count); } } catch (Exception e) { Console.WriteLine(e); } System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); }
public static void Run(string[] args) { Console.Write("Please input server IP Address [127.0.0.1]:"); string ipAddress = Console.ReadLine().Trim().ToLower(); if (ipAddress == "") { ipAddress = "127.0.0.1"; } try { //************** SingConnection Example ********************** Console.Write("Press any key to start single connection example"); Console.ReadKey(); Console.WriteLine(); //Create a SingleConnection instanace that will try to connect to host specified in //ipAddress and port (2500). SingleConnection client = new SingleConnection(new IPEndPoint(IPAddress.Parse(ipAddress), 2500)); client.ReceiveEventHandler += new EventHandler <ReceiveEventArgs>(ReceiveEventHandler); client.Connect(); Console.WriteLine("AsyncSend: Hello world! I am Single"); //Send an asynchronously message to server client.AsyncSend((UInt32)Event.OneWay, Encoding.UTF8.GetBytes("Hello world! I am Single")); int number = 0; try { Console.WriteLine("SyncSend {0}", number); //send a synchronously message to server //send a number with event: Event.Return to server and get the response from server //with the number increased. byte[] retData = client.SyncSend((UInt32)Event.Return, BitConverter.GetBytes(number)); number = BitConverter.ToInt32(retData, 0); Console.WriteLine("Get {0}", number); } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine("Waitting for 10 seconds to finish simple connection example."); System.Threading.Thread.Sleep(10000); client.Close(); //************* SingleConnectionCable Example ***************** Console.Write("Press any key to start single connection cable example"); Console.ReadKey(); Console.WriteLine(); //Create a SingleConnectionCable instance that will try to connect to host specified in //ipAddress and port (2500). //by default, SingleConnectionCable will try to connect automatically and including 6 tcp connections. SingleConnectionCable clientCable = new SingleConnectionCable(new IPEndPoint(IPAddress.Parse(ipAddress), 2500)); clientCable.ReceiveEventHandler += new EventHandler <ReceiveEventArgs>(ReceiveEventHandler); clientCable.Connect(); Console.WriteLine("AsyncSend: Hello world! I am Cable {0}", clientCable.CableId); //Send a one way message to server clientCable.AsyncSend((UInt32)Event.OneWay, Encoding.UTF8.GetBytes(string.Format("Hello world! I am Cable {0}", clientCable.CableId))); //Send object with bin serialization (By Default) Console.WriteLine("Bin serialization"); clientCable.AsyncSend((UInt32)Event.Bin, "Bin serialization"); while (true) { Console.WriteLine("SyncSend {0}", number); try { //send a number with event: Event.Return to server and get the response from server //with the number increased. byte[] retData = clientCable.SyncSend((UInt32)Event.Return, BitConverter.GetBytes(number)); number = BitConverter.ToInt32(retData, 0); Console.WriteLine("Get {0}", number); } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine("Quit when you press ESC. Else continue SyncSend."); //Quit when you press ESC if (Console.ReadKey().KeyChar == 0x1B) { clientCable.Close(); return; } } } catch (Exception e) { Console.WriteLine(e); Console.ReadLine(); } }
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(); } }