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 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(); } }