Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5142bd6
Showing
6 changed files
with
758 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <assert.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <netinet/in.h> | ||
#include <netinet/tcp.h> | ||
#include <pthread.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
|
||
int num_threads = 10000; | ||
unsigned short port = 11111; | ||
int listen_sock; | ||
|
||
void* start_thread(void* _unused) | ||
{ | ||
int fd, r, r2; | ||
char buf[4096]; | ||
|
||
while (1) { | ||
fd = accept(listen_sock, NULL, NULL); | ||
if (fd == -1) | ||
continue; | ||
r2 = 1; | ||
r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &r2, sizeof(r2)); | ||
assert(r == 0); | ||
|
||
while (1) { | ||
r = read(fd, buf, sizeof(buf)); | ||
if (r == 0 || (r == -1 && errno != EINTR)) { | ||
break; | ||
} | ||
r2 = write(fd, buf, r); | ||
assert(r == r2); | ||
} | ||
close(fd); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
int ch, i, r, flag; | ||
struct sockaddr_in listen_addr; | ||
pthread_attr_t tattr; | ||
|
||
while ((ch = getopt(argc, argv, "c:p:")) != -1) { | ||
switch (ch) { | ||
case 'c': | ||
assert(sscanf(optarg, "%d", &num_threads) == 1); | ||
break; | ||
case 'p': | ||
assert(sscanf(optarg, "%hu", &port) == 1); | ||
break; | ||
default: | ||
exit(1); | ||
} | ||
} | ||
|
||
listen_sock = socket(AF_INET, SOCK_STREAM, 0); | ||
assert(listen_sock != -1); | ||
flag = 1; | ||
r = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); | ||
assert(r == 0); | ||
listen_addr.sin_family = AF_INET; | ||
listen_addr.sin_port = htons(port); | ||
listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
r = bind(listen_sock, (struct sockaddr*)&listen_addr, sizeof(listen_addr)); | ||
assert(r == 0); | ||
r = listen(listen_sock, SOMAXCONN); | ||
assert(r == 0); | ||
|
||
pthread_attr_init(&tattr); | ||
pthread_attr_setstacksize(&tattr, 65536); | ||
for (i = 0; i < num_threads; ++i) { | ||
pthread_t tid; | ||
pthread_create(&tid, &tattr, start_thread, NULL); | ||
} | ||
|
||
while (1) { | ||
sleep(60); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include <assert.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <netinet/in.h> | ||
#include <netinet/tcp.h> | ||
#include <pthread.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#include "picohttpparser.h" | ||
|
||
int num_threads = 10000; | ||
unsigned short port = 11111; | ||
int listen_sock; | ||
|
||
void* start_thread(void* _unused) | ||
{ | ||
char buf[16384]; | ||
const char* method, * path; | ||
size_t method_len, path_len, num_headers; | ||
int fd, minor_version, r, r2; | ||
struct phr_header headers[128]; | ||
|
||
while (1) { | ||
fd = accept(listen_sock, NULL, NULL); | ||
if (fd == -1) | ||
continue; | ||
r2 = 1; | ||
r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &r2, sizeof(r2)); | ||
assert(r == 0); | ||
|
||
while (1) { | ||
r = read(fd, buf, sizeof(buf)); | ||
if (r == 0 || (r == -1 && errno != EINTR)) { | ||
break; | ||
} | ||
num_headers = sizeof(headers) / sizeof(headers[0]); | ||
r = phr_parse_request(buf, r, &method, &method_len, &path, &path_len, | ||
&minor_version, headers, &num_headers, 0); | ||
assert(r > 0); | ||
#define RES "HTTP\1.0 200 OK\r\n" \ | ||
"Connection: keep-alive\r\n" \ | ||
"Content-Length: 13\r\n" \ | ||
"Content-Type: text/plain\r\n" \ | ||
"\r\n" \ | ||
"hello world!\n" | ||
r = write(fd, RES, sizeof(RES) - 1); | ||
assert(r == sizeof(RES) - 1); | ||
#undef RES | ||
} | ||
close(fd); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
int ch, i, r, flag; | ||
struct sockaddr_in listen_addr; | ||
pthread_attr_t tattr; | ||
|
||
while ((ch = getopt(argc, argv, "c:p:")) != -1) { | ||
switch (ch) { | ||
case 'c': | ||
assert(sscanf(optarg, "%d", &num_threads) == 1); | ||
break; | ||
case 'p': | ||
assert(sscanf(optarg, "%hu", &port) == 1); | ||
break; | ||
default: | ||
exit(1); | ||
} | ||
} | ||
|
||
listen_sock = socket(AF_INET, SOCK_STREAM, 0); | ||
assert(listen_sock != -1); | ||
flag = 1; | ||
r = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); | ||
assert(r == 0); | ||
listen_addr.sin_family = AF_INET; | ||
listen_addr.sin_port = htons(port); | ||
listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
r = bind(listen_sock, (struct sockaddr*)&listen_addr, sizeof(listen_addr)); | ||
assert(r == 0); | ||
r = listen(listen_sock, SOMAXCONN); | ||
assert(r == 0); | ||
|
||
pthread_attr_init(&tattr); | ||
pthread_attr_setstacksize(&tattr, 65536); | ||
for (i = 0; i < num_threads; ++i) { | ||
pthread_t tid; | ||
pthread_create(&tid, &tattr, start_thread, NULL); | ||
} | ||
|
||
while (1) { | ||
sleep(60); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#include <assert.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <netinet/in.h> | ||
#include <netinet/tcp.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#include "picoev.h" | ||
|
||
unsigned short port = 11111; | ||
int listen_sock; | ||
|
||
static void setup_sock(int fd) | ||
{ | ||
int on = 1, r; | ||
|
||
r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); | ||
assert(r == 0); | ||
r = fcntl(fd, F_SETFL, O_NONBLOCK); | ||
assert(r == 0); | ||
} | ||
|
||
static void read_cb(picoev_loop* loop, int fd, int revents, void* cb_arg) | ||
{ | ||
char buf[256]; | ||
int r, r2; | ||
|
||
assert((revents & PICOEV_READ) != 0); | ||
r = read(fd, buf, sizeof(buf)); | ||
if (r == 0) { | ||
goto CLOSE; | ||
} else if (r == -1) { | ||
if (errno == EINTR || errno == EAGAIN) { | ||
return; | ||
} | ||
goto CLOSE; | ||
} | ||
r2 = write(fd, buf, r); | ||
assert(r == r2); | ||
return; | ||
|
||
CLOSE: | ||
picoev_del(loop, fd); | ||
close(fd); | ||
} | ||
|
||
void* start_thread(void* _unused) | ||
{ | ||
int fd, r, r2; | ||
char buf[4096]; | ||
|
||
while (1) { | ||
fd = accept(listen_sock, NULL, NULL); | ||
if (fd == -1) | ||
continue; | ||
r2 = 1; | ||
r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &r2, sizeof(r2)); | ||
assert(r == 0); | ||
|
||
while (1) { | ||
r = read(fd, buf, sizeof(buf)); | ||
if (r == 0 || (r == -1 && errno != EINTR)) { | ||
break; | ||
} | ||
r2 = write(fd, buf, r); | ||
assert(r == r2); | ||
} | ||
close(fd); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
static void accept_cb(picoev_loop* loop, int fd, int revents, void* cb_arg) | ||
{ | ||
int newfd; | ||
assert((revents & PICOEV_READ) != 0); | ||
if ((newfd = accept(fd, NULL, NULL)) != -1) { | ||
setup_sock(newfd); | ||
picoev_add(loop, newfd, PICOEV_READ, 0, read_cb, NULL); | ||
} | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
int ch, r, flag; | ||
struct sockaddr_in listen_addr; | ||
picoev_loop* loop; | ||
|
||
while ((ch = getopt(argc, argv, "p:")) != -1) { | ||
switch (ch) { | ||
case 'p': | ||
assert(sscanf(optarg, "%hu", &port) == 1); | ||
break; | ||
default: | ||
exit(1); | ||
} | ||
} | ||
|
||
listen_sock = socket(AF_INET, SOCK_STREAM, 0); | ||
assert(listen_sock != -1); | ||
flag = 1; | ||
r = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); | ||
assert(r == 0); | ||
listen_addr.sin_family = AF_INET; | ||
listen_addr.sin_port = htons(port); | ||
listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
r = bind(listen_sock, (struct sockaddr*)&listen_addr, sizeof(listen_addr)); | ||
assert(r == 0); | ||
setup_sock(listen_sock); | ||
r = listen(listen_sock, SOMAXCONN); | ||
assert(r == 0); | ||
|
||
picoev_init(1048576 + 10); | ||
loop = picoev_create_loop(60); | ||
picoev_add(loop, listen_sock, PICOEV_READ, 0, accept_cb, NULL); | ||
while (1) { | ||
picoev_loop_once(loop, 10); | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.