Pipe.c 1.9 KB

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