Commit 47ceee58 authored by Florian Fischer's avatar Florian Fischer
Browse files

use port 12345 as default and merge send_all into client_func

parent e49c52a0
......@@ -16,31 +16,13 @@
#define unlikely(x) __builtin_expect(!!(x), 1)
int port;
static int port = 12345;
_Noreturn void die(const char* msg) {
_Noreturn static void die(const char* msg) {
perror(msg);
exit(EXIT_FAILURE);
}
ssize_t send_all(int fd, const void* buf, size_t len) {
size_t bytes_written = 0;
while (bytes_written < len) {
#ifdef USE_ASYNC_IO
ssize_t new_bytes_written = sync_send(fd, buf, len - bytes_written, MSG_NOSIGNAL);
#else
ssize_t new_bytes_written = send(fd, buf, len - bytes_written, MSG_NOSIGNAL);
#endif
if (unlikely(new_bytes_written < 1)) {
return new_bytes_written;
}
bytes_written += new_bytes_written;
}
return (ssize_t)bytes_written;
}
#define BUF_MAX 4096
void client_func(void* arg) {
int client_fd = *(int*)arg;
......@@ -64,18 +46,28 @@ void client_func(void* arg) {
return;
}
}
ssize_t bytes_send = send_all(client_fd, buf, bytes_recv);
ssize_t bytes_send = 0;
while (bytes_send < bytes_recv) {
#ifdef USE_ASYNC_IO
ssize_t new_bytes_send = sync_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(bytes_send == 0)) {
if (unlikely(new_bytes_send == 0)) {
return;
}
if (unlikely(bytes_send == -1)) {
perror("recv failed");
if (errno != EINTR) {
perror("recv failed");
return;
}
}
bytes_send += new_bytes_send;
}
}
}
......@@ -130,13 +122,10 @@ static void welcome_func() {
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Please give a port number: ./%s [port]\n", argv[0]);
exit(EXIT_FAILURE);
}
if (argc > 1) {
const int decimal = 10;
port = (int)strtol(argv[1], NULL, decimal);
}
init_runtime();
......
Supports Markdown
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