5 * Created by Victor Grishchenko on 3/9/09.
6 * Copyright 2009 Delft Technical University. All rights reserved.
14 #include <sys/select.h>
16 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 //#include <sys/mman.h>
30 #define TINT_SEC ((tint)1000000)
31 #define TINT_MSEC ((tint)1000)
32 #define TINT_uSEC ((tint)1)
33 #define TINT_NEVER ((tint)0x7fffffffffffffffLL)
34 #define MAXDGRAMSZ 1400
35 #define INVALID_SOCKET -1
41 struct sockaddr_in addr;
43 memset(&addr,0,sizeof(struct sockaddr_in));
45 Address(const char* ip, uint16_t port) {
46 addr.sin_family = AF_INET;
47 addr.sin_port = htons(port);
48 inet_aton(ip,&(addr.sin_addr));
50 Address(uint16_t port) {
51 addr.sin_family = AF_INET;
52 addr.sin_port = htons(port);
53 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
55 Address(const struct sockaddr_in& address) : addr(address) {}
56 operator sockaddr_in () {return addr;}
62 uint8_t buf[MAXDGRAMSZ*2];
64 static int Bind(int port);
65 static void Close(int port);
67 static int Wait (int sockcnt, int* sockets, tint usec=0);
70 Datagram (int socket, const Address addr_) : addr(addr_), offset(0),
71 length(0), sock(socket) {}
72 Datagram (int socket) : offset(0), length(0), sock(socket) {
75 int space () const { return MAXDGRAMSZ-length; }
76 int size() const { return length-offset; }
77 std::string str() const { return std::string((char*)buf+offset,size()); }
78 const uint8_t* operator * () const { return buf+offset; }
80 int Push (const uint8_t* data, int l) { // scatter-gather one day
81 int toc = l<space() ? l : space();
82 memcpy(buf+length,data,toc);
86 int Pull (uint8_t** data, int l) {
87 int toc = l<size() ? l : size();
88 //memcpy(data,buf+offset,toc);
96 const Address& address() const { return addr; }
97 void Clear() { offset=length=0; }
99 void PushString (std::string str) {
100 Push((uint8_t*)str.c_str(),str.size());
102 void Push8 (uint8_t b) {
105 void Push16 (uint16_t w) {
106 *(uint16_t*)(buf+length) = htons(w);
109 void Push32 (uint32_t i) {
110 *(uint32_t*)(buf+length) = htonl(i);
113 void Push64 (uint64_t l) {
114 *(uint32_t*)(buf+length) = htonl((uint32_t)(l>>32));
115 *(uint32_t*)(buf+length+4) = htonl((uint32_t)(l&0xffffffff));
118 void PushHash (const Sha1Hash& hash) {
119 Push(hash.bits, Sha1Hash::SIZE);
123 if (size()<1) return 0;
124 return buf[offset++];
127 if (size()<2) return 0;
129 return ntohs(*(uint16_t*)(buf+offset-2));
132 if (size()<4) return 0;
133 uint32_t i = ntohl(*(uint32_t*)(buf+offset));
138 if (size()<8) return 0;
139 uint64_t l = ntohl(*(uint32_t*)(buf+offset));
141 l |= ntohl(*(uint32_t*)(buf+offset+4));
145 Sha1Hash PullHash() {
146 if (size()<Sha1Hash::SIZE) return Sha1Hash::ZERO;
147 offset += Sha1Hash::SIZE;
148 return Sha1Hash(false,(char*)buf+offset-Sha1Hash::SIZE);
150 //std::string to_string () const ;
154 std::string sock2str (struct sockaddr_in addr);