SendHandle.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /* This Hello World demostrate a simple multithread program */
  4. #include "pal.h"
  5. #include "pal_debug.h"
  6. #include "api.h"
  7. int main (int argc, char ** argv)
  8. {
  9. PAL_HANDLE handles[3];
  10. if (argc == 2 && !memcmp(argv[1], "Child", 6)) {
  11. for (int i = 0 ; i < 3 ; i++) {
  12. handles[i] = DkReceiveHandle(pal_control.parent_process);
  13. if (handles[i])
  14. pal_printf("Receive Handle OK\n");
  15. }
  16. char buffer[20];
  17. for (int i = 0 ; i < 3 ; i++) {
  18. if (!handles[i])
  19. continue;
  20. memset(buffer, 0, 20);
  21. switch(PAL_GET_TYPE(handles[i])) {
  22. case pal_type_pipesrv: {
  23. PAL_HANDLE pipe = DkStreamWaitForClient(handles[i]);
  24. if (pipe) {
  25. if (DkStreamRead(pipe, 0, 20, buffer, NULL, 0))
  26. pal_printf("Receive Pipe Handle: %s\n", buffer);
  27. DkObjectClose(pipe);
  28. }
  29. break;
  30. }
  31. case pal_type_udpsrv: {
  32. char uri[20];
  33. if ((DkStreamRead(handles[i], 0, 20, buffer, &uri, 20)))
  34. pal_printf("Receive Socket Handle: %s\n", buffer);
  35. break;
  36. }
  37. case pal_type_file:
  38. if (DkStreamRead(handles[i], 0, 20, buffer, NULL, 0))
  39. pal_printf("Receive File Handle: %s\n", buffer);
  40. break;
  41. default:
  42. break;
  43. }
  44. DkObjectClose(handles[i]);
  45. }
  46. } else {
  47. const char *args[3] = { "SendHandle", "Child", NULL };
  48. PAL_HANDLE child = DkProcessCreate("file:SendHandle", 0, args);
  49. if (child) {
  50. // Sending pipe handle
  51. handles[0] = DkStreamOpen("pipe.srv:1", PAL_ACCESS_RDWR,
  52. 0, PAL_CREAT_TRY, 0);
  53. if (handles[0]) {
  54. pal_printf("Send Handle OK\n");
  55. if (DkSendHandle(child, handles[0])) {
  56. DkObjectClose(handles[0]);
  57. PAL_HANDLE pipe = DkStreamOpen("pipe:1", PAL_ACCESS_RDWR,
  58. 0, 0, 0);
  59. if (pipe) {
  60. DkStreamWrite(pipe, 0, 20, "Hello World", NULL);
  61. DkObjectClose(pipe);
  62. }
  63. } else {
  64. DkObjectClose(handles[0]);
  65. }
  66. }
  67. // Sending udp handle
  68. handles[1] = DkStreamOpen("udp.srv:127.0.0.1:8000", PAL_ACCESS_RDWR,
  69. 0, PAL_CREAT_TRY, 0);
  70. if (handles[1]) {
  71. pal_printf("Send Handle OK\n");
  72. if (DkSendHandle(child, handles[1])) {
  73. DkObjectClose(handles[1]);
  74. PAL_HANDLE socket = DkStreamOpen("udp:127.0.0.1:8000",
  75. PAL_ACCESS_RDWR, 0, 0, 0);
  76. if (socket) {
  77. DkStreamWrite(socket, 0, 20, "Hello World", NULL);
  78. DkObjectClose(socket);
  79. }
  80. } else {
  81. DkObjectClose(handles[1]);
  82. }
  83. }
  84. handles[2] = DkStreamOpen("file:to_send.tmp", PAL_ACCESS_RDWR,
  85. 0600, PAL_CREAT_TRY, 0);
  86. if (handles[2]) {
  87. pal_printf("Send Handle OK\n");
  88. DkStreamWrite(handles[2], 0, 20, "Hello World", NULL);
  89. DkStreamSetLength(handles[2], 4096);
  90. DkSendHandle(child, handles[2]);
  91. DkObjectClose(handles[2]);
  92. }
  93. }
  94. DkObjectClose(child);
  95. }
  96. return 0;
  97. }