Pipe.c 2.0 KB

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