/* * datagram.h * serp++ * * Created by Victor Grishchenko on 3/9/09. * Copyright 2009 Delft Technical University. All rights reserved. * */ #include #include #include #include #include #include #include #include //#include #include #include #include #include #include "hashtree.h" typedef uint64_t tint; #define MAXDGRAMSZ 1400 struct Datagram { struct sockaddr_in addr; int sock; int offset, length; uint8_t buf[MAXDGRAMSZ]; static int Bind(int port); static void Close(int port); static tint Time(); static int Wait (int sockcnt, int* sockets, tint usec=0); Datagram (int socket, struct sockaddr_in& addr_) : addr(addr_), offset(0), length(0), sock(socket) {} Datagram (int socket) : offset(0), length(0), sock(socket) { memset(&addr,0,sizeof(struct sockaddr_in)); } int space () const { return MAXDGRAMSZ-length; } int size() const { return length-offset; } std::string str() const { return std::string((char*)buf+offset,size()); } int Push (const uint8_t* data, int l) { // scatter-gather one day int toc = l>32)); *(uint32_t*)(buf+length+4) = htonl((uint32_t)(l&0xffffffff)); length+=8; } void PushHash (const Sha1Hash& hash) { Push(hash.bits, Sha1Hash::SIZE); } uint8_t Pull8() { if (size()<1) return 0; return buf[offset++]; } uint16_t Pull16() { if (size()<2) return 0; offset+=2; return ntohs(*(uint16_t*)(buf+offset-2)); } uint32_t Pull32() { if (size()<4) return 0; uint32_t i = ntohl(*(uint32_t*)(buf+offset)); offset+=4; return i; } uint64_t Pull64() { if (size()<8) return 0; uint64_t l = ntohl(*(uint32_t*)(buf+offset)); l<<=32; l |= ntohl(*(uint32_t*)(buf+offset+4)); offset+=8; return l; } Sha1Hash PullHash() { if (size()