void t03_websocket_server_receive_small_packet() { INIT_LOCAL(); int length = 0; char *buffer = NULL, buffer2[115]; memset(&ws_status, 0, sizeof(ws_status)); onion *o = websocket_server_new(); onion_request *req = websocket_start_handshake(o); req->connection.listen_point->read = (lpreader_sig_t *) websocket_data_buffer_read; onion_response *res = onion_response_new(req); onion_websocket *ws = onion_websocket_new(req, res); length = websocket_forge_small_packet((char **)&buffer); websocket_data_buffer_write(req, buffer, length); onion_websocket_read(ws, (char *)&buffer2, 120); buffer2[114] = '\0'; FAIL_IF_NOT_EQUAL_STR(buffer2, "Some UTF-8-encoded chars which will be cut at the 117th char so I write some gap-filling text with no meaning unti"); onion_websocket_free(ws); onion_request_free(req); onion_free(o); END_LOCAL(); }
onion_connection_status websocket_example_cont(void *data, onion_websocket *ws, ssize_t data_ready_len){ char tmp[256]; if (data_ready_len>sizeof(tmp)) data_ready_len=sizeof(tmp)-1; int len=onion_websocket_read(ws, tmp, data_ready_len); if (len<=0){ ONION_ERROR("Error reading data: %d: %s (%d)", errno, strerror(errno), data_ready_len); return OCS_NEED_MORE_DATA; } tmp[len]=0; onion_websocket_printf(ws, "Echo: %s", tmp); ONION_INFO("Read from websocket: %d: %s", len, tmp); return OCS_NEED_MORE_DATA; }