controller_patcher/source/utils/TCPServer.hpp

74 lines
1.5 KiB
C++
Raw Normal View History

2020-12-16 01:59:00 +01:00
#ifndef _TCPSERVER_H_
#define _TCPSERVER_H_
2023-04-10 11:45:58 +02:00
#include <netinet/in.h>
2020-12-16 01:59:00 +01:00
#include <sys/select.h>
2021-09-19 21:16:03 +02:00
#include <sys/socket.h>
2020-12-16 01:59:00 +01:00
#include <system/CThread.h>
#include <wut_types.h>
#include "utils/logger.h"
2023-04-10 11:45:58 +02:00
#include <coreinit/cache.h>
2020-12-16 01:59:00 +01:00
class TCPServer {
public:
TCPServer(int32_t port, int32_t priority);
virtual ~TCPServer();
BOOL isConnected() {
return connected;
}
2023-04-10 11:45:58 +02:00
2020-12-16 01:59:00 +01:00
protected:
BOOL shouldExit() {
return (exitThread == 1);
}
int32_t getClientFD() {
return clientfd;
}
int32_t getSocketFD() {
return sockfd;
}
void setThreadPriority(int32_t priority) {
2023-04-10 11:45:58 +02:00
if (pThread != NULL) {
2020-12-16 01:59:00 +01:00
pThread->setThreadPriority(priority);
}
}
struct sockaddr_in getSockAddr() {
return sock_addr;
}
2023-04-10 11:45:58 +02:00
2020-12-16 01:59:00 +01:00
private:
virtual void CloseSockets();
virtual void ErrorHandling();
static void DoTCPThread(CThread *thread, void *arg);
virtual void DoTCPThreadInternal();
virtual BOOL acceptConnection() = 0;
2023-04-10 11:45:58 +02:00
virtual void onConnectionClosed() {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Default onConnectionClosed ");
2020-12-16 01:59:00 +01:00
}
/**
Called when a connection has be accepted.
**/
virtual BOOL whileLoop() = 0;
struct sockaddr_in sock_addr;
2023-04-10 11:45:58 +02:00
volatile int32_t sockfd = -1;
2020-12-16 01:59:00 +01:00
volatile int32_t clientfd = -1;
2023-04-10 11:45:58 +02:00
int32_t port = 0;
2020-12-16 01:59:00 +01:00
volatile BOOL connected = false;
volatile int32_t exitThread = 0;
2023-04-10 11:45:58 +02:00
CThread *pThread = NULL;
2020-12-16 01:59:00 +01:00
};
#endif //_TCPSERVER_H_