libutils/include/utils/TCPServer.hpp

67 lines
1.3 KiB
C++
Raw Normal View History

#ifndef _TCPSERVER_H_
#define _TCPSERVER_H_
2018-06-20 14:44:30 +02:00
#include <sys/select.h>
#include <nsysnet/socket.h>
#include <system/CThread.h>
#include <wut_types.h>
#include <coreinit/cache.h>
#include "utils/logger.h"
class TCPServer {
public:
2018-06-20 14:44:30 +02:00
TCPServer(int32_t port, int32_t priority);
virtual ~TCPServer();
2018-06-20 14:44:30 +02:00
BOOL isConnected() {
return connected;
}
protected:
2018-06-20 14:44:30 +02:00
BOOL shouldExit() {
return (exitThread == 1);
}
2018-06-20 14:44:30 +02:00
int32_t getClientFD() {
return clientfd;
}
2018-06-20 14:44:30 +02:00
int32_t getSocketFD() {
return sockfd;
}
struct sockaddr_in getSockAddr() {
return sock_addr;
}
private:
virtual void CloseSockets();
virtual void ErrorHandling();
static void DoTCPThread(CThread *thread, void *arg);
virtual void DoTCPThreadInternal();
2018-06-20 14:44:30 +02:00
virtual BOOL acceptConnection() = 0;
virtual void onConnectionClosed(){
DEBUG_FUNCTION_LINE("Default onConnectionClosed \n");
}
/**
Called when a connection has be accepted.
**/
2018-06-20 14:44:30 +02:00
virtual BOOL whileLoop() = 0;
struct sockaddr_in sock_addr;
2018-06-20 14:44:30 +02:00
volatile int32_t sockfd = -1;
volatile int32_t clientfd = -1;
2018-06-20 14:44:30 +02:00
int32_t port = 0;
volatile BOOL connected = false;
2018-06-20 14:44:30 +02:00
volatile int32_t exitThread = 0;
CThread *pThread = NULL;
};
#endif //_TCPSERVER_H_