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;
}
Beispiel #2
0
int create_client(const char * host, const char * port, int duration)
{
    int rc = 0;

    // Create noPoll context
    noPollCtx * ctx = nopoll_ctx_new ();
    if (!ctx) {
        LogPrintf("Could not create context");
        return 1;
    }
    if (debug_nopoll) {
       nopoll_log_set_handler(ctx, noPollLogger, NULL);
    }

    // Create connection
    uint64_t begin_time = GetTime();
    noPollConn * conn = nopoll_conn_new (ctx, host, port, NULL, NULL, NULL, NULL);
    if (!nopoll_conn_is_ok(conn)) {
        LogPrintf("Failed setting up connection to %s:%s", host, port);
        return 1;
    }

    // Wait till connected
    if (!nopoll_conn_wait_until_connection_ready (conn, 120)) {
        LogPrintf("Could not get connected to %s:%s within 120 seconds. Failed in %d ms",
                host, port, (int)(GetTime() - begin_time));
        return 1;
    }
    LogPrintf("Connected in %d ms.", (int)(GetTime() - begin_time));

    // Don't make any requests. Just sleep for given duration and exit
    // sleep(duration);
    // LogPrintf("Exiting...");
    // return 0;

    // Start the client's request/response loop
    char message[32];
    int messageCount = 0;
    begin_time = GetTime();
    while (true) {
        // Send request
        int requestLen = (rand() % 32000) + 1;
        int requestCount = (rand() % 64) + 1;
        DebugPrintf("Requesting %d messages of %d bytes each [%d]", requestCount, requestLen, messageCount);
        int len = sprintf(message, "Get %d %d", requestLen, requestCount);
        if (nopoll_conn_send_text(conn, message, len) != len) {
            rc = 1;
            LogPrintf("Could not send message: %d", errno);
            break;
        }

        // Collect responses
        for (int index = 0; index < requestCount; index++) {
            if (getResponse(conn, requestLen, index) < 0) {
                rc = 1;
                break;
            }
            messageCount++;
        }
        if (rc == 1)
            break;

        if ((int)(GetTime() - begin_time) > duration * 1000)
            break;
    }

    LogPrintf("Exited after %d ms. Received %d messages.",
            (int)(GetTime() - begin_time), messageCount);

    if (rc == 1) {
        LogPrintf("Error: Client Failed.");
    }
    nopoll_ctx_unref(ctx);
    return rc;
}
Beispiel #3
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);
}