SendHandle.c 3.8 KB

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