forked from aoki42/cpp_features
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_detail.h
133 lines (115 loc) · 3.03 KB
/
udp_detail.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#pragma once
#include <string>
#include <stdint.h>
#include <atomic>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/enable_shared_from_this.hpp>
#include <boost/function.hpp>
#include <coroutine/coroutine.h>
#include "error.h"
#include "abstract.h"
#include "option.h"
namespace network {
namespace udp_detail {
using namespace boost::asio;
using namespace boost::asio::ip;
using boost_ec = boost::system::error_code;
using boost::shared_ptr;
class UdpPointImpl;
struct _udp_sess_id_t : public ::network::SessionIdBase
{
shared_ptr<UdpPointImpl> udp_point;
udp::endpoint remote_addr;
_udp_sess_id_t(shared_ptr<UdpPointImpl> const& point,
udp::endpoint const& addr)
: udp_point(point), remote_addr(addr)
{}
};
typedef shared_ptr<_udp_sess_id_t> udp_sess_id_t;
class UdpPointImpl
: public Options<UdpPointImpl>, public boost::enable_shared_from_this<UdpPointImpl>
{
public:
UdpPointImpl();
boost_ec goStart(endpoint addr);
void Shutdown();
boost_ec Send(std::string const& host, uint16_t port, const void* data, std::size_t bytes);
boost_ec Send(udp::endpoint destition, const void* data, std::size_t bytes);
boost_ec Connect(endpoint addr);
boost_ec Send(const void* data, size_t bytes);
udp::endpoint LocalAddr();
udp::endpoint RemoteAddr();
udp_sess_id_t GetSessId();
private:
virtual void OnSetMaxPackSize() override;
boost_ec goStart(udp::endpoint local_endpoint);
void DoRecv();
static io_service& GetUdpIoService();
private:
udp::endpoint local_addr_;
udp::endpoint remote_addr_;
shared_ptr<udp::socket> socket_;
std::atomic<bool> shutdown_{false};
std::atomic<bool> init_{false};
Buffer recv_buf_;
};
class UdpPoint
: public Options<UdpPoint>, public ServerBase, public ClientBase
{
public:
UdpPoint() : impl_(new UdpPointImpl())
{
Link(*impl_);
}
virtual ~UdpPoint()
{
Shutdown();
}
boost_ec goStart(endpoint addr)
{
return impl_->goStart(addr);
}
void Shutdown()
{
return impl_->Shutdown();
}
boost_ec Send(std::string const& host, uint16_t port, const void* data, std::size_t bytes)
{
return impl_->Send(host, port, data, bytes);
}
boost_ec Send(udp::endpoint destition, const void* data, std::size_t bytes)
{
return impl_->Send(destition, data, bytes);
}
boost_ec Connect(endpoint addr)
{
return impl_->Connect(addr);
}
boost_ec Send(const void* data, size_t bytes)
{
return impl_->Send(data, bytes);
}
udp::endpoint LocalAddr()
{
return impl_->LocalAddr();
}
udp::endpoint RemoteAddr()
{
return impl_->RemoteAddr();
}
OptionsBase* GetOptions()
{
return this;
}
SessionId GetSessId()
{
return impl_->GetSessId();
}
private:
shared_ptr<UdpPointImpl> impl_;
};
typedef UdpPoint UdpServer;
typedef UdpPoint UdpClient;
} //namespace udp_detail
} //namespace network