mirror of https://github.com/yandex/odyssey.git
machinarium: remove previous test suite directory
This commit is contained in:
parent
3370cf43e7
commit
ed1a98e283
|
@ -1,49 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This example shows fiber context switch (yield)
|
||||
* performance done in one second.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
|
||||
static int csw = 0;
|
||||
|
||||
static void
|
||||
benchmark_worker(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("worker started.\n");
|
||||
while (machine_active(machine)) {
|
||||
csw++;
|
||||
machine_sleep(machine, 0);
|
||||
}
|
||||
printf("worker done.\n");
|
||||
}
|
||||
|
||||
static void
|
||||
benchmark_runner(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("benchmark started.\n");
|
||||
machine_create_fiber(machine, benchmark_worker, machine);
|
||||
machine_sleep(machine, 1000);
|
||||
printf("done.\n");
|
||||
printf("context switches %d in 1 sec.\n", csw);
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, benchmark_runner, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQD0uJYT+C7z4kta
|
||||
PqAgdmYNLDB0hQpiTcyk1MdtwG+Hdc8mRS56/Y8ZJCo76NAIceoSAW5ZXI1GFxlV
|
||||
P/XRg2DbEGez44CkLFEMrTuPNC+fMeVASRE+n8GERJeFWjw/dsHjoW54xDafM1xn
|
||||
KTA8R7O5tue3WGPN6t1r89cRrzoJ3cZ7WDoEkL75wPsYK9t1gwhAXj8zjoJNJUzo
|
||||
zix9WutepIuENecsGaTB0TFsNsTaSDn9YcvsmthulfZb8kDeW85tUQL6l5wHLThM
|
||||
onvwiQouVEoGqYhKhQcuaXHmfXW7w+n1lZ3SFsU62iO63WFsZkwTfgx68FV+7PMX
|
||||
r/I8hOqJAgMBAAECggEAVgWwk5JxBr+sFyveY6ra0pSO79Z4QPfyEJrPA3Bag/ZQ
|
||||
e7Zj0U3iCEdjJS4AOoe4bN3d6UGUU2f5OrNB+ITC/h41JdG5vXvqfZdDUWJZvw7t
|
||||
8/v7ONU1VAY8cN64QOLA4wAy8fae8LVALO8Yiof10DlQGG3Un2SIstLC7e93alG/
|
||||
5K/Xv6fQtgCRUL1Ntcpe93qYdR+uICL0pDCxoSchXVwBdccGzlVBeNhE3Ed6/sUb
|
||||
G0Vt08mgtwBYNcjPHYU02MmTnrbnPCCc/iPFX9Q6shdV/bbo9fIS6kEF1xdtLJJX
|
||||
gyFy6pt/ER4QemAKiRphfYNeLNheC4nrqPh39FiqgQKBgQD6ciSFTazUvkbv+7pu
|
||||
QOSqLljQ5dQLTxA+HDGsAgZOyRut1Z90DfTwii9esIZiQ0PGMpY1ECzQ/wJYPVWe
|
||||
P0qhbXXWiygJx2VLjolUijBIX38vsS+zAn9QSjVYhnLT+BBsj+Xo2RBMJlNbnoFn
|
||||
WI9RazmsSZFlfIYg8XvlIYSGsQKBgQD6JfEl4RE6WEQqi4z4OPwmJ1cH9cg1Ub9w
|
||||
Ie4JZrsCFK4vFXk0wATySntdqtmFUFn8rv8vDmXd6Y77OvLb0zyqj2/SXWIaekd2
|
||||
V0xxmlA9L+Z4i6xG0egUKqWOqry4AwafN/VnhIFqMZLBIJCvSdNT3r4PzV+//jlE
|
||||
sAUUSUdHWQKBgELOc+ODRtkxMKcPlTmX4edrmZGP3TJvErwetTlXCOu3RcTnOTOy
|
||||
DWak1eICYLaRjuCt2depyuqtO6jiI5ak93l8Q3TJhhKlt+WhzgxZGGwV3U2RjFHK
|
||||
FsAKK5TDuJnGkEsFT6T/UhOeHZpQ+9/fhcuIOMg2j6xsvTLId+41NK7hAoGAC17c
|
||||
Jk+ju9Y1SwXK46GbfJR/NUJr1lif7hXsvwqnZ4HPJR7Bwq8TynuuPeS9PH0Zig3e
|
||||
VaI+6SZrtGnKjkl80XfxqktKYU+7ndp8BBHyocVioYofy5xRA1Go8uI579dbXrNp
|
||||
eUoho9UbCaJUUVN2wrI2S3uXZZ/FtKSXpoNqNQkCgYAyihi5Y8KZ/tAA7b6dftSZ
|
||||
J4G3RNaF/CNE0hZ0O8VU5+1BThKmwO/vG1KmpdsmRnxQKXykjPp+8/fIKD7ZOoqm
|
||||
+gRnyTsAdifdZiUHwaZfHn4NKdcaKBGy2NpvSwBB6VZQmeeNXQqCeT4axb9KTIgm
|
||||
JX/UlUk3+ugvEx2RvM2/HQ==
|
||||
-----END PRIVATE KEY-----
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIDezCCAmOgAwIBAgIJAIKv7zWJzYwrMA0GCSqGSIb3DQEBCwUAMFQxCzAJBgNV
|
||||
BAYTAlJVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
|
||||
aWRnaXRzIFB0eSBMdGQxDTALBgNVBAMMBFRFU1QwHhcNMTcwMzMxMTMyODI2WhcN
|
||||
MTgwMzMxMTMyODI2WjBUMQswCQYDVQQGEwJSVTETMBEGA1UECAwKU29tZS1TdGF0
|
||||
ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMQ0wCwYDVQQDDARU
|
||||
RVNUMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9LiWE/gu8+JLWj6g
|
||||
IHZmDSwwdIUKYk3MpNTHbcBvh3XPJkUuev2PGSQqO+jQCHHqEgFuWVyNRhcZVT/1
|
||||
0YNg2xBns+OApCxRDK07jzQvnzHlQEkRPp/BhESXhVo8P3bB46FueMQ2nzNcZykw
|
||||
PEezubbnt1hjzerda/PXEa86Cd3Ge1g6BJC++cD7GCvbdYMIQF4/M46CTSVM6M4s
|
||||
fVrrXqSLhDXnLBmkwdExbDbE2kg5/WHL7JrYbpX2W/JA3lvObVEC+pecBy04TKJ7
|
||||
8IkKLlRKBqmISoUHLmlx5n11u8Pp9ZWd0hbFOtojut1hbGZME34MevBVfuzzF6/y
|
||||
PITqiQIDAQABo1AwTjAdBgNVHQ4EFgQUKND1tPpmYEpGFmXiDKNikwU8eAwwHwYD
|
||||
VR0jBBgwFoAUKND1tPpmYEpGFmXiDKNikwU8eAwwDAYDVR0TBAUwAwEB/zANBgkq
|
||||
hkiG9w0BAQsFAAOCAQEA5dUS0HUeJGTeM4TCQRd5Yp266bIrdk2Ez9tOINQ9+c/S
|
||||
R+++4Jc4laQrH3y4o3CaJQ5ktZJFXQfEXyhohaZHXgnofbjkOaFUhTuInaLF5tSU
|
||||
k6UebaU+TvAuPTl2zf35HRJ3U6vI08tl2fj+I3IAlRKU+JgzCrXfG9HKYKBBKbmh
|
||||
tssvz0xwGR0xqi4OijIv5snoGa7WixlhmW0QwKbKBlGDAq0ngd/ZlpMoY217UCvH
|
||||
YKYJ5AVXKI4iYSSZDN9oSf/MEh1SEqHM8of5kcNBW6M320Bun3Z5Edq1BCT1tjD4
|
||||
rX+iYSFul04DrAD+RW6V+e+NGSJ7SHy0Ceg7D5w/bw==
|
||||
-----END CERTIFICATE-----
|
89
tests/echo.c
89
tests/echo.c
|
@ -1,89 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This example shows basic tcp server.
|
||||
* Read 10 bytes with 5 sec timeout then write back.
|
||||
*
|
||||
* echo 0123456789 | nc -v 127.0.0.1 7778 # to test
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <uv.h>
|
||||
|
||||
static void
|
||||
test_client(void *arg)
|
||||
{
|
||||
machine_io_t client = arg;
|
||||
int fd = machine_fd(client);
|
||||
printf("new connection: %d \n", fd);
|
||||
for (;;) {
|
||||
int rc;
|
||||
/* read 10 bytes (with 5 sec timeout) */
|
||||
rc = machine_read(client, NULL, 10, 5 * 1000);
|
||||
if (rc < 0) {
|
||||
if (machine_read_timedout(client)) {
|
||||
printf("timeout in %d\n", fd);
|
||||
continue;
|
||||
}
|
||||
if (! machine_connected(client)) {
|
||||
printf("client disconnected\n");
|
||||
break;
|
||||
}
|
||||
printf("client read error\n");
|
||||
}
|
||||
/* write 10 bytes */
|
||||
char *buf = machine_read_buf(client);
|
||||
rc = machine_write(client, buf, 10, INT_MAX);
|
||||
if (rc < 0)
|
||||
printf("client write error\n");
|
||||
}
|
||||
printf("client %d disconnected\n", fd);
|
||||
machine_close(client);
|
||||
}
|
||||
|
||||
static void
|
||||
test_server(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
machine_io_t server = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
uv_ip4_addr("127.0.0.1", 7778, &sa);
|
||||
int rc;
|
||||
rc = machine_bind(server, (struct sockaddr*)&sa);
|
||||
if (rc < 0) {
|
||||
printf("bind failed\n");
|
||||
machine_close(server);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("waiting for connections (127.0.0.1:7778)\n");
|
||||
for (;;) {
|
||||
machine_io_t client;
|
||||
rc = machine_accept(server, 16, &client);
|
||||
if (rc < 0) {
|
||||
printf("accept error.\n");
|
||||
} else
|
||||
if (rc == 0) {
|
||||
machine_create_fiber(machine, test_client, client);
|
||||
}
|
||||
}
|
||||
|
||||
/* unreach */
|
||||
machine_close(server);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_server, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
105
tests/makefile
105
tests/makefile
|
@ -1,105 +0,0 @@
|
|||
CC = gcc
|
||||
RM = rm
|
||||
CFLAGS = -I. -Wall -g -O0 -I../src
|
||||
LFLAGS_LIB = ../src/libmachinarium.a -pthread -lssl -lcrypto
|
||||
LFLAGS = $(LFLAGS_LIB)
|
||||
TESTS = test_new \
|
||||
test_create \
|
||||
test_sleep \
|
||||
test_wait \
|
||||
test_condition \
|
||||
test_condition_2 \
|
||||
benchmark \
|
||||
test_io_new \
|
||||
test_connect \
|
||||
test_connect_timeout \
|
||||
test_cancel_connect \
|
||||
test_cancel_connect_2 \
|
||||
test_write \
|
||||
test_read \
|
||||
test_read_timeout \
|
||||
test_accept_timeout \
|
||||
test_cancel_accept \
|
||||
test_client_server \
|
||||
test_client_server_ra
|
||||
|
||||
# test_getaddrinfo \
|
||||
test_getaddrinfo_2 \
|
||||
test_getaddrinfo_3 \
|
||||
test_cancel_sleep \
|
||||
test_cancel_sleep_2 \
|
||||
test_cancel_getaddrinfo \
|
||||
test_cancel_getaddrinfo_2 \
|
||||
test_cancel_read \
|
||||
test_tls_connect \
|
||||
echo
|
||||
all: validate clean $(TESTS)
|
||||
test_new:
|
||||
$(CC) $(CFLAGS) test_new.c $(LFLAGS) -o test_new
|
||||
test_create:
|
||||
$(CC) $(CFLAGS) test_create.c $(LFLAGS) -o test_create
|
||||
test_sleep:
|
||||
$(CC) $(CFLAGS) test_sleep.c $(LFLAGS) -o test_sleep
|
||||
test_sleep_2:
|
||||
$(CC) $(CFLAGS) test_sleep_2.c $(LFLAGS) -o test_sleep_2
|
||||
test_wait:
|
||||
$(CC) $(CFLAGS) test_wait.c $(LFLAGS) -o test_wait
|
||||
test_condition:
|
||||
$(CC) $(CFLAGS) test_condition.c $(LFLAGS) -o test_condition
|
||||
test_condition_2:
|
||||
$(CC) $(CFLAGS) test_condition_2.c $(LFLAGS) -o test_condition_2
|
||||
test_io_new:
|
||||
$(CC) $(CFLAGS) test_io_new.c $(LFLAGS) -o test_io_new
|
||||
test_getaddrinfo:
|
||||
$(CC) $(CFLAGS) test_getaddrinfo.c $(LFLAGS) -o test_getaddrinfo
|
||||
test_getaddrinfo_2:
|
||||
$(CC) $(CFLAGS) test_getaddrinfo_2.c $(LFLAGS) -o test_getaddrinfo_2
|
||||
test_getaddrinfo_3:
|
||||
$(CC) $(CFLAGS) test_getaddrinfo_3.c $(LFLAGS) -o test_getaddrinfo_3
|
||||
test_connect:
|
||||
$(CC) $(CFLAGS) test_connect.c $(LFLAGS) -o test_connect
|
||||
test_write:
|
||||
$(CC) $(CFLAGS) test_write.c $(LFLAGS) -o test_write
|
||||
test_read:
|
||||
$(CC) $(CFLAGS) test_read.c $(LFLAGS) -o test_read
|
||||
test_read_timeout:
|
||||
$(CC) $(CFLAGS) test_read_timeout.c $(LFLAGS) -o test_read_timeout
|
||||
test_accept_timeout:
|
||||
$(CC) $(CFLAGS) test_accept_timeout.c $(LFLAGS) -o test_accept_timeout
|
||||
test_connect_timeout:
|
||||
$(CC) $(CFLAGS) test_connect_timeout.c $(LFLAGS) -o test_connect_timeout
|
||||
test_client_server:
|
||||
$(CC) $(CFLAGS) test_client_server.c $(LFLAGS) -o test_client_server
|
||||
test_client_server_ra:
|
||||
$(CC) $(CFLAGS) test_client_server_ra.c $(LFLAGS) -o test_client_server_ra
|
||||
test_cancel_sleep:
|
||||
$(CC) $(CFLAGS) test_cancel_sleep.c $(LFLAGS) -o test_cancel_sleep
|
||||
test_cancel_sleep_2:
|
||||
$(CC) $(CFLAGS) test_cancel_sleep_2.c $(LFLAGS) -o test_cancel_sleep_2
|
||||
test_cancel_getaddrinfo:
|
||||
$(CC) $(CFLAGS) test_cancel_getaddrinfo.c $(LFLAGS) -o test_cancel_getaddrinfo
|
||||
test_cancel_getaddrinfo_2:
|
||||
$(CC) $(CFLAGS) test_cancel_getaddrinfo_2.c $(LFLAGS) -o test_cancel_getaddrinfo_2
|
||||
test_cancel_connect:
|
||||
$(CC) $(CFLAGS) test_cancel_connect.c $(LFLAGS) -o test_cancel_connect
|
||||
test_cancel_connect_2:
|
||||
$(CC) $(CFLAGS) test_cancel_connect_2.c $(LFLAGS) -o test_cancel_connect_2
|
||||
test_cancel_read:
|
||||
$(CC) $(CFLAGS) test_cancel_read.c $(LFLAGS) -o test_cancel_read
|
||||
test_cancel_accept:
|
||||
$(CC) $(CFLAGS) test_cancel_accept.c $(LFLAGS) -o test_cancel_accept
|
||||
test_tls_connect:
|
||||
$(CC) $(CFLAGS) test_tls_connect.c $(LFLAGS) -o test_tls_connect
|
||||
echo:
|
||||
$(CC) $(CFLAGS) echo.c $(LFLAGS) -o echo
|
||||
benchmark:
|
||||
$(CC) $(CFLAGS) benchmark.c $(LFLAGS) -o benchmark
|
||||
validate:
|
||||
@if [ ! -f ../src/libmachinarium.a ]; then \
|
||||
echo ""; \
|
||||
echo "Please build the static library first."; \
|
||||
echo ""; \
|
||||
exit 1; \
|
||||
fi
|
||||
clean:
|
||||
$(RM) -f $(TESTS)
|
|
@ -1,73 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
server(void *arg)
|
||||
{
|
||||
printf("server: started\n");
|
||||
|
||||
machine_t machine = arg;
|
||||
machine_io_t server = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
sa.sin_port = htons(7778);
|
||||
|
||||
int rc;
|
||||
rc = machine_bind(server, (struct sockaddr*)&sa);
|
||||
if (rc < 0) {
|
||||
printf("server: bind failed\n");
|
||||
machine_close(server);
|
||||
machine_stop(machine);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("server: waiting for connections (127.0.0.1:7778)\n");
|
||||
machine_io_t client;
|
||||
rc = machine_accept(server, &client, 16, 500);
|
||||
if (rc < 0) {
|
||||
printf("accept error: %s\n", machine_error(server));
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
machine_stop(machine);
|
||||
return;
|
||||
}
|
||||
char msg[] = "hello world";
|
||||
rc = machine_write(client, msg, sizeof(msg), INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("server: write error: %s\n", machine_error(client));
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
machine_stop(machine);
|
||||
return;
|
||||
}
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
|
||||
printf("server: done\n");
|
||||
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, server, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child started\n");
|
||||
machine_io_t server = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
sa.sin_port = htons(7778);
|
||||
|
||||
int rc;
|
||||
rc = machine_bind(server, (struct sockaddr*)&sa);
|
||||
if (rc < 0) {
|
||||
printf("server: bind failed: %s\n", machine_error(server));
|
||||
machine_close(server);
|
||||
machine_stop(machine);
|
||||
return;
|
||||
}
|
||||
|
||||
machine_io_t client;
|
||||
rc = machine_accept(server, &client, 16, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("accept error: %s\n", machine_error(server));
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
machine_stop(machine);
|
||||
return;
|
||||
}
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
|
||||
if (machine_cancelled(machine))
|
||||
printf("child marked as cancel\n");
|
||||
printf("child end\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int id = machine_create_fiber(machine, test_connect, machine);
|
||||
machine_sleep(machine, 0);
|
||||
machine_cancel(machine, id);
|
||||
machine_wait(machine, id);
|
||||
|
||||
printf("waiter 1 ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child started\n");
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("8.8.8.8");
|
||||
sa.sin_port = htons(1234);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr *)&sa, INT_MAX);
|
||||
printf("child resumed\n");
|
||||
assert(rc < 0);
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
|
||||
if (machine_cancelled(machine))
|
||||
printf("child marked as cancel\n");
|
||||
printf("child end\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int id = machine_create_fiber(machine, test_connect, machine);
|
||||
machine_sleep(machine, 0);
|
||||
machine_cancel(machine, id);
|
||||
machine_wait(machine, id);
|
||||
|
||||
printf("waiter 1 ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <uv.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
assert(machine_cancelled(machine));
|
||||
printf("child started\n");
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("8.8.8.8");
|
||||
sa.sin_port = htons(1234);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr*)&sa, INT_MAX);
|
||||
printf("child resumed\n");
|
||||
assert(rc < 0);
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
if (machine_cancelled(machine))
|
||||
printf("child marked as cancel\n");
|
||||
printf("child end\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int id = machine_create_fiber(machine, test_connect, machine);
|
||||
machine_cancel(machine, id); /* run cancelled */
|
||||
machine_sleep(machine, 0);
|
||||
machine_wait(machine, id);
|
||||
|
||||
printf("waiter 1 ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_gai(void *arg)
|
||||
{
|
||||
printf("child started\n");
|
||||
machine_t machine = arg;
|
||||
machine_io_t io = machine_create_io(machine);
|
||||
struct addrinfo *res = NULL;
|
||||
int rc = machine_getaddrinfo(io, "abracadabra", "http", NULL, &res, INT_MAX);
|
||||
assert(rc < 0);
|
||||
machine_close(io);
|
||||
assert(res == NULL);
|
||||
if (machine_cancelled(machine))
|
||||
printf("child marked as cancel\n");
|
||||
printf("child done\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int64_t fiber;
|
||||
fiber = machine_create_fiber(machine, test_gai, machine);
|
||||
machine_sleep(machine, 0);
|
||||
machine_cancel(machine, fiber);
|
||||
machine_wait(machine, fiber);
|
||||
|
||||
printf("waiter ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_gai(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
assert(machine_cancelled(machine));
|
||||
printf("child started\n");
|
||||
machine_io_t io = machine_create_io(machine);
|
||||
struct addrinfo *res = NULL;
|
||||
int rc = machine_getaddrinfo(io, "abracadabra", "http", NULL, &res, INT_MAX);
|
||||
assert(rc < 0);
|
||||
machine_close(io);
|
||||
assert(res == NULL);
|
||||
printf("child done\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int64_t fiber;
|
||||
fiber = machine_create_fiber(machine, test_gai, machine);
|
||||
machine_cancel(machine, fiber); /* run cancelled */
|
||||
machine_sleep(machine, 0);
|
||||
machine_wait(machine, fiber);
|
||||
|
||||
printf("waiter ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,118 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <uv.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
server(void *arg)
|
||||
{
|
||||
printf("server: started\n");
|
||||
|
||||
machine_t machine = arg;
|
||||
machine_io_t server = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
uv_ip4_addr("127.0.0.1", 7778, &sa);
|
||||
|
||||
int rc;
|
||||
rc = machine_bind(server, (struct sockaddr*)&sa);
|
||||
if (rc < 0) {
|
||||
printf("server: bind failed\n");
|
||||
machine_close(server);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("server: waiting for connections (127.0.0.1:7778)\n");
|
||||
machine_io_t client;
|
||||
rc = machine_accept(server, &client, 16, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("accept error.\n");
|
||||
machine_close(server);
|
||||
return;
|
||||
}
|
||||
|
||||
machine_sleep(machine, 100 * 1000);
|
||||
|
||||
machine_close(client);
|
||||
machine_close(server);
|
||||
printf("server: done\n");
|
||||
}
|
||||
|
||||
static void
|
||||
client(void *arg)
|
||||
{
|
||||
printf("client: started\n");
|
||||
|
||||
machine_t machine = arg;
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
uv_ip4_addr("127.0.0.1", 7778, &sa);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr*)&sa, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("client: connect failed\n");
|
||||
machine_close(client);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("client: connected\n");
|
||||
|
||||
/* will wait forever */
|
||||
rc = machine_read(client, NULL, 12, INT_MAX);
|
||||
if (rc < 0) {
|
||||
if (machine_cancelled(machine)) {
|
||||
printf("client: read cancelled\n");
|
||||
} else {
|
||||
printf("client: read failed\n");
|
||||
}
|
||||
machine_close(client);
|
||||
return;
|
||||
}
|
||||
|
||||
machine_close(client);
|
||||
|
||||
printf("client: done\n");
|
||||
}
|
||||
|
||||
static void
|
||||
runner(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
int server_id = machine_create_fiber(machine, server, machine);
|
||||
int client_id = machine_create_fiber(machine, client, machine);
|
||||
|
||||
/* switch to server, client */
|
||||
machine_sleep(machine, 0);
|
||||
|
||||
/* switch to client read */
|
||||
machine_sleep(machine, 0);
|
||||
|
||||
/* cancel client read */
|
||||
machine_cancel(machine, client_id);
|
||||
|
||||
machine_sleep(machine, 0);
|
||||
machine_cancel(machine, server_id);
|
||||
machine_sleep(machine, 0);
|
||||
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, runner, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_child(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child started\n");
|
||||
printf("child sleep for 600 seconds\n");
|
||||
machine_sleep(machine, 600 * 1000);
|
||||
printf("child wakeup\n");
|
||||
if (machine_cancelled(machine))
|
||||
printf("child cancelled\n");
|
||||
printf("child 0 ended\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_parent(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("parent started\n");
|
||||
|
||||
int64_t id;
|
||||
id = machine_create_fiber(machine, test_child, machine);
|
||||
machine_sleep(machine, 0);
|
||||
machine_cancel(machine, id);
|
||||
machine_wait(machine, id);
|
||||
|
||||
printf("parent ended\n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_parent, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_child(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
assert(machine_cancelled(machine));
|
||||
printf("child started\n");
|
||||
printf("child sleep for 600 seconds\n");
|
||||
machine_sleep(machine, 600 * 1000);
|
||||
printf("child wakeup\n");
|
||||
if (machine_cancelled(machine))
|
||||
printf("child cancelled\n");
|
||||
printf("child 0 ended\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_parent(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("parent started\n");
|
||||
|
||||
int64_t fiber;
|
||||
fiber = machine_create_fiber(machine, test_child, machine);
|
||||
machine_cancel(machine, fiber); /* run cancelled fiber */
|
||||
machine_sleep(machine, 0);
|
||||
machine_wait(machine, fiber);
|
||||
|
||||
printf("parent ended\n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_parent, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,115 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
server(void *arg)
|
||||
{
|
||||
printf("server: started\n");
|
||||
|
||||
machine_t machine = arg;
|
||||
machine_io_t server = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
sa.sin_port = htons(7778);
|
||||
|
||||
int rc;
|
||||
rc = machine_bind(server, (struct sockaddr*)&sa);
|
||||
if (rc < 0) {
|
||||
printf("server: bind failed\n");
|
||||
machine_close(server);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("server: waiting for connections (127.0.0.1:7778)\n");
|
||||
machine_io_t client;
|
||||
rc = machine_accept(server, &client, 16, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("accept error: %s\n", machine_error(server));
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
return;
|
||||
}
|
||||
char msg[] = "hello world";
|
||||
rc = machine_write(client, msg, sizeof(msg), INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("server: write error: %s\n", machine_error(client));
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
return;
|
||||
}
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
|
||||
printf("server: done\n");
|
||||
}
|
||||
|
||||
static void
|
||||
client(void *arg)
|
||||
{
|
||||
printf("client: started\n");
|
||||
|
||||
machine_t machine = arg;
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
sa.sin_port = htons(7778);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr*)&sa, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("client: connect failed: %s\n", machine_error(client));
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("client: connected\n");
|
||||
|
||||
char buf[16];
|
||||
rc = machine_read(client, buf, 12, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("client: read failed: %s\n", machine_error(client));
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
return;
|
||||
}
|
||||
|
||||
assert(memcmp(buf, "hello world", 12) == 0);
|
||||
|
||||
rc = machine_read(client, buf, 1, INT_MAX);
|
||||
/* eof */
|
||||
assert(rc == -1);
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
|
||||
printf("client: done\n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, server, machine);
|
||||
machine_create_fiber(machine, client, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,147 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
server(void *arg)
|
||||
{
|
||||
printf("server: started\n");
|
||||
|
||||
machine_t machine = arg;
|
||||
machine_io_t server = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
sa.sin_port = htons(7778);
|
||||
|
||||
int rc;
|
||||
rc = machine_bind(server, (struct sockaddr*)&sa);
|
||||
if (rc < 0) {
|
||||
printf("server: bind failed: %s\n", machine_error(server));
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("server: waiting for connections (127.0.0.1:7778)\n");
|
||||
machine_io_t client;
|
||||
rc = machine_accept(server, &client, 16, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("accept error: %s\n", machine_error(server));
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
return;
|
||||
}
|
||||
char msg[] = "hello world" "HELLO WORLD" "a" "b" "c" "333";
|
||||
rc = machine_write(client, msg, sizeof(msg), INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("server: write error: %s\n", machine_error(client));
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
return;
|
||||
}
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
machine_close(server);
|
||||
machine_free_io(server);
|
||||
printf("server: done\n");
|
||||
}
|
||||
|
||||
static void
|
||||
client(void *arg)
|
||||
{
|
||||
printf("client: started\n");
|
||||
|
||||
machine_t machine = arg;
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
sa.sin_port = htons(7778);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr*)&sa, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("client: connect failed\n");
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("client: connected\n");
|
||||
|
||||
rc = machine_set_readahead(client, 1024);
|
||||
if (rc < 0) {
|
||||
printf("client: %s\n", machine_error(client));
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
return;
|
||||
}
|
||||
|
||||
/* read and fill readahead buffer */
|
||||
char buf[16];
|
||||
rc = machine_read(client, buf, 11, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("client: read failed\n");
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
return;
|
||||
}
|
||||
|
||||
assert(memcmp(buf, "hello world", 11) == 0);
|
||||
|
||||
/* read from buffer */
|
||||
rc = machine_read(client, buf, 11, INT_MAX);
|
||||
assert(rc == 0);
|
||||
assert(memcmp(buf, "HELLO WORLD", 11) == 0);
|
||||
|
||||
rc = machine_read(client, buf, 1, INT_MAX);
|
||||
assert(rc == 0);
|
||||
assert(*buf == 'a');
|
||||
|
||||
rc = machine_read(client, buf, 1, INT_MAX);
|
||||
assert(rc == 0);
|
||||
assert(*buf == 'b');
|
||||
|
||||
rc = machine_read(client, buf, 1, INT_MAX);
|
||||
assert(rc == 0);
|
||||
assert(*buf == 'c');
|
||||
|
||||
rc = machine_read(client, buf, 4, INT_MAX);
|
||||
assert(rc == 0);
|
||||
assert(memcmp(buf, "333", 4) == 0);
|
||||
|
||||
/* eof */
|
||||
rc = machine_read(client, buf, 1, INT_MAX);
|
||||
assert(rc == -1);
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
|
||||
printf("client: done\n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, server, machine);
|
||||
machine_create_fiber(machine, client, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_condition(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("condition fiber started\n");
|
||||
int rc = machine_condition(machine, 1000);
|
||||
assert(rc == 0);
|
||||
printf("condition fiber ended\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int64_t a;
|
||||
a = machine_create_fiber(machine, test_condition, machine);
|
||||
|
||||
machine_sleep(machine, 0);
|
||||
machine_signal(machine, a);
|
||||
machine_sleep(machine, 0);
|
||||
|
||||
printf("waiter ended\n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_condition(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("condition fiber started\n");
|
||||
int rc = machine_condition(machine, 1);
|
||||
assert(rc < 0);
|
||||
printf("condition fiber ended\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int64_t a;
|
||||
a = machine_create_fiber(machine, test_condition, machine);
|
||||
machine_sleep(machine, 100);
|
||||
|
||||
int rc;
|
||||
rc = machine_signal(machine, a);
|
||||
assert(rc < 0);
|
||||
|
||||
printf("waiter ended\n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child started\n");
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("213.180.204.3");
|
||||
sa.sin_port = htons(80);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr *)&sa, INT_MAX);
|
||||
if (rc == -1)
|
||||
printf("connection failed: %s\n", machine_error(client));
|
||||
else
|
||||
printf("connected\n");
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
|
||||
printf("child end\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int id = machine_create_fiber(machine, test_connect, machine);
|
||||
machine_sleep(machine, 0);
|
||||
machine_wait(machine, id);
|
||||
|
||||
printf("waiter 1 ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child started\n");
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("8.8.8.8");
|
||||
sa.sin_port = htons(80);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr *)&sa, 100);
|
||||
if (rc == -1)
|
||||
printf("connection failed: %s\n", machine_error(client));
|
||||
else
|
||||
printf("connected\n");
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
|
||||
printf("child end\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int id = machine_create_fiber(machine, test_connect, machine);
|
||||
machine_sleep(machine, 0);
|
||||
machine_wait(machine, id);
|
||||
|
||||
printf("waiter 1 ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
int fiber_call = 0;
|
||||
|
||||
static void
|
||||
fiber(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
fiber_call++;
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, fiber, machine);
|
||||
machine_start(machine);
|
||||
assert(fiber_call == 1);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
fiber(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
machine_io_t io = machine_create_io(machine);
|
||||
struct addrinfo *res = NULL;
|
||||
int rc = machine_getaddrinfo(io, "localhost", "http", NULL, &res, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("failed to resolve address\n");
|
||||
} else {
|
||||
assert(res != NULL);
|
||||
}
|
||||
if (res)
|
||||
freeaddrinfo(res);
|
||||
machine_close(io);
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, fiber, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
fiber(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
machine_io_t io = machine_create_io(machine);
|
||||
struct addrinfo *res = NULL;
|
||||
int rc = machine_getaddrinfo(io, "abracadabra", "http", NULL, &res, INT_MAX);
|
||||
assert(rc < 0);
|
||||
assert(res == NULL);
|
||||
machine_close(io);
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, fiber, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
fiber_1(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
machine_io_t io = machine_create_io(machine);
|
||||
struct addrinfo *res = NULL;
|
||||
int rc = machine_getaddrinfo(io, "localhost", "http", NULL, &res, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("failed to resolve address\n");
|
||||
} else {
|
||||
assert(res != NULL);
|
||||
}
|
||||
if (res)
|
||||
freeaddrinfo(res);
|
||||
machine_close(io);
|
||||
}
|
||||
|
||||
static void
|
||||
fiber_2(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
machine_io_t io = machine_create_io(machine);
|
||||
struct addrinfo *res = NULL;
|
||||
int rc = machine_getaddrinfo(io, "localhost", "http", NULL, &res, INT_MAX);
|
||||
if (rc < 0) {
|
||||
printf("failed to resolve address\n");
|
||||
} else {
|
||||
assert(res != NULL);
|
||||
}
|
||||
if (res)
|
||||
freeaddrinfo(res);
|
||||
machine_close(io);
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, fiber_1, machine);
|
||||
machine_create_fiber(machine, fiber_2, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_io_t io = machine_create_io(machine);
|
||||
|
||||
machine_close(io);
|
||||
machine_free_io(io);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child started\n");
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
sa.sin_port = htons(33413);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr *)&sa, INT_MAX);
|
||||
if (rc == -1)
|
||||
printf("connection failed: %s\n", machine_error(client));
|
||||
else
|
||||
printf("connected\n");
|
||||
|
||||
rc = machine_write(client, "hello world", 11, INT_MAX);
|
||||
if (rc == -1)
|
||||
printf("write failed: %s\n", machine_error(client));
|
||||
|
||||
char buf[10];
|
||||
rc = machine_read(client, buf, 10, INT_MAX);
|
||||
if (rc == -1)
|
||||
printf("read failed: %s\n", machine_error(client));
|
||||
|
||||
printf("%.*s\n", 10, buf);
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
|
||||
printf("child end\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int id = machine_create_fiber(machine, test_connect, machine);
|
||||
machine_sleep(machine, 0);
|
||||
machine_wait(machine, id);
|
||||
|
||||
printf("waiter 1 ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child started\n");
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
sa.sin_port = htons(33413);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr *)&sa, INT_MAX);
|
||||
if (rc == -1)
|
||||
printf("connection failed: %s\n", machine_error(client));
|
||||
else
|
||||
printf("connected\n");
|
||||
|
||||
rc = machine_write(client, "hello world", 11, INT_MAX);
|
||||
if (rc == -1)
|
||||
printf("write failed: %s\n", machine_error(client));
|
||||
|
||||
char buf[10];
|
||||
rc = machine_read(client, buf, 10, 1000);
|
||||
if (rc == -1)
|
||||
printf("read failed: %s\n", machine_error(client));
|
||||
|
||||
printf("%.*s\n", 10, buf);
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
|
||||
printf("child end\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int id = machine_create_fiber(machine, test_connect, machine);
|
||||
machine_sleep(machine, 0);
|
||||
machine_wait(machine, id);
|
||||
|
||||
printf("waiter 1 ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_child(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child started\n");
|
||||
printf("sleep 10 ms\n");
|
||||
machine_sleep(machine, 1000);
|
||||
printf("sleep wakeup\n");
|
||||
printf("child ended\n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_child, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <uv.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("fiber started\n");
|
||||
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
machine_tls_t tls = machine_create_tls(machine);
|
||||
machine_set_tls(client, tls);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
uv_ip4_addr("127.0.0.1", 44330, &sa);
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr*)&sa, 0);
|
||||
if (rc == -1) {
|
||||
char *error;
|
||||
error = machine_error(client);
|
||||
printf("connect error: %s\n", error);
|
||||
} else {
|
||||
printf("connected: %d\n", rc);
|
||||
}
|
||||
|
||||
rc = machine_write(client, "hello", 5, 0);
|
||||
if (rc == -1) {
|
||||
char *error;
|
||||
error = machine_error(client);
|
||||
printf("write error: %s\n", error);
|
||||
}
|
||||
|
||||
machine_close(client);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_connect, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_child_0(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child 0 started\n");
|
||||
machine_sleep(machine, 1000);
|
||||
printf("child 0 ended\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_child_1(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child 1 started\n");
|
||||
machine_sleep(machine, 500);
|
||||
printf("child 1 ended\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int64_t a, b;
|
||||
a = machine_create_fiber(machine, test_child_0, machine);
|
||||
b = machine_create_fiber(machine, test_child_1, machine);
|
||||
|
||||
machine_wait(machine, b);
|
||||
printf("waiter 1 ended \n");
|
||||
|
||||
machine_wait(machine, a);
|
||||
printf("waiter 0 ended \n");
|
||||
|
||||
printf("waiter ended\n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_private.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("child started\n");
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
|
||||
struct sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
sa.sin_port = htons(33413);
|
||||
|
||||
int rc;
|
||||
rc = machine_connect(client, (struct sockaddr *)&sa, INT_MAX);
|
||||
if (rc == -1)
|
||||
printf("connection failed: %s\n", machine_error(client));
|
||||
else
|
||||
printf("connected\n");
|
||||
|
||||
rc = machine_write(client, "hello world", 11, INT_MAX);
|
||||
if (rc == -1)
|
||||
printf("write failed: %s\n", machine_error(client));
|
||||
|
||||
machine_close(client);
|
||||
machine_free_io(client);
|
||||
|
||||
printf("child end\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_waiter(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
|
||||
printf("waiter started\n");
|
||||
|
||||
int id = machine_create_fiber(machine, test_connect, machine);
|
||||
machine_sleep(machine, 0);
|
||||
machine_wait(machine, id);
|
||||
|
||||
printf("waiter 1 ended \n");
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_waiter, machine);
|
||||
machine_start(machine);
|
||||
printf("shutting down\n");
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,147 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
static char *verify = NULL;
|
||||
static char *server = NULL;
|
||||
static char *ca_path = NULL;
|
||||
static char *ca_file = NULL;
|
||||
static char *cert_file = NULL;
|
||||
static char *key_file = NULL;
|
||||
static char *addr = "127.0.0.1";
|
||||
static char *port = "44330";
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("fiber started\n");
|
||||
|
||||
/* resolve */
|
||||
struct addrinfo *ai = NULL;
|
||||
machine_io_t resolver;
|
||||
resolver = machine_create_io(machine);
|
||||
int rc;
|
||||
rc = machine_getaddrinfo(resolver, addr, port, NULL, &ai, 0);
|
||||
if (rc == -1) {
|
||||
char *error;
|
||||
error = machine_error(resolver);
|
||||
printf("machine_getaddrinfo(): %s\n", error);
|
||||
machine_close(resolver);
|
||||
machine_stop(machine);
|
||||
return;
|
||||
}
|
||||
machine_close(resolver);
|
||||
|
||||
/* tls context */
|
||||
machine_tls_t tls;
|
||||
tls = machine_create_tls(machine);
|
||||
if (verify)
|
||||
machine_tls_set_verify(tls, verify);
|
||||
if (server)
|
||||
machine_tls_set_server(tls, server);
|
||||
if (ca_path)
|
||||
machine_tls_set_ca_path(tls, ca_path);
|
||||
if (ca_file)
|
||||
machine_tls_set_ca_file(tls, ca_file);
|
||||
if (cert_file)
|
||||
machine_tls_set_cert_file(tls, cert_file);
|
||||
if (key_file)
|
||||
machine_tls_set_key_file(tls, key_file);
|
||||
|
||||
/* client */
|
||||
machine_io_t client;
|
||||
client = machine_create_io(machine);
|
||||
machine_set_tls(client, tls);
|
||||
|
||||
/* connect */
|
||||
rc = machine_connect(client, ai->ai_addr, 0);
|
||||
freeaddrinfo(ai);
|
||||
if (rc == -1) {
|
||||
char *error;
|
||||
error = machine_error(client);
|
||||
printf("machine_connect(): %s\n", error);
|
||||
machine_close(client);
|
||||
machine_stop(machine);
|
||||
return;
|
||||
}
|
||||
printf("connected to %s:%s\n", addr, port);
|
||||
|
||||
for (;;) {
|
||||
char buf[256];
|
||||
int len;
|
||||
char *p = fgets(buf, sizeof(buf), stdin);
|
||||
if (! p)
|
||||
break;
|
||||
len = strlen(buf);
|
||||
rc = machine_write(client, buf, len, 0);
|
||||
if (rc == -1) {
|
||||
char *error;
|
||||
error = machine_error(client);
|
||||
printf("machine_write(): %s\n", error);
|
||||
}
|
||||
}
|
||||
|
||||
machine_close(client);
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "hv:k:c:r:R:s:a:p:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'h':
|
||||
printf("usage: [-v verify_mode]\n"
|
||||
" [-k key_file]\n"
|
||||
" [-c cert_file]\n"
|
||||
" [-r ca_file ]\n"
|
||||
" [-R ca_path]\n"
|
||||
" [-s server_name]\n"
|
||||
" [-a address]\n"
|
||||
" [-p port]\n");
|
||||
return 0;
|
||||
case 'v':
|
||||
verify = optarg;
|
||||
break;
|
||||
case 'k':
|
||||
key_file = optarg;
|
||||
break;
|
||||
case 'c':
|
||||
cert_file = optarg;
|
||||
break;
|
||||
case 'r':
|
||||
ca_file = optarg;
|
||||
break;
|
||||
case 'R':
|
||||
ca_path = optarg;
|
||||
break;
|
||||
case 's':
|
||||
server = optarg;
|
||||
break;
|
||||
case 'a':
|
||||
addr = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
port = optarg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_connect, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
|
@ -1,177 +0,0 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
static char *verify = NULL;
|
||||
static char *server = NULL;
|
||||
static char *ca_path = NULL;
|
||||
static char *ca_file = NULL;
|
||||
static char *cert_file = NULL;
|
||||
static char *key_file = NULL;
|
||||
static char *addr = "127.0.0.1";
|
||||
static char *port = "44330";
|
||||
|
||||
static void
|
||||
test_client(void *arg)
|
||||
{
|
||||
machine_io_t client = arg;
|
||||
int fd = machine_fd(client);
|
||||
printf("new connection: %d \n", fd);
|
||||
for (;;) {
|
||||
int rc;
|
||||
char buf[10];
|
||||
/* read 10 bytes (with 5 sec timeout) */
|
||||
rc = machine_read(client, buf, 10, 0);
|
||||
if (rc < 0) {
|
||||
if (machine_read_timedout(client)) {
|
||||
printf("timeout in %d\n", fd);
|
||||
continue;
|
||||
}
|
||||
if (! machine_connected(client)) {
|
||||
printf("client disconnected\n");
|
||||
break;
|
||||
}
|
||||
printf("client read error\n");
|
||||
char *error;
|
||||
error = machine_error(client);
|
||||
printf("machine_read(): %s\n", error);
|
||||
break;
|
||||
}
|
||||
/* write 10 bytes */
|
||||
rc = machine_write(client, buf, 10, 0);
|
||||
if (rc < 0)
|
||||
printf("client write error\n");
|
||||
}
|
||||
machine_close(client);
|
||||
}
|
||||
|
||||
static void
|
||||
test_connect(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
printf("fiber started\n");
|
||||
|
||||
/* resolve */
|
||||
struct addrinfo *ai = NULL;
|
||||
machine_io_t resolver;
|
||||
resolver = machine_create_io(machine);
|
||||
int rc;
|
||||
rc = machine_getaddrinfo(resolver, addr, port, NULL, &ai, 0);
|
||||
if (rc == -1) {
|
||||
char *error;
|
||||
error = machine_error(resolver);
|
||||
printf("machine_getaddrinfo(): %s\n", error);
|
||||
machine_close(resolver);
|
||||
machine_stop(machine);
|
||||
return;
|
||||
}
|
||||
machine_close(resolver);
|
||||
|
||||
/* tls context */
|
||||
machine_tls_t tls;
|
||||
tls = machine_create_tls(machine);
|
||||
if (verify)
|
||||
machine_tls_set_verify(tls, verify);
|
||||
if (server)
|
||||
machine_tls_set_server(tls, server);
|
||||
if (ca_path)
|
||||
machine_tls_set_ca_path(tls, ca_path);
|
||||
if (ca_file)
|
||||
machine_tls_set_ca_file(tls, ca_file);
|
||||
if (cert_file)
|
||||
machine_tls_set_cert_file(tls, cert_file);
|
||||
if (key_file)
|
||||
machine_tls_set_key_file(tls, key_file);
|
||||
|
||||
/* server */
|
||||
machine_io_t server;
|
||||
server = machine_create_io(machine);
|
||||
|
||||
/* bind */
|
||||
rc = machine_bind(server, ai->ai_addr);
|
||||
if (rc < 0) {
|
||||
char *error;
|
||||
error = machine_error(server);
|
||||
printf("machine_bind(): %s\n", error);
|
||||
machine_close(server);
|
||||
machine_stop(machine);
|
||||
return;
|
||||
}
|
||||
machine_set_tls(server, tls);
|
||||
|
||||
printf("waiting for connections (%s:%s)\n", addr, port);
|
||||
for (;;) {
|
||||
machine_io_t client;
|
||||
rc = machine_accept(server, 16, &client);
|
||||
if (rc < 0) {
|
||||
char *error;
|
||||
error = machine_error(server);
|
||||
printf("machine_accept(): %s\n", error);
|
||||
} else
|
||||
if (rc == 0) {
|
||||
machine_create_fiber(machine, test_client, client);
|
||||
}
|
||||
}
|
||||
machine_close(server);
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "hv:k:c:r:R:s:a:p:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'h':
|
||||
printf("usage: [-v verify_mode]\n"
|
||||
" [-k key_file]\n"
|
||||
" [-c cert_file]\n"
|
||||
" [-r ca_file ]\n"
|
||||
" [-R ca_path]\n"
|
||||
" [-s server_name]\n"
|
||||
" [-a listen_address]\n"
|
||||
" [-p port]\n");
|
||||
return 0;
|
||||
case 'v':
|
||||
verify = optarg;
|
||||
break;
|
||||
case 'k':
|
||||
key_file = optarg;
|
||||
break;
|
||||
case 'c':
|
||||
cert_file = optarg;
|
||||
break;
|
||||
case 'r':
|
||||
ca_file = optarg;
|
||||
break;
|
||||
case 'R':
|
||||
ca_path = optarg;
|
||||
break;
|
||||
case 's':
|
||||
server = optarg;
|
||||
break;
|
||||
case 'a':
|
||||
addr = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
port = optarg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
machine_t machine = machine_create();
|
||||
machine_create_fiber(machine, test_connect, machine);
|
||||
machine_start(machine);
|
||||
machine_free(machine);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue