JsonRpcServer.cs 952 B

12345678910111213141516171819202122232425262728293031
  1. using PlasticPipe.PlasticProtocol.Messages;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. namespace pkpy
  7. {
  8. public abstract class JsonRpcServer
  9. {
  10. protected abstract Task<string> dispatch(ThreadedVM vm, string request);
  11. public async Task attach(ThreadedVM vm)
  12. {
  13. while (vm.state <= ThreadState.running) await Task.Yield();
  14. switch (vm.state)
  15. {
  16. case ThreadState.suspended:
  17. string request = vm.read_jsonrpc_request();
  18. string response = await dispatch(vm, request);
  19. vm.write_jsonrpc_response(response);
  20. await attach(vm);
  21. break;
  22. case ThreadState.finished:
  23. break;
  24. default:
  25. throw new Exception("Unexpected state");
  26. }
  27. }
  28. }
  29. }