51 #define STEADY_TIME CLOCK_SECOND * 2
53 #define DEFAULT_SEND_INTERVAL CLOCK_SECOND / 2
64 #define VERSION_LT(a, b) ((signed char)((a) - (b)) < 0)
69 #define PRINTF(...) printf(__VA_ARGS__)
76 read_new_datapacket(
struct rudolph0_conn *c)
80 if(c->cb->read_chunk) {
81 len = c->cb->read_chunk(c, c->current.h.chunk * RUDOLPH0_DATASIZE,
82 c->current.data, RUDOLPH0_DATASIZE);
84 c->current.datalen = len;
86 PRINTF(
"read_new_datapacket len %d\n", len);
90 send_nack(
struct rudolph0_conn *c)
92 struct rudolph0_hdr *hdr;
97 hdr->type = TYPE_NACK;
98 hdr->version = c->current.h.version;
99 hdr->chunk = c->current.h.chunk;
101 PRINTF(
"Sending nack for %d:%d\n", hdr->version, hdr->chunk);
102 polite_send(&c->nackc, c->send_interval / 2,
sizeof(
struct rudolph0_hdr));
108 struct rudolph0_conn *c = (
struct rudolph0_conn *)stbroadcast;
110 if(c->current.datalen == RUDOLPH0_DATASIZE) {
111 c->current.h.chunk++;
112 PRINTF(
"Sending data chunk %d next time\n", c->current.h.chunk);
113 read_new_datapacket(c);
116 PRINTF(
"Steady: Sending the same data chunk next time datalen %d, %d\n",
117 c->current.datalen, RUDOLPH0_DATASIZE);
124 struct rudolph0_conn *c = (
struct rudolph0_conn *)stbroadcast;
127 if(p->h.type == TYPE_DATA) {
128 if(c->current.h.version != p->h.version) {
129 PRINTF(
"rudolph0 new version %d\n", p->h.version);
130 c->current.h.version = p->h.version;
131 c->current.h.chunk = 0;
132 c->cb->write_chunk(c, 0, RUDOLPH0_FLAG_NEWFILE, p->data, 0);
133 if(p->h.chunk != 0) {
136 c->cb->write_chunk(c, 0, RUDOLPH0_FLAG_NONE, p->data, p->datalen);
137 c->current.h.chunk++;
139 }
else if(p->h.version == c->current.h.version) {
140 if(p->h.chunk == c->current.h.chunk) {
141 PRINTF(
"received chunk %d\n", p->h.chunk);
142 if(p->datalen < RUDOLPH0_DATASIZE) {
143 c->cb->write_chunk(c, c->current.h.chunk * RUDOLPH0_DATASIZE,
144 RUDOLPH0_FLAG_LASTCHUNK, p->data, p->datalen);
146 c->cb->write_chunk(c, c->current.h.chunk * RUDOLPH0_DATASIZE,
147 RUDOLPH0_FLAG_NONE, p->data, p->datalen);
149 c->current.h.chunk++;
151 }
else if(p->h.chunk > c->current.h.chunk) {
152 PRINTF(
"received chunk %d > %d, sending NACK\n", p->h.chunk, c->current.h.chunk);
164 struct rudolph0_conn *c = (
struct rudolph0_conn *)
165 ((
char *)polite - offsetof(
struct rudolph0_conn,
169 if(p->h.type == TYPE_NACK && c->state == STATE_SENDER) {
170 if(p->h.version == c->current.h.version) {
171 PRINTF(
"Reseting chunk to %d\n", p->h.chunk);
172 c->current.h.chunk = p->h.chunk;
174 PRINTF(
"Wrong version, reseting chunk to 0\n");
175 c->current.h.chunk = 0;
177 read_new_datapacket(c);
183 static const struct stbroadcast_callbacks stbroadcast = { recv, sent };
186 rudolph0_open(
struct rudolph0_conn *c, uint16_t channel,
187 const struct rudolph0_callbacks *cb)
192 c->current.h.version = 0;
193 c->state = STATE_RECEIVER;
194 c->send_interval = DEFAULT_SEND_INTERVAL;
198 rudolph0_close(
struct rudolph0_conn *c)
200 stbroadcast_close(&c->c);
205 rudolph0_send(
struct rudolph0_conn *c, clock_time_t send_interval)
207 c->state = STATE_SENDER;
208 c->current.h.version++;
209 c->current.h.version++;
210 c->current.h.chunk = 0;
211 c->current.h.type = TYPE_DATA;
212 read_new_datapacket(c);
214 c->send_interval = send_interval;
219 rudolph0_force_restart(
struct rudolph0_conn *c)
221 c->current.h.chunk = 0;
226 rudolph0_stop(
struct rudolph0_conn *c)
232 rudolph0_version(
struct rudolph0_conn *c)
234 return c->current.h.version;
238 rudolph0_set_version(
struct rudolph0_conn *c,
int version)
240 c->current.h.version = version;