29
29
30
30
#include <stdio.h>
31
31
#include <string.h>
32
+ #include <errno.h> // needed because mp_is_nonblocking_error uses system error codes
32
33
33
34
#include "py/nlr.h"
34
35
#include "py/runtime.h"
@@ -83,6 +84,9 @@ int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
83
84
84
85
int out_sz = sock_stream -> write (sock , buf , len , & err );
85
86
if (out_sz == MP_STREAM_ERROR ) {
87
+ if (mp_is_nonblocking_error (err )) {
88
+ return MBEDTLS_ERR_SSL_WANT_WRITE ;
89
+ }
86
90
return - err ;
87
91
} else {
88
92
return out_sz ;
@@ -97,6 +101,9 @@ int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
97
101
98
102
int out_sz = sock_stream -> read (sock , buf , len , & err );
99
103
if (out_sz == MP_STREAM_ERROR ) {
104
+ if (mp_is_nonblocking_error (err )) {
105
+ return MBEDTLS_ERR_SSL_WANT_READ ;
106
+ }
100
107
return - err ;
101
108
} else {
102
109
return out_sz ;
@@ -199,6 +206,9 @@ STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errc
199
206
if (ret >= 0 ) {
200
207
return ret ;
201
208
}
209
+ if (ret == MBEDTLS_ERR_SSL_WANT_READ ) {
210
+ ret = MP_EWOULDBLOCK ;
211
+ }
202
212
* errcode = ret ;
203
213
return MP_STREAM_ERROR ;
204
214
}
@@ -210,17 +220,20 @@ STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, in
210
220
if (ret >= 0 ) {
211
221
return ret ;
212
222
}
223
+ if (ret == MBEDTLS_ERR_SSL_WANT_WRITE ) {
224
+ ret = MP_EWOULDBLOCK ;
225
+ }
213
226
* errcode = ret ;
214
227
return MP_STREAM_ERROR ;
215
228
}
216
229
217
230
STATIC mp_obj_t socket_setblocking (mp_obj_t self_in , mp_obj_t flag_in ) {
218
- // Currently supports only blocking mode
219
- ( void ) self_in ;
220
- if (! mp_obj_is_true ( flag_in )) {
221
- mp_not_implemented ( "" );
222
- }
223
- return mp_const_none ;
231
+ mp_obj_ssl_socket_t * o = MP_OBJ_TO_PTR ( self_in );
232
+ mp_obj_t sock = o -> sock ;
233
+ mp_obj_t dest [ 3 ];
234
+ mp_load_method ( sock , MP_QSTR_setblocking , dest );
235
+ dest [ 2 ] = flag_in ;
236
+ return mp_call_method_n_kw ( 1 , 0 , dest ) ;
224
237
}
225
238
STATIC MP_DEFINE_CONST_FUN_OBJ_2 (socket_setblocking_obj , socket_setblocking );
226
239
0 commit comments