#include #include #include #include #include #include #include #include #include #include #include "lair.h" /* Required because of proccreate argument funciton signature */ typedef struct Arg{ Channel *c; int fd; char ldir[40]; } Arg; void conhandler(void *arg) { char buf; int dfd; Arg *a = (Arg*)arg; dfd = accept(a->fd, a->ldir); if(dfd < 0){ fprint(2, "Error opening data fd: %r\n"); threadexits(nil); } while(read(dfd, &buf, 1) == 1) send(a->c, &buf); close(dfd); close(a->fd); threadexits(nil); } void conmanager(void *arg) { int acfd; char adir[40]; Channel *c = arg; Arg a = {c, 0, ""}; threadsetgrp(5); acfd = announce("tcp!*!327", adir); if(acfd < 0){ fprint(2, "Could not announce: %r\n"); threadexits(nil); } for(;;){ a.fd = listen(adir, a.ldir); if(a.fd < 0){ fprint(2, "Could not listen: %r\n"); threadexits(nil); } proccreate(conhandler, &a, 1024); } } Channel* coninit(int buffsize) { Channel *c = chancreate(sizeof(char), buffsize); proccreate(conmanager, c, 1024); return c; } void condestroy(Channel *c) { chanfree(c); threadkillgrp(5); }