Pipe.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "api.h"
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. #define NTRIES 10
  5. int main(int argc, char** argv) {
  6. unsigned long pipeid;
  7. char uri[40];
  8. int ret = DkRandomBitsRead(&pipeid, sizeof(unsigned long));
  9. if (ret < 0) {
  10. pal_printf("DkRandomBitsRead() failed\n");
  11. return -1;
  12. }
  13. pipeid = pipeid % 1024;
  14. snprintf(uri, 40, "pipe.srv:%ld", pipeid);
  15. PAL_HANDLE srv = DkStreamOpen(uri, 0, 0, PAL_CREATE_TRY | PAL_CREATE_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:%ld", pipeid);
  21. PAL_HANDLE cli = DkStreamOpen(uri, PAL_ACCESS_RDWR, 0, PAL_CREATE_TRY | PAL_CREATE_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. }