rws_frame.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef __RWS_FRAME_H__
  23. #define __RWS_FRAME_H__ 1
  24. #include "librws.h"
  25. #include "rws_common.h"
  26. typedef enum _rws_opcode
  27. {
  28. rws_opcode_continuation = 0x0, // %x0 denotes a continuation frame
  29. rws_opcode_text_frame = 0x1, // %x1 denotes a text frame
  30. rws_opcode_binary_frame = 0x2, // %x2 denotes a binary frame
  31. rws_opcode_connection_close = 0x8, // %x8 denotes a connection close
  32. rws_opcode_ping = 0x9, // %x9 denotes a ping
  33. rws_opcode_pong = 0xA // %xA denotes a pong
  34. } rws_opcode;
  35. typedef struct _rws_frame_struct
  36. {
  37. void *data;
  38. size_t data_size;
  39. rws_opcode opcode;
  40. unsigned char mask[4];
  41. rws_bool is_masked;
  42. rws_bool is_finished;
  43. unsigned char header_size;
  44. } _rws_frame;
  45. size_t rws_check_recv_frame_size(const void *data, const size_t data_size);
  46. _rws_frame *rws_frame_create_with_recv_data(const void *data, const size_t data_size);
  47. // data - should be null, and setted by newly created. 'data' & 'data_size' can be null
  48. void rws_frame_fill_with_send_data(_rws_frame *f, const void *data, const size_t data_size);
  49. // combine datas of 2 frames. combined is 'to' /
  50. void rws_frame_combine_datas(_rws_frame *to, _rws_frame *from);
  51. _rws_frame *rws_frame_create(void);
  52. void rws_frame_delete(_rws_frame *f);
  53. void rws_frame_delete_clean(_rws_frame **f);
  54. /* RT-Thread Team add */
  55. size_t rws_frame_get_header_size(const char *data, const size_t len);
  56. size_t rws_frame_get_payload_len(const char *data, const size_t len);
  57. rws_opcode rws_frame_get_opcode(const char *data, const size_t len);
  58. void rws_frame_fill_with_send_bin(_rws_frame *f, const void *data, const size_t data_size, rws_bool is_fin);
  59. #endif