Exemplo n.º 1
0
/**
 * Receive an initialization message, but the message may not be an NDT
 * message, and instead it may be the beginning of the websocket handshake. In
 * that case, set up the websocket handshake and then receive the message.
 */
int recv_msg_plus_websocket(Connection* ctl, TestOptions* test_options,
                            int* msg_type, char* msg_value, int* msg_len) {
    char header[3] = {0};
    int64_t err;
    int received_length;
    if (readn_any(ctl, header, sizeof(header)) != sizeof(header)) {
        log_println(3, "Failed to read %d bytes", sizeof(header));
        return EIO;
    }
    if (strncmp(header, "GET", 3) == 0) {
        // GET starts HTTP stuff, so try and perform the websocket handshake
        err = initialize_websocket_connection(ctl, sizeof(header), "ndt");
        if (err) return err;
        test_options->connection_flags |= WEBSOCKET_SUPPORT;
        err = recv_websocket_ndt_msg(ctl, msg_type, msg_value, msg_len);
        return (err < 0) ? -err : 0;
    } else {
        // It didn't start with GET so it's not a websocket connection
        test_options->connection_flags &= ~WEBSOCKET_SUPPORT;
        *msg_type = header[0];
        received_length = (header[1] << 8) + header[2];
        if (received_length > *msg_len) return EMSGSIZE;
        *msg_len = received_length;
        if (readn_any(ctl, msg_value, *msg_len) != *msg_len) return EIO;
        return 0;
    }
}
Exemplo n.º 2
0
int recv_any_msg(Connection* conn, int* type, void* msg, int* len,
                 int connectionFlags) {
  if (connectionFlags & WEBSOCKET_SUPPORT) {
    return recv_websocket_ndt_msg(conn, type, msg, len);
  } else {
    return recv_msg_any(conn, type, msg, len);
  }
}