static void Main(string[] args) { var dataToSend = new byte[3000 * 3000]; bool usePipe = true; var url = new Uri("ws://localhost:8000/Tester"); WebsocketPipe.IWebsocketPipeDataSocket datasocket; if (usePipe) { datasocket = new WebsocketPipe.WebsocketPipeMemoryMappedFileDataSocket(); } else { datasocket = new WebsocketPipe.WebsocketPipeMSGInternalDataSocket(); } ClientTester = new WebsocketPipe.WebsocketPipe <byte[]>(url, datasocket); ClientTester.LogMethod = (s) => { Console.WriteLine(s); }; ClientTester.MessageRecived += ClientTester_MessageRecived; ClientTester.Connect(); System.Threading.Thread.Sleep(100); ClientTester.Send(dataToSend, (esp) => { }); Console.WriteLine("Press <enter> to exit."); Console.ReadLine(); ClientTester.Disconnect(); ClientTester.Dispose(); }
private static void TestServer_MessageRecived(object sender, WebsocketPipe.WebsocketPipe <byte[]> .MessageEventArgs e) { if (e.RequiresResponse) { e.Response = new byte[2]; } if (pingpong) { Console.WriteLine("Ping at server with " + e.Message.Length + " bytes"); TestServer.Send(e.Message); return; } totalRecivedCount++; }
static void Main(string[] args) { Console.WriteLine("Preparing data..."); int pixelNumberOfBytes = 4; int imgWidth = 10000; int numberOfSends = 100; int imgHeight = 1; bool usePipe = true; bool requestResponses = true; var url = new Uri("ws://localhost:8000/Tester"); WebsocketPipe.IWebsocketPipeDataSocket datasocket; if (usePipe) { datasocket = new WebsocketPipe.WebsocketPipeMemoryMappedFileDataSocket(); } else { datasocket = new WebsocketPipe.WebsocketPipeMSGInternalDataSocket(); } var dataToSend = new byte[imgWidth * imgHeight * pixelNumberOfBytes]; TestServer = new WebsocketPipe.WebsocketPipe <byte[]>(url, datasocket); TestServer.LogMethod = (s) => Console.WriteLine(s); Console.WriteLine("Creating server.."); TestServer.MessageRecived += TestServer_MessageRecived; TestServer.Listen(); if (true) { Console.WriteLine("Creating internal client and testing.."); InternalClient = new WebsocketPipe.WebsocketPipe <byte[]>(url, datasocket); InternalClient.MessageRecived += InternalClient_MessageRecived; InternalClient.Connect(); // Testing send from server. Console.WriteLine("Testing send from server (with response)."); TestServer.Send(new byte[120], (rsp) => { }); Console.WriteLine("Sending single message with response. (Will wait for it...)"); InternalClient.Send(dataToSend, (rsp) => { Console.WriteLine("Recived a response from the server with length " + rsp.Length); }); Stopwatch watch = new Stopwatch(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); // writing first to prepare the serializer. int numberOfSerializations = numberOfSends; ms.Seek(0, System.IO.SeekOrigin.Begin); InternalClient.Serializer.WriteTo(ms, dataToSend); ms.Seek(0, System.IO.SeekOrigin.Begin); InternalClient.Serializer.ReadFrom(ms); watch.Start(); for (int i = 0; i < numberOfSerializations; i++) { ms.Seek(0, System.IO.SeekOrigin.Begin); InternalClient.Serializer.WriteTo(ms, dataToSend); ms.Seek(0, System.IO.SeekOrigin.Begin); InternalClient.Serializer.ReadFrom(ms); } watch.Stop(); double serTime = watch.Elapsed.TotalMilliseconds / numberOfSerializations; Console.WriteLine("Serialization time: " + serTime); Action <byte[]> response = null; int numberOfValidResponses = 0; if (requestResponses) { response = (rsp) => { if (rsp != null) { numberOfValidResponses++; } } } ; watch.Reset(); watch.Start(); for (int i = 0; i < numberOfSends; i++) { InternalClient.Send(dataToSend, response); } int numberOfSleeps = 0; while (totalRecivedCount < numberOfSends) { numberOfSleeps++; System.Threading.Thread.Sleep(1); } watch.Stop(); Console.WriteLine("Has slept " + numberOfSleeps + " times."); Console.WriteLine("Valid resp object count: " + numberOfValidResponses); Console.WriteLine("Total transaction:" + watch.Elapsed.TotalMilliseconds); Console.WriteLine("Total transaction per call:" + watch.Elapsed.TotalMilliseconds / numberOfSends); Console.WriteLine("Total transaction pipe overhead:" + (watch.Elapsed.TotalMilliseconds / numberOfSends - serTime)); if (usePipe) { var mmfds = datasocket as WebsocketPipe.WebsocketPipeMemoryMappedFileDataSocket; Console.WriteLine("Total memory mapped file creations: " + mmfds.TotalNumberOfMemoryMappedFilesCreated); Console.WriteLine("Total active memory mapped file: " + mmfds.TotalActiveMemoryMappedFiles); } InternalClient.Disconnect(); } pingpong = true; Console.WriteLine("Listening to service at :" + TestServer.Address.ToString()); Console.WriteLine("Press <enter> to exit."); Console.ReadLine(); Console.Write("Stopping the server ... "); TestServer.StopListening(); TestServer.Dispose(); Console.WriteLine("OK."); }
static void Main(string[] args) { // starting the menu. bool continuteToNext = true; menu = new EasyConsole.Menu() .Add("Exit", () => { continuteToNext = false; }) .Add("Connect", () => { Com.Connect(); Console.WriteLine("Connected to remote."); }) .Add("Listen", () => { Com.Listen(); Console.WriteLine("Listening for connections."); }) .Add("Flip logging", () => { Com.DoLogging = !Com.DoLogging; Console.WriteLine("Logging is " + (Com.DoLogging ? "ON" : "OFF")); }) .Add("Send warning command to client.", () => { Console.WriteLine("Write something to log <enter for default log>:"); string theWarning = Console.ReadLine(); if (theWarning == null || theWarning.Length == 0) { theWarning = "[Somekinda default warning...]"; } NPMessage rsp = Com.Send(NPMessageType.Warning, theWarning, (NPMessageNamepathData)null); if (rsp == null) { return; } Console.WriteLine("Expose has responded with: " + rsp.Text == null ? "[UNKNOWN!]" : rsp.Text); }) .Add("Send simple set", () => { Console.WriteLine("Waht is the property to set (string)? [<enter> = 'TestString']"); string theprop = Console.ReadLine(); Console.WriteLine("Waht is the value to set (string)? [<enter> = 'Lama?']"); string thevalue = Console.ReadLine(); Com.Send(NPMessageType.Set, "", new NPMessageNamepathData( theprop == "" ? "TestString" : theprop, thevalue == "" ? "Lama?" : thevalue)); }) .Add("Send test matrix", () => { Console.WriteLine("Waht is the property to set (string)? [<enter> = 'TestMatrix']"); string theprop = Console.ReadLine(); Console.WriteLine("MatirixSize a*a, a= ? [<enter> = '1000']"); string thevalue = Console.ReadLine(); int a = int.TryParse(thevalue, out a) ? a : 1000; theprop = theprop == "" ? "TestMatrix" : theprop; Array mat = MakeLargeNumericMatrix <double>(a); Com.Send(NPMessageType.Set, "", new NPMessageNamepathData(theprop, mat), true); Console.WriteLine("Updating " + theprop + " with matrix of length " + mat.Length); }) .Add("Send repeating large matrix until stopped.", () => { Console.WriteLine("Waht is the property to set (string)? [<enter> = 'TestMatrix']"); string theprop = Console.ReadLine(); Console.WriteLine("MatirixSize a*a, a= ? [<enter> = '1000']"); string thevalue = Console.ReadLine(); int a = int.TryParse(thevalue, out a) ? a : 1000; theprop = theprop == "" ? "TestMatrix" : theprop; Console.WriteLine("Starting continues updates of " + theprop + " with matrix of length " + a * a); bool isRunning = true; Task.Run(() => { while (isRunning) { Array mat = MakeLargeNumericMatrix <double>(a); Com.Send(NPMessageType.Set, "", new NPMessageNamepathData(theprop, mat), true); Console.WriteLine("Send success."); } }); Console.WriteLine("Any key to stop."); Console.ReadKey(); isRunning = false; }) .Add("Send Bing and small", () => { Console.WriteLine("Sending big and small data to check data sync."); Console.WriteLine("Send big (no need for response)."); Com.Send(NPMessage.FromValue(new double[10000], NPMessageType.Set, "big")); Console.WriteLine("Send small (need for response)."); Com.Send(NPMessage.FromValue("", NPMessageType.Invoke, "small"), true); }) .Add("Send fast large matrix to update, (Fail on labview?)", () => { Console.WriteLine("Waht is the property to set (string)? [<enter> = 'TestMatrix']"); string theprop = Console.ReadLine(); Console.WriteLine("Delay between sends [ms] ? [<enter> = '100']"); string thevalue = Console.ReadLine(); Console.WriteLine("# of messages to send ? [<enter> = '100']"); string thecount = Console.ReadLine(); int dt = int.TryParse(thevalue, out dt) ? dt : 100; int n = int.TryParse(thecount, out n) ? n : 100; bool isRunning = true; int sentCount = 0; Task.Run(() => { while (isRunning) { System.Threading.Thread.Sleep(dt); Array mat = MakeLargeNumericMatrix <double>(1000); Com.Send(NPMessageType.Set, "", new NPMessageNamepathData(theprop, mat), false); sentCount++; if (sentCount > n) { Console.WriteLine("Completed send of " + sentCount + " messages"); isRunning = false; } } }); Console.WriteLine("Any key to stop."); Console.ReadKey(); isRunning = false; Console.WriteLine("Sent " + sentCount + " messages."); }) .Add("Flip set silent mode", () => { SilentSetMode = !SilentSetMode; SilentSetModeCount = 0; Console.WriteLine("Silent set moe: " + SilentSetMode); }) .Add("Test get response: ", () => { Console.WriteLine("Waht is the property to get? [<enter> = 'TestString']"); string theprop = Console.ReadLine(); if (theprop.Length == 0) { theprop = "TestString,TestMatrix"; } string[] props = theprop.Split(',').Select(p => p.Trim()).ToArray(); NPMessage rsp = Com.Get(props); // printing the information gathered. if (rsp == null || rsp.NamePaths == null) { Console.WriteLine("Property '" + theprop + "' not found."); } else { Console.WriteLine("Got Property '" + theprop + "':"); foreach (var npd in rsp.NamePaths) { Console.WriteLine("\t" + npd.Namepath + "(" + npd.Value.GetType() + "): " + npd.Value); } } }) .Add("Test multiple connection clients", () => { var clientA = new CSCom.CSCom(); var clientB = new CSCom.CSCom(); clientA.Connect(); Console.WriteLine("Connected client A."); clientB.Connect(); Console.WriteLine("Connected client B."); clientA.Send(NPMessage.FromValue(new byte[200]), true); Console.WriteLine("Send client a ok."); clientB.Send(NPMessage.FromValue(new byte[200]), true); Console.WriteLine("Send client b ok."); Console.WriteLine("Press any key to disconnect..."); Console.ReadKey(); clientA.Stop(); clientB.Stop(); }) .Add("Test websocket client server", () => { Console.WriteLine("Client server test"); WebsocketPipe.WebsocketPipeWS server = new WebsocketPipe.WebsocketPipeWS(new Uri("ws://localhost:50001/CSCom")); WebsocketPipe.WebsocketPipeWS client = new WebsocketPipe.WebsocketPipeWS(new Uri("ws://localhost:50001/CSCom")); int smsgIdx = 0, cmsgIdx = 0; Action <string, int, byte[]> pnt = (caller, idx, data) => { Console.WriteLine(caller + " recived msg (#" + idx + "), with " + (data == null ? 0 : data.Length) + " bytes"); }; server.MessageRecived += (s, e) => { pnt("server", smsgIdx, e.Data); smsgIdx++; }; client.MessageRecived += (s, e) => { pnt("client", cmsgIdx, e.Data); cmsgIdx++; }; server.Listen(); client.Connect(); byte[] bigdata = new byte[10000]; byte[] smalldata = new byte[1]; Console.WriteLine("Connected."); Console.WriteLine("Sending from client:"); int n = 5; for (int i = 0; i < n; i++) { client.Send(bigdata); client.Send(smalldata); } System.Threading.Thread.Sleep(1000); Console.WriteLine("Sending from server:"); for (int i = 0; i < n; i++) { server.Send(bigdata); server.Send(smalldata); } System.Threading.Thread.Sleep(1000); client.Stop(); server.Stop(); }) .Add("Test websocket pipe client server", () => { Console.WriteLine("Client server test"); WebsocketPipe.WebsocketPipe <byte[]> server = new WebsocketPipe.WebsocketPipe <byte[]>(new Uri("ws://localhost:50001/CSCom")); WebsocketPipe.WebsocketPipe <byte[]> client = new WebsocketPipe.WebsocketPipe <byte[]>(new Uri("ws://localhost:50001/CSCom")); server.Timeout = 1000; client.Timeout = 1000; int smsgIdx = 0, cmsgIdx = 0; object loglock = new object(); Action <string, int, byte[], int> pnt = (caller, idx, data, inblock) => { Console.WriteLine(caller + " recived msg (#" + idx + "), with " + (data == null ? 0 : data.Length) + " bytes, " + inblock + " in block"); }; server.MessageRecived += (s, e) => { lock (loglock) { pnt("server", smsgIdx, e.Message, e.NumberOfMessagesInBlock); smsgIdx++; } }; client.MessageRecived += (s, e) => { lock (loglock) { pnt("client", cmsgIdx, e.Message, e.NumberOfMessagesInBlock); cmsgIdx++; } }; server.Listen(); System.Threading.Thread.Sleep(10); client.Connect(); System.Threading.Thread.Sleep(10); byte[] bigdata = new byte[1000000]; byte[] smalldata = new byte[1]; Console.WriteLine("Connected."); Console.WriteLine("Sending from client:"); int n = 5; for (int i = 0; i < n; i++) { client.Send(bigdata); client.Send(smalldata); } Console.WriteLine("Completed sending, waiting for recive."); while (smsgIdx < n) { System.Threading.Thread.Sleep(10); } System.Threading.Thread.Sleep(10); Console.WriteLine("Sending from server:"); for (int i = 0; i < n; i++) { server.Send(bigdata); server.Send(smalldata); } Console.WriteLine("Completed sending, waiting for recive."); while (cmsgIdx < n) { System.Threading.Thread.Sleep(10); } System.Threading.Thread.Sleep(10); Console.WriteLine("Complete. Closing."); client.Stop(); server.Stop(); client.Dispose(); server.Dispose(); }) .Add("Test memory mapped file stack", () => { WebsocketPipe.MemoryMappedBinaryStack stack = new WebsocketPipe.MemoryMappedBinaryStack("Lama this stack"); List <byte[]> els = new List <byte[]>(); int n = 100; Random rn = new Random(); int totalSize = 0; Console.WriteLine("Writing arrays of sizes:"); for (int i = 0; i < n; i++) { byte[] ar = new byte[rn.Next(1000000)]; totalSize += ar.Length; els.Add(ar); Console.Write((i == 0 ? "" : ", ") + ar.Length); } Console.WriteLine(); Stopwatch watch = new Stopwatch(); watch.Start(); //stack.Lock(); foreach (byte[] ar in els) { stack.Push(ar); } //stack.UnLock(); watch.Stop(); Console.WriteLine(); Console.WriteLine("Pushed total of " + totalSize + " [bytes] in " + els.Count + " elements in [ms]: " + watch.Elapsed.TotalMilliseconds); Console.WriteLine("Reading..."); watch.Reset(); watch.Start(); //stack.Lock(); for (int i = 0; i < els.Count; i++) { byte[] read = stack.Pop(); byte[] shouldBe = els[els.Count - i - 1]; Console.WriteLine("read " + read.Length + " ?= " + shouldBe.Length + " " + (read.Length == shouldBe.Length ? "OK" : "ERROR")); } //stack.UnLock(); Console.WriteLine("Read total of " + totalSize + " [bytes] in " + els.Count + " elements in [ms]: " + watch.Elapsed.TotalMilliseconds); watch.Reset(); watch.Start(); stack.Push(els.ToArray()); watch.Stop(); Console.WriteLine("Multipush same array in [ms] " + watch.Elapsed.TotalMilliseconds); watch.Reset(); watch.Start(); byte[][] readEls = stack.Empty().Reverse().ToArray(); watch.Stop(); Console.WriteLine("Multipop (empty) same array in [ms] " + watch.Elapsed.TotalMilliseconds); int errored = 0; for (int i = 0; i < els.Count; i++) { byte[] read = readEls[i]; byte[] shouldBe = els[i]; if (read.Length != shouldBe.Length) { Console.WriteLine("read " + read.Length + " ?= " + shouldBe.Length + " -> ERROR"); errored++; } } Console.WriteLine(errored + " errors found."); stack.Dispose(); }) .Add("Test memory mapped file queue", () => { WebsocketPipe.MemoryMappedBinaryQueue queue = new WebsocketPipe.MemoryMappedBinaryQueue("Lama this stack"); List <byte[]> els = new List <byte[]>(); int n = 100; Random rn = new Random(); int totalSize = 0; Console.WriteLine("Writing arrays of sizes:"); for (int i = 0; i < n; i++) { byte[] ar = new byte[rn.Next(1000000)]; ar[0] = 1; totalSize += ar.Length; els.Add(ar); Console.Write((i == 0 ? "" : ", ") + ar.Length); } Console.WriteLine(); Stopwatch watch = new Stopwatch(); watch.Start(); //stack.Lock(); foreach (byte[] ar in els) { queue.Enqueue(ar); } //stack.UnLock(); watch.Stop(); Console.WriteLine(); Console.WriteLine("Pushed total of " + totalSize + " [bytes] in " + els.Count + " elements in [ms]: " + watch.Elapsed.TotalMilliseconds); Console.WriteLine("Reading..."); watch.Reset(); watch.Start(); //stack.Lock(); for (int i = 0; i < els.Count; i++) { byte[] read = queue.Dequeue(); byte[] shouldBe = els[i]; Console.WriteLine("read " + read.Length + " ?= " + shouldBe.Length + " " + (read.Length == shouldBe.Length ? "OK" : "ERROR")); } //stack.UnLock(); Console.WriteLine("Read total of " + totalSize + " [bytes] in " + els.Count + " elements in [ms]: " + watch.Elapsed.TotalMilliseconds); watch.Reset(); watch.Start(); queue.Enqueue(els.ToArray()); watch.Stop(); Console.WriteLine("Multipush same array in [ms] " + watch.Elapsed.TotalMilliseconds); watch.Reset(); watch.Start(); byte[][] readEls = queue.Empty().ToArray(); watch.Stop(); Console.WriteLine("Multipop (empty) same array in [ms] " + watch.Elapsed.TotalMilliseconds); int errored = 0; for (int i = 0; i < els.Count; i++) { byte[] read = readEls[i]; byte[] shouldBe = els[i]; if (read.Length != shouldBe.Length) { Console.WriteLine("read " + read.Length + " ?= " + shouldBe.Length + " -> ERROR"); errored++; } } Console.WriteLine(errored + " errors found."); queue.Dispose(); }) .Add("Open matlab file", () => { System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog(); ofd.Filter = "Matlab Files|*.m"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { CSCom.CSCom.OpenMatlabFile(ofd.FileName); } }) .Add("Test event dispatch", () => { var last = DateTime.Now; int delay = 1000; DelayedEventDispatch dispatch = new DelayedEventDispatch(); dispatch.Ready += (s, e) => { DateTime now = DateTime.Now; if ((now - last).TotalMilliseconds < delay) { Console.WriteLine("Dispatch error."); } else { Console.WriteLine("Dispatch"); } last = now; }; for (int i = 0; i < 10000; i++) { dispatch.Trigger(delay); if (i % 2 == 0) { System.Threading.Thread.Sleep(1); } } Console.WriteLine("ok"); }); Com.MessageRecived += On_MessageRecived; Com.Log += Client_Log; while (continuteToNext) { menu.Display(); if (!continuteToNext) { break; } //Console.WriteLine("Press q to exit or any other key to continue."); //continuteToNext = Console.ReadKey(true).Key != ConsoleKey.Q; } Console.WriteLine("Any key to exit..."); Console.ReadKey(); }