summaryrefslogtreecommitdiffstats
path: root/src/server/net.c
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2018-05-06 19:01:13 +0300
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2018-05-06 19:01:13 +0300
commitdb88777a2f63d7dcd31e5e0442839213784eeb82 (patch)
tree6622d876ba737e3d73cd4d5fa81ba927d050d7f2 /src/server/net.c
parent3736b2e92b63d69f2278de2ad72901413a70012d (diff)
downloadcoffeetemp-db88777a2f63d7dcd31e5e0442839213784eeb82.tar.gz
coffeetemp-db88777a2f63d7dcd31e5e0442839213784eeb82.tar.bz2
coffeetemp-db88777a2f63d7dcd31e5e0442839213784eeb82.zip
server: major refactor with preparations for switchable UIs.
Diffstat (limited to 'src/server/net.c')
-rw-r--r--src/server/net.c31
1 files changed, 16 insertions, 15 deletions
diff --git a/src/server/net.c b/src/server/net.c
index f858907..eaab32d 100644
--- a/src/server/net.c
+++ b/src/server/net.c
@@ -29,6 +29,7 @@
#include <string.h>
#include <stdlib.h>
#include <limits.h>
+#include "util.h"
#include "net.h"
struct netstate {
@@ -63,13 +64,13 @@ int net_init(const unsigned short int port)
if (available_count) {
for (i = 0; !state[i].available; ++i) {
if (i == count) {
- ret = NET_ERROR;
+ ret = ERROR;
goto out;
}
}
} else {
if (++count == INT_MAX) {
- ret = NET_ERROR;
+ ret = ERROR;
goto out;
}
@@ -82,7 +83,7 @@ int net_init(const unsigned short int port)
bindaddress.sin_port = htons(port);
state[i].sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (bind(state[i].sock, (struct sockaddr*) &bindaddress, sizeof(bindaddress))) {
- ret = NET_ERROR;
+ ret = ERROR;
goto out;
}
@@ -93,17 +94,17 @@ int net_init(const unsigned short int port)
state[i].data = malloc(dgsize);
memset(state[i].data, 0, dgsize);
state[i].bufsize = dgsize;
- state[i].status = NET_NONEWDATA;
+ state[i].status = NONEWDATA;
pthread_mutex_init(&state[i].datamutex, NULL);
if (pthread_create(&state[i].listner, NULL, dolisten, state + i)) {
- ret = NET_ERROR;
+ ret = ERROR;
goto out;
}
ret = count;
out:
- if (ret == NET_ERROR && sizechanged) {
+ if (ret == ERROR && sizechanged) {
state = realloc(state, sizeof(struct netstate) * --count);
}
@@ -117,7 +118,7 @@ int net_close(int nd)
pthread_mutex_lock(&initmutex);
if (nd > count || nd < 1 || state[nd - 1].available) {
- ret = NET_ERROR;
+ ret = ERROR;
} else {
--nd;
pthread_cancel(state[nd].listner);
@@ -125,7 +126,7 @@ int net_close(int nd)
close(state[nd].sock);
free(state[nd].data);
state[nd].available = 1;
- ret = NET_OK;
+ ret = A_OK;
}
pthread_mutex_unlock(&initmutex);
@@ -137,7 +138,7 @@ int net_getlastdata(int nd, char * const data)
int ret;
if (nd > count || nd < 1 || state[--nd].available) {
- ret = NET_ERROR;
+ ret = ERROR;
} else if (!(ret = state[nd].status)) {
memcpy(data, state[nd].data, dgsize);
}
@@ -169,13 +170,13 @@ static void *dolisten(void * state)
if (st->data[0] == 'I' && !(strcmp(st->data, servermagic))) {
sendto(st->sock, clientmagic, sizeof(clientmagic),
MSG_DONTWAIT, (struct sockaddr *) &clientaddr, sizeof(clientaddr));
- st->status = NET_NONEWDATA; /* consume packet and lie about it */
+ st->status = NONEWDATA; /* consume packet and lie about it */
}
}
/* no packets in five seconds */
if ((now.tv_sec - st->lastreply.tv_sec) >= 5) {
- st->status = NET_DEAD;
+ st->status = DEAD;
} else if (st->data[0] != 'I') {
/* we don't actually want to have NONEWDATA set from this loop,
* barring the one exeption that is after arrival of the beacon
@@ -183,7 +184,7 @@ static void *dolisten(void * state)
* due to polling being generally much more frequent than the
* actual packet rate.
*/
- st->status = NET_OK;
+ st->status = A_OK;
}
pthread_mutex_unlock(&st->datamutex);
@@ -213,12 +214,12 @@ static int getpacket(char *data, size_t buffsize, ssize_t *recvbufsize, int sock
if (ret < 0) {
/* NOOP */
} else if (!ret) {
- ret = NET_NONEWDATA;
+ ret = NONEWDATA;
} else if (pfd.revents & POLLIN) {
*recvbufsize = recvfrom(sock, data, buffsize, MSG_DONTWAIT, (struct sockaddr *) sender, &sender_len);
- ret = NET_OK;
+ ret = A_OK;
} else {
- ret = NET_NONEWDATA;
+ ret = NONEWDATA;
}
return ret;