forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add back the tox_loop
implementation for low latency.
#1821
Open
iphydf
wants to merge
1
commit into
TokTok:master
Choose a base branch
from
iphydf:tox-loop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#include <pthread.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
#include "../toxcore/tox.h" | ||
|
||
#include "check_compat.h" | ||
#include "../testing/misc_tools.h" | ||
|
||
/* The CI containers respond poorly to ::1 as a localhost address | ||
* You're encouraged to -D FORCE_TESTS_IPV6 on a local test */ | ||
#ifdef TOX_LOCALHOST | ||
#undef TOX_LOCALHOST | ||
#endif | ||
#ifdef FORCE_TESTS_IPV6 | ||
#define TOX_LOCALHOST "::1" | ||
#else | ||
#define TOX_LOCALHOST "127.0.0.1" | ||
#endif | ||
|
||
#ifdef TCP_RELAY_PORT | ||
#undef TCP_RELAY_PORT | ||
#endif | ||
#define TCP_RELAY_PORT 33431 | ||
|
||
typedef struct Loop_Test { | ||
int start_count; | ||
int stop_count; | ||
pthread_mutex_t mutex; | ||
Tox *tox; | ||
} Loop_Test; | ||
|
||
static void tox_loop_cb_start(Tox *tox, void *data) | ||
{ | ||
Loop_Test *userdata = (Loop_Test *)data; | ||
pthread_mutex_lock(&userdata->mutex); | ||
++userdata->start_count; | ||
} | ||
|
||
static void tox_loop_cb_stop(Tox *tox, void *data) | ||
{ | ||
Loop_Test *userdata = (Loop_Test *)data; | ||
++userdata->stop_count; | ||
pthread_mutex_unlock(&userdata->mutex); | ||
} | ||
|
||
static void *tox_loop_worker(void *data) | ||
{ | ||
Loop_Test *userdata = (Loop_Test *)data; | ||
Tox_Err_Loop err; | ||
tox_loop(userdata->tox, userdata, &err); | ||
ck_assert_msg(err == TOX_ERR_LOOP_OK, "tox_loop error: %d", err); | ||
return nullptr; | ||
} | ||
|
||
static void test_tox_loop(void) | ||
{ | ||
pthread_t worker, worker_tcp; | ||
Tox_Err_Options_New err_opts; | ||
struct Tox_Options *opts = tox_options_new(&err_opts); | ||
ck_assert_msg(err_opts == TOX_ERR_OPTIONS_NEW_OK, "tox_options_new: %d\n", err_opts); | ||
tox_options_set_experimental_thread_safety(opts, true); | ||
|
||
Loop_Test *userdata = (Loop_Test *)calloc(1, sizeof(Loop_Test)); | ||
ck_assert(userdata != nullptr); | ||
uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; | ||
|
||
userdata->start_count = 0; | ||
userdata->stop_count = 0; | ||
pthread_mutex_init(&userdata->mutex, nullptr); | ||
|
||
tox_options_set_tcp_port(opts, TCP_RELAY_PORT); | ||
Tox_Err_New err_new; | ||
userdata->tox = tox_new(opts, &err_new); | ||
ck_assert_msg(err_new == TOX_ERR_NEW_OK, "tox_new: %d\n", err_new); | ||
tox_callback_loop_begin(userdata->tox, tox_loop_cb_start); | ||
tox_callback_loop_end(userdata->tox, tox_loop_cb_stop); | ||
pthread_create(&worker, nullptr, tox_loop_worker, userdata); | ||
|
||
tox_self_get_dht_id(userdata->tox, dpk); | ||
|
||
tox_options_default(opts); | ||
tox_options_set_experimental_thread_safety(opts, true); | ||
Loop_Test userdata_tcp; | ||
userdata_tcp.start_count = 0; | ||
userdata_tcp.stop_count = 0; | ||
pthread_mutex_init(&userdata_tcp.mutex, nullptr); | ||
userdata_tcp.tox = tox_new(opts, &err_new); | ||
ck_assert_msg(err_new == TOX_ERR_NEW_OK, "tox_new: %d\n", err_new); | ||
tox_callback_loop_begin(userdata_tcp.tox, tox_loop_cb_start); | ||
tox_callback_loop_end(userdata_tcp.tox, tox_loop_cb_stop); | ||
pthread_create(&worker_tcp, nullptr, tox_loop_worker, &userdata_tcp); | ||
|
||
pthread_mutex_lock(&userdata_tcp.mutex); | ||
Tox_Err_Bootstrap error; | ||
ck_assert_msg(tox_add_tcp_relay(userdata_tcp.tox, TOX_LOCALHOST, TCP_RELAY_PORT, dpk, &error), "Add relay error, %i", | ||
error); | ||
ck_assert_msg(tox_bootstrap(userdata_tcp.tox, TOX_LOCALHOST, 33445, dpk, &error), "Bootstrap error, %i", error); | ||
pthread_mutex_unlock(&userdata_tcp.mutex); | ||
|
||
c_sleep(1000); | ||
|
||
tox_loop_stop(userdata->tox); | ||
void *retval = nullptr; | ||
pthread_join(worker, &retval); | ||
ck_assert_msg((uintptr_t)retval == 0, "tox_loop didn't return 0"); | ||
|
||
tox_kill(userdata->tox); | ||
ck_assert_msg(userdata->start_count == userdata->stop_count, "start and stop must match (start = %d, stop = %d)", | ||
userdata->start_count, userdata->stop_count); | ||
|
||
tox_loop_stop(userdata_tcp.tox); | ||
pthread_join(worker_tcp, &retval); | ||
ck_assert_msg((uintptr_t)retval == 0, "tox_loop didn't return 0"); | ||
|
||
tox_kill(userdata_tcp.tox); | ||
ck_assert_msg(userdata_tcp.start_count == userdata_tcp.stop_count, "start and stop must match (start = %d, stop = %d)", | ||
userdata_tcp.start_count, userdata_tcp.stop_count); | ||
|
||
tox_options_free(opts); | ||
free(userdata); | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
test_tox_loop(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#!/bin/bash | ||
|
||
run | ||
run "$@" | ||
#run -DVANILLA_NACL -I/usr/include/sodium "$@" | ||
run -DHAVE_LIBEV "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
options should have a prefix like TOX_ or TOXCORE_