private void OnReceivedCommand(object?sender, ReceiveCommandEventArgs e) { string content = e.Command; int i = FindColonCharIndex(content); string command = (i == -1) ? content : content.Substring(0, i); string args = (i == -1) ? "" : content.Substring(i + 1); App.Current.Dispatcher.BeginInvoke(new Action( () => ReceivedCommand?.Invoke(this, new CommandReceivedEventArgs(command, args)) )); }
// This handles the client connection. It assumes that the client will send a command and // expect a reply. The reply will be a dictionary of serialisable objects. private void HandleConnection(object obj) { Socket workerSocket = obj as Socket; // Loop until we are told to stop, either by the ServerSide object // or by the client while (true) { // Wait until there is data on the socket while (workerSocket.Available == 0 && !mreStop.WaitOne(0, false)) { // This thread just handles this socket, so it does not matter if it blocks. Thread.Sleep(100); } // See if we should stop listening if (mreStop.WaitOne(0, false)) { break; // This thread will die } // Read the command passed by the client and convert it to a string. byte[] buffer = new byte[workerSocket.Available]; int count = workerSocket.Receive(buffer); string command = Encoding.ASCII.GetString(buffer, 0, count); if (command == "END") { // if the client sends an END command stop this thread. break; // This thread will die } // This object will be filled with results by the handler IList <KeyValuePair <string, string> > results = new List <KeyValuePair <string, string> >(); // Call the handlers, this will block ReceivedCommand.Invoke(command, results); // Serialize the results, so that they can be returne to the client. BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stm = new MemoryStream(); formatter.Serialize(stm, results); byte[] buf = stm.GetBuffer(); // Send the results back to the client, stm.Length is the number of bytes written // into the stream. workerSocket.Send(buf, (int)stm.Length, SocketFlags.None); // Loop, and wait for the client to send another command. } CleanUp(workerSocket); }
private void onCommandRecognized(CommandEventArgs e) { ReceivedCommand?.Invoke(this, e); }
public void Reset() { ReceivedCommand.Invoke(new ResetCommand()); }
public void Cancel() { ReceivedCommand.Invoke(new CancelCommand()); }
private void OnReceivedCommand(ReceivedCommandEventArgs args) { ReceivedCommand?.Invoke(this, args); }