Compare commits

...

2 Commits

Author SHA1 Message Date
Paul Sokolovsky 0db59023ba extmod/modwebrepl: Make GET_FILE operation non-blocking.
In the sense that while GET_FILE transfers its data, REPL still works.
This is done by requiring client to send 1-byte block before WebREPL
server transfers next block of data.
2016-08-02 00:17:46 +03:00
Paul Sokolovsky d45f91e8a4 extmod/modwebrepl: Factor out "GET" iteration to write_file_chunk(). 2016-08-02 00:17:46 +03:00
1 changed files with 33 additions and 26 deletions

View File

@ -111,6 +111,22 @@ STATIC mp_obj_t webrepl_make_new(const mp_obj_type_t *type, size_t n_args, size_
return o;
}
STATIC int write_file_chunk(mp_obj_webrepl_t *self) {
const mp_stream_p_t *file_stream =
mp_get_stream_raise(self->cur_file, MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
byte readbuf[2 + 256];
int err;
mp_uint_t out_sz = file_stream->read(self->cur_file, readbuf + 2, sizeof(readbuf) - 2, &err);
if (out_sz == MP_STREAM_ERROR) {
return out_sz;
}
readbuf[0] = out_sz;
readbuf[1] = out_sz >> 8;
DEBUG_printf("webrepl: Sending %d bytes of file\n", out_sz);
write_webrepl(self->sock, readbuf, 2 + out_sz);
return out_sz;
}
STATIC void handle_op(mp_obj_webrepl_t *self) {
mp_obj_t open_args[2] = {
mp_obj_new_str(self->hdr.fname, strlen(self->hdr.fname), false),
@ -122,8 +138,6 @@ STATIC void handle_op(mp_obj_webrepl_t *self) {
}
self->cur_file = mp_builtin_open(2, open_args, (mp_map_t*)&mp_const_empty_map);
const mp_stream_p_t *file_stream =
mp_get_stream_raise(self->cur_file, MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
#if 0
struct mp_stream_seek_t seek = { .offset = self->hdr.offset, .whence = 0 };
@ -137,24 +151,7 @@ STATIC void handle_op(mp_obj_webrepl_t *self) {
if (self->hdr.type == PUT_FILE) {
self->data_to_recv = self->hdr.size;
} else if (self->hdr.type == GET_FILE) {
byte readbuf[2 + 256];
int err;
// TODO: It's not ideal that we block connection while sending file
// and don't process any input.
while (1) {
mp_uint_t out_sz = file_stream->read(self->cur_file, readbuf + 2, sizeof(readbuf) - 2, &err);
assert(out_sz != MP_STREAM_ERROR);
readbuf[0] = out_sz;
readbuf[1] = out_sz >> 8;
DEBUG_printf("webrepl: Sending %d bytes of file\n", out_sz);
write_webrepl(self->sock, readbuf, 2 + out_sz);
if (out_sz == 0) {
break;
}
}
write_webrepl_resp(self->sock, 0);
self->hdr_to_recv = sizeof(struct webrepl_file);
self->data_to_recv = 1;
}
}
@ -242,17 +239,27 @@ STATIC mp_uint_t _webrepl_read(mp_obj_t self_in, void *buf, mp_uint_t size, int
buf_sz += sz;
}
DEBUG_printf("webrepl: Writing %lu bytes to file\n", buf_sz);
int err;
mp_uint_t res = mp_stream_write_exactly(self->cur_file, filebuf, buf_sz, &err);
if (err != 0 || res != buf_sz) {
assert(0);
if (self->hdr.type == PUT_FILE) {
DEBUG_printf("webrepl: Writing %lu bytes to file\n", buf_sz);
int err;
mp_uint_t res = mp_stream_write_exactly(self->cur_file, filebuf, buf_sz, &err);
if (err != 0 || res != buf_sz) {
assert(0);
}
} else if (self->hdr.type == GET_FILE) {
assert(buf_sz == 1);
assert(self->data_to_recv == 0);
assert(filebuf[0] == 0);
mp_uint_t out_sz = write_file_chunk(self);
if (out_sz != 0) {
self->data_to_recv = 1;
}
}
if (self->data_to_recv == 0) {
mp_stream_close(self->cur_file);
self->hdr_to_recv = sizeof(struct webrepl_file);
DEBUG_printf("webrepl: Finished writing file\n");
DEBUG_printf("webrepl: Finished file operation %d\n", self->hdr.type);
write_webrepl_resp(self->sock, 0);
}