py/stream: Add adapter methods with POSIX-compatible signatures.

Previoussly such read() and write() methods were used by modussl_axtls,
move to py/stream for reuse.
This commit is contained in:
Paul Sokolovsky 2016-07-30 00:25:06 +03:00
parent abd5a57ea1
commit 4f1b0292db
3 changed files with 64 additions and 30 deletions

View File

@ -200,34 +200,4 @@ const mp_obj_module_t mp_module_ussl = {
.globals = (mp_obj_dict_t*)&mp_module_ssl_globals,
};
// These functions might be split to stream_posix.c. They are referenced by
// axtls os_port.h .
ssize_t mp_stream_posix_write(void *sock_obj, const void *buf, size_t len);
ssize_t mp_stream_posix_read(void *sock_obj, void *buf, size_t len);
int mp_stream_errno;
ssize_t mp_stream_posix_write(void *sock_obj, const void *buf, size_t len) {
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)sock_obj;
const mp_stream_p_t *stream_p = o->type->protocol;
mp_uint_t out_sz = stream_p->write(o, buf, len, &mp_stream_errno);
if (out_sz == MP_STREAM_ERROR) {
return -1;
} else {
return out_sz;
}
}
ssize_t mp_stream_posix_read(void *sock_obj, void *buf, size_t len) {
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)sock_obj;
const mp_stream_p_t *stream_p = o->type->protocol;
mp_uint_t out_sz = stream_p->read(o, buf, len, &mp_stream_errno);
if (out_sz == MP_STREAM_ERROR) {
return -1;
} else {
return out_sz;
}
}
#endif // MICROPY_PY_USSL

View File

@ -510,3 +510,61 @@ STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
return mp_obj_new_int(res);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);
/*
* POSIX-like functions
*
* These functions have POSIX-compatible signature (except for "void *stream"
* first argument instead of "int fd"). They are useful to port existing
* POSIX-compatible software to work with MicroPython streams.
*/
// errno-like variable. If any of the functions below returned with error
// status, this variable will contain error no.
int mp_stream_errno;
ssize_t mp_stream_posix_write(mp_obj_t stream, const void *buf, size_t len) {
mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
const mp_stream_p_t *stream_p = o->type->protocol;
mp_uint_t out_sz = stream_p->write(stream, buf, len, &mp_stream_errno);
if (out_sz == MP_STREAM_ERROR) {
return -1;
} else {
return out_sz;
}
}
ssize_t mp_stream_posix_read(mp_obj_t stream, void *buf, size_t len) {
mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
const mp_stream_p_t *stream_p = o->type->protocol;
mp_uint_t out_sz = stream_p->read(stream, buf, len, &mp_stream_errno);
if (out_sz == MP_STREAM_ERROR) {
return -1;
} else {
return out_sz;
}
}
off_t mp_stream_posix_lseek(mp_obj_t stream, off_t offset, int whence) {
const mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
const mp_stream_p_t *stream_p = o->type->protocol;
struct mp_stream_seek_t seek_s;
seek_s.offset = offset;
seek_s.whence = whence;
mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &mp_stream_errno);
if (res == MP_STREAM_ERROR) {
return -1;
}
return seek_s.offset;
}
int mp_stream_posix_fsync(mp_obj_t stream) {
mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
const mp_stream_p_t *stream_p = o->type->protocol;
mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_FLUSH, 0, &mp_stream_errno);
if (res == MP_STREAM_ERROR) {
return -1;
}
return res;
}

View File

@ -84,6 +84,12 @@ mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf, mp_uint_t size, int *errcode,
void mp_stream_write_adaptor(void *self, const char *buf, size_t len);
// Functions with POSIX-compatible signatures
ssize_t mp_stream_posix_write(mp_obj_t stream, const void *buf, size_t len);
ssize_t mp_stream_posix_read(mp_obj_t stream, void *buf, size_t len);
off_t mp_stream_posix_lseek(mp_obj_t stream, off_t offset, int whence);
int mp_stream_posix_fsync(mp_obj_t stream);
#if MICROPY_STREAMS_NON_BLOCK
#define mp_is_nonblocking_error(errno) ((errno) == EAGAIN || (errno) == EWOULDBLOCK)
#else