2020-12-16 01:59:00 +01:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2023-04-10 11:45:58 +02:00
|
|
|
#include <utils/TCPServer.hpp>
|
2020-12-16 01:59:00 +01:00
|
|
|
|
|
|
|
#include <network/net.h>
|
2023-04-10 11:45:58 +02:00
|
|
|
#include <utils/logger.h>
|
2020-12-16 01:59:00 +01:00
|
|
|
|
|
|
|
#define wiiu_errno (*__gh_errno_ptr())
|
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
TCPServer::TCPServer(int32_t port, int32_t priority) {
|
|
|
|
this->port = port;
|
|
|
|
this->sockfd = -1;
|
2020-12-16 01:59:00 +01:00
|
|
|
this->clientfd = -1;
|
2023-04-10 11:45:58 +02:00
|
|
|
memset(&(this->sock_addr), 0, sizeof(this->sock_addr));
|
2020-12-16 01:59:00 +01:00
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
pThread = CThread::create(TCPServer::DoTCPThread, (void *) this, CThread::eAttributeAffCore2, priority);
|
2020-12-16 01:59:00 +01:00
|
|
|
pThread->resumeThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPServer::~TCPServer() {
|
|
|
|
CloseSockets();
|
2020-12-16 02:04:31 +01:00
|
|
|
//DEBUG_FUNCTION_LINE("Thread will be closed");
|
2020-12-16 01:59:00 +01:00
|
|
|
exitThread = 1;
|
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
ICInvalidateRange((void *) &exitThread, 4);
|
|
|
|
DCFlushRange((void *) &exitThread, 4);
|
2020-12-16 01:59:00 +01:00
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
if (pThread != NULL) {
|
2020-12-16 02:04:31 +01:00
|
|
|
//DEBUG_FUNCTION_LINE("Deleting it!");
|
2020-12-16 01:59:00 +01:00
|
|
|
delete pThread;
|
|
|
|
}
|
2020-12-16 02:04:31 +01:00
|
|
|
//DEBUG_FUNCTION_LINE("Thread done");
|
2020-12-16 01:59:00 +01:00
|
|
|
pThread = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPServer::CloseSockets() {
|
|
|
|
if (this->sockfd != -1) {
|
2021-09-19 21:16:03 +02:00
|
|
|
close(this->sockfd);
|
2020-12-16 01:59:00 +01:00
|
|
|
}
|
|
|
|
if (this->clientfd != -1) {
|
2021-09-19 21:16:03 +02:00
|
|
|
close(this->clientfd);
|
2020-12-16 01:59:00 +01:00
|
|
|
}
|
2023-04-10 11:45:58 +02:00
|
|
|
this->sockfd = -1;
|
2020-12-16 01:59:00 +01:00
|
|
|
this->clientfd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPServer::ErrorHandling() {
|
|
|
|
CloseSockets();
|
2023-04-10 11:45:58 +02:00
|
|
|
OSSleepTicks(OSMicrosecondsToTicks(1000 * 1000 * 2));
|
2020-12-16 01:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TCPServer::DoTCPThreadInternal() {
|
|
|
|
int32_t ret;
|
|
|
|
socklen_t len;
|
|
|
|
connected = false;
|
|
|
|
while (1) {
|
2023-04-10 11:45:58 +02:00
|
|
|
if (exitThread) {
|
2020-12-16 01:59:00 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-04-10 11:45:58 +02:00
|
|
|
memset(&(this->sock_addr), 0, sizeof(sock_addr));
|
|
|
|
sock_addr.sin_family = AF_INET;
|
|
|
|
sock_addr.sin_port = this->port;
|
2020-12-16 01:59:00 +01:00
|
|
|
sock_addr.sin_addr.s_addr = 0;
|
|
|
|
|
|
|
|
this->sockfd = ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2023-04-10 11:45:58 +02:00
|
|
|
if (ret == -1) {
|
2020-12-16 01:59:00 +01:00
|
|
|
ErrorHandling();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int32_t enable = 1;
|
|
|
|
|
|
|
|
setsockopt(this->sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
|
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
ret = bind(this->sockfd, (sockaddr *) &sock_addr, 16);
|
|
|
|
if (ret < 0) {
|
2020-12-16 01:59:00 +01:00
|
|
|
ErrorHandling();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ret = listen(this->sockfd, 1);
|
2023-04-10 11:45:58 +02:00
|
|
|
if (ret < 0) {
|
2020-12-16 01:59:00 +01:00
|
|
|
ErrorHandling();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2020-12-16 02:04:31 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Waiting for a connection");
|
2023-04-10 11:45:58 +02:00
|
|
|
if (exitThread) {
|
2020-12-16 01:59:00 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-04-10 11:45:58 +02:00
|
|
|
len = 16;
|
|
|
|
clientfd = ret = accept(sockfd, (sockaddr *) &(sock_addr), &len);
|
2020-12-16 01:59:00 +01:00
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
if (ret == -1) {
|
2020-12-16 01:59:00 +01:00
|
|
|
ErrorHandling();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
if (!acceptConnection()) {
|
2020-12-16 01:59:00 +01:00
|
|
|
ErrorHandling();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
connected = true;
|
|
|
|
|
2020-12-16 02:04:31 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Connection accepted");
|
2020-12-16 01:59:00 +01:00
|
|
|
|
|
|
|
whileLoop();
|
|
|
|
|
2020-12-16 02:04:31 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Client disconnected");
|
2020-12-16 01:59:00 +01:00
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
if (clientfd != -1) {
|
2021-09-19 21:16:03 +02:00
|
|
|
close(clientfd);
|
2020-12-16 01:59:00 +01:00
|
|
|
}
|
|
|
|
clientfd = -1;
|
2023-04-10 11:45:58 +02:00
|
|
|
} while (0);
|
2020-12-16 02:04:31 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Closing TCPServer");
|
2020-12-16 01:59:00 +01:00
|
|
|
connected = false;
|
|
|
|
onConnectionClosed();
|
|
|
|
CloseSockets();
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-16 02:04:31 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Ending DoTCPThreadInternal");
|
2020-12-16 01:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TCPServer::DoTCPThread(CThread *thread, void *arg) {
|
2023-04-10 11:45:58 +02:00
|
|
|
TCPServer *args = (TCPServer *) arg;
|
2020-12-16 01:59:00 +01:00
|
|
|
return args->DoTCPThreadInternal();
|
|
|
|
}
|