+static void server_on_session_add(struct afb_proto_ws *protows, struct readbuf *rb)
+{
+ uint16_t sessionid;
+ const char *sessionstr;
+
+ if (readbuf_uint16(rb, &sessionid) && readbuf_string(rb, &sessionstr, NULL))
+ protows->server_itf->on_session_create(protows->closure, sessionid, sessionstr);
+}
+
+static void server_on_session_drop(struct afb_proto_ws *protows, struct readbuf *rb)
+{
+ uint16_t sessionid;
+
+ if (readbuf_uint16(rb, &sessionid))
+ protows->server_itf->on_session_remove(protows->closure, sessionid);
+}
+
+static void server_on_token_add(struct afb_proto_ws *protows, struct readbuf *rb)
+{
+ uint16_t tokenid;
+ const char *tokenstr;
+
+ if (readbuf_uint16(rb, &tokenid) && readbuf_string(rb, &tokenstr, NULL))
+ protows->server_itf->on_token_create(protows->closure, tokenid, tokenstr);
+}
+
+static void server_on_token_drop(struct afb_proto_ws *protows, struct readbuf *rb)
+{
+ uint16_t tokenid;
+
+ if (readbuf_uint16(rb, &tokenid))
+ protows->server_itf->on_token_remove(protows->closure, tokenid);
+}
+
+static void server_on_event_unexpected(struct afb_proto_ws *protows, struct readbuf *rb)
+{
+ uint16_t eventid;
+
+ if (readbuf_uint16(rb, &eventid))
+ protows->server_itf->on_event_unexpected(protows->closure, eventid);
+}
+
+/* on version offer */
+static void server_on_version_offer(struct afb_proto_ws *protows, struct readbuf *rb)
+{
+ uint8_t count;
+ uint8_t *versions;
+ uint8_t version;
+ uint8_t v;
+ uint32_t id;
+
+ /* reads the descid */
+ if (readbuf_uint32(rb, &id)
+ && id == WSAPI_IDENTIFIER
+ && readbuf_uint8(rb, &count)
+ && count > 0
+ && (versions = (uint8_t*)readbuf_get(rb, (uint32_t)count))) {
+ version = WSAPI_VERSION_UNSET;
+ while (count) {
+ v = versions[--count];
+ if (v >= WSAPI_VERSION_MIN
+ && v <= WSAPI_VERSION_MAX
+ && (version == WSAPI_VERSION_UNSET || version < v))
+ version = v;
+ }
+ if (version != WSAPI_VERSION_UNSET) {
+ if (send_version_set(protows, version) >= 0) {
+ protows->version = version;
+ return;
+ }
+ }
+ }
+ afb_proto_ws_hangup(protows);
+}
+