int brl_use_mainloop(brl_t *brl, void *ml, brl_mainloop_ops_t *ops, brl_line_cb_t cb, void *user_data) { if (brl != NULL) { if (brl->ml != NULL || brl->ml_ops != NULL) { errno = EBUSY; return -1; } brl->line_cb = cb; brl->user_data = user_data; brl->ml = ml; brl->ml_ops = ops; brl->ml_w = brl->ml_ops->add_watch(ml, brl->fd, _brl_io_cb, brl); if (brl->ml_w != NULL) { disable_blocking(brl); return 0; } else errno = EIO; } else errno = EFAULT; return -1; }
static int setup_terminal(brl_t *brl) { if (!isatty(brl->fd)) { errno = ENOTTY; return -1; } if (tcgetattr(brl->fd, &brl->term_mode) == -1) return -1; if (enable_rawmode(brl) != 0) return -1; brl->term_flags = fcntl(brl->fd, F_GETFL); brl->term_blck = TRUE; #if 0 if (disable_blocking(brl) == 0) return 0; else return -1; #else return 0; #endif }
int connect_connections(const struct settings *settings, const struct client_request * req, SOCKET *client, unsigned int *clients) { const struct client_request_details * details = req->details; // Loop all the client requests for this thread while (details != NULL) { unsigned int i = details->n; if (settings->verbose) { char addr[NI_MAXHOST + NI_MAXSERV + 1]; // Print the host/port addr_to_ipstr((const struct sockaddr *)&details->addr, details->addr_len, addr, sizeof(addr)); printf(" Core %d: Connecting %d client%s to %s\n", req->cores, details->n, details->n > 1 ? "s" : "", addr); } // Connect all the clients while (i > 0) { int send_socket_size, recv_socket_size; SOCKET s = socket( AF_INET, settings->type, settings->protocol); if (s == INVALID_SOCKET) { fprintf(stderr, "%s:%d socket() error (%d) %s\n", __FILE__, __LINE__, ERRNO, strerror(ERRNO)); return -1; } #ifndef WIN32 #ifndef USE_EPOLL // In GNU world, a socket can't be >= FD_SETSIZE, otherwise it can't be placed into a set if ( s >= FD_SETSIZE ) { fprintf(stderr, "%s:%d socket() value too large for fd_set (%d >= %d)\n", __FILE__, __LINE__, s, FD_SETSIZE ); return -1; } #endif #endif send_socket_size = set_socket_send_buffer(s, settings->socket_size); if (send_socket_size < 0) { fprintf(stderr, "%s:%d set_socket_send_buffer() error (%d) %s\n", __FILE__, __LINE__, ERRNO, strerror(ERRNO)); goto cleanup; } recv_socket_size = set_socket_recv_buffer(s, settings->socket_size); if (recv_socket_size < 0) { fprintf(stderr, "%s:%d set_socket_recv_buffer() error (%d) %s\n", __FILE__, __LINE__, ERRNO, strerror(ERRNO)); goto cleanup; } if (settings->verbose) { // TODO tidy this printf("client socket size: %d/%d\n", send_socket_size, recv_socket_size); } if (settings->disable_nagles) { if (disable_nagle(s) == SOCKET_ERROR) { fprintf(stderr, "%s:%d disable_nagle() error (%d) %s\n", __FILE__, __LINE__, ERRNO, strerror(ERRNO)); goto cleanup; } // This works around a big where disabling Nagle's does not actually stop packets being grouped // I don't think its a bug, more that stack notices there are multiple packets queued that // haven't been sent yet, so optimistically groups them. if ( enable_maxseq ( s , settings->message_size ) == SOCKET_ERROR ) { fprintf(stderr, "%s:%d enable_maxseq() error (%d) %s\n", __FILE__, __LINE__, ERRNO, strerror(ERRNO)); goto cleanup; } } if (set_socket_timeout(s, CONTROL_TIMEOUT) ) { fprintf(stderr, "%s:%d set_socket_timeout() error (%d) %s\n", __FILE__, __LINE__, ERRNO, strerror(ERRNO)); goto cleanup; } if (connect_ign_signal(s, (const struct sockaddr *)&details->addr, (int)details->addr_len ) == SOCKET_ERROR) { fprintf(stderr, "%s:%d connect() error (%d) %s\n", __FILE__, __LINE__, ERRNO, strerror(ERRNO)); goto cleanup; } // Always disable blocking (to work around linux bug) if (disable_blocking(s) == SOCKET_ERROR) { fprintf(stderr, "%s:%d disable_blocking() error (%d) %s\n", __FILE__, __LINE__, ERRNO, strerror(ERRNO)); goto cleanup; } assert ( s != INVALID_SOCKET ); assert ( *client == INVALID_SOCKET ); *client++ = s; // Add socket s to the end of the array and move along (*clients)++; // Increment the count of clients i--; continue; cleanup: // This cleanup section is within the loop so we can cleanup s closesocket(s); return -1; } // move onto the next client request details = details->next; } return 0; }