8000 extmod/modusocket: Add sendall function. · larsks/micropython@edf41d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit edf41d2

Browse files
iabdalkaderdpgeorge
authored andcommitted
extmod/modusocket: Add sendall function.
1 parent b9d2f1e commit edf41d2

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

extmod/modusocket.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,40 @@ STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
235235
}
236236
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);
237237

238+
STATIC mp_obj_t socket_sendall(mp_obj_t self_in, mp_obj_t buf_in) {
239+
mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
240+
if (self->nic == MP_OBJ_NULL) {
241+
// not connected
242+
mp_raise_OSError(MP_EPIPE);
243+
}
244+
mp_buffer_info_t bufinfo;
245+
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
246+
247+
int _errno;
248+
mp_uint_t ret = 0;
249+
if (self->timeout == 0) {
250+
ret = self->nic_type->send(self, bufinfo.buf, bufinfo.len, &_errno);
251+
if (ret == -1) {
252+
mp_raise_OSError(_errno);
253+
} else if (bufinfo.len > ret) {
254+
mp_raise_OSError(MP_EAGAIN);
255+
}
256+
} else {
257+
// TODO: In CPython3.5, socket timeout should apply to the
258+
// entire sendall() operation, not to individual send() chunks.
259+
while (bufinfo.len != 0) {
260+
ret = self->nic_type->send(self, bufinfo.buf, bufinfo.len, &_errno);
261+
if (ret == -1) {
262+
mp_raise_OSError(_errno);
263+
}
264+
bufinfo.len -= ret;
265+
bufinfo.buf = (char *)bufinfo.buf + ret;
266+
}
267+
}
268+
return mp_obj_new_int_from_uint(ret);
269+
}
270+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_sendall_obj, socket_sendall);
271+
238272
// method socket.recv(bufsize)
239273
STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
240274
mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -412,6 +446,7 @@ STATIC const mp_rom_map_elem_t socket_locals_dict_table[] = {
412446
{ MP_ROM_QSTR(MP_QSTR_accept), MP_ROM_PTR(&socket_accept_obj) },
413447
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&socket_connect_obj) },
414448
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&socket_send_obj) },
449+
{ MP_ROM_QSTR(MP_QSTR_sendall), MP_ROM_PTR(&socket_sendall_obj) },
415450
{ MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&socket_recv_obj) },
416451
{ MP_ROM_QSTR(MP_QSTR_sendto), MP_ROM_PTR(&socket_sendto_obj) },
417452
{ MP_ROM_QSTR(MP_QSTR_recvfrom), MP_ROM_PTR(&socket_recvfrom_obj) },

0 commit comments

Comments
 (0)
0