Pipe.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "api.h"
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. int main(int argc, char** argv, char** envp) {
  5. char buffer1[20] = "Hello World 1", buffer2[20] = "Hello World 2";
  6. char buffer3[20], buffer4[20];
  7. int ret;
  8. PAL_HANDLE pipe1 = DkStreamOpen("pipe.srv:1", PAL_ACCESS_RDWR, 0, 0, 0);
  9. if (pipe1) {
  10. pal_printf("Pipe Creation 1 OK\n");
  11. // DEP 10/24/16: Try to read some attributes of the pipe
  12. PAL_STREAM_ATTR attr;
  13. if (!DkStreamAttributesQueryByHandle(pipe1, &attr)) {
  14. pal_printf("Failed to get any attributes from the pipesrv\n");
  15. return -1;
  16. } else
  17. pal_printf("Pipe Attribute Query 1 on pipesrv returned OK\n");
  18. // DEP: would be nice to sanity check the attributes.
  19. // Job for another day...
  20. PAL_HANDLE pipe2 = DkStreamOpen("pipe:1", PAL_ACCESS_RDWR, 0, 0, 0);
  21. if (pipe2) {
  22. // DEP 10/24/16: We should also be able to wait for a connection
  23. // on this handle
  24. // PAL_HANDLE pipe3 = DkObjectsWaitAny(1, &pipe1, 0);
  25. PAL_HANDLE pipe3 = DkStreamWaitForClient(pipe1);
  26. if (pipe3) {
  27. pal_printf("Pipe Connection 1 OK\n");
  28. ret = DkStreamWrite(pipe3, 0, 20, buffer1, NULL);
  29. if (ret > 0)
  30. pal_printf("Pipe Write 1 OK\n");
  31. ret = DkStreamRead(pipe2, 0, 20, buffer3, NULL, 0);
  32. if (ret > 0)
  33. pal_printf("Pipe Read 1: %s\n", buffer3);
  34. ret = DkStreamWrite(pipe2, 0, 20, buffer2, NULL);
  35. if (ret > 0)
  36. pal_printf("Pipe Write 2 OK\n");
  37. ret = DkStreamRead(pipe3, 0, 20, buffer4, NULL, 0);
  38. if (ret > 0)
  39. pal_printf("Pipe Read 2: %s\n", buffer4);
  40. }
  41. }
  42. }
  43. return 0;
  44. }