diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-09-18 20:43:47 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-09-18 20:43:47 +0000 |
commit | af6f9ae173e641a15639b59238bd5e86113a9113 (patch) | |
tree | 2b58c7c7a89cc2406a1efd44f7aa5c6b2eaa4e85 /src/util/mst.c | |
parent | c1ef5b83f7c31c7d6db19d1eb3fa0d1fcef2d595 (diff) | |
download | gnunet-af6f9ae173e641a15639b59238bd5e86113a9113.tar.gz gnunet-af6f9ae173e641a15639b59238bd5e86113a9113.zip |
more work on new MST and service logic
Diffstat (limited to 'src/util/mst.c')
-rw-r--r-- | src/util/mst.c | 67 |
1 files changed, 54 insertions, 13 deletions
diff --git a/src/util/mst.c b/src/util/mst.c index 578ba8e04..82a21b880 100644 --- a/src/util/mst.c +++ b/src/util/mst.c | |||
@@ -144,15 +144,20 @@ do_align: | |||
144 | { | 144 | { |
145 | /* need to align or need more space */ | 145 | /* need to align or need more space */ |
146 | mst->pos -= mst->off; | 146 | mst->pos -= mst->off; |
147 | memmove (ibuf, &ibuf[mst->off], mst->pos); | 147 | memmove (ibuf, |
148 | &ibuf[mst->off], | ||
149 | mst->pos); | ||
148 | mst->off = 0; | 150 | mst->off = 0; |
149 | } | 151 | } |
150 | if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) | 152 | if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) |
151 | { | 153 | { |
152 | delta = | 154 | delta |
153 | GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - | 155 | = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) |
154 | (mst->pos - mst->off), size); | 156 | - (mst->pos - mst->off), |
155 | GNUNET_memcpy (&ibuf[mst->pos], buf, delta); | 157 | size); |
158 | GNUNET_memcpy (&ibuf[mst->pos], | ||
159 | buf, | ||
160 | delta); | ||
156 | mst->pos += delta; | 161 | mst->pos += delta; |
157 | buf += delta; | 162 | buf += delta; |
158 | size -= delta; | 163 | size -= delta; |
@@ -178,23 +183,29 @@ do_align: | |||
178 | { | 183 | { |
179 | /* can get more space by moving */ | 184 | /* can get more space by moving */ |
180 | mst->pos -= mst->off; | 185 | mst->pos -= mst->off; |
181 | memmove (ibuf, &ibuf[mst->off], mst->pos); | 186 | memmove (ibuf, |
187 | &ibuf[mst->off], | ||
188 | mst->pos); | ||
182 | mst->off = 0; | 189 | mst->off = 0; |
183 | } | 190 | } |
184 | if (mst->curr_buf < want) | 191 | if (mst->curr_buf < want) |
185 | { | 192 | { |
186 | /* need to get more space by growing buffer */ | 193 | /* need to get more space by growing buffer */ |
187 | GNUNET_assert (0 == mst->off); | 194 | GNUNET_assert (0 == mst->off); |
188 | mst->hdr = GNUNET_realloc (mst->hdr, want); | 195 | mst->hdr = GNUNET_realloc (mst->hdr, |
196 | want); | ||
189 | ibuf = (char *) mst->hdr; | 197 | ibuf = (char *) mst->hdr; |
190 | mst->curr_buf = want; | 198 | mst->curr_buf = want; |
191 | } | 199 | } |
192 | hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; | 200 | hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; |
193 | if (mst->pos - mst->off < want) | 201 | if (mst->pos - mst->off < want) |
194 | { | 202 | { |
195 | delta = GNUNET_MIN (want - (mst->pos - mst->off), size); | 203 | delta = GNUNET_MIN (want - (mst->pos - mst->off), |
204 | size); | ||
196 | GNUNET_assert (mst->pos + delta <= mst->curr_buf); | 205 | GNUNET_assert (mst->pos + delta <= mst->curr_buf); |
197 | GNUNET_memcpy (&ibuf[mst->pos], buf, delta); | 206 | GNUNET_memcpy (&ibuf[mst->pos], |
207 | buf, | ||
208 | delta); | ||
198 | mst->pos += delta; | 209 | mst->pos += delta; |
199 | buf += delta; | 210 | buf += delta; |
200 | size -= delta; | 211 | size -= delta; |
@@ -278,12 +289,15 @@ copy: | |||
278 | { | 289 | { |
279 | if (size + mst->pos > mst->curr_buf) | 290 | if (size + mst->pos > mst->curr_buf) |
280 | { | 291 | { |
281 | mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos); | 292 | mst->hdr = GNUNET_realloc (mst->hdr, |
293 | size + mst->pos); | ||
282 | ibuf = (char *) mst->hdr; | 294 | ibuf = (char *) mst->hdr; |
283 | mst->curr_buf = size + mst->pos; | 295 | mst->curr_buf = size + mst->pos; |
284 | } | 296 | } |
285 | GNUNET_assert (size + mst->pos <= mst->curr_buf); | 297 | GNUNET_assert (size + mst->pos <= mst->curr_buf); |
286 | GNUNET_memcpy (&ibuf[mst->pos], buf, size); | 298 | GNUNET_memcpy (&ibuf[mst->pos], |
299 | buf, | ||
300 | size); | ||
287 | mst->pos += size; | 301 | mst->pos += size; |
288 | } | 302 | } |
289 | if (purge) | 303 | if (purge) |
@@ -318,8 +332,35 @@ GNUNET_MST_read (struct GNUNET_MessageStreamTokenizer *mst, | |||
318 | int purge, | 332 | int purge, |
319 | int one_shot) | 333 | int one_shot) |
320 | { | 334 | { |
321 | GNUNET_assert (0); // not implemented | 335 | ssize_t ret; |
322 | return GNUNET_SYSERR; | 336 | size_t left; |
337 | char *buf; | ||
338 | |||
339 | left = mst->curr_buf - mst->pos; | ||
340 | buf = (char *) mst->hdr; | ||
341 | ret = GNUNET_NETWORK_socket_recv (sock, | ||
342 | &buf[mst->pos], | ||
343 | left); | ||
344 | if (-1 == ret) | ||
345 | { | ||
346 | if ( (EAGAIN == errno) || | ||
347 | (EINTR == errno) ) | ||
348 | return GNUNET_OK; | ||
349 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, | ||
350 | "recv"); | ||
351 | return GNUNET_SYSERR; | ||
352 | } | ||
353 | if (0 == ret) | ||
354 | { | ||
355 | /* other side closed connection, treat as error */ | ||
356 | return GNUNET_SYSERR; | ||
357 | } | ||
358 | mst->pos += ret; | ||
359 | return GNUNET_MST_from_buffer (mst, | ||
360 | NULL, | ||
361 | 0, | ||
362 | purge, | ||
363 | one_shot); | ||
323 | } | 364 | } |
324 | 365 | ||
325 | 366 | ||