forked from aping-fo/reliablyt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPBase.h
70 lines (50 loc) · 1.31 KB
/
UDPBase.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* qianqians
* 2014-9-11
*/
#include <WinSock2.h>
#include <map>
#include <vector>
#include <boost/atomic.hpp>
#include <boost/signals2.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/cstdint.hpp>
#pragma comment(lib, "WS2_32.lib")
class UDPSession;
typedef boost::function<void(time_t) > timefn;
struct OVERLAPPEDEX{
boost::function<void(int, int) > fn;
OVERLAPPED op;
};
class UDPBase{
public:
UDPBase(char * ip, short port);
~UDPBase();
public:
/*
* post a time callback
*/
void post_timer(time_t t, timefn fn);
protected:
virtual void handle_receive_from(char * recvbuf, int len, sockaddr_in & remote_addr, int error) = 0;
virtual void handle_send_to(boost::shared_ptr<char> sendbuf, int len, int error);
void send_to(boost::shared_ptr<char> buf, int len, sockaddr_in & remote_addr);
private:
void run();
void _handle_receive_from(boost::shared_ptr<WSABUF> recvbuf, boost::shared_ptr<sockaddr_in> remote_addr, int len, int error);
void handle_time();
protected:
boost::mutex maptimefnmutex;
std::vector<std::pair<time_t, timefn> > vinserttimer;
std::map<time_t, timefn> maptimefn;
boost::uint64_t ttime;
boost::uint64_t t;
clock_t tclock;
boost::uint64_t unixtime();
private:
SOCKET s;
HANDLE h;
boost::thread_group th_group;
boost::atomic_bool iswork;
};