@@ -111,6 +111,22 @@ STATIC mp_obj_t webrepl_make_new(const mp_obj_type_t *type, size_t n_args, size_
111111 return o ;
112112}
113113
114+ STATIC int write_file_chunk (mp_obj_webrepl_t * self ) {
115+ const mp_stream_p_t * file_stream =
116+ mp_get_stream_raise (self -> cur_file , MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL );
117+ byte readbuf [2 + 256 ];
118+ int err ;
119+ mp_uint_t out_sz = file_stream -> read (self -> cur_file , readbuf + 2 , sizeof (readbuf ) - 2 , & err );
120+ if (out_sz == MP_STREAM_ERROR ) {
121+ return out_sz ;
122+ }
123+ readbuf [0 ] = out_sz ;
124+ readbuf [1 ] = out_sz >> 8 ;
125+ DEBUG_printf ("webrepl: Sending %d bytes of file\n" , out_sz );
126+ write_webrepl (self -> sock , readbuf , 2 + out_sz );
127+ return out_sz ;
128+ }
129+
114130STATIC void handle_op (mp_obj_webrepl_t * self ) {
115131 mp_obj_t open_args [2 ] = {
116132 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) {
122138 }
123139
124140 self -> cur_file = mp_builtin_open (2 , open_args , (mp_map_t * )& mp_const_empty_map );
125- const mp_stream_p_t * file_stream =
126- mp_get_stream_raise (self -> cur_file , MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL );
127141
128142 #if 0
129143 struct mp_stream_seek_t seek = { .offset = self -> hdr .offset , .whence = 0 };
@@ -137,17 +151,11 @@ STATIC void handle_op(mp_obj_webrepl_t *self) {
137151 if (self -> hdr .type == PUT_FILE ) {
138152 self -> data_to_recv = self -> hdr .size ;
139153 } else if (self -> hdr .type == GET_FILE ) {
140- byte readbuf [2 + 256 ];
141- int err ;
142154 // TODO: It's not ideal that we block connection while sending file
143155 // and don't process any input.
144156 while (1 ) {
145- mp_uint_t out_sz = file_stream -> read (self -> cur_file , readbuf + 2 , sizeof ( readbuf ) - 2 , & err );
157+ mp_uint_t out_sz = write_file_chunk (self );
146158 assert (out_sz != MP_STREAM_ERROR );
147- readbuf [0 ] = out_sz ;
148- readbuf [1 ] = out_sz >> 8 ;
149- DEBUG_printf ("webrepl: Sending %d bytes of file\n" , out_sz );
150- write_webrepl (self -> sock , readbuf , 2 + out_sz );
151159 if (out_sz == 0 ) {
152160 break ;
153161 }
0 commit comments