Skip to content

Commit

Permalink
feat: Add binary packing functions for tox events.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 7, 2022
1 parent 9218566 commit a84c8bc
Show file tree
Hide file tree
Showing 48 changed files with 831 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
git
libconfig-dev
libgtest-dev
libmsgpack-dev
libopus-dev
libsodium-dev
libvpx-dev
Expand Down Expand Up @@ -79,7 +80,7 @@ jobs:
toxcore/*/*.c
toxencryptsave/*.c
-lpthread
$(pkg-config --cflags --libs libsodium opus vpx)
$(pkg-config --cflags --libs libsodium msgpack opus vpx)

static-analysis:
working_directory: ~/work
Expand Down
7 changes: 6 additions & 1 deletion .github/scripts/cmake-linux
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ set -eu

NPROC=$(nproc)

sudo apt-get install -y --no-install-recommends libopus-dev libsodium-dev libvpx-dev ninja-build
sudo apt-get install -y --no-install-recommends \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
ninja-build

. ".github/scripts/flags-$CC.sh"

Expand Down
7 changes: 6 additions & 1 deletion .github/scripts/cmake-osx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ NPROC=$(sysctl -n hw.physicalcpu)
# Workaround for bug in Homebrew where it only finds an old Ruby version.
brew update

brew install libconfig libsodium libvpx opus
brew install \
libconfig \
libsodium \
libvpx \
msgpack \
opus

. ".github/scripts/flags-clang.sh"

Expand Down
2 changes: 1 addition & 1 deletion .github/scripts/cmake-win32
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

i686=true
x86_64=false
Expand Down
2 changes: 1 addition & 1 deletion .github/scripts/cmake-win64
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

i686=false
x86_64=true
Expand Down
4 changes: 1 addition & 3 deletions .github/scripts/cmake-windows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ set -eu
NPROC=$(nproc)

ci_install() {
cd other/docker/windows

docker build \
--build-arg SUPPORT_ARCH_i686="$i686" \
--build-arg SUPPORT_ARCH_x86_64="$x86_64" \
--build-arg SUPPORT_TEST=true \
-t toxcore-"$WINDOWS_ARCH" \
.
other/docker/windows
}

ci_script() {
Expand Down
8 changes: 7 additions & 1 deletion .github/scripts/sonar-prepare
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

set -eu

sudo apt-get install -y --no-install-recommends libconfig-dev libopus-dev libsodium-dev libvpx-dev ninja-build
sudo apt-get install -y --no-install-recommends \
libconfig-dev \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
ninja-build

. ".github/scripts/flags-gcc.sh"

Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ jobs:
sudo apt-get install -y --no-install-recommends
tcc
libconfig-dev
libmsgpack-dev
libopus-dev
libsodium-dev
libvpx-dev
- name: Build with TCC
run:
tcc
-Dinline=static
-o send_message_test
-Wall -Werror
-bench -g
Expand All @@ -111,7 +113,7 @@ jobs:
toxcore/*.c
toxcore/*/*.c
toxencryptsave/*.c
$(pkg-config --cflags --libs libsodium opus vpx)
$(pkg-config --cflags --libs libsodium msgpack opus vpx)
- name: Run the test
run: "./send_message_test | grep 'tox clients connected'"
- name: Build amalgamation file with TCC
Expand All @@ -124,7 +126,7 @@ jobs:
-o send_message_test
-Wall -Werror
-bench -g
$(pkg-config --cflags --libs libsodium opus vpx)
$(pkg-config --cflags --libs libsodium msgpack opus vpx)
- name: Run the test again
run: "./send_message_test | grep 'tox clients connected'"

Expand All @@ -149,8 +151,8 @@ jobs:
toxcore/*.c
toxcore/*/*.c
toxencryptsave/*.c
-D__COMPCERT__ -DDISABLE_VLA
-lpthread $(pkg-config --cflags --libs libsodium opus vpx)
-D__COMPCERT__ -DDISABLE_VLA -Dinline=
-lpthread $(pkg-config --cflags --libs libsodium msgpack opus vpx)
- name: Run the test
run: "./send_message_test | grep 'tox clients connected'"

Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/tox_events.c
toxcore/tox_events.h)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS} ${toxcore_SOURCE_DIR}/toxcore/tox_events.h^tox)
set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${MSGPACK_LIBRARIES})
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} msgpack)

# LAYER 10: Dispatch recorded events to callbacks.
# -------------------
Expand Down
13 changes: 13 additions & 0 deletions auto_tests/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ TESTS = \
set_name_test \
set_status_message_test \
TCP_test \
tox_events_test \
tox_dispatch_test \
tox_many_tcp_test \
tox_many_test \
tox_one_test \
Expand All @@ -49,16 +51,19 @@ endif

AUTOTEST_CFLAGS = \
$(LIBSODIUM_CFLAGS) \
$(MSGPACK_CFLAGS) \
$(NACL_CFLAGS)

AUTOTEST_LDADD = \
$(LIBSODIUM_LDFLAGS) \
$(MSGPACK_LDFLAGS) \
$(NACL_LDFLAGS) \
libmisc_tools.la \
libauto_test_support.la \
libtoxcore.la \
libtoxencryptsave.la \
$(LIBSODIUM_LIBS) \
$(MSGPACK_LIBS) \
$(NACL_OBJECTS) \
$(NACL_LIBS)

Expand Down Expand Up @@ -198,6 +203,14 @@ TCP_test_SOURCES = ../auto_tests/TCP_test.c
TCP_test_CFLAGS = $(AUTOTEST_CFLAGS)
TCP_test_LDADD = $(AUTOTEST_LDADD)

tox_events_test_SOURCES = ../auto_tests/tox_events_test.c
tox_events_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_events_test_LDADD = $(AUTOTEST_LDADD)

tox_dispatch_test_SOURCES = ../auto_tests/tox_dispatch_test.c
tox_dispatch_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_dispatch_test_LDADD = $(AUTOTEST_LDADD)

tox_many_tcp_test_SOURCES = ../auto_tests/tox_many_tcp_test.c
tox_many_tcp_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_many_tcp_test_LDADD = $(AUTOTEST_LDADD)
Expand Down
41 changes: 35 additions & 6 deletions auto_tests/tox_dispatch_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "../toxcore/tox_events.h"
#include "check_compat.h"

// Set to true to produce an msgpack file at /tmp/test.mp.
static const bool want_dump_events = false;

static void handle_events_friend_message(Tox *tox, const Tox_Event_Friend_Message *event, void *user_data)
{
bool *success = (bool *)user_data;
Expand All @@ -24,17 +27,43 @@ static void handle_events_friend_message(Tox *tox, const Tox_Event_Friend_Messag
*success = true;
}

static void dump_events(const char *path, const Tox_Events *events)
{
if (want_dump_events) {
FILE *fh = fopen(path, "w");
ck_assert(fh != nullptr);
const uint32_t len = tox_events_bytes_size(events);
uint8_t *buf = (uint8_t *)malloc(len);
ck_assert(buf != nullptr);
tox_events_get_bytes(events, buf);
fwrite(buf, 1, len, fh);
free(buf);
fclose(fh);
}
}

static void print_events(Tox_Events *events)
{
if (tox_events_bytes_size(events) > 24) {
tox_events_print(events);
}

tox_events_free(events);
}

static bool await_message(Tox **toxes, const Tox_Dispatch *dispatch)
{
for (uint32_t i = 0; i < 100; ++i) {
// Ignore events on tox 1.
tox_events_free(tox_events_iterate(toxes[0], nullptr));
print_events(tox_events_iterate(toxes[0], nullptr));
// Check if tox 2 got the message from tox 1.
Tox_Events *events = tox_events_iterate(toxes[1], nullptr);

dump_events("/tmp/test.mp", events);

bool success = false;
tox_dispatch_invoke(dispatch, events, toxes[1], &success);
tox_events_free(events);
print_events(events);

if (success) {
return true;
Expand Down Expand Up @@ -81,8 +110,8 @@ static void test_tox_events(void)
while (tox_self_get_connection_status(toxes[0]) == TOX_CONNECTION_NONE ||
tox_self_get_connection_status(toxes[1]) == TOX_CONNECTION_NONE) {
// Ignore connection events for now.
tox_events_free(tox_events_iterate(toxes[0], nullptr));
tox_events_free(tox_events_iterate(toxes[1], nullptr));
print_events(tox_events_iterate(toxes[0], nullptr));
print_events(tox_events_iterate(toxes[1], nullptr));

c_sleep(tox_iteration_interval(toxes[0]));
}
Expand All @@ -92,8 +121,8 @@ static void test_tox_events(void)
while (tox_friend_get_connection_status(toxes[0], 0, nullptr) == TOX_CONNECTION_NONE ||
tox_friend_get_connection_status(toxes[1], 0, nullptr) == TOX_CONNECTION_NONE) {
// Ignore connection events for now.
tox_events_free(tox_events_iterate(toxes[0], nullptr));
tox_events_free(tox_events_iterate(toxes[1], nullptr));
print_events(tox_events_iterate(toxes[0], nullptr));
print_events(tox_events_iterate(toxes[1], nullptr));

c_sleep(tox_iteration_interval(toxes[0]));
}
Expand Down
8 changes: 3 additions & 5 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ find_library(SOCKET_LIBRARIES socket )

# For toxcore.
pkg_use_module(LIBSODIUM libsodium )
pkg_use_module(MSGPACK msgpack )

# For toxav.
pkg_use_module(OPUS "opus;Opus" )
pkg_use_module(VPX "vpx;libvpx" )
pkg_use_module(OPUS "opus;Opus" )
pkg_use_module(VPX "vpx;libvpx" )

# For tox-bootstrapd.
pkg_use_module(LIBCONFIG libconfig )

# For tox-spectest.
pkg_use_module(MSGPACK msgpack )

###############################################################################
#
# :: For MSVC Windows builds.
Expand Down
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ PKG_CHECK_MODULES([LIBSODIUM], [libsodium],
LIBSODIUM_FOUND="no"
])

PKG_CHECK_MODULES([MSGPACK], [msgpack],
[
MSGPACK_FOUND="yes"
],
[
MSGPACK_FOUND="no"
])

if test "x$WANT_NACL" = "xyes"; then
NACL_LIBS=
NACL_LDFLAGS=
Expand Down
2 changes: 2 additions & 0 deletions other/analysis/check_includes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ ALLOWLIST: Tuple[str, ...] = (
"stdbool.h",
"stddef.h",
"stdint.h",
# msgpack, currently not abstracted away
"msgpack.h",
# toxav stuff, maybe not worth abstracting away
"opus.h",
"vpx/vp8cx.h",
Expand Down
1 change: 1 addition & 0 deletions other/bootstrap_daemon/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN ["apk", "--no-cache", "add",\
"linux-headers",\
"libconfig-dev",\
"libconfig-static",\
"msgpack-c-dev",\
"libsodium-dev",\
"libsodium-static",\
"ninja",\
Expand Down
1 change: 1 addition & 0 deletions other/docker/autotools/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
ca-certificates \
curl \
libconfig-dev \
libmsgpack-dev \
libopus-dev \
libtool \
libvpx-dev \
Expand Down
1 change: 1 addition & 0 deletions other/docker/circleci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN apt-get update && \
clang \
cmake \
libconfig-dev \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
Expand Down
46 changes: 46 additions & 0 deletions other/docker/tcc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
FROM ubuntu:20.04

RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
libc-dev \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
pkg-config \
tcc \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /work
COPY auto_tests/ /work/auto_tests/
COPY testing/ /work/testing/
COPY toxav/ /work/toxav/
COPY toxcore/ /work/toxcore/
COPY toxencryptsave/ /work/toxencryptsave/

RUN tcc \
-Dinline=static \
-o send_message_test \
-Wall -Werror \
-bench -g \
auto_tests/auto_test_support.c \
auto_tests/send_message_test.c \
testing/misc_tools.c \
toxav/*.c \
toxcore/*.c \
toxcore/*/*.c \
toxencryptsave/*.c \
$(pkg-config --cflags --libs libsodium msgpack opus vpx)

COPY other/make_single_file /work/other/
RUN \
other/make_single_file \
auto_tests/auto_test_support.c \
auto_tests/send_message_test.c \
testing/misc_tools.c | \
tcc - \
-o send_message_test \
-Wall -Werror \
-bench -g \
$(pkg-config --cflags --libs libsodium msgpack opus vpx)
5 changes: 5 additions & 0 deletions other/docker/tcc/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -eux

docker build -t toxchat/c-toxcore:tcc -f other/docker/tcc/Dockerfile .
Loading

0 comments on commit a84c8bc

Please sign in to comment.