SendHandle.c 3.6 KB

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