u_short win32_ntohs(u_short netshort) { #ifndef WIN32_NO_SOCKETS StartSockets(); #endif return ntohs(netshort); }
u_long win32_ntohl(u_long netlong) { #ifndef WIN32_NO_SOCKETS StartSockets(); #endif return ntohl(netlong); }
u_short win32_htons(u_short hostshort) { #ifndef WIN32_NO_SOCKETS StartSockets(); #endif return htons(hostshort); }
/* in no sockets Win32 builds, these use the inline functions defined in * perl.h */ u_long win32_htonl(u_long hostlong) { #ifndef WIN32_NO_SOCKETS StartSockets(); #endif return htonl(hostlong); }
/* select contributed by Vincent R. Slyngstad ([email protected]) */ int win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout) { int r; #ifdef USE_SOCKETS_AS_HANDLES int i, fd, save_errno = errno; FD_SET nrd, nwr, nex; /* winsock seems incapable of dealing with all three null fd_sets, * so do the (millisecond) sleep as a special case */ if (!(rd || wr || ex)) { if (timeout) Sleep(timeout->tv_sec * 1000 + timeout->tv_usec / 1000); /* do the best we can */ else Sleep(UINT_MAX); return 0; } StartSockets(); FD_ZERO(&nrd); FD_ZERO(&nwr); FD_ZERO(&nex); for (i = 0; i < nfds; i++) { fd = TO_SOCKET(i); if (rd && PERL_FD_ISSET(i,rd)) FD_SET((unsigned)fd, &nrd); if (wr && PERL_FD_ISSET(i,wr)) FD_SET((unsigned)fd, &nwr); if (ex && PERL_FD_ISSET(i,ex)) FD_SET((unsigned)fd, &nex); } errno = save_errno; SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout)); save_errno = errno; for (i = 0; i < nfds; i++) { fd = TO_SOCKET(i); if (rd && PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd)) PERL_FD_CLR(i,rd); if (wr && PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr)) PERL_FD_CLR(i,wr); if (ex && PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex)) PERL_FD_CLR(i,ex); } errno = save_errno; #else SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout)); #endif return r; }
SOCKET win32_socket(int af, int type, int protocol) { SOCKET s; StartSockets(); if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET) errno = get_last_socket_error(); else s = OPEN_SOCKET(s); return s; }
void Task_SocketServer(void* params) { (void)params; //start socket server and accept a connection if(StartSockets() == RET_FAILURE) {TASK_RETURN_ERROR(ERROR_UNKNOWN, "Socket start fail");} for(;;){ if(UpdateSockets() == RET_FAILURE) {TASK_RETURN_ERROR(ERROR_UNKNOWN, "Socket update fail");} vTaskDelay(1); } StopSockets(); }
SOCKET win32_socket(int af, int type, int protocol) { SOCKET s; #ifndef USE_SOCKETS_AS_HANDLES SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET); #else StartSockets(); if((s = socket(af, type, protocol)) == INVALID_SOCKET) errno = WSAGetLastError(); else s = OPEN_SOCKET(s); #endif /* USE_SOCKETS_AS_HANDLES */ return s; }
u_short win32_ntohs(u_short netshort) { StartSockets(); return ntohs(netshort); }
u_long win32_ntohl(u_long netlong) { StartSockets(); return ntohl(netlong); }
u_short win32_htons(u_short hostshort) { StartSockets(); return htons(hostshort); }
u_long win32_htonl(u_long hostlong) { StartSockets(); return htonl(hostlong); }
/* select contributed by Vincent R. Slyngstad ([email protected]) */ int win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout) { int r; #ifdef USE_SOCKETS_AS_HANDLES Perl_fd_set dummy; int i, fd, bit, offset; FD_SET nrd, nwr, nex, *prd, *pwr, *pex; /* winsock seems incapable of dealing with all three null fd_sets, * so do the (millisecond) sleep as a special case */ if (!(rd || wr || ex)) { if (timeout) Sleep(timeout->tv_sec * 1000 + timeout->tv_usec / 1000); /* do the best we can */ else Sleep(UINT_MAX); return 0; } StartSockets(); PERL_FD_ZERO(&dummy); if (!rd) rd = &dummy, prd = NULL; else prd = &nrd; if (!wr) wr = &dummy, pwr = NULL; else pwr = &nwr; if (!ex) ex = &dummy, pex = NULL; else pex = &nex; FD_ZERO(&nrd); FD_ZERO(&nwr); FD_ZERO(&nex); for (i = 0; i < nfds; i++) { fd = TO_SOCKET(i); if (PERL_FD_ISSET(i,rd)) FD_SET(fd, &nrd); if (PERL_FD_ISSET(i,wr)) FD_SET(fd, &nwr); if (PERL_FD_ISSET(i,ex)) FD_SET(fd, &nex); } SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout)); for (i = 0; i < nfds; i++) { fd = TO_SOCKET(i); if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd)) PERL_FD_CLR(i,rd); if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr)) PERL_FD_CLR(i,wr); if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex)) PERL_FD_CLR(i,ex); } #else SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout)); #endif return r; }
static DECLARE_THREAD_FUNCTION(NetThread, param) { SOCKET_TYPE listenfd, fdmax, i, clientfd; socklen_t len; int leni; fd_set master, read_fds; struct sockaddr_in addr; GDisplay * g; netPriv * priv; (void)param; // Start the sockets layer StartSockets(); gfxSleepMilliseconds(100); // Make sure the thread has time to start. /* clear the master and temp sets */ FD_ZERO(&master); FD_ZERO(&read_fds); if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == (SOCKET_TYPE)-1) gfxHalt("GDISP: uGFXnet - Socket failed"); memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(GDISP_GFXNET_PORT); if (bind(listenfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) gfxHalt("GDISP: uGFXnet - Bind failed"); if (listen(listenfd, 10) == -1) gfxHalt("GDISP: uGFXnet - Listen failed"); /* add the listener to the master set */ FD_SET(listenfd, &master); /* keep track of the biggest file descriptor */ fdmax = listenfd; /* so far, it's this one*/ #if GDISP_GFXNET_BROKEN_LWIP_ACCEPT { #warning "Using GDISP_GFXNET_BROKEN_LWIP_ACCEPT limits the number of displays and the use of GFXNET. Avoid if possible!" len = sizeof(addr); if((clientfd = accept(listenfd, (struct sockaddr *)&addr, &len)) == (SOCKET_TYPE)-1) gfxHalt("GDISP: uGFXnet - Accept failed"); // Look for a display that isn't connected for(g = 0; (g = (GDisplay *)gdriverGetNext(GDRIVER_TYPE_DISPLAY, (GDriver *)g));) { // Ignore displays for other controllers #ifdef GDISP_DRIVER_LIST if (gvmt(g) != &GDISPVMT_uGFXnet) continue; #endif if (!(g->flags & GDISP_FLG_CONNECTED)) break; } // Was anything found? if (!g) { // No Just close the connection closesocket(clientfd); gfxHalt("GDISP: uGFXnet - Can't find display for connection"); return 0; } // Save the descriptor FD_SET(clientfd, &master); if (clientfd > fdmax) fdmax = clientfd; priv = g->priv; memset(priv, 0, sizeof(netPriv)); priv->netfd = clientfd; //printf(New connection from %s on socket %d allocated to display %u\n", inet_ntoa(addr.sin_addr), clientfd, disp+1); // Send the initialisation data (2 words at a time) priv->data[0] = GNETCODE_INIT; priv->data[1] = GNETCODE_VERSION; sendpkt(priv->netfd, priv->data, 2); priv->data[0] = GDISP_SCREEN_WIDTH; priv->data[1] = GDISP_SCREEN_HEIGHT; sendpkt(priv->netfd, priv->data, 2); priv->data[0] = GDISP_LLD_PIXELFORMAT; priv->data[1] = (g->flags & GDISP_FLG_HASMOUSE) ? 1 : 0; MUTEX_ENTER; sendpkt(priv->netfd, priv->data, 2); MUTEX_EXIT; // The display is now working g->flags |= GDISP_FLG_CONNECTED; // Send a redraw all #if GFX_USE_GWIN && GWIN_NEED_WINDOWMANAGER gdispGClear(g, gwinGetDefaultBgColor()); gwinRedrawDisplay(g, FALSE); #endif } #endif /* loop */ for(;;) { /* copy it */ read_fds = master; if (select(fdmax+1, &read_fds, 0, 0, 0) == -1) gfxHalt("GDISP: uGFXnet - Select failed"); // Run through the existing connections looking for data to be read for(i = 0; i <= fdmax; i++) { if(!FD_ISSET(i, &read_fds)) continue; // Handle new connections if(i == listenfd) { len = sizeof(addr); if((clientfd = accept(listenfd, (struct sockaddr *)&addr, &len)) == (SOCKET_TYPE)-1) gfxHalt("GDISP: uGFXnet - Accept failed"); // Look for a display that isn't connected for(g = 0; (g = (GDisplay *)gdriverGetNext(GDRIVER_TYPE_DISPLAY, (GDriver *)g));) { // Ignore displays for other controllers #ifdef GDISP_DRIVER_LIST if (gvmt(g) != &GDISPVMT_uGFXnet) continue; #endif if (!(g->flags & GDISP_FLG_CONNECTED)) break; } // Was anything found? if (!g) { // No Just close the connection closesocket(clientfd); //printf(New connection from %s on socket %d rejected as all displays are already connected\n", inet_ntoa(addr.sin_addr), clientfd); continue; } // Save the descriptor FD_SET(clientfd, &master); if (clientfd > fdmax) fdmax = clientfd; priv = g->priv; memset(priv, 0, sizeof(netPriv)); priv->netfd = clientfd; //printf(New connection from %s on socket %d allocated to display %u\n", inet_ntoa(addr.sin_addr), clientfd, disp+1); // Send the initialisation data (2 words at a time) priv->data[0] = GNETCODE_INIT; priv->data[1] = GNETCODE_VERSION; sendpkt(priv->netfd, priv->data, 2); priv->data[0] = GDISP_SCREEN_WIDTH; priv->data[1] = GDISP_SCREEN_HEIGHT; sendpkt(priv->netfd, priv->data, 2); priv->data[0] = GDISP_LLD_PIXELFORMAT; priv->data[1] = (g->flags & GDISP_FLG_HASMOUSE) ? 1 : 0; MUTEX_ENTER; sendpkt(priv->netfd, priv->data, 2); MUTEX_EXIT; // The display is now working g->flags |= GDISP_FLG_CONNECTED; // Send a redraw all #if GFX_USE_GWIN && GWIN_NEED_WINDOWMANAGER gdispGClear(g, gwinGetDefaultBgColor()); gwinRedrawDisplay(g, FALSE); #endif continue; } // Handle data from a client // Look for a display that is connected and the socket descriptor matches for(g = 0; (g = (GDisplay *)gdriverGetNext(GDRIVER_TYPE_DISPLAY, (GDriver *)g));) { // Ignore displays for other controllers #ifdef GDISP_DRIVER_LIST if (gvmt(g) != &GDISPVMT_uGFXnet) continue; #endif priv = g->priv; if ((g->flags & GDISP_FLG_CONNECTED) && priv->netfd == i) break; } if (!g) gfxHalt("GDISP: uGFXnet - Got data from unrecognized connection"); if ((g->flags & GDISP_FLG_HAVEDATA)) { // The higher level is still processing the previous data. // Give it a chance to run by coming back to this data. gfxSleepMilliseconds(1); continue; } /* handle data from a client */ MUTEX_ENTER; if ((leni = recv(i, ((char *)priv->data)+priv->databytes, sizeof(priv->data)-priv->databytes, 0)) <= 0) { // Socket closed or in error state MUTEX_EXIT; g->flags &= ~GDISP_FLG_CONNECTED; memset(priv, 0, sizeof(netPriv)); closesocket(i); FD_CLR(i, &master); continue; } MUTEX_EXIT; // Do we have a full reply yet priv->databytes += leni; if (priv->databytes < sizeof(priv->data)) continue; priv->databytes = 0; // Convert network byte or to host byte order priv->data[0] = ntohs(priv->data[0]); priv->data[1] = ntohs(priv->data[1]); // Process the data received switch(priv->data[0]) { #if GINPUT_NEED_MOUSE case GNETCODE_MOUSE_X: priv->mousex = priv->data[1]; break; case GNETCODE_MOUSE_Y: priv->mousey = priv->data[1]; break; case GNETCODE_MOUSE_B: priv->mousebuttons = priv->data[1]; // Treat the button event as the sync signal #if GINPUT_MOUSE_POLL_PERIOD == TIME_INFINITE ginputMouseWakeup(); #endif break; #endif case GNETCODE_CONTROL: case GNETCODE_READ: g->flags |= GDISP_FLG_HAVEDATA; break; case GNETCODE_KILL: gfxHalt("GDISP: uGFXnet - Display sent KILL command"); break; default: // Just ignore unrecognised data break; } } } return 0; }
/* select contributed by Vincent R. Slyngstad ([email protected]) */ int win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout) { int r; int i, fd, save_errno = errno; FD_SET nrd, nwr, nex; bool just_sleep = TRUE; StartSockets(); FD_ZERO(&nrd); FD_ZERO(&nwr); FD_ZERO(&nex); for (i = 0; i < nfds; i++) { if (rd && PERL_FD_ISSET(i,rd)) { fd = TO_SOCKET(i); FD_SET((unsigned)fd, &nrd); just_sleep = FALSE; } if (wr && PERL_FD_ISSET(i,wr)) { fd = TO_SOCKET(i); FD_SET((unsigned)fd, &nwr); just_sleep = FALSE; } if (ex && PERL_FD_ISSET(i,ex)) { fd = TO_SOCKET(i); FD_SET((unsigned)fd, &nex); just_sleep = FALSE; } } /* winsock seems incapable of dealing with all three fd_sets being empty, * so do the (millisecond) sleep as a special case */ if (just_sleep) { if (timeout) Sleep(timeout->tv_sec * 1000 + timeout->tv_usec / 1000); /* do the best we can */ else Sleep(UINT_MAX); return 0; } errno = save_errno; SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, (PTIMEVAL)timeout)); save_errno = errno; for (i = 0; i < nfds; i++) { if (rd && PERL_FD_ISSET(i,rd)) { fd = TO_SOCKET(i); if (!FD_ISSET(fd, &nrd)) PERL_FD_CLR(i,rd); } if (wr && PERL_FD_ISSET(i,wr)) { fd = TO_SOCKET(i); if (!FD_ISSET(fd, &nwr)) PERL_FD_CLR(i,wr); } if (ex && PERL_FD_ISSET(i,ex)) { fd = TO_SOCKET(i); if (!FD_ISSET(fd, &nex)) PERL_FD_CLR(i,ex); } } errno = save_errno; return r; }
char FAR * win32_inet_ntoa(struct in_addr in) { StartSockets(); return inet_ntoa(in); }
unsigned long win32_inet_addr(const char FAR *cp) { StartSockets(); return inet_addr(cp); }
/** * Our main function. * There are two prototypes - one for systems with a command line and one for embedded systems without one. */ int main(proto_args) { uint16_t cmd[5]; unsigned cnt; // Initialize and clear the display gfxInit(); font = gdispOpenFont("UI2"); // Open the connection gdispDrawStringBox(0, 0, gdispGetWidth(), gdispGetHeight(), "Connecting to host...", font, White, justifyCenter); StartSockets(); netfd = doConnect(cmd_args); if (netfd == (SOCKET_TYPE)-1) gfxHalt("Could not connect to the specified server"); gdispClear(Black); // Get the initial packet from the host if (!getpkt(cmd, 2)) goto alldone; if (cmd[0] != GNETCODE_INIT || cmd[1] != GNETCODE_VERSION) gfxHalt("Oops - The protocol doesn't look like one we understand"); // Get the rest of the initial arguments if (!getpkt(cmd, 4)) goto alldone; // cmd[] = width, height, pixelformat, hasmouse // We will ignore size mismatches but the pixel format must match if (cmd[2] != GDISP_PIXELFORMAT) gfxHalt("Oops - The remote display is using a different pixel format to us.\nTry defining GDISP_PIXELFORMAT in your gfxconf.h file."); #if GFX_USE_GINPUT && GINPUT_NEED_MOUSE // Start the mouse thread if needed if (cmd[3]) gfxThreadClose(gfxThreadCreate(waNetThread, sizeof(waNetThread), HIGH_PRIORITY, NetThread, 0)); #endif // Process incoming instructions while(getpkt(cmd, 1)) { switch(cmd[0]) { case GNETCODE_FLUSH: gdispFlush(); break; case GNETCODE_PIXEL: if (!getpkt(cmd, 3)) goto alldone; // cmd[] = x, y, color gdispDrawPixel(cmd[0], cmd[1], cmd[2]); break; case GNETCODE_FILL: if (!getpkt(cmd, 5)) goto alldone; // cmd[] = x, y, cx, cy, color gdispFillArea(cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]); break; case GNETCODE_BLIT: if (!getpkt(cmd, 4)) goto alldone; // cmd[] = x, y, cx, cy - Followed by cx * cy pixels gdispStreamStart(cmd[0],cmd[1],cmd[2],cmd[3]); for(cnt = (unsigned)cmd[2] * cmd[3]; cnt; cnt--) { if (!getpkt(cmd, 1)) goto alldone; gdispStreamColor(cmd[0]); } gdispStreamStop(); break; #if GDISP_NEED_PIXELREAD case GNETCODE_READ: if (!getpkt(cmd, 2)) goto alldone; // cmd[] = x, y - Response is GNETCODE_READ,color cmd[1] = gdispGetPixelColor(cmd[0], cmd[1]); cmd[0] = GNETCODE_READ; if (!sendpkt(cmd, 2)) goto alldone; break; #endif #if GDISP_NEED_SCROLL case GNETCODE_SCROLL: if (!getpkt(cmd, 5)) goto alldone; // cmd[] = x, y, cx, cy, lines gdispVerticalScroll(cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], Black); break; #endif case GNETCODE_CONTROL: if (!getpkt(cmd, 2)) goto alldone; // cmd[] = what,data - Response is GNETCODE_CONTROL, 0x0000 (fail) or GNETCODE_CONTROL, 0x0001 (success) gdispControl(cmd[0], (void *)(unsigned)cmd[1]); switch(cmd[0]) { case GDISP_CONTROL_ORIENTATION: cmd[1] = (uint16_t)gdispGetOrientation() == cmd[1] ? 1 : 0; break; case GDISP_CONTROL_POWER: cmd[1] = (uint16_t)gdispGetPowerMode() == cmd[1] ? 1 : 0; break; case GDISP_CONTROL_BACKLIGHT: cmd[1] = (uint16_t)gdispGetBacklight() == cmd[1] ? 1 : 0; break; default: cmd[1] = 0; break; } cmd[0] = GNETCODE_CONTROL; if (!sendpkt(cmd, 2)) goto alldone; break; default: gfxHalt("Oops - The host has sent invalid commands"); break; } } alldone: closesocket(netfd); gfxHalt("Connection closed"); return 0; }