static void TEST__server(const char* sport) { int i; unsigned short nport; LSOCK lsock; EIO_Status status; /* Create listening socket */ if (sscanf(sport, "%hu%n", &nport, &i) < 1 || sport[i]) { nport = 0; i = 0; } status = LSOCK_CreateEx(nport, N_RECONNECT * 10, &lsock, fSOCK_LogOn); if (status == eIO_Success && !nport && sport[i]) { FILE* fp; nport = LSOCK_GetPort(lsock, eNH_HostByteOrder); if (nport && (fp = fopen(sport, "w")) != 0) { if (fprintf(fp, "%hu", nport) < 1 || fflush(fp) != 0) status = eIO_Unknown; fclose(fp); } else status = eIO_Unknown; } CORE_LOGF(eLOG_Note, ("TEST__server(port = %hu)", nport)); assert(status == eIO_Success); /* Accept connections from clients and run test sessions */ for (;;) { char full[80]; char addr[80]; char port[10]; SOCK sock; status = LSOCK_Accept(lsock, NULL, &sock); assert(status == eIO_Success); assert(SOCK_GetPeerAddressString (sock, full,sizeof(full))); assert(SOCK_GetPeerAddressStringEx(sock, addr,sizeof(addr),eSAF_IP)); assert(SOCK_GetPeerAddressStringEx(sock, port,sizeof(port),eSAF_Port)); assert(strcmp(full, strcat(strcat(addr, ":"), port)) == 0); /* Test the simplest randezvous(plain request-reply) * The two peer functions are: * "TEST__[client|server]_1(SOCK sock)" */ TEST__server_1(sock); status = LSOCK_Accept(lsock, NULL, &sock); assert(status == eIO_Success); /* Test a more complex case * The two peer functions are: * "TEST__[client|server]_2(SOCK sock)" */ TEST__server_2(sock, lsock); } }
static void TEST__server(unsigned short port) { LSOCK lsock; EIO_Status status; CORE_LOGF(eLOG_Note, ("TEST__server(port = %hu)", port)); /* Create listening socket */ status = LSOCK_CreateEx(port, N_RECONNECT * 10, &lsock, fSOCK_LogOn); assert(status == eIO_Success); /* Accept connections from clients and run test sessions */ for (;;) { char full[80]; char addr[80]; char port[10]; SOCK sock; status = LSOCK_Accept(lsock, NULL, &sock); assert(status == eIO_Success); assert(SOCK_GetPeerAddressString (sock, full,sizeof(full))); assert(SOCK_GetPeerAddressStringEx(sock, addr,sizeof(addr),eSAF_IP)); assert(SOCK_GetPeerAddressStringEx(sock, port,sizeof(port),eSAF_Port)); assert(strcmp(full, strcat(strcat(addr, ":"), port)) == 0); /* Test the simplest randezvous(plain request-reply) * The two peer functions are: * "TEST__[client|server]_1(SOCK sock)" */ TEST__server_1(sock); status = LSOCK_Accept(lsock, NULL, &sock); assert(status == eIO_Success); /* Test a more complex case * The two peer functions are: * "TEST__[client|server]_2(SOCK sock)" */ #ifdef DO_RECONNECT TEST__server_2(sock, lsock); #else TEST__server_2(sock, 0); #endif #ifdef TEST_SRV1_ONCE /* Close listening socket */ assert(LSOCK_Close(lsock) == eIO_Success); /* Finish after the first session */ break; #endif } /* for */ }
static void TEST__server_2(SOCK sock, LSOCK lsock) { EIO_Status status; size_t n_io, n_io_done; char buf[TEST_BUFSIZE]; STimeout r_to, w_to, rc_to; size_t i; CORE_LOG(eLOG_Note, "TEST__server_2(TS2)"); r_to.sec = 0; r_to.usec = 0; w_to = r_to; rc_to.sec = 30; rc_to.usec = 123456; /* goto */ l_reconnect: /* reconnection loopback */ SOCK_SetDataLogging(sock, eOn); status = SOCK_SetTimeout(sock, eIO_Read, &r_to); assert(status == eIO_Success); status = SOCK_SetTimeout(sock, eIO_Write, &w_to); assert(status == eIO_Success); for (i = 0; ; i++) { char* x_buf; /* read data from socket */ n_io = sizeof(buf); status = SOCK_Read(sock, buf, n_io, &n_io_done, eIO_ReadPlain); switch ( status ) { case eIO_Success: CORE_LOGF(eLOG_Note, ("TS2::read:" " [%lu], status=%7s, n_io=%5lu, n_io_done=%5lu", (unsigned long)i, IO_StatusStr(status), (unsigned long)n_io, (unsigned long)n_io_done)); assert(n_io_done > 0); break; case eIO_Closed: CORE_LOG(eLOG_Note, "TS2::read: connection closed"); assert(SOCK_Status(sock, eIO_Read) == eIO_Closed); /* close connection */ status = SOCK_Close(sock); assert(status == eIO_Success || status == eIO_Closed); /* reconnect */ if ( !lsock ) return; CORE_LOG(eLOG_Note, "TS2::reconnect"); if ((status = LSOCK_Accept(lsock, &rc_to, &sock)) != eIO_Success) return; assert(SOCK_Status(sock, eIO_Read) == eIO_Success); /* !!! */ goto l_reconnect; case eIO_Timeout: CORE_LOGF(eLOG_Note, ("TS2::read:" " [%lu] timeout expired: %5u sec, %6u usec", (unsigned long)i, r_to.sec, r_to.usec)); assert(n_io_done == 0); s_DoubleTimeout(&r_to); status = SOCK_SetTimeout(sock, eIO_Read, &r_to); assert(status == eIO_Success); assert(SOCK_Status(sock, eIO_Read) == eIO_Timeout); break; default: CORE_LOGF(eLOG_Fatal, ("TS2::read: status = %d", (int) status)); } /* switch */ /* write(just the same) data back to client */ n_io = n_io_done; x_buf = buf; while ( n_io ) { status = SOCK_Write(sock, buf, n_io, &n_io_done, eIO_WritePersist); switch ( status ) { case eIO_Success: CORE_LOGF(eLOG_Note, ("TS2::write:" " [%lu], status=%7s, n_io=%5lu, n_io_done=%5lu", (unsigned long)i, IO_StatusStr(status), (unsigned long)n_io, (unsigned long)n_io_done)); assert(n_io_done > 0); break; case eIO_Closed: CORE_LOG(eLOG_Fatal, "TS2::write: connection closed"); return; case eIO_Timeout: CORE_LOGF(eLOG_Note, ("TS2::write:" " [%lu] timeout expired: %5u sec, %6u usec", (unsigned long)i, w_to.sec, w_to.usec)); assert(n_io_done == 0); s_DoubleTimeout(&w_to); status = SOCK_SetTimeout(sock, eIO_Write, &w_to); assert(status == eIO_Success); break; default: CORE_LOGF(eLOG_Fatal, ("TS2::write: status = %d", (int) status)); } /* switch */ n_io -= n_io_done; x_buf += n_io_done; } } }