tcp.c (606B)
1 #include "a.h" 2 3 struct Pfd 4 { 5 int fd; 6 }; 7 8 static Pfd* 9 httpconnect(char *host) 10 { 11 char buf[1024]; 12 Pfd *pfd; 13 int fd; 14 15 snprint(buf, sizeof buf, "tcp!%s!http", host); 16 if((fd = dial(buf, nil, nil, nil)) < 0) 17 return nil; 18 pfd = emalloc(sizeof *pfd); 19 pfd->fd = fd; 20 return pfd; 21 } 22 23 static void 24 httpclose(Pfd *pfd) 25 { 26 if(pfd == nil) 27 return; 28 close(pfd->fd); 29 free(pfd); 30 } 31 32 static int 33 httpwrite(Pfd *pfd, void *v, int n) 34 { 35 return writen(pfd->fd, v, n); 36 } 37 38 static int 39 httpread(Pfd *pfd, void *v, int n) 40 { 41 return read(pfd->fd, v, n); 42 } 43 44 Protocol http = { 45 httpconnect, 46 httpread, 47 httpwrite, 48 httpclose, 49 };