/* @internal Function used to read content from the provided connection. */ int __myqtt_web_socket_receive (MyQttConn * conn, unsigned char * buffer, int buffer_len) { noPollConn * _conn; int result; MyQttCtx * ctx; MyQttMutex * mutex; /* check if the connection has the greetings completed and it * is initiator role */ _conn = myqtt_conn_get_data (conn, "__my:ws:conn"); ctx = conn->ctx; /* get mutex */ mutex = myqtt_conn_get_data (conn, "ws:mutex"); if (mutex == NULL) { myqtt_log (MYQTT_LEVEL_CRITICAL, "unable to find mutex to protect ssl object to read data"); return -1; } /* end if */ /* call to acquire mutex, read and release */ myqtt_mutex_lock (mutex); result = nopoll_conn_read (_conn, (char *) buffer, buffer_len, nopoll_false, 0); myqtt_mutex_unlock (mutex); if (result == -1) { /* check connection status to notify that no data was * available */ if (nopoll_conn_is_ok (_conn)) return -2; myqtt_log (MYQTT_LEVEL_CRITICAL, "Found noPollConn-id=%d (%p) read error myqtt conn-id=%d (session: %d), errno=%d (shutting down)", nopoll_conn_get_id (_conn), _conn, myqtt_conn_get_id (conn), myqtt_conn_get_socket (conn), errno); /* shutdown connection */ nopoll_conn_set_socket (_conn, -1); myqtt_conn_shutdown (conn); } /* end if */ return result; }
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; }
static int np_wt_read_request (void * args) { NpIOReq * req = (NpIOReq *)args; int rval = 0; trace(LOG_PROTOCOL, "read request size:%d",req->bufsz); while (rval == 0) { rval = nopoll_conn_read (req->np_sock, req->bufp, req->bufsz, nopoll_false, 10); if ((rval <= 0) && !nopoll_conn_is_ok(req->np_sock)) { rval = 0; /* Treat error as EOF */ break; } else if (rval < 0) { errno = ERR_OTHER; req->error_msg = loc_strdup("Failure reading socket"); break; } } trace(LOG_PROTOCOL, "read size:%d",rval); return rval; }