#else
#include <arpa/inet.h>
#endif
-//#define RND_DROP 10
#include <glog/logging.h>
#include "datagram.h"
tint Datagram::now = Datagram::Time();
tint Datagram::epoch = now;
uint32_t Datagram::Address::LOCALHOST = INADDR_LOOPBACK;
+uint64_t Datagram::dgrams_up=0, Datagram::dgrams_down=0,
+ Datagram::bytes_up=0, Datagram::bytes_down=0;
char* Datagram::TimeStr (tint time) {
static char ret_str[128];
}
int Datagram::Send () {
-#ifdef RND_DROP
- if (rand()%RND_DROP==0) {
- Time();
- dprintf("%s datagram killed\n",TimeStr());
- return size();
- }
-#endif
int r = sendto(sock,(const char *)buf+offset,length-offset,0,
(struct sockaddr*)&(addr.addr),sizeof(struct sockaddr_in));
//offset=0;
//length=0;
+ dgrams_up++;
+ bytes_up+=size();
Time();
return r;
}
#else
PLOG(ERROR)<<"on recv";
#endif
+ dgrams_down++;
+ bytes_down+=length;
Time();
return length;
}
SOCKET Datagram::Wait (int sockcnt, SOCKET* sockets, tint usec) {
- dprintf("waiting (%i socks)\n",sockcnt);
struct timeval timeout;
timeout.tv_sec = usec/TINT_SEC;
timeout.tv_usec = usec%TINT_SEC;
static char* TimeStr(tint time=0);
static SOCKET Wait (int sockcnt, SOCKET* sockets, tint usec=0);
static tint now, epoch;
+ static uint64_t dgrams_up, dgrams_down, bytes_up, bytes_down;
Datagram (SOCKET socket, const Address addr_) : addr(addr_), offset(0),
length(0), sock(socket) {}
void p2tp::Shutdown (int sock_des) {
for(int i=0; i<Channel::socket_count; i++)
- if (Channel::sockets[i]==sock_des)
+ if (sock_des==-1 || Channel::sockets[i]==sock_des) {
+ Datagram::Close(Channel::sockets[i]);
Channel::sockets[i] = Channel::sockets[--Channel::socket_count];
- Datagram::Close(sock_des);
+ }
}
/** Run send/receive loop for the specified amount of time. */
void Loop (tint till);
/** Stop listening to a port. */
- void Shutdown (int sock_des);
+ void Shutdown (int sock_des=-1);
/** Open a file, start a transmission; fill it with content for a given root hash;
in case the hash is omitted, the file is a fresh submit. */