| |
1 /* |
| |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| |
3 * |
| |
4 * Copyright 2025 Olaf Wintermann. All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright |
| |
10 * notice, this list of conditions and the following disclaimer. |
| |
11 * |
| |
12 * 2. Redistributions in binary form must reproduce the above copyright |
| |
13 * notice, this list of conditions and the following disclaimer in the |
| |
14 * documentation and/or other materials provided with the distribution. |
| |
15 * |
| |
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
26 * POSSIBILITY OF SUCH DAMAGE. |
| |
27 */ |
| |
28 |
| |
29 #include <stdio.h> |
| |
30 #include <stdlib.h> |
| |
31 #include <unistd.h> |
| |
32 |
| |
33 #include "message.h" |
| |
34 |
| |
35 UiMessageHandler* uic_simple_msg_handler(int in, int out, msg_received_callback callback) { |
| |
36 UiSimpleMessageHandler *handler = malloc(sizeof(UiSimpleMessageHandler)); |
| |
37 handler->handler.start = uic_simple_msg_handler_start; |
| |
38 handler->handler.stop = uic_simple_msg_handler_stop; |
| |
39 handler->handler.send = uic_simple_msg_handler_send; |
| |
40 handler->handler.callback = callback; |
| |
41 handler->in = in; |
| |
42 handler->out = out; |
| |
43 handler->outbuf = cxBufferCreate(NULL, 4096, NULL, CX_BUFFER_FREE_CONTENTS | CX_BUFFER_AUTO_EXTEND); |
| |
44 handler->stop = 0; |
| |
45 pthread_mutex_init(&handler->queue_lock, NULL); |
| |
46 pthread_mutex_init(&handler->avlbl_lock, NULL); |
| |
47 pthread_cond_init(&handler->available, NULL); |
| |
48 return (UiMessageHandler*)handler; |
| |
49 } |
| |
50 |
| |
51 int uic_simple_msg_handler_start(UiMessageHandler *handler) { |
| |
52 UiSimpleMessageHandler *sh = (UiSimpleMessageHandler*)handler; |
| |
53 if(pthread_create(&sh->in_thread, NULL, uic_simple_msg_handler_in_thread, sh)) { |
| |
54 return 1; |
| |
55 } |
| |
56 if(pthread_create(&sh->out_thread, NULL, uic_simple_msg_handler_out_thread, sh)) { |
| |
57 return 1; |
| |
58 } |
| |
59 return 0; |
| |
60 } |
| |
61 |
| |
62 int uic_simple_msg_handler_stop(UiMessageHandler *handler) { |
| |
63 UiSimpleMessageHandler *sh = (UiSimpleMessageHandler*)handler; |
| |
64 pthread_mutex_lock(&sh->queue_lock); |
| |
65 sh->stop = 0; |
| |
66 pthread_cond_signal(&sh->available); |
| |
67 pthread_mutex_unlock(&sh->queue_lock); |
| |
68 close(sh->in); |
| |
69 sh->in = -1; |
| |
70 |
| |
71 pthread_join(sh->in_thread, NULL); |
| |
72 pthread_join(sh->out_thread, NULL); |
| |
73 |
| |
74 return 0; |
| |
75 } |
| |
76 |
| |
77 int uic_simple_msg_handler_send(UiMessageHandler *handler, cxstring msg) { |
| |
78 UiSimpleMessageHandler *sh = (UiSimpleMessageHandler*)handler; |
| |
79 pthread_mutex_lock(&sh->queue_lock); |
| |
80 cxBufferWrite(msg.ptr, 1, msg.length, sh->outbuf); |
| |
81 pthread_cond_signal(&sh->available); |
| |
82 pthread_mutex_unlock(&sh->queue_lock); |
| |
83 return 0; |
| |
84 } |
| |
85 |
| |
86 #define HEADERBUF_SIZE 64 |
| |
87 |
| |
88 void* uic_simple_msg_handler_in_thread(void *data) { |
| |
89 UiSimpleMessageHandler *handler = data; |
| |
90 |
| |
91 char *msg = NULL; |
| |
92 size_t msg_size = 0; |
| |
93 size_t msg_pos = 0; // currently received message length |
| |
94 |
| |
95 char headerbuf[HEADERBUF_SIZE]; |
| |
96 size_t headerpos = 0; |
| |
97 |
| |
98 char buf[2048]; |
| |
99 ssize_t r; |
| |
100 while((r = read(handler->in, buf, 2024)) > 0) { |
| |
101 char *buffer = buf; |
| |
102 size_t available = r; |
| |
103 |
| |
104 while(available > 0) { |
| |
105 if(msg) { |
| |
106 // read message |
| |
107 size_t need = msg_size - msg_pos; |
| |
108 size_t cplen = r > need ? need : available; |
| |
109 memcpy(msg+msg_pos, buffer, cplen); |
| |
110 buffer += cplen; |
| |
111 available -= cplen; |
| |
112 msg_pos += cplen; |
| |
113 if(msg_pos == msg_size) { |
| |
114 // message complete |
| |
115 //fprintf(stderr, "send: %.*s\n", (int)msg_size, msg); |
| |
116 if(handler->handler.callback) { |
| |
117 handler->handler.callback(cx_mutstrn(msg, msg_size)); |
| |
118 } |
| |
119 msg = NULL; |
| |
120 msg_size = 0; |
| |
121 msg_pos = 0; |
| |
122 } |
| |
123 } else { |
| |
124 size_t header_max = HEADERBUF_SIZE - headerpos - 1; |
| |
125 if(header_max > available) { |
| |
126 header_max = available; |
| |
127 } |
| |
128 // search for line break |
| |
129 int i; |
| |
130 int header_complete = 0; |
| |
131 for(i=0;i<header_max;i++) { |
| |
132 if(buffer[i] == '\n') { |
| |
133 header_complete = 1; |
| |
134 break; |
| |
135 } |
| |
136 } |
| |
137 i++; |
| |
138 memcpy(headerbuf+headerpos, buffer, i); |
| |
139 headerpos += i; |
| |
140 buffer += i; |
| |
141 available -= i; |
| |
142 |
| |
143 if(header_complete) { |
| |
144 headerbuf[headerpos-1] = 0; // terminate buffer |
| |
145 char *end; |
| |
146 long length = strtol(headerbuf, &end, 10); |
| |
147 if(*end == '\0') { |
| |
148 //fprintf(stderr, "header: %d\n", (int)length); |
| |
149 msg = malloc(length); |
| |
150 msg_size = length; |
| |
151 headerpos = 0; |
| |
152 } else { |
| |
153 fprintf(stderr, "Error: invalid message {%s}\n", headerbuf); |
| |
154 } |
| |
155 } else if(headerpos+1 >= HEADERBUF_SIZE) { |
| |
156 fprintf(stderr, "Error: message header too big\n"); |
| |
157 exit(-1); |
| |
158 } |
| |
159 } |
| |
160 } |
| |
161 |
| |
162 |
| |
163 } |
| |
164 perror("error"); |
| |
165 fprintf(stderr, "stop simple_msg_handler_in_thread\n"); |
| |
166 |
| |
167 return NULL; |
| |
168 } |
| |
169 |
| |
170 void* uic_simple_msg_handler_out_thread(void *data) { |
| |
171 UiSimpleMessageHandler *handler = data; |
| |
172 |
| |
173 return NULL; |
| |
174 } |