byte[] ISerialize.GetBytes(object obj) { if (obj == null) { return(null); } var instance = obj as SoapMessage; MemoryStream ms = new MemoryStream(); SimpleBinSerializer.Write(ms, instance.Id); SimpleBinSerializer.Write(ms, instance.Head); SimpleBinSerializer.Write(ms, instance.Body); return(ms.ToArray()); }
public byte[] GetBytes(ref TestMessage obj) { if (obj == null) { return(null); } MemoryStream ms = new MemoryStream(); SimpleBinSerializer.Write(ms, obj.Id); SimpleBinSerializer.Write(ms, obj.Name); SimpleBinSerializer.Write(ms, obj.Data); return(ms.ToArray()); }
object ISerialize.GetObject(byte[] data) { if (data == null) { return(null); } SoapMessage msg = new SoapMessage(); MemoryStream ms = new MemoryStream(data); msg.Id = SimpleBinSerializer.ToString(ms); msg.Head = SimpleBinSerializer.ToString(ms); msg.Body = SimpleBinSerializer.ToString(ms); return(msg); }
public TestMessage GetObject(byte[] data) { if (data == null) { return(null); } TestMessage testMessage = new TestMessage(); MemoryStream ms = new MemoryStream(data); testMessage.Id = SimpleBinSerializer.ToInt32(ms); testMessage.Name = SimpleBinSerializer.ToString(ms); testMessage.Data = SimpleBinSerializer.ToData(ms); return(testMessage); }
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(); } }
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(); } }