/** * network_init: * * Platform specific socket library initialization. * * Returns: true (1) if successful, otherwise false (0). **/ bool network_init(void) { #ifdef _WIN32 WSADATA wsaData; #endif static bool inited = false; if (inited) return true; #if defined(_WIN32) if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { network_deinit(); return false; } #elif defined(__CELLOS_LV2__) && !defined(__PSL1GHT__) int timeout_count = 10; cellSysmoduleLoadModule(CELL_SYSMODULE_NET); sys_net_initialize_network(); if (cellNetCtlInit() < 0) return false; for (;;) { int state; if (cellNetCtlGetState(&state) < 0) return false; if (state == CELL_NET_CTL_STATE_IPObtained) break; retro_sleep(500); timeout_count--; if (timeout_count < 0) return 0; } #elif defined(VITA) SceNetInitParam initparam; if (sceNetShowNetstat() == SCE_NET_ERROR_ENOTINIT) { _net_compat_net_memory = malloc(COMPAT_NET_INIT_SIZE); initparam.memory = _net_compat_net_memory; initparam.size = COMPAT_NET_INIT_SIZE; initparam.flags = 0; sceNetInit(&initparam); sceNetCtlInit(); } retro_epoll_fd = sceNetEpollCreate("epoll", 0); #elif defined(GEKKO) char t[16]; if (if_config(t, NULL, NULL, TRUE, 10) < 0) return false; #elif defined(WIIU) socket_lib_init(); #elif defined(_3DS) _net_compat_net_memory = (u32*)memalign(SOC_ALIGN, SOC_BUFFERSIZE); if (_net_compat_net_memory == NULL) return false; Result ret = socInit(_net_compat_net_memory, SOC_BUFFERSIZE);//WIFI init if (ret != 0) return false; #else signal(SIGPIPE, SIG_IGN); /* Do not like SIGPIPE killing our app. */ #endif inited = true; return true; }
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; }