static switch_status_t rtmp_tcp_read(rtmp_session_t *rsession, unsigned char *buf, switch_size_t *len) { //rtmp_io_tcp_t *io = (rtmp_io_tcp_t*)rsession->profile->io; rtmp_tcp_io_private_t *io_pvt = rsession->io_private; switch_status_t status = SWITCH_STATUS_SUCCESS; #ifdef RTMP_DEBUG_IO switch_size_t olen = *len; #endif switch_assert(*len > 0 && *len < 1024000); do { status = switch_socket_recv(io_pvt->socket, (char*)buf, len); } while(status != SWITCH_STATUS_SUCCESS && SWITCH_STATUS_IS_BREAK(status)); #ifdef RTMP_DEBUG_IO { int i; fprintf(rsession->io_debug_in, "recv %p max=%"SWITCH_SIZE_T_FMT" got=%"SWITCH_SIZE_T_FMT"\n< ", (void*)buf, olen, *len); for (i = 0; i < *len; i++) { fprintf(rsession->io_debug_in, "%02X ", (uint8_t)buf[i]); if (i != 0 && i % 32 == 0) { fprintf(rsession->io_debug_in, "\n> "); } } fprintf(rsession->io_debug_in, "\n\n"); fflush(rsession->io_debug_in); } #endif return status; }
static JSBool socket_read_bytes(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { js_socket_obj_t *socket = JS_GetPrivate(cx, obj); if (socket == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to find js object.\n"); return JS_FALSE; } if (argc == 1) { int32 bytes_to_read; switch_status_t ret; switch_size_t len; JS_ValueToInt32(cx, argv[0], &bytes_to_read); len = (switch_size_t) bytes_to_read; if (socket->buffer_size < len) { socket->read_buffer = switch_core_alloc(socket->pool, len + 1); socket->buffer_size = bytes_to_read + 1; } socket->saveDepth = JS_SuspendRequest(cx); ret = switch_socket_recv(socket->socket, socket->read_buffer, &len); JS_ResumeRequest(cx, socket->saveDepth); if (ret != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "switch_socket_send failed: %d.\n", ret); *rval = BOOLEAN_TO_JSVAL(JS_FALSE); } else { socket->read_buffer[len] = 0; *rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, socket->read_buffer)); } } return JS_TRUE; }
static JSBool socket_read(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { js_socket_obj_t *socket = JS_GetPrivate(cx, obj); char *delimiter = "\n"; switch_status_t ret = SWITCH_STATUS_FALSE; switch_size_t len = 1; switch_size_t total_length = 0; int can_run = TRUE; char tempbuf[2]; if (socket == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to find js object.\n"); return JS_FALSE; } if (argc == 1) { delimiter = JS_GetStringBytes(JS_ValueToString(cx, argv[0])); } if (socket->read_buffer == 0) { socket->read_buffer = switch_core_alloc(socket->pool, socket->buffer_size); } socket->saveDepth = JS_SuspendRequest(cx); while (can_run == TRUE) { ret = switch_socket_recv(socket->socket, tempbuf, &len); if (ret != SWITCH_STATUS_SUCCESS) break; tempbuf[1] = 0; if (tempbuf[0] == delimiter[0]) break; else if (tempbuf[0] == '\r' && delimiter[0] == '\n') continue; else { // Buffer is full, let's increase it. if (total_length == socket->buffer_size - 1) { switch_size_t new_size = socket->buffer_size + 4196; char *new_buffer = switch_core_alloc(socket->pool, socket->buffer_size); memcpy(new_buffer, socket->read_buffer, total_length); socket->buffer_size = new_size; socket->read_buffer = new_buffer; } socket->read_buffer[total_length] = tempbuf[0]; ++total_length; } } JS_ResumeRequest(cx, socket->saveDepth); if (ret != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "socket receive failed: %d.\n", ret); *rval = BOOLEAN_TO_JSVAL(JS_FALSE); } else { socket->read_buffer[total_length] = 0; *rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, socket->read_buffer)); } return JS_TRUE; }
switch_status_t skinny_read_packet(listener_t *listener, skinny_message_t **req) { skinny_message_t *request; switch_size_t mlen, bytes = 0; char mbuf[SKINNY_MESSAGE_MAXSIZE] = ""; char *ptr; switch_status_t status = SWITCH_STATUS_SUCCESS; request = calloc(1,SKINNY_MESSAGE_MAXSIZE); if (!request) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to allocate memory.\n"); return SWITCH_STATUS_MEMERR; } ptr = mbuf; while (listener_is_ready(listener)) { uint8_t do_sleep = 1; if (listener->expire_time && listener->expire_time < switch_epoch_time_now(NULL)) { switch_safe_free(request); return SWITCH_STATUS_TIMEOUT; } if(bytes < SKINNY_MESSAGE_FIELD_SIZE) { /* We have nothing yet, get length header field */ mlen = SKINNY_MESSAGE_FIELD_SIZE - bytes; } else { /* We now know the message size */ mlen = request->length + 2*SKINNY_MESSAGE_FIELD_SIZE - bytes; } status = switch_socket_recv(listener->sock, ptr, &mlen); if (listener->expire_time && listener->expire_time < switch_epoch_time_now(NULL)) { switch_safe_free(request); return SWITCH_STATUS_TIMEOUT; } if (!listener_is_ready(listener)) { switch_safe_free(request); break; } if (!switch_status_is_timeup(status) && !SWITCH_STATUS_IS_BREAK(status) && (status != SWITCH_STATUS_SUCCESS)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Socket break with status=%d.\n", status); switch_safe_free(request); return SWITCH_STATUS_FALSE; } if(mlen) { bytes += mlen; if(bytes >= SKINNY_MESSAGE_FIELD_SIZE) { do_sleep = 0; ptr += mlen; memcpy(request, mbuf, bytes); #ifdef SKINNY_MEGA_DEBUG switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Got request: length=%d,version=%x,type=%x\n", request->length,request->version,request->type); #endif if(request->length < SKINNY_MESSAGE_FIELD_SIZE) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Skinny client sent invalid data. Length should be greater than 4 but got %d.\n", request->length); switch_safe_free(request); return SWITCH_STATUS_FALSE; } if(request->length + 2*SKINNY_MESSAGE_FIELD_SIZE > SKINNY_MESSAGE_MAXSIZE) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Skinny client sent too huge data. Got %d which is above threshold %d.\n", request->length, SKINNY_MESSAGE_MAXSIZE - 2*SKINNY_MESSAGE_FIELD_SIZE); switch_safe_free(request); return SWITCH_STATUS_FALSE; } if(bytes >= request->length + 2*SKINNY_MESSAGE_FIELD_SIZE) { /* Message body */ *req = request; /* Do not free here, caller needs to do it */ return SWITCH_STATUS_SUCCESS; } } } if (listener->digit_timeout_time && listener->digit_timeout_time < switch_mono_micro_time_now()) { switch_safe_free(request); return SWITCH_STATUS_TIMEOUT; } if (do_sleep) { switch_cond_next(); } } switch_safe_free(request); return SWITCH_STATUS_SUCCESS; }