1 /*
2  * Copyright (c) 2017 Fastly, Kazuho Oku
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 #ifndef quicly_recvstate_h
23 #define quicly_recvstate_h
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #include <assert.h>
30 #include <stddef.h>
31 #include "picotls.h"
32 #include "quicly/ranges.h"
33 
34 typedef struct st_quicly_recvstate_t {
35     /**
36      * ranges that have been received (starts and remains non-empty until transfer completes)
37      */
38     quicly_ranges_t received;
39     /**
40      * starting offset of data
41      */
42     uint64_t data_off;
43     /**
44      * end_of_stream offset (or UINT64_MAX)
45      */
46     uint64_t eos;
47 } quicly_recvstate_t;
48 
49 void quicly_recvstate_init(quicly_recvstate_t *state);
50 void quicly_recvstate_init_closed(quicly_recvstate_t *state);
51 void quicly_recvstate_dispose(quicly_recvstate_t *state);
52 static int quicly_recvstate_transfer_complete(quicly_recvstate_t *state);
53 static size_t quicly_recvstate_bytes_available(quicly_recvstate_t *state);
54 /**
55  * Records that the range identified by (off, *len) has been received. When 0 (success) is returned, *len contains the number of
56  * bytes that might have been newly received and therefore need to be written to the receive buffer (this number of bytes counts
57  * backward from the end of given range).
58  */
59 int quicly_recvstate_update(quicly_recvstate_t *state, uint64_t off, size_t *len, int is_fin, size_t max_ranges);
60 int quicly_recvstate_reset(quicly_recvstate_t *state, uint64_t eos_at, uint64_t *bytes_missing);
61 
62 /* inline definitions */
63 
quicly_recvstate_transfer_complete(quicly_recvstate_t * state)64 inline int quicly_recvstate_transfer_complete(quicly_recvstate_t *state)
65 {
66     return state->received.num_ranges == 0;
67 }
68 
quicly_recvstate_bytes_available(quicly_recvstate_t * state)69 inline size_t quicly_recvstate_bytes_available(quicly_recvstate_t *state)
70 {
71     uint64_t total = quicly_recvstate_transfer_complete(state) ? state->eos : state->received.ranges[0].end;
72     assert(state->data_off <= total);
73     return total - state->data_off;
74 }
75 
76 #ifdef __cplusplus
77 }
78 #endif
79 
80 #endif
81