more examples

This commit is contained in:
David du Colombier 2014-08-03 18:59:15 +02:00
parent 02b74edba1
commit 58868245a2
3 changed files with 131 additions and 2 deletions

View File

@ -14,7 +14,7 @@ OFILES=\
task.o\
ip.o\
all: $(LIB) primes tcpproxy testdelay
all: $(LIB) echo httpload primes tcpload tcpproxy testdelay
$(OFILES): taskimpl.h task.h 386-ucontext.h power-ucontext.h ip.h
@ -32,9 +32,15 @@ CFLAGS=-Wall -Wextra -c -I. -ggdb
$(LIB): $(OFILES)
ar rvc $(LIB) $(OFILES)
echo: echo.o $(LIB)
$(CC) -o echo echo.o $(LIB)
primes: primes.o $(LIB)
$(CC) -o primes primes.o $(LIB)
tcpload: tcpload.o $(LIB)
$(CC) -o tcpload tcpload.o $(LIB)
tcpproxy: tcpproxy.o $(LIB)
$(CC) -o tcpproxy tcpproxy.o $(LIB) $(TCPLIBS)
@ -48,7 +54,7 @@ testdelay1: testdelay1.o $(LIB)
$(CC) -o testdelay1 testdelay1.o $(LIB)
clean:
rm -f *.o primes tcpproxy testdelay testdelay1 httpload $(LIB)
rm -f *.o echo primes tcpload tcpproxy httpload testdelay testdelay1 $(LIB)
install: $(LIB)
cp $(LIB) /usr/local/lib

70
echo.c Normal file
View File

@ -0,0 +1,70 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <task.h>
#include <stdlib.h>
#include <sys/socket.h>
enum
{
STACK = 32768
};
static int verbose;
char *server;
void echotask(void*);
int*
mkfd2(int fd1, int fd2)
{
int *a;
a = malloc(2*sizeof a[0]);
if(a == 0){
fprintf(stderr, "out of memory\n");
abort();
}
a[0] = fd1;
a[1] = fd2;
return a;
}
void
taskmain(int argc, char **argv)
{
int cfd, fd;
int rport;
char remote[46];
if(argc != 3){
fprintf(stderr, "usage: tcpproxy server port\n");
taskexitall(1);
}
server = argv[1];
if((fd = netannounce(TCP, 0, atoi(argv[2]))) < 0){
fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno));
taskexitall(1);
}
fdnoblock(fd);
while((cfd = netaccept(fd, remote, &rport)) >= 0){
if(verbose)
fprintf(stderr, "connection from %s:%d\n", remote, rport);
taskcreate(echotask, (void*)(uintptr_t)cfd, STACK);
}
}
void
echotask(void *v)
{
char buf[512];
int fd, n;
fd = (int)(uintptr_t)v;
while((n = fdread(fd, buf, sizeof buf)) > 0)
fdwrite(fd, buf, n);
close(fd);
}

53
tcpload.c Normal file
View File

@ -0,0 +1,53 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <task.h>
#include <stdlib.h>
enum
{
STACK = 32768
};
char *server;
int port;
void fetchtask(void*);
void
taskmain(int argc, char **argv)
{
int i, n;
if(argc != 4){
fprintf(stderr, "usage: httpload n server port\n");
taskexitall(1);
}
n = atoi(argv[1]);
server = argv[2];
port = atoi(argv[3]);
for(i=0; i<n; i++)
taskcreate(fetchtask, 0, STACK);
}
void
fetchtask(void *v)
{
int fd, i;
char buf[512];
(void)v;
for(i=0; i<1000; i++){
if((fd = netdial(TCP, server, port)) < 0){
fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate());
continue;
}
snprintf(buf, sizeof buf, "xxxxxxxxxx");
fdwrite(fd, buf, strlen(buf));
fdread(fd, buf, sizeof buf);
close(fd);
}
}