File: evd-websocket-common.h

package info (click to toggle)
event-dance 0.1.20-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,068 kB
  • sloc: ansic: 25,946; sh: 11,340; makefile: 418; xml: 249; python: 27
file content (164 lines) | stat: -rw-r--r-- 7,088 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * evd-websocket-common.h
 *
 * EventDance, Peer-to-peer IPC library <http://eventdance.org>
 *
 * Copyright (C) 2012, Igalia S.L.
 *
 * Authors:
 *   Eduardo Lima Mitev <elima@igalia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 3, or (at your option) any later version as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License at http://www.gnu.org/licenses/lgpl-3.0.txt
 * for more details.
 */

#ifndef __EVD_WEBSOCKET_COMMON_H__
#define __EVD_WEBSOCKET_COMMON_H__

#include <glib.h>
#include <gio/gio.h>

#include <evd-web-service.h>
#include <evd-http-connection.h>
#include <evd-http-request.h>
#include <evd-peer.h>

G_BEGIN_DECLS

#define EVD_WEBSOCKET_DATA_KEY "org.eventdance.lib.Websocket.CONN_DATA"

#define EVD_WEBSOCKET_MAX_PAYLOAD_SIZE 0x40000000

typedef void     (* EvdWebsocketFrameCb)         (EvdHttpConnection *conn,
                                                  const gchar       *frame,
                                                  gsize              frame_length,
                                                  gboolean           is_binary,
                                                  gpointer           user_data);
typedef void     (* EvdWebsocketCloseCb)         (EvdHttpConnection *conn,
                                                  gboolean           gracefully,
                                                  gpointer           user_data);

typedef struct _EvdWebsocketData EvdWebsocketData;

typedef gboolean (* EvdWebsocketProcessDataFunc)    (EvdWebsocketData *data);
typedef gboolean (* EvdWebsocketSendCloseFrameFunc) (EvdWebsocketData  *data,
                                                     guint16            code,
                                                     const gchar       *reason,
                                                     GError           **error);
typedef gboolean (* EvdWebsocketSendDataFrameFunc)  (EvdWebsocketData  *data,
                                                     const gchar       *frame,
                                                     gsize              frame_len,
                                                     EvdMessageType     frame_type,
                                                     GError           **error);

typedef enum
{
  /* common states */
  EVD_WEBSOCKET_STATE_IDLE,
  EVD_WEBSOCKET_STATE_CLOSING,
  EVD_WEBSOCKET_STATE_CLOSED,
  EVD_WEBSOCKET_STATE_READING_PAYLOAD_LEN,

  /* version 00 specific */
  EVD_WEBSOCKET_STATE_READING_BINARY_FRAME,
  EVD_WEBSOCKET_STATE_READING_TEXT_FRAME,

  /* version 08 specific */
  EVD_WEBSOCKET_STATE_READING_HEADER,
  EVD_WEBSOCKET_STATE_READING_MASKING_KEY,
  EVD_WEBSOCKET_STATE_READING_PAYLOAD,

  /* padding for future expansion */
  EVD_WEBSOCKET_STATE_PADDING0,
  EVD_WEBSOCKET_STATE_PADDING1,
  EVD_WEBSOCKET_STATE_PADDING2,
  EVD_WEBSOCKET_STATE_PADDING3,
  EVD_WEBSOCKET_STATE_PADDING4,
  EVD_WEBSOCKET_STATE_PADDING5,
  EVD_WEBSOCKET_STATE_PADDING6,
  EVD_WEBSOCKET_STATE_PADDING7
} EvdWebsocketStates;

struct _EvdWebsocketData
{
  gint ref_count;

  guint version;
  gboolean server;

  EvdHttpConnection *conn;

  EvdWebsocketFrameCb frame_cb;
  EvdWebsocketCloseCb close_cb;

  EvdWebsocketProcessDataFunc process_data_func;
  EvdWebsocketSendCloseFrameFunc send_close_frame_func;
  EvdWebsocketSendDataFrameFunc send_data_frame_func;

  gpointer user_data;
  GDestroyNotify user_data_destroy_notify;

  guint8 state;

  GString *buf;
  gsize buf_len;
  gsize offset;

  guint8 opcode;
  gsize payload_len;
  gchar *frame_data;
  gsize frame_len;

  gboolean close_frame_sent;

  gboolean fin;
  gboolean masked;
  guint8 masking_key[4];
  gchar *extensions_data;
  gsize extension_len;
};

void                evd_websocket_common_handle_handshake_request        (EvdWebService       *web_service,
                                                                          EvdHttpConnection   *conn,
                                                                          EvdHttpRequest      *request,
                                                                          GAsyncReadyCallback  callback,
                                                                          gpointer             user_data);
gboolean            evd_websocket_common_handle_handshake_request_finish (GAsyncResult  *result,
                                                                          GError       **error);

void                evd_websocket_common_setup_connection                (EvdHttpConnection              *conn,
                                                                          guint8                          version,
                                                                          gboolean                        is_server,
                                                                          EvdWebsocketProcessDataFunc     process_data_func,
                                                                          EvdWebsocketSendCloseFrameFunc  send_close_frame_func,
                                                                          EvdWebsocketSendDataFrameFunc   send_data_frame_func);

void                evd_websocket_common_bind                            (EvdHttpConnection   *conn,
                                                                          EvdWebsocketFrameCb  frame_cb,
                                                                          EvdWebsocketCloseCb  close_cb,
                                                                          gpointer             user_data,
                                                                          GDestroyNotify       user_data_destroy_notify);
gboolean            evd_websocket_common_is_bound                        (EvdHttpConnection *conn);

gboolean            evd_websocket_common_close                           (EvdHttpConnection  *conn,
                                                                          guint16             code,
                                                                          const gchar        *reason,
                                                                          GError            **error);

gboolean            evd_websocket_common_send                            (EvdHttpConnection  *conn,
                                                                          const gchar        *frame,
                                                                          gsize               frame_len,
                                                                          EvdMessageType      frame_type,
                                                                          GError            **error);

G_END_DECLS

#endif /* __EVD_WEBSOCKET_COMMON_H__ */