#ifndef DATAGRAM_H
#define DATAGRAM_H
-#include <stdlib.h>
-#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
-#include <stdio.h>
-#include <string>
-#ifdef _WIN32
- #include <winsock2.h>
- #include "compat.h"
-#else
- typedef int SOCKET;
-
- #include <arpa/inet.h>
- #include <sys/select.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
-#endif
#include "hashtree.h"
#include "compat.h"
Address(const char* ip_port);
Address(uint16_t port) {
clear();
- set_ipv4(LOCALHOST);
+ set_ipv4((uint32_t)INADDR_ANY);
set_port(port);
}
Address(uint32_t ipv4addr, uint16_t port) {
};
+typedef void (*sockcb_t) (SOCKET);
+struct sckrwecb_t {
+ sckrwecb_t (SOCKET s=0, sockcb_t mr=NULL, sockcb_t mw=NULL, sockcb_t oe=NULL) :
+ sock(s), may_read(mr), may_write(mw), on_error(oe) {}
+ SOCKET sock;
+ sockcb_t may_read;
+ sockcb_t may_write;
+ sockcb_t on_error;
+};
+
+
/** UDP datagram class, a nice wrapping around sendto/recvfrom/select.
Reading/writing from/to a datagram is done in a FIFO (deque) fashion:
written data is appended to the tail (push) while read data is
int offset, length;
uint8_t buf[MAXDGRAMSZ*2];
+#define DGRAM_MAX_SOCK_OPEN 128
+ static int sock_count;
+ static sckrwecb_t sock_open[DGRAM_MAX_SOCK_OPEN];
+
public:
/** bind to the address */
- static SOCKET Bind(Address address);
+ static SOCKET Bind(Address address, sckrwecb_t callbacks=sckrwecb_t());
+
/** close the port */
- static void Close(int port);
+ static void Close(SOCKET sock);
+
/** the current time */
static tint Time();
+
/** wait till one of the sockets has some io to do; usec is the timeout */
- static SOCKET Wait (int sockcnt, SOCKET* sockets, tint usec=0);
+ static SOCKET Wait (tint usec);
+
+ static bool Listen3rdPartySocket (sckrwecb_t cb) ;
+
+ static void Shutdown ();
+
+ static SOCKET default_socket()
+ { return sock_count ? sock_open[0].sock : INVALID_SOCKET; }
+
static tint now, epoch, start;
static uint64_t dgrams_up, dgrams_down, bytes_up, bytes_down;