Skip to content

Commit d6236e8

Browse files
committed
extmod/modwebsocket: Implement MP_STREAM_SET_DATA_OPTS ioctl.
Allows to set fragment type (txt/bin/etc.) for output records.
1 parent 6837dba commit d6236e8

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

extmod/modwebsocket.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum {
4343
FRAME_CLOSE = 0x8, FRAME_PING, FRAME_PONG
4444
};
4545

46-
enum { BLOCKING_WRITE = 1 };
46+
enum { BLOCKING_WRITE = 0x80 };
4747

4848
typedef struct _mp_obj_websocket_t {
4949
mp_obj_base_t base;
@@ -69,7 +69,7 @@ STATIC mp_obj_t websocket_make_new(const mp_obj_type_t *type, size_t n_args, siz
6969
o->to_recv = 2;
7070
o->mask_pos = 0;
7171
o->buf_pos = 0;
72-
o->opts = 0;
72+
o->opts = FRAME_TXT;
7373
if (n_args > 1 && args[1] == mp_const_true) {
7474
o->opts |= BLOCKING_WRITE;
7575
}
@@ -185,7 +185,7 @@ STATIC mp_uint_t websocket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int
185185
STATIC mp_uint_t websocket_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
186186
mp_obj_websocket_t *self = self_in;
187187
assert(size < 126);
188-
byte header[] = {0x81, size};
188+
byte header[] = {0x80 | (self->opts & FRAME_OPCODE_MASK), size};
189189

190190
mp_obj_t dest[3];
191191
if (self->opts & BLOCKING_WRITE) {
@@ -212,6 +212,11 @@ STATIC mp_uint_t websocket_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t
212212
switch (request) {
213213
case MP_STREAM_GET_DATA_OPTS:
214214
return self->ws_flags & FRAME_OPCODE_MASK;
215+
case MP_STREAM_SET_DATA_OPTS: {
216+
int cur = self->opts & FRAME_OPCODE_MASK;
217+
self->opts = (self->opts & ~FRAME_OPCODE_MASK) | (arg & FRAME_OPCODE_MASK);
218+
return cur;
219+
}
215220
default:
216221
*errcode = EINVAL;
217222
return MP_STREAM_ERROR;

0 commit comments

Comments
 (0)