Skip to content

Commit b6da15e

Browse files
committed
esp32/modsocket.c: fix copyright, socket_recv gets param and exception
1 parent 51de5fd commit b6da15e

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

esp32/modsocket.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
/*
22
* This file is part of the Micro Python project, http://micropython.org/
33
*
4+
* Development of the code in this file was sponsored by Microbric Pty Ltd
5+
* and Mnemote Pty Ltd
6+
*
47
* The MIT License (MIT)
58
*
6-
* Copyright (c) 2016 Nick Moore
9+
* Copyright (c) 2016, 2017 Nick Moore @mnemote
10+
*
711
* Based on extmod/modlwip.c
812
* Copyright (c) 2013, 2014 Damien P. George
913
* Copyright (c) 2015 Galen Hazelwood
@@ -60,6 +64,11 @@ STATIC mp_obj_t socket_close(const mp_obj_t arg0) {
6064
}
6165
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
6266

67+
static int exception_from_errno(int _errno) {
68+
// XXX add more specific exceptions
69+
mp_raise_OSError(_errno);
70+
}
71+
6372
static int _socket_getaddrinfo2(const mp_obj_t host, const mp_obj_t portx, struct addrinfo **resp) {
6473
const struct addrinfo hints = {
6574
.ai_family = AF_INET,
@@ -167,21 +176,25 @@ STATIC mp_obj_t socket_setblocking(const mp_obj_t arg0, const mp_obj_t arg1) {
167176
}
168177
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
169178

170-
STATIC mp_obj_t socket_recv(const mp_obj_t arg0) {
179+
STATIC mp_obj_t socket_recv(mp_uint_t n_args, const mp_obj_t *args) {
171180
byte buf[1024];
172-
socket_obj_t *self = MP_OBJ_TO_PTR(arg0);
173-
int x = lwip_recvfrom(self->fd, buf, sizeof(buf), 0, NULL, NULL);
181+
socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
182+
size_t len = (n_args > 1) ? MIN(mp_obj_get_int(args[1]), sizeof(buf)) : sizeof(buf);
183+
int x = lwip_recvfrom(self->fd, buf, len, 0, NULL, NULL);
174184
if (x >= 0) return mp_obj_new_bytes(buf, x);
175-
return mp_const_none;
185+
if (errno == EWOULDBLOCK) return b'';
186+
exception_from_errno(errno);
176187
}
177-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_recv_obj, socket_recv);
188+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_recv_obj, 1, 2, socket_recv);
178189

179190
STATIC mp_obj_t socket_send(const mp_obj_t arg0, const mp_obj_t arg1) {
180191
socket_obj_t *self = MP_OBJ_TO_PTR(arg0);
181192
mp_uint_t datalen;
182193
const char *data = mp_obj_str_get_data(arg1, &datalen);
183194
int x = lwip_write(self->fd, data, datalen);
184-
return mp_obj_new_int(x);
195+
if (x >= 0) return mp_obj_new_int(x);
196+
if (errno == EWOULDBLOCK) return mp_obj_new_int(0);
197+
exception_from_errno(errno);
185198
}
186199
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);
187200

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy