Pipe.c 1.8 KB

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