Beispiel #1
0
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
    int ret;
    fd_set rfds, wfds, *rp, *wp;
    struct timeval tv, *tp;
    double t;
    if (timeout_iszero(tm)) return IO_TIMEOUT;  /* optimize timeout == 0 case */
    do {
        /* must set bits within loop, because select may have modifed them */
        rp = wp = NULL;
        if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; }
        if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; }
        t = timeout_getretry(tm);
        tp = NULL;
        if (t >= 0.0) {
            tv.tv_sec = (int)t;
            tv.tv_usec = (int)((t-tv.tv_sec)*1.0e6);
            tp = &tv;
        }
        ret = select(*ps+1, rp, wp, NULL, tp);
    } while (ret == -1 && errno == EINTR);
    if (ret == -1) return errno;
    if (ret == 0) return IO_TIMEOUT;
    if (sw == WAITFD_C && FD_ISSET(*ps, &rfds)) return IO_CLOSED;
    return IO_DONE;
}
Beispiel #2
0
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
#if LUA_SOCKET_USE_SELECT
    int ret;
    fd_set rfds, wfds, *rp, *wp;
    struct timeval tv, *tp;
    double t;
    if (timeout_iszero(tm)) return IO_TIMEOUT;  /* optimize timeout == 0 case */
    do {
        /* must set bits within loop, because select may have modifed them */
        rp = wp = NULL;
        if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; }
        if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; }
        t = timeout_getretry(tm);
        tp = NULL;
        if (t >= 0.0) {
            tv.tv_sec = (int)t;
            tv.tv_usec = (int)((t-tv.tv_sec)*1.0e6);
            tp = &tv;
        }
        #if APIABSTRACTION_PS3
			ret = socketselect(*ps+1, rp, wp, NULL, tp);
		#else
			ret = select(*ps+1, rp, wp, NULL, tp);  
		#endif
    } while (ret == -1 && peusock_errno == peusock_EINTR);
    if (ret == -1) return peusock_errno;
    if (ret == 0) return IO_TIMEOUT;
    if (sw == WAITFD_C && FD_ISSET(*ps, &rfds)) return IO_CLOSED;
    return IO_DONE;
#else
	printf("socket_waitfd is not implemented for this platform! don't call it!");
	return IO_UNKNOWN;
#endif
}
Beispiel #3
0
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
    int ret;
    lua_State *L = lua_this();
    int t = (int)(timeout_getretry(tm)*1e3);
    if (timeout_iszero(tm)) return IO_TIMEOUT;  /* optimize timeout == 0 case */
    ret = luaL_wait(L, *ps, (sw == WAITFD_W || sw == WAITFD_C) ? 1 : 0, t);
    if (ret == -1 && sw == WAITFD_C) return IO_CLOSED;
    if (ret == 0) return IO_TIMEOUT;
    return IO_DONE;
}
Beispiel #4
0
/*-------------------------------------------------------------------------*\
* Select with timeout control
\*-------------------------------------------------------------------------*/
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, 
        p_timeout tm) {
    int ret;
    do {
        struct timeval tv;
        double t = timeout_getretry(tm);
        tv.tv_sec = (int) t;
        tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6);
        /* timeout = 0 means no wait */
        ret = select(n, rfds, wfds, efds, t >= 0.0 ? &tv: NULL);
    } while (ret < 0 && errno == EINTR);
    return ret;
}
Beispiel #5
0
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
        p_timeout tm, const sigset_t* mask) {
    int ret;
    //do
    {
        struct timespec ts;
        double t = timeout_getretry(tm);
        ts.tv_sec = (int) t;
        ts.tv_nsec = (int) ((t - ts.tv_sec) * 1.0e9);
        /* timeout = 0 means no wait */
        ret = pselect(n, rfds, wfds, efds, (const struct timespec*) (t >= 0.0 ? &ts: NULL), mask);
    }
    //while (ret < 0 && errno == EINTR);
    return ret;
}
Beispiel #6
0
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
    int ret;
    struct pollfd pfd;
    pfd.fd = *ps;
    pfd.events = sw;
    pfd.revents = 0;
    if (timeout_iszero(tm)) return IO_TIMEOUT;  /* optimize timeout == 0 case */
    do {
		int t = (int)(timeout_getretry(tm)*1e3);
		ret = poll(&pfd, 1, t >= 0? t: -1);
	} while (ret == -1 && peusock_errno == peusock_EINTR);
    if (ret == -1) return peusock_errno;
    if (ret == 0) return IO_TIMEOUT;
    if (sw == WAITFD_C && (pfd.revents & (POLLIN|POLLERR))) return IO_CLOSED;
    return IO_DONE;
}
Beispiel #7
0
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, 
        p_timeout tm) {
    int ret;
    do {
        struct timeval tv;
        double t = timeout_getretry(tm);
        tv.tv_sec = (int) t;
        tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6);
        /* timeout = 0 means no wait */
#if APIABSTRACTION_PS3
        ret = socketselect(n, rfds, wfds, efds, t >= 0.0 ? &tv: NULL);
#else
        ret = select(n, rfds, wfds, efds, t >= 0.0 ? &tv: NULL);
#endif
    } while (ret < 0 && peusock_errno == peusock_EINTR);
    return ret;
}
Beispiel #8
0
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
	/*fd_set rfds, wfds, *rp, *wp;*/
	/*struct timeval tv, *tp;*/
	double t;
	SceNetId eid = -1;
	SceNetEpollEvent ev;
	int nevents;
	SceNetEpollEvent events[MAX_EVENTS];
	int ret;

	if (timeout_iszero(tm)) return IO_TIMEOUT;  /* optimize timeout == 0 case */
	do {
		eid = sceNetEpollCreate("waitfd", 0);
		if (eid < 0) {
			printf("sceNetEpollCreate() failed (errno=%d)\n", peusock_errno);
			return sce_net_errno;
		}

		/* must set bits within loop, because select may have modifed them */
		memset(&ev, 0, sizeof(ev));
		if (sw & WAITFD_R)
			ev.events = SCE_NET_EPOLLIN;
		if (sw & WAITFD_W)
			ev.events = ev.events | SCE_NET_EPOLLOUT;
		ev.data.ext.id = *ps;

		ret = sceNetEpollControl(eid, SCE_NET_EPOLL_CTL_ADD, *ps, &ev);
		if (ret < 0) {
			printf("sceNetEpollControl(ADD) failed (errno=%d)\n", sce_net_errno);
			return sce_net_errno;
		}

		/*do stuff*/
		
		t = timeout_getretry(tm); /* time left for the call*/
		/*
		tp = NULL;
		if (t >= 0.0) {
			tv.tv_sec = (int)t;
			tv.tv_usec = (int)((t-tv.tv_sec)*1.0e6);
			tp = &tv;
		}
		*/
		/*ret = socketselect(*ps+1, rp, wp, NULL, tp);*/
		nevents = sceNetEpollWait(eid, events, MAX_EVENTS, t >= 0? t: -1);

		/* reference code for deleting events from epoll */
#if 0
		/* delete SCE_NET_EPOLLOUT */
		memset(&ev, 0, sizeof(ev));
		ev.events = SCE_NET_EPOLLIN;
		ev.data.ext.id = s;
		ret = sceNetEpollControl(eid, SCE_NET_EPOLL_CTL_MOD, *ps, &ev);
		if (ret < 0) {
			printf("sceNetEpollControl(MOD) failed (errno=%d)\n",
				sce_net_errno);
			return sce_net_errno;
		}
#endif
		/* destroy epoll object*/
		sceNetEpollDestroy(eid);

	} while (nevents == -1 && peusock_errno == peusock_EINTR);
	if (nevents == -1) return peusock_errno;
	if (nevents == 0) return IO_TIMEOUT;
	/*if (sw == WAITFD_C && FD_ISSET(*ps, &rfds)) return IO_CLOSED;*/
	return IO_DONE;
}
Beispiel #9
0
int lua_socket_waitfd(p_socket ps, int sw, p_timeout tm){
    int ret;
#ifdef WIN32
#define WAITFD_E        4

    fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
    struct timeval tv, *tp = NULL;
    double t;
    if (timeout_iszero(tm)) return IO_TIMEOUT;  /* optimize timeout == 0 case */
    if (sw & WAITFD_R) {
        FD_ZERO(&rfds);
		FD_SET(*ps, &rfds);
        rp = &rfds;
    }
    if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; }
    if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; }
    if ((t = lua_timeout_get(tm)) >= 0.0) {
        tv.tv_sec = (int) t;
        tv.tv_usec = (int) ((t-tv.tv_sec)*1.0e6);
        tp = &tv;
    }
    ret = select(0, rp, wp, ep, tp);
    if (ret == -1) return WSAGetLastError();
    if (ret == 0) return IO_TIMEOUT;
    if (sw == WAITFD_C && FD_ISSET(*ps, &efds)) return IO_CLOSED;
#else
#ifdef SOCKET_POLL
    struct pollfd pfd;
    pfd.fd = *ps;
    pfd.events = sw;
    pfd.revents = 0;
    if (timeout_iszero(tm)) return IO_TIMEOUT;  /* optimize timeout == 0 case */
    do {
		int t = (int)(timeout_getretry(tm)*1e3);
		ret = poll(&pfd, 1, t >= 0? t: -1);
	} while (ret == -1 && errno == EINTR);
    if (ret == -1) return errno;
    if (ret == 0) return IO_TIMEOUT;
    if (sw == WAITFD_C && (pfd.revents & (POLLIN|POLLERR))) return IO_CLOSED;
#else
    fd_set rfds, wfds, *rp, *wp;
    struct timeval tv, *tp;
    double t;
    if (timeout_iszero(tm)) return IO_TIMEOUT;  /* optimize timeout == 0 case */
    do {
        /* must set bits within loop, because select may have modifed them */
        rp = wp = NULL;
        if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; }
        if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; }
        t = lua_timeout_getretry(tm);
        tp = NULL;
        if (t >= 0.0) {
            tv.tv_sec = (int)t;
            tv.tv_usec = (int)((t-tv.tv_sec)*1.0e6);
            tp = &tv;
        }
        ret = select(*ps+1, rp, wp, NULL, tp);
    } while (ret == -1 && errno == EINTR);
    if (ret == -1) return errno;
    if (ret == 0) return IO_TIMEOUT;
    if (sw == WAITFD_C && FD_ISSET(*ps, &rfds)) return IO_CLOSED;
#endif
#endif
    return IO_DONE;
}