Skip to content

Commit 6f49e48

Browse files
committed
replace QUdpSocket to sockfd
1 parent ae4c15a commit 6f49e48

File tree

11 files changed

+544
-187
lines changed

11 files changed

+544
-187
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ target_wrapper.*
4141

4242
# QtCreator CMake
4343
CMakeLists.txt.user*
44+
45+
*.docx
46+
*.vsdx
47+
.vs
48+
.vscode
49+
*.code-workspace

DogcomSocket.cpp

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#ifdef WIN32
2+
#include <winsock2.h>
3+
#else
4+
#include <sys/socket.h>
5+
#include <netinet/in.h>
6+
#include <arpa/inet.h>
7+
#include <string.h>
8+
#include <unistd.h>
9+
#endif
10+
#include <string>
11+
#include <time.h>
12+
#include <QDebug>
13+
#include "DogcomSocket.h"
14+
15+
using std::string;
16+
17+
char DogcomSocket::AUTH_SERVER[20] = "10.100.61.3";
18+
19+
DogcomSocket::DogcomSocket() {
20+
}
21+
22+
void DogcomSocket::init(){
23+
int r;
24+
#ifdef WIN32
25+
WORD sockVersion = MAKEWORD(2, 2);
26+
WSADATA wsadata;
27+
if ((r = WSAStartup(sockVersion, &wsadata)) != 0) {
28+
throw DogcomSocketException(DogcomError::WSA_START_UP, r);
29+
}
30+
#endif
31+
32+
memset(&bind_addr, 0, sizeof(bind_addr));
33+
bind_addr.sin_family = AF_INET;
34+
#ifdef WIN32
35+
bind_addr.sin_addr.S_un.S_addr = inet_addr("0.0.0.0");
36+
#else
37+
bind_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
38+
#endif
39+
bind_addr.sin_port = htons(BIND_PORT);
40+
41+
memset(&dest_addr, 0, sizeof(dest_addr));
42+
dest_addr.sin_family = AF_INET;
43+
#ifdef WIN32
44+
dest_addr.sin_addr.S_un.S_addr = inet_addr(AUTH_SERVER);
45+
#else
46+
dest_addr.sin_addr.s_addr = inet_addr(AUTH_SERVER);
47+
#endif
48+
dest_addr.sin_port = htons(DEST_PORT);
49+
50+
srand(time(nullptr));
51+
52+
// create socket
53+
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
54+
#ifdef WIN32
55+
throw DogcomSocketException(DogcomError::SOCKET, WSAGetLastError());
56+
#else
57+
throw DogcomSocketException(DogcomError::SOCKET,sockfd);
58+
#endif
59+
}
60+
61+
// bind socket
62+
if (bind(sockfd, (struct sockaddr *) &bind_addr, sizeof(bind_addr)) < 0) {
63+
#ifdef WIN32
64+
throw DogcomSocketException(DogcomError::BIND, WSAGetLastError());
65+
#else
66+
throw DogcomSocketException(DogcomError::BIND,sockfd);
67+
#endif
68+
}
69+
70+
// set timeout
71+
#ifdef WIN32
72+
int timeout = 3000;
73+
#else
74+
struct timeval timeout;
75+
timeout.tv_sec = 3;
76+
timeout.tv_usec = 0;
77+
#endif
78+
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout,
79+
sizeof(timeout)) < 0) {
80+
#ifdef WIN32
81+
throw DogcomSocketException(DogcomError::SET_SOCK_OPT_TIMEOUT,
82+
WSAGetLastError());
83+
#else
84+
throw DogcomSocketException(DogcomError::SET_SOCK_OPT_TIMEOUT,
85+
sockfd);
86+
#endif
87+
}
88+
89+
//set port reuse
90+
91+
int optval = 1;
92+
#ifdef WIN32
93+
if ((r = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *) &optval, sizeof(optval))) < 0) {
94+
throw DogcomSocketException(DogcomError::SET_SOCK_OPT_REUSE, r);
95+
}
96+
#else
97+
if ((r = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *) &optval, sizeof(optval))) < 0) {
98+
throw DogcomSocketException(DogcomError::SET_SOCK_OPT_REUSE,r);
99+
}
100+
101+
if ((r = setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (char *) &optval, sizeof(optval))) < 0) {
102+
throw DogcomSocketException(DogcomError::SET_SOCK_OPT_REUSE,r);
103+
}
104+
#endif
105+
}
106+
107+
int DogcomSocket::write(const char *buf, int len) {
108+
return sendto(sockfd, buf, len, 0, (struct sockaddr *) &dest_addr, sizeof(dest_addr));
109+
}
110+
111+
int DogcomSocket::read(char *buf) {
112+
#ifdef WIN32
113+
int addrlen = sizeof(dest_addr);
114+
#else
115+
socklen_t addrlen=sizeof(dest_addr);
116+
#endif
117+
return recvfrom(sockfd, buf, 1024, 0, (struct sockaddr *) &dest_addr, &addrlen);
118+
}
119+
120+
DogcomSocket::~DogcomSocket() {
121+
qDebug()<<"DogcomSocket destructor";
122+
#ifdef WIN32
123+
if (sockfd != -1)
124+
closesocket(sockfd);
125+
WSACleanup();
126+
#else
127+
if (sockfd != -1)
128+
close(sockfd);
129+
#endif
130+
qDebug()<<"socket closed";
131+
}
132+
133+
const char *DogcomSocketException::what() const noexcept {
134+
static char buf[1024];
135+
switch (errCode) {
136+
case DogcomError::WSA_START_UP:
137+
snprintf(buf, sizeof(buf),
138+
"WSAStartup failed. Error code: %d",
139+
realErrCode);
140+
break;
141+
case DogcomError::SOCKET:
142+
snprintf(buf, sizeof(buf),
143+
"socket failed. Error code: %d",
144+
realErrCode);
145+
break;
146+
case DogcomError::BIND:
147+
snprintf(buf, sizeof(buf),
148+
"bind failed. Error code: %d",
149+
realErrCode);
150+
break;
151+
case DogcomError::SET_SOCK_OPT_TIMEOUT:
152+
snprintf(buf,sizeof(buf),
153+
"timeout failed. Error code: %d",
154+
realErrCode);
155+
break;
156+
case DogcomError::SET_SOCK_OPT_REUSE:
157+
snprintf(buf, sizeof(buf),
158+
"port reuse failed. Error code: %d",
159+
realErrCode);
160+
break;
161+
}
162+
return buf;
163+
}
164+
165+
DogcomSocketException::DogcomSocketException(int errCode, int realErrCode)
166+
: errCode(errCode), realErrCode(realErrCode) {
167+
}

DogcomSocket.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#ifndef MYUDPSOCKET_DOGCOMSOCKET_H
2+
#define MYUDPSOCKET_DOGCOMSOCKET_H
3+
4+
#include <exception>
5+
6+
#ifdef WIN32
7+
#include <winsock2.h>
8+
#else
9+
#include <sys/socket.h>
10+
#include <netinet/in.h>
11+
#include <arpa/inet.h>
12+
#include <string.h>
13+
#include <unistd.h>
14+
#endif
15+
16+
namespace DogcomError {
17+
enum DogcomSocketError {
18+
WSA_START_UP = 0x10,
19+
SOCKET,
20+
BIND,
21+
SET_SOCK_OPT_TIMEOUT,
22+
SET_SOCK_OPT_REUSE
23+
};
24+
}
25+
26+
class DogcomSocketException : public std::exception {
27+
public:
28+
int errCode = -1;
29+
int realErrCode = -1;
30+
const char *what() const noexcept override;
31+
DogcomSocketException(int errCode, int realErrCode);
32+
};
33+
34+
class DogcomSocket {
35+
private:
36+
int sockfd = -1;
37+
struct sockaddr_in bind_addr;
38+
struct sockaddr_in dest_addr;
39+
const static int BIND_PORT = 61440;
40+
const static int DEST_PORT = 61440;
41+
static char AUTH_SERVER[20];
42+
43+
public:
44+
DogcomSocket();
45+
void init();
46+
int write(const char *buf, int len);
47+
int read(char *buf);
48+
49+
virtual ~DogcomSocket();
50+
51+
};
52+
53+
#endif //MYUDPSOCKET_DOGCOMSOCKET_H

DrCOM_JLU_Qt.pro

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ SOURCES += \
3636
dogcom.cpp \
3737
encrypt/md4.cpp \
3838
encrypt/md5.cpp \
39-
encrypt/sha1.cpp
39+
encrypt/sha1.cpp \
40+
DogcomSocket.cpp
4041

4142
HEADERS += \
4243
mainwindow.h \
@@ -46,7 +47,8 @@ HEADERS += \
4647
dogcom.h \
4748
encrypt/md4.h \
4849
encrypt/md5.h \
49-
encrypt/sha1.h
50+
encrypt/sha1.h \
51+
DogcomSocket.h
5052

5153
FORMS += \
5254
mainwindow.ui
@@ -65,6 +67,7 @@ DEFINES += QAPPLICATION_CLASS=QApplication
6567

6668
VERSION = 1.0.0.5
6769

70+
win32:LIBS += -lwsock32
6871
# 更新日志:
6972
# v 0.0.0.0 实现基本功能
7073
# v 1.0.0.1 修复适配高DPI时只窗口大小适配但字号不适配的bug

constants.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ enum {
77
// 离线原因
88
OFF_UNKNOWN,
99
OFF_USER_LOGOUT,
10-
OFF_BIND_FAILED,
1110
OFF_CHALLENGE_FAILED,
1211
OFF_CHECK_MAC,
1312
OFF_SERVER_BUSY,
@@ -22,6 +21,14 @@ enum {
2221
OFF_MUST_USE_DHCP,
2322
OFF_TIMEOUT,
2423

24+
// 初始化失败的原因
25+
OFF_WSA_STARTUP,
26+
OFF_CREATE_SOCKET,
27+
OFF_BIND_FAILED,
28+
OFF_SET_SOCKET_TIMEOUT,
29+
OFF_SET_SOCKET_REUSE,
30+
31+
2532
// challenge 成功 获取到服务器返回的ip地址
2633
OBTAIN_IP_ADDRESS,
2734

0 commit comments

Comments
 (0)