int main( int argc , char **argv ) { unsigned long cliaddr; unsigned short cliport; int ufd , nfd; fprintf( stderr, "start\n"); uinit(INADDR_ANY); fprintf( stderr, "inited\n"); ufd = usocket_serv( 8 ); fprintf( stderr, "socketd %d\n", ufd ); ulisten( ufd ); fprintf( stderr, "listened\n"); while(1){ int r =uproc(); /* fprintf( stderr, "uproced %d\n" , r);*/ nfd = uaccept( ufd , &cliaddr ,&cliport); if( nfd >= 0 ){ fprintf( stderr, "accepted! nfd:%d %x %d\n" , nfd ,cliaddr , ntohs(cliport) ); break; } } while(1){ int r = uproc(); fprintf( stderr,"." ); if( ureadable( nfd ) ){ char buffer[800]; int r; fprintf( stderr, "REadable!!\n"); bzero( buffer ,sizeof(buffer)); if( (r = uread( nfd, buffer , sizeof( buffer ) )) >= 0 ){ int wr; fprintf( stderr , "ReadData %d: [%s]\n" ,r, buffer ); wr = uwrite( nfd , buffer , r ); fprintf( stderr ,"Wrote %dbytes\n",wr ); } } usleep(100*1000); } }
int main(int argc, char** argv) { #if (defined WIN32 || defined _WIN32) WSADATA wsadata; int rc = WSAStartup(MAKEWORD(2,0), &wsadata); if (rc) return 1; #endif upoll_t* upq = upoll_create(32); intptr_t sd1 = usocket(PF_INET, SOCK_STREAM, IPPROTO_TCP); printf("server: %li\n", sd1); printf("bind: %d\n", ubind(sd1, "127.0.0.1", "1976")); printf("listen: %d\n", ulisten(sd1, 128)); upoll_event_t ev1, ev2; upoll_event_t events[8]; ev1.events = UPOLLIN; ev1.data.fd = sd1; upoll_ctl(upq, UPOLL_CTL_ADD, sd1, &ev1); int r = 0, x = 0; while (1) { int i, e; e = upoll_wait(upq, events, 8, -1); printf("events: %i\n", e); if (x++ == 50) return 0; for (i = 0; i < e; i++) { char buf[4096]; if (events[i].events & UPOLLERR) { printf("ERROR on %li\n", events[i].data.fd); upoll_ctl(upq, UPOLL_CTL_DEL, events[i].data.fd, NULL); uclose(events[i].data.fd); } else if (events[i].events & UPOLLIN) { printf("have POLLIN on %li\n", events[i].data.fd); if (events[i].data.fd == sd1) { int sd2 = uaccept(sd1); printf("uaccept: %i\n", sd2); ev2.events = UPOLLIN; ev2.data.fd = sd2; upoll_ctl(upq, UPOLL_CTL_ADD, sd2, &ev2); } else { int cfd = events[i].data.fd; memset(buf, 0, 4096); printf("client input...\n"); r = uread(cfd, buf, 4096); printf("read %i bytes\n", r); if (r == 0) { printf("EOF DETECTED\n"); upoll_ctl(upq, UPOLL_CTL_DEL, events[i].data.fd, NULL); uclose(events[i].data.fd); } else { ev2.events = UPOLLOUT; upoll_ctl(upq, UPOLL_CTL_MOD, cfd, &ev2); } } } else if (events[i].events & UPOLLOUT) { printf("client writable...\n"); int cfd = events[i].data.fd; int w = uwrite(cfd, buf, r); ev2.events = UPOLLIN; printf("wrote %i bytes, mod for UPOLLIN again\n", w); upoll_ctl(upq, UPOLL_CTL_MOD, cfd, &ev2); } } } #if (defined WIN32 || defined _WIN32) WSACleanup(); #endif return 0; }
/* Single threaded version of pping server. * Uses select() to create connections and serve * previously created as well. * * TODO: read all buffer at once * TODO: better failures diagnostric * TODO: place accept+uNotInherit into a critical section to prevent * descriptors inheritance on fork * */ U_THREAD_PROC(pping_server_lstn_thread_proc_st, arg) { if (uThreadBlockAllSignals(NULL) != 0) d_printf1("Failed to block signals for pping server thread."); U_SSET allset, rset; pping_server *pps = (pping_server*)arg; int res; char c = PPING_DISCONNECT_MSG; bool maxfd_valid = true; USOCKET client_sock, maxfd, i; U_SSET_ZERO(&allset); U_SSET_SET(pps->sock, &allset); maxfd = pps->sock + 1; while (true) { rset = allset; res = uselect_read_arr(&rset, maxfd, NULL, NULL); if (res == U_SOCKET_ERROR) { SYS_FAILURE_SERVER("Failure in pping server (select() call failed)."); } /* Iterate through ready to read client descriptors */ for (i = 0; i < maxfd; i++) { if(U_SSET_ISSET(i, &rset) && i != pps->sock) { res = urecv(i, &c, sizeof(c), NULL); if( res != sizeof(c) ) { SYS_FAILURE_SERVER("Failure in pping server (recv() call failed, one of the clients may be down)."); } if (c == PPING_DISCONNECT_MSG) { U_SSET_CLR(i, &allset); if(uclose_socket(i, NULL) == U_SOCKET_ERROR) SYS_FAILURE_SERVER("Failure in pping server (cannot close a socket descriptor on disconnect)."); if ( maxfd-1 == i ) maxfd_valid = false; } #if (defined(EL_DEBUG) && (EL_DEBUG == 1)) else if (c == PPING_PROC_EXCEPTION_MSG) { if (client_exception_handler(i, pps) != 1) return 0; } #endif else if (c != PPING_KEEP_ALIVE_MSG) SYS_FAILURE_SERVER("Failure in pping server (unexpected message from client)."); } } /* Renew maximum descriptor number */ if( !maxfd_valid ) { for (i = maxfd-1; i >= 0; i--) { if(U_SSET_ISSET(i, &allset)) { maxfd = i + 1; maxfd_valid = true; break; } } } /* Create a new connection */ if (U_SSET_ISSET(pps->sock, &rset)) { client_sock = uaccept(pps->sock, NULL); if(client_sock == U_INVALID_SOCKET) SYS_FAILURE_SERVER("Failure in pping server (accept() call failed)."); U_SSET_SET(client_sock, &allset); if (client_sock > maxfd-1) { maxfd = client_sock + 1; } /* Check if governor wants to shutdown us */ if (pps->close_lstn_thread) { for (i = 0; i < maxfd; i++) { if(U_SSET_ISSET(i, &allset) && i != pps->sock && uclose_socket(i, NULL) == U_SOCKET_ERROR) SYS_FAILURE_SERVER("Failure in pping server (cannot close a socket descriptor)."); } break; } /* Make the client descriptor unheritable */ if (uNotInheritDescriptor(UHANDLE(client_sock), NULL) != 0) SYS_FAILURE_SERVER("Failure in pping server (cannot make a socket descriptor unheritable)."); } } return 0; }