int main(void) { switch_t s; chan_t c; packet_t p; path_t in; int sock; crypt_init(); s = switch_new(0); if(util_loadjson(s) != 0 || (sock = util_server(0,1000)) <= 0) { printf("failed to startup %s or %s\n", strerror(errno), crypt_err()); return -1; } printf("loaded hashname %s\n",s->id->hexname); // create/send a ping packet c = chan_new(s, bucket_get(s->seeds, 0), "link", 0); p = chan_packet(c); chan_send(c, p); util_sendall(s,sock); in = path_new("ipv4"); while(util_readone(s, sock, in) == 0) { switch_loop(s); while((c = switch_pop(s))) { printf("channel active %d %s %s\n",c->state,c->hexid,c->to->hexname); if(util_cmp(c->type,"connect") == 0) ext_connect(c); if(util_cmp(c->type,"link") == 0) ext_link(c); if(util_cmp(c->type,"path") == 0) ext_path(c); while((p = chan_pop(c))) { printf("unhandled channel packet %.*s\n", p->json_len, p->json); packet_free(p); } if(c->state == ENDED) chan_free(c); } util_sendall(s,sock); } perror("exiting"); return 0; }
// chunk the packet out void thtp_send(chan_t c, lob_t req) { lob_t chunk; unsigned char *raw; unsigned short len, space; if(!c || !req) return; DEBUG_PRINTF("THTP sending %.*s %.*s",req->json_len,req->json,req->body_len,req->body); raw = lob_raw(req); len = lob_len(req); while(len) { chunk = chan_packet(c); if(!chunk) return; // TODO backpressure space = lob_space(chunk); if(space > len) space = len; lob_body(chunk,raw,space); if(len==space) lob_set(chunk,"end","true",4); chan_send(c,chunk); raw+=space; len-=space; } }