static void * handle_packets(void *arg) { #ifdef _WIN32 SOCKET *fdp; #else int *fdp; #endif char *buf; ssize_t length; #ifdef _WIN32 fdp = (SOCKET *)arg; #else fdp = (int *)arg; #endif if ((buf = (char *)malloc(MAX_PACKET_SIZE)) == NULL) { return (NULL); } for (;;) { length = recv(*fdp, buf, MAX_PACKET_SIZE, 0); if (length > 0) { usrsctp_conninput(fdp, buf, (size_t)length, 0); } } free(buf); return (NULL); }
static void * handle_packets(void *arg) { #ifdef _WIN32 SOCKET *fdp; #else int *fdp; #endif char *dump_buf; ssize_t length; char buf[MAX_PACKET_SIZE]; #ifdef _WIN32 fdp = (SOCKET *)arg; #else fdp = (int *)arg; #endif for (;;) { length = recv(*fdp, buf, MAX_PACKET_SIZE, 0); if (length > 0) { if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) { fprintf(stderr, "%s", dump_buf); usrsctp_freedumpbuffer(dump_buf); } usrsctp_conninput(fdp, buf, (size_t)length, 0); } } return (NULL); }
gpointer sctp_thread(gpointer user_data) { struct rtcdc_peer_connection *peer = (struct rtcdc_peer_connection *)user_data; struct rtcdc_transport *transport = peer->transport; struct ice_transport *ice = transport->ice; struct dtls_transport *dtls = transport->dtls; struct sctp_transport *sctp = transport->sctp; while (!peer->exit_thread && !ice->negotiation_done) g_usleep(2500); if (peer->exit_thread) return NULL; while (!peer->exit_thread && !dtls->handshake_done) g_usleep(2500); if (peer->exit_thread) return NULL; char buf[BUFFER_SIZE]; while (!peer->exit_thread) { if (BIO_ctrl_pending(sctp->incoming_bio) <= 0 && BIO_ctrl_pending(sctp->outgoing_bio) <= 0) g_usleep(2500); if (BIO_ctrl_pending(sctp->incoming_bio) > 0) { g_mutex_lock(&sctp->sctp_mutex); int nbytes = BIO_read(sctp->incoming_bio, buf, sizeof buf); g_mutex_unlock(&sctp->sctp_mutex); #ifdef DEBUG_SCTP send(sctp->incoming_stub, buf, nbytes, 0); #endif if (nbytes > 0) { usrsctp_conninput(sctp, buf, nbytes, 0); } } if (BIO_ctrl_pending(sctp->outgoing_bio) > 0) { g_mutex_lock(&sctp->sctp_mutex); int nbytes = BIO_read(sctp->outgoing_bio, buf, sizeof buf); g_mutex_unlock(&sctp->sctp_mutex); #ifdef DEBUG_SCTP send(sctp->outgoing_stub, buf, nbytes, 0); #endif if (nbytes > 0) { g_mutex_lock(&dtls->dtls_mutex); SSL_write(dtls->ssl, buf, nbytes); g_mutex_unlock(&dtls->dtls_mutex); } } } return NULL; }
/* * Class: net_tomp2p_sctp_core_Sctp * Method: on_network_in * Signature: (J[BII)V */ JNIEXPORT void JNICALL Java_net_tomp2p_sctp_core_Sctp_on_1network_1in (JNIEnv *env, jclass clazz, jlong ptr, jbyteArray pkt, jint off, jint len) { jbyte *pkt_; pkt_ = (*env)->GetByteArrayElements(env, pkt, NULL); if (pkt_) { usrsctp_conninput( (void *) (intptr_t) ptr, pkt_ + off, len, /* ecn_bits */ 0); (*env)->ReleaseByteArrayElements(env, pkt, pkt_, JNI_ABORT); } }
static DWORD WINAPI #else static void * #endif handle_packets(void *arg) { #ifdef _WIN32 SOCKET *fdp; #else int *fdp; #endif char *dump_buf; ssize_t length; char buf[MAX_PACKET_SIZE]; #ifdef _WIN32 fdp = (SOCKET *)arg; #else fdp = (int *)arg; #endif for (;;) { #if defined(__NetBSD__) pthread_testcancel(); #endif length = recv(*fdp, buf, MAX_PACKET_SIZE, 0); if (length > 0) { if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) { fprintf(stderr, "%s", dump_buf); usrsctp_freedumpbuffer(dump_buf); } usrsctp_conninput(fdp, buf, (size_t)length, 0); } } #ifdef _WIN32 return 0; #else return (NULL); #endif }
static void * handle_packets(void *arg) { #ifdef _WIN32 SOCKET *fdp; #else int *fdp; #endif ssize_t length; char buf[MAX_PACKET_SIZE]; #ifdef _WIN32 fdp = (SOCKET *)arg; #else fdp = (int *)arg; #endif for (;;) { length = recv(*fdp, buf, MAX_PACKET_SIZE, 0); if (length > 0) { usrsctp_conninput(fdp, buf, (size_t)length, 0); } } return (NULL); }
void *janus_sctp_thread(void *data) { janus_sctp_association *sctp = (janus_sctp_association *)data; if(sctp == NULL) { JANUS_LOG(LOG_ERR, "Invalid SCTP association, closing thread\n"); g_thread_unref(g_thread_self()); return NULL; } JANUS_LOG(LOG_INFO, "[%"SCNu64"] Starting thread for SCTP association\n", sctp->handle_id); janus_sctp_message *message = NULL; gboolean sent_data = FALSE; while(sctp->dtls != NULL && sctp_running) { /* Anything to do at all? */ janus_mutex_lock(&sctp->mutex); if(!sctp->ready || (g_queue_is_empty(sctp->in_messages) && g_queue_is_empty(sctp->out_messages))) { janus_mutex_unlock(&sctp->mutex); g_usleep (10000); continue; } /* Check incoming messages */ if(!g_queue_is_empty(sctp->in_messages) && sent_data) { while(!g_queue_is_empty(sctp->in_messages)) { message = g_queue_pop_head(sctp->in_messages); if(message == NULL) continue; #ifdef DEBUG_SCTP if(sctp->debug_dump != NULL) { /* Dump incoming message */ char *dump = usrsctp_dumppacket(message->buffer, message->length, SCTP_DUMP_INBOUND); if(dump != NULL) { fwrite(dump, sizeof(char), strlen(dump), sctp->debug_dump); fflush(sctp->debug_dump); usrsctp_freedumpbuffer(dump); } } #endif /* Pass this data to the SCTP association */ janus_mutex_unlock(&sctp->mutex); usrsctp_conninput((void *)sctp, message->buffer, message->length, 0); janus_mutex_lock(&sctp->mutex); janus_sctp_message_destroy(message); message = NULL; } } /* Check outgoing messages */ if(sctp->dtls == NULL) { /* No DTLS stack anymore, we're done */ janus_mutex_unlock(&sctp->mutex); break; } if(!g_queue_is_empty(sctp->out_messages)) { while(!g_queue_is_empty(sctp->out_messages)) { message = g_queue_pop_head(sctp->out_messages); if(message == NULL) continue; #ifdef DEBUG_SCTP if(sctp->debug_dump != NULL) { /* Dump incoming message */ char *dump = usrsctp_dumppacket(message->buffer, message->length, SCTP_DUMP_OUTBOUND); if(dump != NULL) { fwrite(dump, sizeof(char), strlen(dump), sctp->debug_dump); fflush(sctp->debug_dump); usrsctp_freedumpbuffer(dump); } } #endif /* Encapsulate this data in DTLS and send it */ janus_dtls_send_sctp_data((janus_dtls_srtp *)sctp->dtls, message->buffer, message->length); janus_sctp_message_destroy(message); message = NULL; if(!sent_data) sent_data = TRUE; } } janus_mutex_unlock(&sctp->mutex); } JANUS_LOG(LOG_INFO, "[%"SCNu64"] Leaving SCTP association thread\n", sctp->handle_id); /* This association has been destroyed, wait a bit and then free all the resources */ g_usleep (1*G_USEC_PER_SEC); g_queue_free(sctp->in_messages); sctp->in_messages = NULL; g_queue_free(sctp->out_messages); sctp->out_messages = NULL; sctp->thread = NULL; #ifdef DEBUG_SCTP if(sctp->debug_dump != NULL) fclose(sctp->debug_dump); sctp->debug_dump = NULL; #endif g_free(sctp); sctp = NULL; g_thread_unref(g_thread_self()); return NULL; }