Skip to content
This repository was archived by the owner on Jul 15, 2021. It is now read-only.

Commit 6764abd

Browse files
author
年迈的老秋风Windy
committed
Implement DNS Lookup
We changed the implemention of socket(which Ipv4 and Ipv6 is no longer separated). We also add a DNS Lookup Cls in the EasyCrossPlatform::Network::Socket namespace
1 parent 2f607e5 commit 6764abd

14 files changed

Lines changed: 554 additions & 1064 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <EasyCrossPlatform.h>
2+
#include <iostream>
3+
using namespace EasyCrossPlatform::Network::Socket;
4+
5+
int main(int argc, char** args) {
6+
std::cout << "hi" << std::endl;
7+
DNSRequest myRequest;
8+
myRequest.Init();
9+
DNSResult myResult = myRequest.getDomainAddr_IPv4v6("www.xsyds.cn",80);
10+
std::cout << "Request " << (myResult.NumResults != 0 ? "succeeded" : "failed") << std::endl;
11+
if (myResult.NumResults > 0) {
12+
for (auto iter = myResult.Ips.begin(); iter != myResult.Ips.end(); iter++) {
13+
std::cout << "IpAddress: " << (*iter).getIPString() << std::endl;
14+
}
15+
}
16+
myRequest.Destroy();
17+
18+
std::cin.get();
19+
20+
return 0;
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <EasyCrossPlatform.h>
2+
#include <iostream>
3+
4+
class MySocket{
5+
public:
6+
static void ConnectCB(bool Succeeded, void* ClassPtr){
7+
EasyCrossPlatform::Network::Socket::TCPAsyncClientSocket* MyClass = (EasyCrossPlatform::Network::Socket::TCPAsyncClientSocket*) ClassPtr;
8+
std::cout << "Socket Connected" << std::endl;
9+
}
10+
static void MsgCB(const std::string& Msg, void* ClassPtr){
11+
EasyCrossPlatform::Network::Socket::TCPAsyncClientSocket* MyClass = (EasyCrossPlatform::Network::Socket::TCPAsyncClientSocket*) ClassPtr;
12+
std::cout << "SocketMsg:" << Msg << std::endl;
13+
}
14+
void DisconnectCB(void* ClassPtr){
15+
EasyCrossPlatform::Network::Socket::TCPAsyncClientSocket* MyClass = (EasyCrossPlatform::Network::Socket::TCPAsyncClientSocket*) ClassPtr;
16+
std::cout << "SocketDis" << std::endl;
17+
}
18+
void ErrorCB(int errCode, const std::string& errDescription, void* ClassPtr){
19+
EasyCrossPlatform::Network::Socket::TCPAsyncClientSocket* MyClass = (EasyCrossPlatform::Network::Socket::TCPAsyncClientSocket*) ClassPtr;
20+
std::cout << "Error:" << errDescription << std::endl;
21+
}
22+
};
23+
int main(int argc, char** args) {
24+
std::cout << "hi" << std::endl;
25+
26+
EasyCrossPlatform::Network::Socket::TCPAsyncClientSocket mSocket;
27+
mSocket.Init();
28+
//Set CallBack
29+
mSocket.ConnectCallBack = MySocket::ConnectCB;
30+
mSocket.MsgCallBack = MySocket::MsgCB;
31+
mSocket.DisconnectCallBack = MySocket::DisconnectCB;
32+
mSocket.ErrorCallBack = MySocket::ErrorCB;
33+
//Set CallBack finish.
34+
mSocket.setRemoteIPAddr(EasyCrossPlatform::Network::Socket::IpAddr("127.0.0.1",700, true));
35+
mSocket.Connect();
36+
system("pause");
37+
mSocket.SendMsg("2333");
38+
system("pause");
39+
mSocket.Disconnect();
40+
mSocket.Destroy();
41+
return 0;
42+
}

examples/Network/Socket/TCPClientIPv4/main.cpp

Lines changed: 0 additions & 45 deletions
This file was deleted.

examples/Network/Socket/TCPServerIPv4_Echo/main.cpp renamed to examples/Network/Socket/TCPServer_Echo/main.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
#include <EasyCrossPlatform.h>
22
#include <iostream>
33
using namespace EasyCrossPlatform::Network::Socket;
4-
std::vector<TCPAsyncClientSocketv4*> myClients;
4+
std::vector<TCPAsyncClientSocket*> myClients;
55
class MyServerFunction{
66
public:
7-
static void onServerNewConn(TCPAsyncClientSocketv4* newClientSocket, void* ServerClassPtr) {
8-
TCPAsyncServerSocketv4* MyServer = (TCPAsyncServerSocketv4*)ServerClassPtr;
7+
static void onServerNewConn(TCPAsyncClientSocket* newClientSocket, void* ServerClassPtr) {
8+
TCPAsyncServerSocket* MyServer = (TCPAsyncServerSocket*)ServerClassPtr;
99
myClients.push_back(newClientSocket);
1010
}
1111
static void onServerError(int errCode, const std::string& errInfo, void* ServerClassPtr) {
12-
TCPAsyncServerSocketv4* MyServer = (TCPAsyncServerSocketv4*)ServerClassPtr;
12+
TCPAsyncServerSocket* MyServer = (TCPAsyncServerSocket*)ServerClassPtr;
1313
//std::cout << "SrvError:" << errInfo << std::endl;
1414
}
1515
static void onClientConnect(bool Succeed, void* ClientPtr){
16-
TCPAsyncClientSocketv4* MyClient = (TCPAsyncClientSocketv4*)ClientPtr;
16+
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
1717

1818
//std::cout << "ClientConnect:" << MyClient->getRemoteAddr().getIPString() << ":" << MyClient->getRemoteAddr().getPort() << std::endl;
1919
}
2020
static void onClientDisconnect(void* ClientPtr) {
21-
TCPAsyncClientSocketv4* MyClient = (TCPAsyncClientSocketv4*)ClientPtr;
21+
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
2222
//std::cout << "ClientDisconnect:" << std::endl;
2323
MyClient->Destroy();
2424
for (auto i = myClients.begin(); i != myClients.end(); i++) {
@@ -30,19 +30,19 @@ class MyServerFunction{
3030
}
3131
}
3232
static void onClientMsg(const std::string& data, void* ClientPtr) {
33-
TCPAsyncClientSocketv4* MyClient = (TCPAsyncClientSocketv4*)ClientPtr;
33+
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
3434
//std::cout << MyClient->getRemoteAddr().getIPString() << ":(" << data << ")" << std::endl;
3535
MyClient->SendMsg(data);
3636
}
3737
static void onClientError(int errCode, const std::string& errInfo, void* ClientPtr) {
38-
TCPAsyncClientSocketv4* MyClient = (TCPAsyncClientSocketv4*)ClientPtr;
38+
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
3939
//std::cout << "ClientError:" << errInfo << std::endl;
4040
}
4141
};
4242
int main(int argc, char** args) {
4343
std::cout << "hi" << std::endl;
4444

45-
EasyCrossPlatform::Network::Socket::TCPAsyncServerSocketv4 mSocket;
45+
EasyCrossPlatform::Network::Socket::TCPAsyncServerSocket mSocket;
4646
mSocket.Init();
4747
mSocket.ClientConnectCallBack = MyServerFunction::onClientConnect;
4848
mSocket.ClientDisconnectCallBack = MyServerFunction::onClientDisconnect;
@@ -51,7 +51,7 @@ int main(int argc, char** args) {
5151
mSocket.ServerErrorCallBack = MyServerFunction::onServerError;
5252
mSocket.ServerNewConnCallBack = MyServerFunction::onServerNewConn;
5353
//Binding 0.0.0.0 means binding every interface.
54-
mSocket.Listen(EasyCrossPlatform::Network::Socket::IpAddrV4("0.0.0.0", 700),200);
54+
mSocket.Listen(EasyCrossPlatform::Network::Socket::IpAddr("0.0.0.0", 700, true),200);
5555
//system("pause");
5656
std::cin.get();
5757
std::cout << "----" << std::endl;

examples/Network/Socket/UDPClient+ServerIPv4/main.cpp renamed to examples/Network/Socket/UDPClient+Server/main.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#include <EasyCrossPlatform.h>
22
#include <iostream>
33

4-
class MySocket : public EasyCrossPlatform::Network::Socket::UDPAsyncSocketv4{
4+
class MySocket : public EasyCrossPlatform::Network::Socket::UDPAsyncSocket{
55
public:
66
//Inherit from Constructor, You can choose not to inherit constructor as long as you use Class.Init() and Class.Destroy() in your code.
7-
MySocket() : UDPAsyncSocketv4() {}
8-
MySocket(const EasyCrossPlatform::Network::Socket::IpAddrV4& RemoteIP) : UDPAsyncSocketv4(RemoteIP) {}
9-
~MySocket() { UDPAsyncSocketv4::~UDPAsyncSocketv4(); }
7+
MySocket() : UDPAsyncSocket() {}
8+
MySocket(const EasyCrossPlatform::Network::Socket::IpAddr& RemoteIP) : UDPAsyncSocket(RemoteIP) {}
9+
~MySocket() { UDPAsyncSocket::~UDPAsyncSocket(); }
1010
//Inherit Finish
1111

12-
void onMsg(const EasyCrossPlatform::Network::Socket::IpAddrV4& SourceIP, const std::string& data){
13-
std::cout << "SocketMsg:(" << ((EasyCrossPlatform::Network::Socket::IpAddrV4) SourceIP).getIPString() << "), " << data << std::endl;
12+
void onMsg(const EasyCrossPlatform::Network::Socket::IpAddr& SourceIP, const std::string& data){
13+
std::cout << "SocketMsg:(" << ((EasyCrossPlatform::Network::Socket::IpAddr) SourceIP).getIPString() << "), " << data << std::endl;
1414
}
1515
void onError(int errCode, const std::string& errDescription) {
1616
std::cout << "Error:" << errDescription << std::endl;
@@ -21,9 +21,10 @@ int main(int argc, char** args) {
2121

2222
MySocket mSocket;
2323
mSocket.Init();
24-
mSocket.Listen(EasyCrossPlatform::Network::Socket::IpAddrV4("0.0.0.0", 700));
24+
mSocket.Listen(EasyCrossPlatform::Network::Socket::IpAddr("0.0.0.0", 700, true));
25+
//UDP: If you want every computer in the LAN to recieve, type 255.255.255.255 for IP Address.
2526
system("pause");
26-
mSocket.sendMsg(EasyCrossPlatform::Network::Socket::IpAddrV4("127.0.0.1", 700),"Hi");
27+
mSocket.sendMsg(EasyCrossPlatform::Network::Socket::IpAddr("127.0.0.1", 700, true),"Hi");
2728
system("pause");
2829
mSocket.StopListen();
2930
mSocket.Destroy();

include/EasyCrossPlatform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
#include <XSYDJson.h>
1111
#include <XSYDTCPSocket.h>
1212
#include <XSYDUDPSocket.h>
13+
#include <XSYDDNS.h>
1314
#endif //__EasyCrossPlatformFile__

include/XSYDDNS.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef __EasyCrossPlatform_DNSQueryFile__
2+
#define __EasyCrossPlatform_DNSQueryFile__
3+
#include <EasyCP_Common.h>
4+
#include <XSYDSocketResImpl.h>
5+
#include <libuv/uv.h>
6+
namespace EasyCrossPlatform {
7+
namespace Network {
8+
namespace Socket {
9+
struct DNSResult {
10+
unsigned int NumResults;
11+
std::vector<IpAddr> Ips;
12+
};
13+
class DNSRequest {
14+
private:
15+
16+
protected:
17+
uv_getaddrinfo_t* m_RequestHandle;
18+
bool Inited;
19+
addrinfo m_RequestHints;
20+
bool onProgress;
21+
DNSResult valuebackArray;
22+
bool progressSucceed;
23+
static void m_uv_resolved_cb(uv_getaddrinfo_t* resolver, int status, struct addrinfo *res);
24+
void resetHint();
25+
public:
26+
DNSRequest();
27+
DNSRequest(const DNSRequest& oldRequest);
28+
29+
DNSResult getDomainAddr_IPv4v6(const std::string& Domain, unsigned short Port);
30+
DNSResult getDomainAddr_IPv4Only(const std::string& Domain, unsigned short Port);
31+
DNSResult getDomainAddr_IPv6Only(const std::string& Domain, unsigned short Port);
32+
void Init();
33+
void Destroy();
34+
35+
~DNSRequest();
36+
};
37+
}
38+
}
39+
}
40+
#endif

include/XSYDSocketResImpl.h

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,29 @@
66
namespace EasyCrossPlatform {
77
namespace Network {
88
namespace Socket {
9-
//IpV4地址类封装, 绑定全部网卡填0.0.0.0
10-
class IpAddrV4 {
11-
friend class TCPAsyncClientSocketv4;
12-
friend class UDPAsyncSocketv4;
13-
private:
14-
15-
protected:
16-
sockaddr_in m_Addr;
17-
public:
18-
IpAddrV4();
19-
IpAddrV4(const std::string& IpAddress, const unsigned short Port);
20-
IpAddrV4(const char *IpAddress, const unsigned short Port);
21-
bool setIPAddress(const std::string& IpAddress, const unsigned short Port);
22-
bool setIPAddress(const char * IpAddress, const unsigned short Port);
23-
bool setIPAddress(const sockaddr_in& newAddr);
24-
std::string getIPString();
25-
unsigned short getPort();
26-
sockaddr_in getIPAddress();
27-
};
28-
class IpAddrV6 {
29-
friend class TCPAsyncClientSocketv6;
30-
friend class UDPAsyncSocketv6;
31-
private:
32-
33-
protected:
34-
sockaddr_in6 m_Addr;
35-
public:
36-
IpAddrV6();
37-
IpAddrV6(const std::string& IpAddress, const unsigned short Port);
38-
IpAddrV6(const char *IpAddress, const unsigned short Port);
39-
bool setIPAddress(const std::string& IpAddress, const unsigned short Port);
40-
bool setIPAddress(const char * IpAddress, const unsigned short Port);
41-
bool setIPAddress(const sockaddr_in6& newAddr);
42-
std::string getIPString();
43-
unsigned short getPort();
44-
sockaddr_in6 getIPAddress();
9+
//IpV4绑定全部网卡填0.0.0.0
10+
//IpV6绑定全部网卡填::
11+
class IpAddr {
12+
friend class UDPAsyncSocket;
13+
friend class TCPAsyncClientSocket;
14+
friend class TCPAsyncServerSocket;
15+
friend class DNSRequest;
16+
protected:
17+
sockaddr m_Addr;
18+
bool m_isIpV4;
19+
public:
20+
IpAddr();
21+
IpAddr(const std::string& IpAddress, const unsigned short Port, bool AddrIpV4);
22+
IpAddr(const char* IpAddress, const unsigned short Port, bool AddrIpV4);
23+
IpAddr(const sockaddr& newAddr);
24+
IpAddr(const IpAddr& oldAddr);
25+
bool setIPAddress(const std::string& IpAddress, const unsigned short Port, bool AddrIpV4);
26+
bool setIPAddress(const char* IpAddress, const unsigned short Port, bool AddrIpV4);
27+
bool setIPAddress(const sockaddr& newAddr);
28+
std::string getIPString();
29+
bool addrIsIPV4();
30+
unsigned short getPort();
31+
sockaddr getIPAddress();
4532
};
4633
class SocketParam {
4734
private:

0 commit comments

Comments
 (0)