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

Commit 02c35d7

Browse files
author
年迈的老秋风Windy
committed
Fix Linux Segmentation Fault Bug
I fixed the segmentation fault bug on linux. This works in theory, and I will test it tomorrow.
1 parent 9a04af3 commit 02c35d7

16 files changed

Lines changed: 109 additions & 123 deletions

File tree

examples/Network/Socket/DNSLookup/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <EasyCrossPlatform.h>
1+
#include <EasyCrossPlatform.h>
22
#include <iostream>
33
using namespace EasyCrossPlatform::Network::Socket;
44

examples/Network/Socket/TCPClient/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <EasyCrossPlatform.h>
1+
#include <EasyCrossPlatform.h>
22
#include <iostream>
33

44
class MySocket {

examples/Network/Socket/TCPServer_Echo/main.cpp

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,60 @@
1-
#include <EasyCrossPlatform.h>
1+
#include <EasyCrossPlatform.h>
22
#include <iostream>
33
using namespace EasyCrossPlatform::Network::Socket;
44
std::vector<TCPAsyncClientSocket*> myClients;
55
std::mutex myMutex;
6-
class MyServerFunction{
7-
public:
8-
static void onServerNewConn(void* newClientSocket, void* ServerClassPtr) {
9-
TCPAsyncClientSocket* MyClientSocket = (TCPAsyncClientSocket*) newClientSocket;
10-
TCPAsyncServerSocket* MyServer = (TCPAsyncServerSocket*)ServerClassPtr;
11-
myClients.push_back(MyClientSocket);
12-
}
13-
static void onServerError(int errCode, const std::string& errInfo, void* ServerClassPtr) {
14-
TCPAsyncServerSocket* MyServer = (TCPAsyncServerSocket*)ServerClassPtr;
15-
//std::cout << "SrvError:" << errInfo << std::endl;
16-
}
17-
static void onClientConnect(bool Succeed, void* ClientPtr){
18-
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
19-
20-
//std::cout << "ClientConnect:" << MyClient->getRemoteAddr().getIPString() << ":" << MyClient->getRemoteAddr().getPort() << std::endl;
21-
}
22-
static void onClientDisconnect(void* ClientPtr) {
23-
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
24-
//std::cout << "ClientDisconnect:" << std::endl;
25-
26-
MyClient->Destroy();
27-
28-
myMutex.lock();
29-
bool ClientsEmpty = myClients.empty();
30-
6+
class MyServerFunction {
7+
public:
8+
static void onServerNewConn(void* newClientSocket, void* ServerClassPtr) {
9+
TCPAsyncClientSocket* MyClientSocket = (TCPAsyncClientSocket*)newClientSocket;
10+
TCPAsyncServerSocket* MyServer = (TCPAsyncServerSocket*)ServerClassPtr;
11+
myClients.push_back(MyClientSocket);
12+
}
13+
static void onServerError(int errCode, const std::string& errInfo, void* ServerClassPtr) {
14+
TCPAsyncServerSocket* MyServer = (TCPAsyncServerSocket*)ServerClassPtr;
15+
std::cout << "SrvError:" << errInfo << std::endl;
16+
}
17+
static void onClientConnect(bool Succeed, void* ClientPtr) {
18+
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
19+
//std::cout << "ClientConnect:" << MyClient->getRemoteAddr().getIPString() << ":" << MyClient->getRemoteAddr().getPort() << std::endl;
20+
}
21+
static void onClientDisconnect(void* ClientPtr) {
22+
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
23+
MyClient->Destroy();
24+
25+
myMutex.lock();
26+
bool ClientsEmpty = myClients.empty();
3127

32-
if (!ClientsEmpty) {
33-
for (auto i = myClients.begin(); i != myClients.end(); i++) {
34-
if ((*i) == MyClient) {
35-
delete MyClient;
36-
i = myClients.erase(i);
37-
break;
38-
}
28+
29+
if (!ClientsEmpty) {
30+
for (auto i = myClients.begin(); i != myClients.end(); i++) {
31+
if ((*i) == MyClient) {
32+
delete MyClient;
33+
i = myClients.erase(i);
34+
break;
3935
}
4036
}
41-
myMutex.unlock();
42-
}
43-
static void onClientMsg(const std::string& data, void* ClientPtr) {
44-
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
45-
//std::cout << MyClient->getRemoteAddr().getIPString() << ":(" << data << ")" << std::endl;
46-
MyClient->SendMsg(data);
47-
}
48-
static void onClientError(int errCode, const std::string& errInfo, void* ClientPtr) {
49-
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
50-
//std::cout << "ClientError:" << errInfo << std::endl;
5137
}
38+
myMutex.unlock();
39+
}
40+
static void onClientMsg(const std::string& data, void* ClientPtr) {
41+
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
42+
//std::cout << MyClient->getRemoteAddr().getIPString() << ":(" << data << ")" << std::endl;
43+
MyClient->SendMsg(data);
44+
}
45+
static void onClientError(int errCode, const std::string& errInfo, void* ClientPtr) {
46+
TCPAsyncClientSocket* MyClient = (TCPAsyncClientSocket*)ClientPtr;
47+
//std::cout << "ClientError:" << errInfo << std::endl;
48+
}
5249
};
5350
int main(int argc, char** args) {
5451
std::cout << "hi" << std::endl;
55-
EasyCrossPlatform::Network::Socket::SocketWorker myListeningWorker;
56-
EasyCrossPlatform::Network::Socket::SocketWorker myClientWorker1;
57-
EasyCrossPlatform::Network::Socket::SocketWorker myClientWorker2;
58-
std::deque<EasyCrossPlatform::Network::Socket::SocketWorker*> myWorkers;
59-
myWorkers.push_back(&myClientWorker1);
60-
myWorkers.push_back(&myClientWorker2);
52+
SocketWorker myListeningWorker;
6153

62-
EasyCrossPlatform::Network::Socket::TCPAsyncServerSocket mSocket;
63-
mSocket.setWorkers(&myListeningWorker, myWorkers);
54+
TCPAsyncServerSocket mSocket;
55+
//On Unix/Linux systems, libuv's server socket and the socket after connection established can be only served on one uv_loop_t handle.
56+
//We are now only accepting 1 SocketWorker for each Server Socket due to cross-platform concerns.
57+
mSocket.setWorkers(&myListeningWorker);
6458
mSocket.Init();
6559
mSocket.ClientConnectCallBack = MyServerFunction::onClientConnect;
6660
mSocket.ClientDisconnectCallBack = MyServerFunction::onClientDisconnect;
@@ -69,9 +63,9 @@ int main(int argc, char** args) {
6963
mSocket.ServerErrorCallBack = MyServerFunction::onServerError;
7064
mSocket.ServerNewConnCallBack = MyServerFunction::onServerNewConn;
7165
//Binding 0.0.0.0 means binding every interface.
72-
//Keep in mind that Linux users need to use root permission to bind the socket to 700. Otherwise you need to find a bigger number like 25535.
73-
mSocket.Listen(EasyCrossPlatform::Network::Socket::IpAddr("0.0.0.0", 700, true),200);
74-
66+
//Keep in mind that Linux users need to use root permission to bind the socket to 700. Otherwise you need to find a bigger number like 25535(Port range from 1-65535).
67+
mSocket.Listen(IpAddr("0.0.0.0", 25565, true), 200);
68+
7569
std::cin.get();
7670
std::cout << "----" << std::endl;
7771
//If you want every computer in the LAN to recieve, type 255.255.255.255 for IP Address.

examples/Network/Socket/UDPClient+Server/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <EasyCrossPlatform.h>
1+
#include <EasyCrossPlatform.h>
22
#include <iostream>
33

44
class MySocket : public EasyCrossPlatform::Network::Socket::UDPAsyncSocket{

include/XSYDEncryption.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
}
6464
namespace Base64 {
6565
char const constexpr alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
66-
char const constexpr padding('=');
66+
char const constexpr padding = '=';
6767
std::string base64Encode(std::string const &plaintext);
6868
std::string base64Decode(std::string const &encryptedtext);
6969
}

include/XSYDMultiTask.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
unsigned int getRecommendedThreadNum();
99
typedef void (*SpecificWorkPtr)(std::thread::id ThreadID, void* Parameters, bool * RunningSign, std::mutex *Mutex);
1010
struct WorkInfo {
11-
SpecificWorkPtr MyWork;
12-
bool * RunningSign;
13-
std::mutex *mMutex;
14-
void * Parameters;
11+
SpecificWorkPtr MyWork = NULL;
12+
bool * RunningSign = NULL;
13+
std::mutex *mMutex = NULL;
14+
void * Parameters = NULL;
1515
};
1616
class SingleWork {
1717
private:
1818
std::thread mThread;
1919
SpecificWorkPtr MyWork;
2020
static int DoingJob(WorkInfo MyInfo);
2121
protected:
22-
bool RunningSign;
22+
bool RunningSign = false;
2323
public:
2424

2525
SingleWork();
@@ -34,9 +34,9 @@
3434
class SingleWorkCls {
3535
private:
3636
std::thread mThread;
37-
std::mutex* TempMutex;
38-
void* TempParameter;
39-
bool RunningSign;
37+
std::mutex* TempMutex = NULL;
38+
void* TempParameter = NULL;
39+
bool RunningSign = false;
4040
static int DoingJob(SingleWorkCls* ClassPtr);
4141
protected:
4242

@@ -50,29 +50,29 @@
5050
~SingleWorkCls();
5151
};
5252
struct WorkerInfo {
53-
SingleWork* wInfo;
54-
SingleWorkCls *wClsInfo;
55-
void* wParameters;
53+
SingleWork* wInfo = NULL;
54+
SingleWorkCls *wClsInfo = NULL;
55+
void* wParameters = NULL;
5656
};
5757
struct WorksInfo {
58-
std::deque<WorkerInfo>* CurrentWorksAddr;
59-
std::deque<WorkerInfo>* PendingWorksAddr;
58+
std::deque<WorkerInfo>* CurrentWorksAddr = NULL;
59+
std::deque<WorkerInfo>* PendingWorksAddr = NULL;
6060
unsigned int MaxThread;
61-
std::mutex *SharedMutex;
62-
std::mutex *LineMutex;
61+
std::mutex *SharedMutex = NULL;
62+
std::mutex *LineMutex = NULL;
6363
};
6464
class WorkPool {
6565
private:
6666
static void SuperviseThreads(std::thread::id ThreadID, void* Parameters, bool * RunningSign, std::mutex *Mutex);
67-
unsigned int MaxThread = 4;
67+
unsigned int MaxThread = 4U;
6868
std::deque<WorkerInfo> CurrentWorks;
6969
std::deque <WorkerInfo> PendingWorks;
7070
std::mutex MyMutex;
7171
std::mutex LineMutex;
72-
SingleWork* SupervisingThread;
73-
bool Started;
72+
SingleWork* SupervisingThread = NULL;
73+
bool Started = false;
7474
public:
75-
WorkPool(const unsigned int ThreadNum = 4);
75+
WorkPool(const unsigned int ThreadNum = 4U);
7676
~WorkPool();
7777
void Start();
7878
void Stop();

include/XSYDSocketResImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
friend class DNSRequest;
1616
protected:
1717
sockaddr m_Addr;
18-
bool m_isIpV4;
18+
bool m_isIpV4 = true;
1919
public:
2020
IpAddr();
2121
IpAddr(const std::string& IpAddress, const unsigned short Port, bool AddrIpV4);
@@ -50,7 +50,7 @@
5050

5151
public:
5252
SocketWorker();
53-
unsigned int m_num_Client;
53+
unsigned int m_num_Client = 0U;
5454
std::shared_ptr<uv_loop_t> m_uv_loop;
5555
EasyCrossPlatform::Thread::SingleWork m_MTManager;
5656
void Start();

include/XSYDTCPSocket.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
static std::map<SocketWorker*,std::map<uv_tcp_t*, std::vector<TCPAsyncClientSocket*>>> m_MyClassPtrs;
2222
IpAddr m_remoteAddr;
2323
std::shared_ptr<uv_tcp_t> m_ClientSocketHandle;
24-
bool m_Connected;
25-
bool Inited;
26-
bool Closing;
24+
bool m_Connected = false;
25+
bool Inited = false;
26+
bool Closing = false;
2727
static std::mutex sharedMutex;
2828
static void m_uv_connect_cb(uv_connect_t* req, int status);
2929
static void m_uv_close_cb(uv_handle_t* handle);
@@ -32,7 +32,7 @@
3232
static void m_uv_write_cb(uv_write_t* req, int status);
3333
static void m_uv_alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
3434

35-
SocketWorker* mySocketWorker;
35+
SocketWorker* mySocketWorker = NULL;
3636
void onConnected(bool Succeeded);
3737
void onMsg(const std::string& Msg);
3838
void onDisconnect();
@@ -71,18 +71,17 @@
7171
static void m_uv_connection_cb(uv_stream_t* server, int status);
7272

7373
std::shared_ptr<uv_tcp_t> m_SocketHandle;
74-
IpAddr m_myIP;
75-
bool isListening;
76-
bool hasInted;
77-
int m_QueueLength;
78-
SocketWorker* myListenWorker;
79-
std::deque<SocketWorker*> myClientWorker;
74+
IpAddr m_myIP = IpAddr();
75+
bool isListening = false;
76+
bool hasInted = false;
77+
int m_QueueLength = 0;
78+
SocketWorker* myListenWorker = NULL;
8079
public:
8180
TCPAsyncServerSocket();
8281
TCPAsyncServerSocket(const IpAddr& myIP, int QueLength);
8382
TCPAsyncServerSocket(const TCPAsyncServerSocket& oldServer);
8483

85-
void setWorkers(SocketWorker* listeningWorker, std::deque<SocketWorker*>& clientWorkers);
84+
void setWorkers(SocketWorker* Worker);
8685
void Init();
8786
void Destroy();
8887

src/XSYDChrono.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <XSYDChrono.h>
1+
#include <XSYDChrono.h>
22

33
double EasyCrossPlatform::Chrono::systemTime()
44
{

src/XSYDDNS.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <XSYDDNS.h>
1+
#include <XSYDDNS.h>
22

33
void EasyCrossPlatform::Network::Socket::DNSRequest::m_uv_resolved_cb(uv_getaddrinfo_t * resolver, int status, addrinfo * res)
44
{
@@ -133,7 +133,7 @@ void EasyCrossPlatform::Network::Socket::DNSRequest::Init()
133133
this->onProgress = false;
134134
this->m_RequestHandle = new uv_getaddrinfo_t;
135135
this->m_RequestHandle->data = (void*) this;
136-
if (SocketParam::m_num_Client == 0) {
136+
if (SocketParam::m_num_Client == 0U) {
137137
uv_loop_init(&SocketParam::m_uv_loop);
138138
SocketParam::Start();
139139
}
@@ -148,7 +148,7 @@ void EasyCrossPlatform::Network::Socket::DNSRequest::Destroy()
148148
this->Inited = false;
149149
delete this->m_RequestHandle;
150150
SocketParam::m_num_Client--;
151-
if (SocketParam::m_num_Client == 0) {
151+
if (SocketParam::m_num_Client == 0U) {
152152
uv_stop(&SocketParam::m_uv_loop);
153153
SocketParam::Stop();
154154
}

0 commit comments

Comments
 (0)