C++ 重新获得WinSock2包装器的设计思想
让我们创建类C++ 重新获得WinSock2包装器的设计思想,c++,sockets,networking,winsock,winsock2,C++,Sockets,Networking,Winsock,Winsock2,让我们创建类Ip::Address和Ip::Endpoint 第一个设计理念。 Ip::Address Ip::Address::Address(const std::string& stringRepresentation) Ip::Address Ip::Address::Address(const std::string& stringRepresentation) 此类将在内部表示为\u addr中的,或者在6\u addr中的 Ip::Endpoint Ip::En
Ip::Address
和Ip::Endpoint
第一个设计理念。
Ip::Address
Ip::Address::Address(const std::string& stringRepresentation)
Ip::Address
Ip::Address::Address(const std::string& stringRepresentation)
此类将在内部表示为\u addr中的,或者在6\u addr
中的
Ip::Endpoint
Ip::Endpoint::Endpoint(const Ip::Address& address, const std::uint16_t port)
此类将包含一个Ip::Address
,并且在内部表示为sockaddr\u in
或sockaddr\u in 6
Ip::Endpoint
Ip::Endpoint::Endpoint(const Ip::Address& address, const std::uint16_t port)
这是我想出的设计,但我对它不是很满意。原因如下。让我们以IPv4为例。Ip::Endpoint
在内部表示为sockaddr\u in
,其中包含in\u addr
。由于Ip::Endpoint
还包含一个Ip::Address
,该地址在内部表示为in_addr
,因此in_addr
结构在Ip::Endpoint
中出现两次
第二个设计理念。
Ip::Address
Ip::Address::Address(const std::string& stringRepresentation)
Ip::Address
Ip::Address::Address(const std::string& stringRepresentation)
此类将在内部表示为
中的sockaddr\u或6中的sockaddr\u
Ip::Endpoint
Ip::Endpoint::Endpoint(const Ip::Address& address, const std::uint16_t port)
此类将包含一个Ip::Address
,并使用其内部表示来内部表示自身-Ip::Endpoint
将依赖于Ip::Address::internal
(sockaddr\u in
或sockaddr\u in 6
)
对于Ip::Endpoint
,这种方法将消除在\u addr
/的\u addr6
结构中有两个。但这也会使Ip::Address
的“内部表示”太大。一个Ip::Address
只需要in\u addr
,而不需要sockaddr\u in
你的想法是什么?只是一个想法,看看boost::asio
,它有一个很好的设计(IMO,其他人会不同意…)@Nim,asio的ip::endpoint在内部表示为/sockaddr\u in 6中的sockaddr\u。当您从中请求ip::address时,它将从sockaddr\u in::in\u addr中重建。我真的不喜欢在构造函数中获取Ip::Address和port,然后扔掉Ip::Address——只是在库的用户执行Ip::Endpoint::Address()时重建它。