Fixed network lib bug on linux

This commit is contained in:
AlexandreRouma 2021-07-16 18:13:18 +02:00
parent ead7ee153a
commit 45e4c21870

View File

@ -61,7 +61,7 @@ namespace net {
} }
int ConnClass::read(int count, uint8_t* buf) { int ConnClass::read(int count, uint8_t* buf) {
assert(connectionOpen); if (!connectionOpen) { return -1; }
std::lock_guard lck(readMtx); std::lock_guard lck(readMtx);
#ifdef _WIN32 #ifdef _WIN32
int ret = recv(_sock, (char*)buf, count, 0); int ret = recv(_sock, (char*)buf, count, 0);
@ -79,7 +79,7 @@ namespace net {
} }
bool ConnClass::write(int count, uint8_t* buf) { bool ConnClass::write(int count, uint8_t* buf) {
assert(connectionOpen); if (!connectionOpen) { return false; }
std::lock_guard lck(writeMtx); std::lock_guard lck(writeMtx);
#ifdef _WIN32 #ifdef _WIN32
int ret = send(_sock, (char*)buf, count, 0); int ret = send(_sock, (char*)buf, count, 0);
@ -97,7 +97,7 @@ namespace net {
} }
void ConnClass::readAsync(int count, uint8_t* buf, void (*handler)(int count, uint8_t* buf, void* ctx), void* ctx) { void ConnClass::readAsync(int count, uint8_t* buf, void (*handler)(int count, uint8_t* buf, void* ctx), void* ctx) {
assert(connectionOpen); if (!connectionOpen) { return; }
// Create entry // Create entry
ConnReadEntry entry; ConnReadEntry entry;
entry.count = count; entry.count = count;
@ -116,7 +116,7 @@ namespace net {
} }
void ConnClass::writeAsync(int count, uint8_t* buf) { void ConnClass::writeAsync(int count, uint8_t* buf) {
assert(connectionOpen); if (!connectionOpen) { return; }
// Create entry // Create entry
ConnWriteEntry entry; ConnWriteEntry entry;
entry.count = count; entry.count = count;
@ -194,7 +194,7 @@ namespace net {
} }
Conn ListenerClass::accept() { Conn ListenerClass::accept() {
assert(listening); if (!listening) { return NULL; }
std::lock_guard lck(acceptMtx); std::lock_guard lck(acceptMtx);
Socket _sock; Socket _sock;
@ -210,7 +210,7 @@ namespace net {
} }
void ListenerClass::acceptAsync(void (*handler)(Conn conn, void* ctx), void* ctx) { void ListenerClass::acceptAsync(void (*handler)(Conn conn, void* ctx), void* ctx) {
assert(listening); if (!listening) { return; }
// Create entry // Create entry
ListenerAcceptEntry entry; ListenerAcceptEntry entry;
entry.handler = handler; entry.handler = handler;
@ -231,16 +231,17 @@ namespace net {
std::lock_guard lck(acceptQueueMtx); std::lock_guard lck(acceptQueueMtx);
stopWorker = true; stopWorker = true;
} }
acceptQueueCnd.notify_all();
if (listening) { if (listening) {
#ifdef _WIN32 #ifdef _WIN32
closesocket(sock); closesocket(sock);
#else #else
::shutdown(sock, SHUT_RDWR);
::close(sock); ::close(sock);
#endif #endif
} }
acceptQueueCnd.notify_all();
if (acceptWorkerThread.joinable()) { acceptWorkerThread.join(); } if (acceptWorkerThread.joinable()) { acceptWorkerThread.join(); }