Example #1
0
void publish_and_consume_message(const char *msg_to_publish) {
  amqp_connection_state_t connection_state = setup_connection_and_channel();

  queue_declare(connection_state, test_queue_name);
  basic_publish(connection_state, msg_to_publish);

  uint64_t body_size;
  char *msg = consume_message(connection_state, test_queue_name, &body_size);

  assert(body_size == strlen(msg_to_publish));
  assert(strncmp(msg_to_publish, msg, body_size) == 0);
  free(msg);

  close_and_destroy_connection(connection_state);
}
Example #2
0
static void process_frames(wiced_tcp_socket_t *sock, websocket_msg_handler_t binary_msg_handler, websocket_msg_handler_t text_msg_handler, void *ctx) {
    wiced_result_t ret;
    char *buf, *cur;
    uint32_t len;
    websocket_frame_t frame;
    websocket_message_t msg;
    wiced_bool_t exit_loop = WICED_FALSE;

    message_init(&msg);

    while (WICED_TRUE) {
        buf = NULL;
        ret = receive_data(sock, 3000, &buf, &len);

        if (ret == WICED_TIMEOUT) {
            continue;
        }

        if (ret != WICED_SUCCESS) {
            WPRINT_LIB_INFO( ("(WebSocket) Failed to read from socket (err=%u)\n", ret) );
            break;
        }

        WPRINT_LIB_DEBUG( ("(WebSocket) Received %u bytes\n", (unsigned int)len) );

        cur = buf;
        while (len > 0) {
            if (get_frame(&cur, &len, &frame) != WICED_SUCCESS) {
                WPRINT_LIB_INFO( ("(WebSocket) Failed to read frame (err=%u)\n", ret) );
                exit_loop = WICED_TRUE;
                break;
            }

            if (frame.opcode == WS_OPCODE_PING) {
                WPRINT_LIB_DEBUG( ("(WebSocket) Received PING frame.\n"));
                send_control_frame(sock, WS_OPCODE_PONG, frame.payload, frame.payload_len);
            }
            else if (frame.opcode == WS_OPCODE_PONG) {
                WPRINT_LIB_DEBUG( ("(WebSocket) Received PONG frame.\n"));
                /* Nothing to do. */
            }
            else if (frame.opcode == WS_OPCODE_CLOSE) {
                WPRINT_LIB_DEBUG( ("(WebSocket) Received CLOSE frame.\n") );
                /* Echo the CLOSE frame with the 2-byte status code. */
                if (frame.payload_len >= 2) {
                    send_control_frame(sock, WS_OPCODE_CLOSE, frame.payload, 2);
                }
                else {
                    send_control_frame(sock, WS_OPCODE_CLOSE, NULL, 0);
                }
                exit_loop = WICED_TRUE;
                break;
            }
            else if (frame.opcode == WS_OPCODE_CONTINUATION) {
                WPRINT_LIB_DEBUG( ("(WebSocket) Received CONTINUATION frame.\n") );

                if (msg.is_active) {
                    message_append(&msg, frame.payload, frame.payload_len);

                    if (frame.is_final) {
                        consume_message(&msg, binary_msg_handler, text_msg_handler, ctx);
                    }
                }
                else {
                    exit_loop = WICED_TRUE;
                    break;
                }
            }
            else if (frame.opcode == WS_OPCODE_TEXT) {
                WPRINT_LIB_DEBUG( ("(WebSocket) Received TEXT frame.\n") );

                if (msg.is_active) {
                    WPRINT_LIB_DEBUG( ("(WebSocket) A pending message already exists.\n") );
                    exit_loop = WICED_TRUE;
                    break;
                }

                message_start(&msg, WICED_TRUE, frame.payload, frame.payload_len);

                if (frame.is_final) {
                    consume_message(&msg, binary_msg_handler, text_msg_handler, ctx);
                }
            }
            else if (frame.opcode == WS_OPCODE_BINARY) {
                WPRINT_LIB_DEBUG( ("(WebSocket) Received BINARY frame.\n") );

                if (msg.is_active) {
                    WPRINT_LIB_DEBUG( ("(WebSocket) A pending message already exists.\n") );
                    exit_loop = WICED_TRUE;
                    break;
                }

                message_start(&msg, WICED_FALSE, frame.payload, frame.payload_len);

                if (frame.is_final) {
                    consume_message(&msg, binary_msg_handler, text_msg_handler, ctx);
                }
            }
            else {
                WPRINT_LIB_INFO(("(WebSocket) Received invalid opcode %02X\n", frame.opcode));
                exit_loop = WICED_TRUE;
                break;
            }
        }

        free(buf);

        if (exit_loop) {
            WPRINT_LIB_INFO( ("(WebSocket) Exiting read loop.\n") );
            break;
        }
    }

    message_deinit(&msg);
}