1 #include <PortLink.h> 2 3 #include <stdlib.h> 4 #include <stdio.h> 5 #include <string.h> 6 7 8 const int32 kBufferSize = 2048; 9 10 11 void 12 get_next_message(BPrivate::PortLink &link, int32 expectedCode) 13 { 14 int32 code; 15 if (link.GetNextMessage(code) != B_OK) { 16 fprintf(stderr, "get message failed!\n"); 17 exit(-1); 18 } 19 if (code != expectedCode) { 20 fprintf(stderr, "code is wrong (%ld)!\n", code); 21 exit(-1); 22 } 23 } 24 25 26 int 27 main() 28 { 29 port_id port = create_port(100, "portlink"); 30 31 BPrivate::PortLink sender(port, -1); 32 BPrivate::PortLink receiver(-1, port); 33 34 sender.StartMessage('tst1'); 35 sender.Attach<int32>(42); 36 37 sender.StartMessage('tst2'); 38 sender.AttachString(NULL); 39 sender.AttachString(""); 40 sender.AttachString("Gurkensalat"); 41 42 sender.StartMessage('tst3', 100000); 43 sender.Attach(&port, 100000); 44 if (sender.EndMessage() == B_OK) { 45 fprintf(stderr, "attaching huge message succeeded!\n"); 46 return -1; 47 } 48 49 // force overlap 50 char test[kBufferSize + 2048]; 51 sender.StartMessage('tst4'); 52 sender.Attach(test, kBufferSize - 40); 53 54 // force buffer grow 55 sender.StartMessage('tst5'); 56 sender.Attach(test, sizeof(test)); 57 58 status_t status = sender.Flush(); 59 if (status != B_OK) { 60 fprintf(stderr, "flushing messages failed: %ld, %s!\n", 61 status, strerror(status)); 62 return -1; 63 } 64 65 get_next_message(receiver, 'tst1'); 66 67 int32 value; 68 if (receiver.Read<int32>(&value) != B_OK) { 69 fprintf(stderr, "reading message failed!\n"); 70 return -1; 71 } 72 73 if (value != 42) { 74 fprintf(stderr, "value is wrong: %ld!\n", value); 75 return -1; 76 } 77 78 get_next_message(receiver, 'tst2'); 79 80 for (int32 i = 0; i < 4; i++) { 81 char *string; 82 if (receiver.ReadString(&string) != B_OK) { 83 if (i == 3) 84 continue; 85 86 fprintf(stderr, "reading string failed!\n"); 87 return -1; 88 } else if (i == 3) { 89 fprintf(stderr, "reading string succeeded, but shouldn't!\n"); 90 return -1; 91 } 92 free(string); 93 } 94 95 get_next_message(receiver, 'tst4'); 96 get_next_message(receiver, 'tst5'); 97 98 int32 code; 99 status = receiver.GetNextMessage(code, 0); 100 if (status != B_WOULD_BLOCK) { 101 fprintf(stderr, "reading would not block!\n"); 102 return -1; 103 } 104 105 puts("All OK!"); 106 return 0; 107 } 108