aboutsummaryrefslogtreecommitdiffstats
path: root/include/tcpserver.h
blob: 10ef5d3c3a2462eebea446822693c4cda7a07a74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (C) 2022 The Qt Company Ltd.
//

#pragma once

#include <iostream>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>

#define MAX_CLIENTS 30
#define TRUE 1
#define FALSE 0

#if __APPLE__ || __MACH__ || __linux__
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros
    #include "curl/curl.h"
#else
    #include "win/unistd.h"
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <iphlpapi.h>
    //#include <ws2def.h>
    #pragma comment(lib, "Ws2_32.lib")
    #include <io.h>
    #include "curl/curl.h"
    #include <windows.h>
    //#define close closesocket
#endif

#define BUFFER_SIZE 1024

class TcpServer
{
public:
    explicit TcpServer(uint16_t serverPort);
    ~TcpServer() {}

    int init(uint16_t serverPort);
    std::string listenToClients(int &socket);
    int sendResponse(int socketIndex, const std::string &message);

private:
    int m_masterSocket;
    int m_addrlen;
    int m_clientSocket[MAX_CLIENTS];
    char m_buffer[BUFFER_SIZE]; // data buffer
    fd_set m_readfds; // set of socket descriptors
    struct sockaddr_in m_address;
};