|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Paul Sokolovsky |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdint.h> |
| 29 | + |
| 30 | +#include "py/nlr.h" |
| 31 | +#include "py/obj.h" |
| 32 | +#include "py/stream.h" |
| 33 | + |
| 34 | +#if MICROPY_PY_WEBSOCKET |
| 35 | + |
| 36 | +enum { FRAME_HEADER, FRAME_OPT, PAYLOAD }; |
| 37 | + |
| 38 | +typedef struct _mp_obj_websocket_t { |
| 39 | + mp_obj_base_t base; |
| 40 | + mp_obj_t sock; |
| 41 | + uint32_t mask; |
| 42 | + byte state; |
| 43 | + byte to_recv; |
| 44 | + byte mask_pos; |
| 45 | + byte buf[4]; |
| 46 | +} mp_obj_websocket_t; |
| 47 | + |
| 48 | +STATIC mp_obj_t websocket_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 49 | + assert(n_args == 1); |
| 50 | + mp_obj_websocket_t *o = m_new_obj(mp_obj_websocket_t); |
| 51 | + o->base.type = type; |
| 52 | + o->sock = args[0]; |
| 53 | + o->state = FRAME_HEADER; |
| 54 | + o->to_recv = 2; |
| 55 | + o->mask_pos = 0; |
| 56 | + return o; |
| 57 | +} |
| 58 | + |
| 59 | +STATIC mp_uint_t websocket_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { |
| 60 | + mp_obj_websocket_t *self = self_in; |
| 61 | + assert(size < 126); |
| 62 | + byte header[] = {0x81, size}; |
| 63 | + |
| 64 | + mp_uint_t out_sz = mp_stream_writeall(self->sock, header, sizeof(header), errcode); |
| 65 | + if (out_sz == MP_STREAM_ERROR) { |
| 66 | + return MP_STREAM_ERROR; |
| 67 | + } |
| 68 | + return mp_stream_writeall(self->sock, buf, size, errcode); |
| 69 | +} |
| 70 | + |
| 71 | +STATIC const mp_map_elem_t websocket_locals_dict_table[] = { |
| 72 | + { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, |
| 73 | +}; |
| 74 | +STATIC MP_DEFINE_CONST_DICT(websocket_locals_dict, websocket_locals_dict_table); |
| 75 | + |
| 76 | +STATIC const mp_stream_p_t websocket_stream_p = { |
| 77 | +// .read = websocket_read, |
| 78 | + .write = websocket_write, |
| 79 | +}; |
| 80 | + |
| 81 | +STATIC const mp_obj_type_t websocket_type = { |
| 82 | + { &mp_type_type }, |
| 83 | + .name = MP_QSTR_websocket, |
| 84 | + .make_new = websocket_make_new, |
| 85 | + .stream_p = &websocket_stream_p, |
| 86 | + .locals_dict = (mp_obj_t)&websocket_locals_dict, |
| 87 | +}; |
| 88 | + |
| 89 | +STATIC const mp_map_elem_t websocket_module_globals_table[] = { |
| 90 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_websocket) }, |
| 91 | + { MP_OBJ_NEW_QSTR(MP_QSTR_websocket), (mp_obj_t)&websocket_type }, |
| 92 | +}; |
| 93 | + |
| 94 | +STATIC MP_DEFINE_CONST_DICT(websocket_module_globals, websocket_module_globals_table); |
| 95 | + |
| 96 | +const mp_obj_module_t mp_module_websocket = { |
| 97 | + .base = { &mp_type_module }, |
| 98 | + .name = MP_QSTR_websocket, |
| 99 | + .globals = (mp_obj_dict_t*)&websocket_module_globals, |
| 100 | +}; |
| 101 | + |
| 102 | +#endif // MICROPY_PY_WEBSOCKET |
0 commit comments