Skip to content
Snippets Groups Projects
Commit 9d93ebaa authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Revert last commit that used poll()

Borland hates it.
parent d0b94c03
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #1567 passed
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "gen_defs.h" #include "gen_defs.h"
#include "genwrap.h"
#include "sockwrap.h" #include "sockwrap.h"
#include "dirwrap.h" #include "dirwrap.h"
#include "multisock.h" #include "multisock.h"
...@@ -256,18 +255,22 @@ static void btox(char *hexstr, const char *srcbuf, size_t srcbuflen, size_t hexs ...@@ -256,18 +255,22 @@ static void btox(char *hexstr, const char *srcbuf, size_t srcbuflen, size_t hexs
static BOOL read_socket(SOCKET sock, char *buffer, size_t len, int (*lprintf)(int level, const char *fmt, ...)) static BOOL read_socket(SOCKET sock, char *buffer, size_t len, int (*lprintf)(int level, const char *fmt, ...))
{ {
fd_set socket_set;
struct timeval tv;
size_t i; size_t i;
int j,rd; int j,rd;
unsigned char ch; unsigned char ch;
char err[128]; char err[128];
struct pollfd pfd;
for (i=0;i<len;i++) { for (i=0;i<len;i++) {
pfd.fd = sock; FD_ZERO(&socket_set);
pfd.events = POLLIN; FD_SET(sock,&socket_set);
pfd.revents = 0;
if ((j = poll(&pfd, 1, 1000)) > 0) { // We'll wait 1 secs to read from the socket.
tv.tv_sec=1;
tv.tv_usec=0;
if((j=select(sock+1,&socket_set,NULL,NULL,&tv))>0) {
rd = recv(sock,&ch,1,0); rd = recv(sock,&ch,1,0);
if (rd == 0) { if (rd == 0) {
lprintf(LOG_WARNING,"%04d multisock read_socket() - remote closed the connection",sock); lprintf(LOG_WARNING,"%04d multisock read_socket() - remote closed the connection",sock);
...@@ -286,7 +289,7 @@ static BOOL read_socket(SOCKET sock, char *buffer, size_t len, int (*lprintf)(in ...@@ -286,7 +289,7 @@ static BOOL read_socket(SOCKET sock, char *buffer, size_t len, int (*lprintf)(in
return FALSE; return FALSE;
} else { } else {
lprintf(LOG_WARNING,"%04d multisock read_socket() - poll() returned [%d] with error [%s].",sock,j,socket_strerror(socket_errno,err,sizeof(err))); lprintf(LOG_WARNING,"%04d multisock read_socket() - select() returned [%d] with error [%s].",sock,j,socket_strerror(socket_errno,err,sizeof(err)));
return FALSE; return FALSE;
} }
} }
...@@ -321,50 +324,44 @@ static BOOL read_socket_line(SOCKET sock, char *buffer, size_t buflen, int (*lpr ...@@ -321,50 +324,44 @@ static BOOL read_socket_line(SOCKET sock, char *buffer, size_t buflen, int (*lpr
SOCKET DLLCALL xpms_accept(struct xpms_set *xpms_set, union xp_sockaddr * addr, SOCKET DLLCALL xpms_accept(struct xpms_set *xpms_set, union xp_sockaddr * addr,
socklen_t * addrlen, unsigned int timeout, uint32_t flags, void **cb_data) socklen_t * addrlen, unsigned int timeout, uint32_t flags, void **cb_data)
{ {
fd_set read_fs;
size_t i; size_t i;
struct timeval tv;
struct timeval *tvp;
SOCKET max_sock=0;
SOCKET ret; SOCKET ret;
char hapstr[128]; char hapstr[128];
char haphex[256]; char haphex[256];
char *p, *tok; char *p, *tok;
long l; long l;
void *vp; void *vp;
struct pollfd *pfds;
int timeo;
nfds_t cnt = 0;
if (xpms_set->sock_count < 1) { // Just sleep()..
SLEEP(timeout);
return INVALID_SOCKET;
}
pfds = malloc(sizeof(struct pollfd) * xpms_set->sock_count);
if (pfds == NULL)
return INVALID_SOCKET;
FD_ZERO(&read_fs);
for(i=0; i<xpms_set->sock_count; i++) { for(i=0; i<xpms_set->sock_count; i++) {
if(xpms_set->socks[i].sock == INVALID_SOCKET) if(xpms_set->socks[i].sock == INVALID_SOCKET)
continue; continue;
pfds[cnt].fd = xpms_set->socks[i].sock; FD_SET(xpms_set->socks[i].sock, &read_fs);
pfds[cnt].events = POLLIN; if(xpms_set->socks[i].sock >= max_sock)
pfds[cnt].revents = 0; max_sock=xpms_set->socks[i].sock+1;
cnt++;
} }
if(timeout==XPMS_FOREVER) if(timeout==XPMS_FOREVER)
timeo = -1; tvp=NULL;
else else {
timeo = timeout; tv.tv_sec=timeout/1000;
tv.tv_usec=(timeout%1000)*1000;
switch(poll(pfds, cnt, timeo)) { tvp=&tv;
}
switch(select(max_sock, &read_fs, NULL, NULL, tvp)) {
case 0: case 0:
return INVALID_SOCKET; return INVALID_SOCKET;
case -1: case -1:
return SOCKET_ERROR; return SOCKET_ERROR;
default: default:
for (i = 0, cnt = 0; i < xpms_set->sock_count; i++) { for(i=0; i<xpms_set->sock_count; i++) {
if(xpms_set->socks[i].sock == INVALID_SOCKET) if(xpms_set->socks[i].sock == INVALID_SOCKET)
continue; continue;
if (pfds[cnt++].revents & POLLIN) { if(FD_ISSET(xpms_set->socks[i].sock, &read_fs)) {
if(cb_data) if(cb_data)
*cb_data=xpms_set->socks[i].cb_data; *cb_data=xpms_set->socks[i].cb_data;
ret = accept(xpms_set->socks[i].sock, &addr->addr, addrlen); ret = accept(xpms_set->socks[i].sock, &addr->addr, addrlen);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment