Pipe.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #define NTRIES 10
  6. int main (int argc, char ** argv)
  7. {
  8. unsigned long pipeid;
  9. char uri[40];
  10. DkRandomBitsRead(&pipeid, sizeof(unsigned long));
  11. pipeid = pipeid % 1024;
  12. pal_snprintf(uri, 40, "pipe.srv:%d", pipeid);
  13. PAL_HANDLE srv = DkStreamOpen(uri, 0, 0,
  14. PAL_CREAT_TRY|PAL_CREAT_ALWAYS, 0);
  15. if (!srv) {
  16. pal_printf("not able to create server (%s)\n", uri);
  17. return -1;
  18. }
  19. pal_snprintf(uri, 40, "pipe:%d", pipeid);
  20. PAL_HANDLE cli = DkStreamOpen(uri, PAL_ACCESS_RDWR, 0,
  21. PAL_CREAT_TRY|PAL_CREAT_ALWAYS, 0);
  22. if (!cli) {
  23. pal_printf("not able to create client\n");
  24. return -1;
  25. }
  26. DkStreamGetName(cli, uri, 40);
  27. pal_printf("pipe connect as %s\n", uri);
  28. PAL_HANDLE conn = DkStreamWaitForClient(srv);
  29. if (!cli) {
  30. pal_printf("not able to accept client\n");
  31. return -1;
  32. }
  33. DkStreamGetName(conn, uri, 40);
  34. pal_printf("pipe accepted as %s\n", uri);
  35. DkObjectClose(srv);
  36. int i;
  37. for (i = 0 ; i < NTRIES ; i++) {
  38. int bytes = DkStreamWrite(cli, 0, 12, "Hello World", NULL);
  39. if (!bytes) {
  40. pal_printf("not able to send to client\n");
  41. return -1;
  42. }
  43. }
  44. for (i = 0 ; i < NTRIES ; i++) {
  45. char buffer[12];
  46. int bytes = DkStreamRead(conn, 0, 12, buffer, NULL, 0);
  47. if (!bytes) {
  48. pal_printf("not able to receive from server\n");
  49. return -1;
  50. }
  51. pal_printf("read from server: %s\n", buffer);
  52. }
  53. DkObjectClose(cli);
  54. DkObjectClose(conn);
  55. return 0;
  56. }