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);
}
Exemplo n.º 2
0
/********** Client Side Code **********/
int getResponse(noPollConn * conn, int requestLen, int index) {
    noPollMsg * msg;
    int count = 0;
    uint64_t beginTime = GetTime();
    int msglen = 0;
    while (true) {
        count++;
        msg = nopoll_conn_get_msg(conn);
        if (msg != NULL) {
            noPollOpCode code = nopoll_msg_opcode(msg);
            if (!((code == NOPOLL_BINARY_FRAME) || (code == NOPOLL_CONTINUATION_FRAME))) {
                LogPrintf("Received unexpected message type %d", code);
                return -1;
            }
            msglen += nopoll_msg_get_payload_size(msg);
            DebugPrintf("Retrieved message %d frag %d:%d of size %d (collected %d of %d)",
                index, nopoll_msg_is_fragment(msg), nopoll_msg_is_final(msg),
                    nopoll_msg_get_payload_size(msg), msglen, requestLen);
            if (nopoll_msg_is_final(msg) && (msglen != requestLen)) {
                DebugPrintf("Final flag set arbitrarily??");
            }
            if (msglen == requestLen) {
                if (!nopoll_msg_is_final(msg))
                    DebugPrintf("Final flag not set??");
                break;
            }
            nopoll_msg_unref(msg);
        }
        if (!nopoll_conn_is_ok(conn)) {
            LogPrintf("Client disconnected!!");
            return -1;
        }
        nopoll_sleep(100);
    }

    DebugPrintf("Retrieved full message %d in %d ms looped: %d",
            index, (int) (GetTime() - beginTime), count);
    if (msglen != requestLen) {
        LogPrintf("Received invalid response length %d != %d", msglen, requestLen);
        return -1;
    }
    nopoll_msg_unref(msg);
    return 0;
}
Exemplo n.º 3
0
/** 
 * @internal Function used to handle incoming data from from the
 * connection and to notify this data on the connection.
 */
void nopoll_loop_process_data (noPollCtx * ctx, noPollConn * conn)
{
	noPollMsg * msg;

	/* call to get messages from the connection */
	msg = nopoll_conn_get_msg (conn);
	if (msg == NULL)
		return;

	/* found message, notify it */
	if (conn->on_msg) 
		conn->on_msg (ctx, conn, msg, conn->on_msg_data);
	else if (ctx->on_msg)
		ctx->on_msg (ctx, conn, msg, ctx->on_msg_data);

	/* release message */
	nopoll_msg_unref (msg);
	return;
}
Exemplo n.º 4
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);
}