Commit e03dc427 authored by Florian Fischer's avatar Florian Fischer
Browse files

remove C implementation

parent 21ba0074
Pipeline #52766 failed with stages
in 31 seconds
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright © 2020 Florian Fischer
/* #include <assert.h> */
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "config.h"
#define EMPER_ASYNC_LIB
#define EMPER_ASYNC_NETWORK
#include "emper.h"
#define unlikely(x) __builtin_expect(!!(x), 1)
static int port = 12345;
_Noreturn static void die(const char* msg) {
perror(msg);
exit(EXIT_FAILURE);
}
#define BUF_MAX 4096
void client_func(void* arg) {
int client_fd = *(int*)arg;
free(arg);
char buf[BUF_MAX];
for (;;) {
#ifdef USE_ASYNC_IO
ssize_t bytes_recv = emper_recv(client_fd, (void*)&buf, BUF_MAX, MSG_WAITALL);
#else
ssize_t bytes_recv = recv(client_fd, (void*)&buf, BUF_MAX, MSG_WAITALL);
#endif
// socket was shutdown
if (unlikely(bytes_recv == 0)) {
return;
}
if (unlikely(bytes_recv == -1)) {
perror("recv failed");
if (errno != EINTR) {
return;
}
}
ssize_t bytes_send = 0;
while (bytes_send < bytes_recv) {
#ifdef USE_ASYNC_IO
ssize_t new_bytes_send = emper_send(client_fd, buf, bytes_recv - bytes_send, MSG_NOSIGNAL);
#else
ssize_t new_bytes_send = send(client_fd, buf, bytes_recv - bytes_send, MSG_NOSIGNAL);
#endif
// socket was shutdown
if (unlikely(new_bytes_send == 0)) {
return;
}
if (unlikely(bytes_send == -1)) {
if (errno != EINTR) {
perror("recv failed");
return;
}
}
bytes_send += new_bytes_send;
}
}
}
#define BACKLOG 5
static void welcome_func() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
die("socket failed");
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr)); // NOLINT
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(port);
int reuseaddr = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) == -1) {
die("setsockopt failed");
}
if (bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) {
die("bind failed");
}
if (listen(sockfd, BACKLOG) != 0) {
die("listen failed");
}
for (;;) {
#ifdef USE_ASYNC_IO
int client_fd = emper_accept(sockfd, NULL, NULL);
#else
socklen_t address_len = sizeof(servaddr);
int client_fd = accept(sockfd, (struct sockaddr*)&servaddr, &address_len);
#endif
if (unlikely(client_fd < 0)) {
perror("accept failed");
}
int* passable_fd = malloc(sizeof(int));
if (unlikely(!passable_fd)) {
die("allocating passable fd failed");
}
*passable_fd = client_fd;
fiber* client_fiber = fiber_from(client_func, (void*)passable_fd);
schedule(client_fiber);
}
}
int main(int argc, char* argv[]) {
if (argc > 1) {
const int decimal = 10;
port = (int)strtol(argv[1], NULL, decimal);
}
init_runtime();
fiber* welcome_fiber = fiber_from0(welcome_func);
schedule(welcome_fiber);
wait_until_runtime_finished();
}
......@@ -10,11 +10,3 @@ cpp_exe = executable('emper-echo-server',
'emper-echo-server.cpp',
include_directories : [inc],
dependencies: dependencies)
emper_c = emper_proj.get_variable('emper_c')
c_exe = executable('emper-echo-server-c',
'emper-echo-server.c',
include_directories : [inc],
dependencies: dependencies,
link_with: emper_c)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment