O2 2.0
A communication protocol for interactive music and media applications.
websock.h
1// websock.h -- websocket server for O2
2//
3// Roger Dannenberg
4// based on websocketserver.srp by Hongbo Fang and Roger Dannenberg
5// Feb 2020
6
32#ifndef O2_NO_WEBSOCKETS
33class Http_conn;
34
35class Http_server: public Proxy_info {
36public:
37 long page_len; // string length of page
38 const char *root;
39 Http_server(int port, const char *root);
40 virtual ~Http_server();
41
42 // Implement the Net_interface:
43 // do nothing, just start receiving messages:
44 virtual O2err accepted(Fds_info *conn);
45 virtual O2err deliver(O2netmsg_ptr msg) { return O2_FAIL; } // server
46};
47
48
49class Http_reader;
50
51class Http_conn: public Bridge_info {
52public:
53 const char *root; // root of web pages
54 int port; // server port
55 Vec<char> inbuf;
56 int inf;
57 Http_reader *reader;
58 // link used to create protocol's pending_ws_sender list:
59 Http_conn *next_pending; // could probably use next instead for this list,
60 // but next is an O2node* used for hash table collisions. Right now,
61 // Http_conn is unnamed and not hashed, but it seems safer to reserve
62 // next for hash table use.
63 // websocket info:
64 bool is_web_socket;
65 bool sent_close_command; // sent a CLOSE command
66 bool confirmed_ensemble; // first message must be /_o2/ws/dy "ensemble"
67 int maskx; // index for masking key
68 int payload_offset; // state to help parse incoming data
69 int ws_msg_len;
70 O2message_ptr outgoing; // pending outgoing messages: To send a message,
71 // we parse it, and if we're already sending a message, we could already
72 // be using the message parsing memory. In that case, we append the
73 // message to this outgoing queue (a list). After each message is sent,
74 // we sequentially send messages from this outgoing queue. They may end
75 // up in another queue associated with the websocket's socket. These
76 // will contain "raw" data ready for TCP send.
77
78 Http_conn(Fds_info *conn, const char *root, int port);
79
80 virtual ~Http_conn();
81
82 O2err websocket_upgrade(const char *key, int msg_len);
83 O2err process_as_web_socket();
84 const char *find_field(const char *name, const char *value, int length);
85
86 O2err ws_msg_is_complete(const char **error);
87 O2err handle_websocket_msg(const char **error);
88 O2err send_msg_later(O2message_ptr msg);
89
90 virtual O2err close(); // close the connection
91 virtual O2err accepted(Fds_info *conn) { return O2_FAIL; } // not a server
92 virtual O2err deliver(O2netmsg_ptr msg);
93 virtual O2err send(bool block);
94
95};
96
97
98// file reader to asynchronously read web pages
99#ifdef WIN32
100class Http_reader: public O2obj {
101#else
102class Http_reader: public Proxy_info {
103#endif
104public:
105 int port; // server port
106 O2netmsg_ptr data;
107 O2netmsg_ptr *last_ref; // pointer to last msg in data, could be &data
108 // *last_ref is where async read puts the content
109 long data_len; // how much have we read?
110 Fds_info *fds_info; // file descriptor for file to be read
111 Http_conn *conn;
112#ifdef WIN32
113 HANDLE inf; // handle for the opened file to be read
114 Http_reader *next; // used to make a list of active readers
115 OVERLAPPED overlapped;
116 bool ready_for_read;
117 void poll(); // poll method for async reads
118#else
119 aiocb cb; // asynchronous read control block
120#endif
121
122 Http_reader(const char *c_path, Http_conn *connection, int port);
123
124 ~Http_reader();
125
126 virtual O2err accepted(Fds_info *conn) { return O2_FAIL; } // not a server
127 virtual O2err deliver(O2netmsg_ptr msg);
128 virtual O2netmsg_ptr prepare_new_read();
129 virtual void read_operation_completed(int n);
130 virtual O2err read_eof();
131
132};
133#endif
Definition: bridge.h:146
Definition: o2network.h:147
Definition: websock.h:51
Definition: websock.h:102
Definition: websock.h:35
Definition: o2obj.h:9
Definition: o2node.h:291
Definition: vec.h:6
O2err
return values used generally by O2 functions
Definition: o2.h:329
@ O2_FAIL
a non-specific error occurred.
Definition: o2.h:339
an O2 message container
Definition: o2.h:690
Definition: o2network.h:57