summaryrefslogtreecommitdiff
path: root/src/base/serverSelect.c
blob: 710142f37993ce31cc0b8cc03b94d3a6fa3b24f7 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>

#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>

#ifndef __WIN32__
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/select.h>
#else
#include <io.h>
#include <winsock2.h>
#endif

#include "server.h"

static struct timeval tv;
static fd_set readfds;
static fd_set writefds;
static int max_fd;

void
restartSelect()
{
#ifndef PUBLIC_SERVER
    tv.tv_sec = 0;
    tv.tv_usec = 0;
#endif

#ifdef PUBLIC_SERVER
    tv.tv_sec = 0;
    tv.tv_usec = 1000;
#endif

    FD_ZERO(&readfds);
    FD_ZERO(&writefds);
    max_fd = 0;
}

void
addSockToSelectRead(int sock)
{
    //printf("addSockToSelectRead %d\n", sock);

    FD_SET(sock, &readfds);

    if (sock > max_fd) {
        max_fd = sock;
    }
}

void
addSockToSelectWrite(int sock)
{
    //printf("addSockToSelectWrite %d\n", sock);

    FD_SET(sock, &writefds);

    if (sock > max_fd) {
        max_fd = sock;
    }
}

int
actionSelect()
{
    int ret;

#ifdef PUBLIC_SERVER
    list_t *listClient;
    listClient = getListServerClient();

    if (listClient->count == 0) {
        ret = select(max_fd + 1, &readfds, &writefds, (fd_set *) NULL, NULL);
        setServerTimer();
    }
    else {
        ret = select(max_fd + 1, &readfds, &writefds, (fd_set *) NULL, &tv);
    }
#endif

#ifndef PUBLIC_SERVER
    ret = select(max_fd + 1, &readfds, &writefds, (fd_set *) NULL, &tv);
#endif

    return ret;
}

int
isChangeSockInSelectRead(int sock)
{
    int ret;

    ret = FD_ISSET(sock, &readfds);

    //printf("isChangeSockInSelect %d -> %d\n", sock, ret);

    return ret;
}

int
isChangeSockInSelectWrite(int sock)
{
    int ret;

    ret = FD_ISSET(sock, &writefds);

    //printf("isChangeSockInSelect %d -> %d\n", sock, ret);

    return ret;
}