int Socket::accept(InetAddr &addr) { struct sockaddr_in saddr; int connfd = acceptConn(fd_ , saddr); addr.setPureAddr(saddr); return connfd; }
int AzCam3DataConn::receive (rts2core::Block *block) { recvs++; if (isConnState (CONN_DELETE)) return -1; if (sock >= 0 && block->isForRead (sock)) { int rec; if (isConnState (CONN_CONNECTING)) { return acceptConn (); } if (headerSize < 256) { rec = recv (sock, header + headerSize, 256 - headerSize, 0); if (rec < 0) return -1; headerSize += rec; if (headerSize == 256) { std::cerr << "header " << header << std::endl; sscanf (header, "%ld", &dataSize); headerSize = 256+1; } return 0; } static char rbuf[2048*4]; rec = recv (sock, rbuf, 2048*4, 0); if (rec > 0) { if (outFile == 0) { outFile = open ("/tmp/m.fits", O_CREAT | O_TRUNC | O_WRONLY, 00666); if (outFile < 0) return -1; } write (outFile, rbuf, rec); fsync (outFile); dataSize -= rec; if (dataSize <= 0) { close (outFile); dataSize = 0; headerSize = 0; wasRecvd = true; return rec; } } return rec; } return 0; }
void serverLoop() { fd_set fds, orig_fds; FDType fd_echolisten, fd_cmdlisten; int max_fd; int rc; PortType port; port = 0; setupListenSock(&fd_echolisten, &port); rc = listen(fd_echolisten, 8); if (rc < 0) { fprintf(stderr, "Error: server listen failed (%d)\n", errno); exit(1); } printf("Echo Port = %d\n", port); EchoConnection::globalInit(); port = 0; setupListenSock(&fd_cmdlisten, &port); rc = listen(fd_cmdlisten, 8); if (rc < 0) { fprintf(stderr, "Error: server listen failed (%d)\n", errno); exit(1); } printf("Cmd Port = %d\n", port); CmdConnection::globalInit(); FD_ZERO(&orig_fds); FD_SET(fd_echolisten, &orig_fds); FD_SET(fd_cmdlisten, &orig_fds); max_fd = fd_cmdlisten; Connections::init(max_fd); while (1) { fds = orig_fds; rc = select(max_fd+1, &fds, NULL, NULL, NULL); if (rc < 0) { fprintf(stderr, "Error: server select failed (%d)\n", errno); exit(1); } if (rc == 0) { fprintf(stderr, "What Select timed out\n"); exit(1); } else { if (FD_ISSET(fd_echolisten, &fds)) { // got a new echo connection // printf("got an echo connection on %d\n", fd_echolisten); acceptConn(fd_echolisten, &max_fd, &orig_fds, new EchoConnection); rc--; FD_CLR(fd_echolisten, &fds); } if (FD_ISSET(fd_cmdlisten, &fds)) { // got a new cmd connection printf("got an command connection on %d\n", fd_cmdlisten); acceptConn(fd_cmdlisten, &max_fd, &orig_fds, new CmdConnection); rc--; FD_CLR(fd_cmdlisten, &fds); } FDType i = 0; while (rc > 0 && i <= max_fd) { if (FD_ISSET(i, &fds)) { // printf("Found activity on fd=%d\n", i); if (Connections::processConnectionForFD(i)==0) { FD_CLR(i, &orig_fds); close(i); } } i++; } } } }