forked from aoki42/cpp_features
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstract.cpp
127 lines (114 loc) · 3.39 KB
/
abstract.cpp
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
#include "abstract.h"
#include <boost/regex.hpp>
namespace network {
char const* proto_type_s[] = {
"unkown",
"tcp",
"udp",
"tls",
"http",
"https",
"zk",
};
proto_type str2proto(std::string const& s)
{
static int n = sizeof(proto_type_s) / sizeof(char const*);
for (int i = 0; i < n; ++i)
if (strcmp(s.c_str(), proto_type_s[i]) == 0)
return proto_type(i);
return proto_type::unkown;
}
std::string proto2str(proto_type proto)
{
static int n = sizeof(proto_type_s) / sizeof(char const*);
if ((int)proto >= n)
return proto_type_s[0];
return proto_type_s[(int)proto];
}
Protocol Protocol::v4()
{
return Protocol(0);
}
Protocol Protocol::v6()
{
return Protocol(0);
}
int Protocol::type() const
{
switch (proto_)
{
case proto_type::unkown:
break;
case proto_type::tcp:
return ::boost::asio::ip::tcp::v4().type();
case proto_type::udp:
return ::boost::asio::ip::udp::v4().type();
case proto_type::tls:
break;
case proto_type::http:
return ::boost::asio::ip::tcp::v4().type();
case proto_type::https:
break;
case proto_type::zk:
break;
}
return 0;
}
int Protocol::protocol() const
{
switch (proto_)
{
case proto_type::unkown:
break;
case proto_type::tcp:
return ::boost::asio::ip::tcp::v4().protocol();
case proto_type::udp:
return ::boost::asio::ip::udp::v4().protocol();
case proto_type::tls:
break;
case proto_type::http:
return ::boost::asio::ip::tcp::v4().protocol();
case proto_type::https:
break;
case proto_type::zk:
break;
}
return 0;
}
int Protocol::family() const
{
return family_;
}
std::string Protocol::endpoint::to_string(boost_ec & ec) const
{
std::string url;
if (proto_ != proto_type::unkown) {
url += proto2str(proto_) + "://";
}
url += address().to_string(ec);
if (ec) return "";
url += ":";
url += std::to_string(port());
url += path_;
return url;
}
endpoint Protocol::endpoint::from_string(std::string const& url, boost_ec & ec)
{
if (url.empty()) {
ec = MakeNetworkErrorCode(eNetworkErrorCode::ec_url_parse_error);
return endpoint();
}
static ::boost::regex re("((.*)://)?([^:/]+)(:(\\d+))?(/.*)?");
boost::smatch result;
bool ok = boost::regex_match(url, result, re);
if (!ok) {
ec = MakeNetworkErrorCode(eNetworkErrorCode::ec_url_parse_error);
return endpoint();
}
endpoint ep(::boost::asio::ip::address::from_string(result[3].str(), ec), atoi(result[5].str().c_str()));
if (ec) return endpoint();
ep.proto_ = str2proto(result[2].str());
ep.path_ = result[6].str();
return ep;
}
} //namespace network