Pipe.c 2.0 KB

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