static int channel_np_wait_until_connection_ready(noPollConn * conn, int timeout, int is_ssl) {
        long int total_timeout = timeout;
        int socket = nopoll_conn_socket(conn);

        /* check if the connection already finished its connection
           handshake */
        do {
            int rc;
#if defined(__linux__)
            struct pollfd ufd;
            memset(&ufd, 0, sizeof ufd);
            ufd.fd = socket;
            ufd.events = POLLIN;
            rc = poll(&ufd, 1, 10 * 1000);
#else
            struct timeval tv;
            fd_set readfds;
            fd_set writefds;
            fd_set errorfds;
            FD_ZERO(&readfds);
            FD_ZERO(&writefds);
            FD_ZERO(&errorfds);
            FD_SET(socket, &readfds);

            tv.tv_usec = 10 * 1000;
            tv.tv_sec = 0;

            /* Wait for some event to occur on file descriptor */

            rc = select(socket + 1, &readfds, &writefds, &errorfds, &tv);
#endif

            if (rc == -1) break;

            /* For SSL connection, we need to call nopoll_conn_get_msg
             * in order to handle SSL accept. One may expect this is
             * done in nopoll_conn_is_ready()...
             */

            if (is_ssl) nopoll_conn_get_msg (conn);

            /* check if the connection is ok */
            if (! nopoll_conn_is_ok (conn)) 
                return nopoll_false;

            /* reduce the amount of time we have to wait. This computation
             * is not fully accurate but overall it is okay*/
            total_timeout = total_timeout - 10;
        } while (! nopoll_conn_is_ready (conn) && (total_timeout > 0));

        /* report if the connection is ok */
        return nopoll_conn_is_ready (conn);
}
/* following _wt_ functions are called from a worker thread so caution is required
 * to keep its operations thread safe
 */
static int np_wt_accept (void * args) {
    int rc;
    ServerNP * si = (ServerNP *)args;

    si->np_sock = nopoll_conn_accept (si->np_ctx, si->np_listener);
    if (si->np_sock == NULL) {
        /* nopoll library does not always set errno, in this case create 
         * a fake one.
         */
        if (errno == 0) errno = EINVAL;
        return -1;
    }

    (void) nopoll_conn_set_sock_block(nopoll_conn_socket(si->np_sock), nopoll_false);
    rc = channel_np_wait_until_connection_ready(si->np_sock, 1000, si->is_ssl);
    (void) nopoll_conn_set_sock_block(nopoll_conn_socket(si->np_sock), nopoll_true);

    if (rc == 0) {
        errno = EINVAL;
        return -1;
    }
    assert (rc);
    assert(nopoll_conn_is_ready(si->np_sock));
    return (si->np_sock != NULL);
}
static int np_wt_connect(void * args) {
    ChannelConnectInfo * info = (ChannelConnectInfo *)args;
    noPollConn * conn;
    
    if (info->is_ssl) {
        noPollConnOpts * opts = NULL;
        opts = nopoll_conn_opts_new ();
#ifdef _WRS_KERNEL
        /* For VxWorks SSL peer certificate verification does not work; let's
         * disable this for now.
         */
        nopoll_conn_opts_ssl_peer_verify (opts, nopoll_false);
#endif
	nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_TLSV1_1);
        conn = nopoll_conn_tls_new (info->np_ctx, opts, info->host, info->port, NULL, info->get_url, info->host_name, NULL);
    } else {
        conn = nopoll_conn_new (info->np_ctx, info->host, info->port, NULL, info->get_url, NULL, NULL);
    }

    if (! nopoll_conn_is_ok (conn)) {
        nopoll_conn_close(conn);
        errno = ECONNREFUSED;
        return -1;
    }

    /* nopoll_conn_wait_until_connection_ready() can return true even if
     * the connection is not ready but simply ok; no clue why. Let's check
     * again that the connection is ready.
     */

    if (! nopoll_conn_wait_until_connection_ready (conn, 10) || ! nopoll_conn_is_ready(conn)) {
        nopoll_conn_close(conn);
        errno = EPERM;
        return -1;
    }

    assert (nopoll_conn_is_ready (conn));
    assert (nopoll_conn_is_ok (conn));

    /* Set the socket in blocking mode */
    (void) nopoll_conn_set_sock_block(nopoll_conn_socket(conn), nopoll_true);

    info->np_sock = conn;
    return 0;
}
Пример #4
0
/** 
 * @brief Allows to create a new MQTT connection to a MQTT
 * broker/server running MQTT over WebSocket.
 *
 * @param ctx The context where the operation will take place.
 *
 * @param client_identifier The client identifier that uniquely
 * identifies this MQTT client from others.  It can be NULL to let
 * MQTT 3.1.1 servers to assign a default client identifier BUT
 * clean_session must be set to axl_true. This is done automatically
 * by the library (setting clean_session to axl_true when NULL is
 * provided).
 *
 * @param clean_session Flag to clean client session or to reuse the
 * existing one. If set to axl_false, you must provide a valid
 * client_identifier (otherwise the function will fail).
 *
 * @param keep_alive Keep alive configuration in seconds after which
 * the server/broker will close the connection if it doesn't detect
 * any activity. Setting 0 will disable keep alive mechanism.
 *
 * @param conn A reference to an established noPollConn connection
 * that is connecting to the MQTT over WebSocket server we want to
 * connect to. 
 *
 * @param opts Optional connection options. See \ref myqtt_conn_opts_new
 *
 * @param on_connected Async notification handler that will be called
 * once the connection fails or it is completed. In the case this
 * handler is configured the caller will not be blocked. In the case
 * this parameter is NULL, the caller will be blocked until the
 * connection completes or fails.
 *
 * @param user_data User defined pointer that will be passed to the on_connected handler (in case it is defined).
 *
 * @return A reference to the newli created connection or NULL if
 * on_connected handler is provided. In both cases, the reference
 * returned (or received at the on_connected handler) must be checked
 * with \ref myqtt_conn_is_ok. 
 *
 * <b>About pending messages / queued messages </b>
 *
 * After successful connection with clean_session set to axl_false and
 * defined client identifier, the library will resend any queued or in
 * flight QoS1/QoS2 messages (as well as QoS0 if they were
 * stored). This is done in background without intefering the caller.
 *
 * If you need to get the number of queued messages that are going to
 * be sent use \ref myqtt_storage_queued_messages_offline. In the case
 * you need the number remaining during the process use \ref
 * myqtt_storage_queued_messages.
 *
 * See \ref myqtt_conn_new for more information.
 *
 * <b>About reconnecting</b>
 *
 * If you enable automatic reconnect support after connection close
 * (\ref myqtt_conn_opts_set_reconnect), remember to also configure
 * the recover handler by using \ref myqtt_conn_opts_set_init_session_setup_ptr. That function should
 * implement a complete reconnect and return a noPollConn reference
 * used by the internal session setup. If you don't configure this,
 * the function will disable reconnect support even if you enable it.
 *
 */
MyQttConn        * myqtt_web_socket_conn_new            (MyQttCtx        * ctx,
							 const char      * client_identifier,
							 axl_bool          clean_session,
							 int               keep_alive,
							 noPollConn      * conn,
							 MyQttConnOpts   * opts,
							 MyQttConnNew      on_connected, 
							 axlPointer        user_data)
{

	/* check if the conn reference is not defined. In that case,
	   try to craete it with the init session setup ptr */
	if (conn == NULL && opts && opts->init_session_setup_ptr)
		conn = opts->init_session_setup_ptr (ctx, NULL, opts->init_user_data, opts->init_user_data2, opts->init_user_data3);

	/* report what we are doing */
	myqtt_log (MYQTT_LEVEL_DEBUG, "Creating new MQTT over WebSocket connection to %s:%s (is ready:%d, socket: %d)",
		   nopoll_conn_host (conn), nopoll_conn_port (conn), nopoll_conn_is_ready (conn), nopoll_conn_socket (conn));

	/* check and disable reconnect if it is not configured the
	   recover handler */
	if (opts && opts->reconnect) {
		if (opts->init_session_setup_ptr == NULL) {
			myqtt_log (MYQTT_LEVEL_CRITICAL, "Disable reconnect flag because user didn't provide a recover handler (myqtt_conn_opts_set_recover_session_setup_ptr)");
			opts->reconnect = axl_false; /* disable it */
		} /* end if */
	} /* end opts */

	/* associate context */
	__myqtt_web_socket_associate_ctx (ctx, nopoll_conn_ctx (conn));

	/* call to create the connection */
	return myqtt_conn_new_full_common (ctx, client_identifier, clean_session, keep_alive, 
					   /* destination host and port but only as a matter of reporting 
					      because we are handling everything through the setup handler */
					   nopoll_conn_host (conn), nopoll_conn_port (conn), 
					   /* this is the handler that will establish the connection on top
					      of the provided noPoll connection */
					   __myqtt_web_socket_session_setup, conn, 
					   /* additional user handlers */
					   on_connected, -1, opts, user_data);
}
Пример #5
0
nopoll_bool test_sending_and_check_echo (noPollConn * conn, const char * label, const char * msg)
{
  char  buffer[1024];
  int   length = strlen (msg);
  int   bytes_read;

  /* wait for the reply */
  while (nopoll_true) {
    if (nopoll_conn_is_ready (conn))
      break;
    nopoll_sleep (10000);
  } /* end if */

  /* send content text(utf-8) */
  printf ("%s: sending content..\n", label);
  if (nopoll_conn_send_text (conn, msg, length) != length) {
    printf ("ERROR: Expected to find proper send operation..\n");
    return nopoll_false;
  }

  /* wait for the reply (try to read 1024, blocking and with a 3 seconds timeout) */
  bytes_read = nopoll_conn_read (conn, buffer, length, nopoll_true, 3000);
  if (bytes_read > 0)
    buffer[bytes_read] = 0;

  if (bytes_read != length) {
    printf ("ERROR: expected to find 14 bytes but found %d..\n", bytes_read);
    return nopoll_false;
  } /* end if */

  /* check content received */
  if (! nopoll_cmp (buffer, msg)) {
    printf ("ERROR: expected to find message 'This is a test' but something different was received: '%s'..\n",
	    buffer);
    return nopoll_false;
  } /* end if */

  printf ("%s: received reply and echo matches..\n", label);

  /* return that we sent and received the echo reply */
  return nopoll_true;
}
Пример #6
0
void*          webSocketery()
{
  noPollCtx			*ctx = nopoll_ctx_new();
  noPollMsg			*msg;
  noPollRole			*role = nopoll_conn_role(NOPOLL_ROLE_UNKNOWN);
  struct noPollHandshake	*fetch;
  noPollConnOpts		*opts;

  /* Comment the log lines to disable loging Debug Warnings and Critical errors (Better not)*/
  //nopoll_log_color_enable(ctx, true);
  //nopoll_log_enable(ctx, nopoll_true);
  /* Initializing the cookie options */
  opts = nopoll_conn_opts_new();

  if (!ctx)
    puts("error ctx is nill");
 
  //To add Cookies use this method below
  /* nopoll_conn_opts_set_cookie(opts, "BAYEUX_BROWSER=56a9-mchhnynonz6ji8a6hs1sh49; JSESSIONID=8gz8e00htqrl15vcm3o9yi95f"); */

  // Websocketery Works for mtgox and others servers but not for m.zpush.ovh it keeps rejecting me for an unknown f*****g 400 error ! Use Methods below to connect to server, a working example is provided
  //nopoll_conn_new(ctx, ip, port, host, get, protocols, origin)    nopoll_conn_new_opts(ctx, opts, ip, port, host, get, protocols, origin)// 
  

  //noPollConn *conn = nopoll_conn_new(ctx , "54.171.156.38" ,"80" ,"m.zpush.ovh:8080" ,"ws://m.zpush.ovh:8080/str/strd" ,NULL, "Authorize");
  noPollConn *conn = nopoll_conn_new(ctx, "54.238.149.121", "80", "websocket.mtgox.com", "/mtgox", NULL, "chat");
  
  if (!nopoll_conn_wait_until_connection_ready(conn, 50) )
    { puts("nopoll_conn failed, timeout"); return (0);}
  if (nopoll_conn_send_text (conn, "hello how are you doing, do we connect ?", 40) != 40)
    {puts("send text just failed...."); return(0);}
  else
    {
      while (! nopoll_conn_is_ready (conn)) {

	if (! nopoll_conn_is_ok (conn)) {
	  printf ("ERROR (4.1 jkd412): expected to find proper connection handshake finished, but found connection is broken: session=%d, errno=%d : %s..\n",
		  (int) nopoll_conn_socket (conn), errno, strerror (errno));
	  return nopoll_false;
	} /* end if */

	/* wait a bit 10ms */
	nopoll_sleep (10000);
      } /* end if */
      nopoll_conn_close (conn);

      /* finish */
      nopoll_ctx_unref (ctx);

      puts("nopoll conn sucess");
      while (nopoll_true)
	{
	  if (nopoll_conn_is_ready(conn))
	    {
	      puts("break");
	      break;
	    }
	  nopoll_sleep(10000);
	}
	  msg = nopoll_conn_get_msg(conn);
	  if (msg)
	    printf("Msg received = %s\n", nopoll_msg_get_payload(msg));
	    
	  if (!nopoll_conn_is_ok(conn))
	    {
	      puts("------------ Connection Dead ----------------");
	      return nopoll_false;
	    }
	
    }
    nopoll_ctx_unref(ctx);
    return (0);
}