Skip to content

Commit 5886824

Browse files
committed
more examples
1 parent 02b74ed commit 5886824

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

Makefile

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ OFILES=\
1414
task.o\
1515
ip.o\
1616

17-
all: $(LIB) primes tcpproxy testdelay
17+
all: $(LIB) echo httpload primes tcpload tcpproxy testdelay
1818

1919
$(OFILES): taskimpl.h task.h 386-ucontext.h power-ucontext.h ip.h
2020

@@ -32,9 +32,15 @@ CFLAGS=-Wall -Wextra -c -I. -ggdb
3232
$(LIB): $(OFILES)
3333
ar rvc $(LIB) $(OFILES)
3434

35+
echo: echo.o $(LIB)
36+
$(CC) -o echo echo.o $(LIB)
37+
3538
primes: primes.o $(LIB)
3639
$(CC) -o primes primes.o $(LIB)
3740

41+
tcpload: tcpload.o $(LIB)
42+
$(CC) -o tcpload tcpload.o $(LIB)
43+
3844
tcpproxy: tcpproxy.o $(LIB)
3945
$(CC) -o tcpproxy tcpproxy.o $(LIB) $(TCPLIBS)
4046

@@ -48,7 +54,7 @@ testdelay1: testdelay1.o $(LIB)
4854
$(CC) -o testdelay1 testdelay1.o $(LIB)
4955

5056
clean:
51-
rm -f *.o primes tcpproxy testdelay testdelay1 httpload $(LIB)
57+
rm -f *.o echo primes tcpload tcpproxy httpload testdelay testdelay1 $(LIB)
5258

5359
install: $(LIB)
5460
cp $(LIB) /usr/local/lib

echo.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <errno.h>
4+
#include <unistd.h>
5+
#include <task.h>
6+
#include <stdlib.h>
7+
#include <sys/socket.h>
8+
9+
enum
10+
{
11+
STACK = 32768
12+
};
13+
14+
static int verbose;
15+
16+
char *server;
17+
void echotask(void*);
18+
19+
int*
20+
mkfd2(int fd1, int fd2)
21+
{
22+
int *a;
23+
24+
a = malloc(2*sizeof a[0]);
25+
if(a == 0){
26+
fprintf(stderr, "out of memory\n");
27+
abort();
28+
}
29+
a[0] = fd1;
30+
a[1] = fd2;
31+
return a;
32+
}
33+
34+
void
35+
taskmain(int argc, char **argv)
36+
{
37+
int cfd, fd;
38+
int rport;
39+
char remote[46];
40+
41+
if(argc != 3){
42+
fprintf(stderr, "usage: tcpproxy server port\n");
43+
taskexitall(1);
44+
}
45+
server = argv[1];
46+
47+
if((fd = netannounce(TCP, 0, atoi(argv[2]))) < 0){
48+
fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno));
49+
taskexitall(1);
50+
}
51+
fdnoblock(fd);
52+
while((cfd = netaccept(fd, remote, &rport)) >= 0){
53+
if(verbose)
54+
fprintf(stderr, "connection from %s:%d\n", remote, rport);
55+
taskcreate(echotask, (void*)(uintptr_t)cfd, STACK);
56+
}
57+
}
58+
59+
void
60+
echotask(void *v)
61+
{
62+
char buf[512];
63+
int fd, n;
64+
65+
fd = (int)(uintptr_t)v;
66+
67+
while((n = fdread(fd, buf, sizeof buf)) > 0)
68+
fdwrite(fd, buf, n);
69+
close(fd);
70+
}

tcpload.c

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <errno.h>
4+
#include <unistd.h>
5+
#include <task.h>
6+
#include <stdlib.h>
7+
8+
enum
9+
{
10+
STACK = 32768
11+
};
12+
13+
char *server;
14+
int port;
15+
16+
void fetchtask(void*);
17+
18+
void
19+
taskmain(int argc, char **argv)
20+
{
21+
int i, n;
22+
23+
if(argc != 4){
24+
fprintf(stderr, "usage: httpload n server port\n");
25+
taskexitall(1);
26+
}
27+
n = atoi(argv[1]);
28+
server = argv[2];
29+
port = atoi(argv[3]);
30+
31+
for(i=0; i<n; i++)
32+
taskcreate(fetchtask, 0, STACK);
33+
}
34+
35+
void
36+
fetchtask(void *v)
37+
{
38+
int fd, i;
39+
char buf[512];
40+
41+
(void)v;
42+
43+
for(i=0; i<1000; i++){
44+
if((fd = netdial(TCP, server, port)) < 0){
45+
fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate());
46+
continue;
47+
}
48+
snprintf(buf, sizeof buf, "xxxxxxxxxx");
49+
fdwrite(fd, buf, strlen(buf));
50+
fdread(fd, buf, sizeof buf);
51+
close(fd);
52+
}
53+
}

0 commit comments

Comments
 (0)