public Action defaultServiceAction() { Action serviceAction = () => { if (Util.verbose) { Console.Write("\n starting Receiver.defaultServiceAction"); } Message msg = null; while (true) { msg = rcvr.getMessage(); // note use of non-service method to deQ messages // Console.Write("\n Received message:"); Console.Write("\n sender is {0}", msg.fromUrl); if (msg.messageID > 0) { HiResTimer timer = latencyTimers.ElementAt(msg.messageID); timer.Stop(); latencyValues[msg.messageID] = timer.ElapsedMicroseconds; Console.Write("\n Request ID: {0}\n", msg.messageID); Console.Write("\n content is {0}\n", msg.content); Console.Write("\n Latency is {0} microseconds\n", latencyValues.ElementAt(msg.messageID)); } rcvCount++; //serverProcessMessage(msg); if (msg.content == "closeReceiver") { break; } } }; return(serviceAction); }
//----< Entry point for Server >----------------------------------------- static void Main(string[] args) { Util.verbose = false; Server srvr = new Server(); srvr.ProcessCommandLine(args); Console.Title = "Server"; Console.Write(String.Format("\n Starting CommService server listening on port {0}", srvr.port)); Console.Write("\n ====================================================\n"); Sender sndr = new Sender(Util.makeUrl(srvr.address, srvr.port)); Receiver rcvr = new Receiver(srvr.port, srvr.address); srvr.CalculatePerf(); // - serviceAction defines what the server does with received messages // - This serviceAction just announces incoming messages Action serviceAction = () => { Message msg = null; while (true) { msg = rcvr.getMessage(); // note use of non-service method to deQ messages Console.Write("\n Received message:"); Console.Write("\n sender is {0}", msg.fromUrl); Console.Write("\n Request ID: {0}\n", msg.messageID); Console.Write("\n content is {0}\n", msg.content); if (msg.content == "connection start message") { Console.Write("\n client connected\n"); continue; } else if (msg.content == "done") { Console.Write("\n client has finished\n"); continue; } else if (msg.content == "closeServer") { Console.Write("received closeServer"); break; } else if (msg.type == 0) { sndr.sendMessage(srvr.ProcessQuery(msg)); } else if (msg.type == 1) { sndr.sendMessage(srvr.ProcessAddKey(msg)); } else if (msg.type == 2) { sndr.sendMessage(srvr.ProcessRmvKey(msg)); } else if (msg.type == 3) { sndr.sendMessage(srvr.ProcessGetValue(msg)); } else if (msg.type == 4) { sndr.sendMessage(srvr.ProcessEditKey(msg)); } else if (msg.type == 5) { sndr.sendMessage(srvr.ProcessDBDisplay(msg)); } else if (msg.type == 6) { sndr.sendMessage(srvr.ProcessPersist(msg)); } else if (msg.type == 7) { sndr.sendMessage(srvr.ProcessRestore(msg)); } else if (msg.type == 8) { sndr.sendMessage(srvr.ProcessTypeReq(msg)); } msg.content = "received " + msg.content + " from " + msg.fromUrl; Util.swapUrls(ref msg); } }; if (rcvr.StartService()) { rcvr.doService(serviceAction); // This serviceAction is asynchronous so the call doesn't block. } Util.waitForUser(); }
//----< get Receiver and Sender running >---------------------------- void setupChannel() { rcvr = new Receiver(localPort, localAddress); Action serviceAction = () => { try { Message rmsg = null; while (true) { rmsg = rcvr.getMessage(); //Action act = () => { postRcvMsg(rmsg.content); }; Action act = () => { processRcvMsg(rmsg); }; Dispatcher.Invoke(act, System.Windows.Threading.DispatcherPriority.Background); } } catch(Exception ex) { Action act = () => { lStat.Text = ex.Message; }; Dispatcher.Invoke(act); } }; if (rcvr.StartService()) { rcvr.doService(serviceAction); } sndr = new wpfSender(lStat, this.Dispatcher); }