/* Low-level receive functions. * * On failure, sets errmsg to errno string and returns -1. * On success, returns number of bytes read. * Does not attempt to reconnect nor throw any exceptions. * * On failure, the calling function is responsible for disconnecting */ int mongo_io_recv_header(mongo_connection *con, mongo_server_options *options, int timeout, void *data, int size, char **error_message) { int status = mongo_io_wait_with_timeout((int) (long)con->socket, timeout ? timeout : options->socketTimeoutMS, error_message); if (status != 0) { /* We don't care which failure it was, it just failed and the error_message has been set */ *error_message = strdup("Timed out waiting for header data"); return -80; } status = recv((int) (long) con->socket, data, size, 0); if (status == -1) { *error_message = strdup(strerror(errno)); #ifndef WIN32 if (errno == ECONNRESET) { return -32; } else { return -31; } #else return -31; #endif } else if (status == 0) { *error_message = strdup("Remote server has closed the connection"); return -32; } return status; }
/* * Low-level receive functions. * * On failure, sets errmsg to errno string and returns -1. * On success, returns number of bytes read. * Does not attempt to reconnect nor throw any exceptions. * * On failure, the calling function is responsible for disconnecting */ int mongo_io_recv_header(mongo_connection *con, mongo_server_options *options, void *data, int size, char **error_message) { int status = mongo_io_wait_with_timeout((int) (long)con->socket, options ? options->socketTimeoutMS : 0, error_message); if (status != 0) { /* We don't care which failure it was, it just failed and the error_message has been set */ return -1; } status = recv((int) (long) con->socket, data, size, 0); if (status == -1) { *error_message = strdup(strerror(errno)); return -1; } else if (status == 0) { *error_message = strdup("The socket is closed"); return -1; } return status; }
int mongo_io_recv_data(mongo_connection *con, mongo_server_options *options, int timeout, void *data, int size, char **error_message) { int num = 1, received = 0; /* this can return FAILED if there is just no more data from db */ while (received < size && num > 0) { int len = 4096 < (size - received) ? 4096 : size - received; if (mongo_io_wait_with_timeout((int) (long) con->socket, timeout ? timeout : options->socketTimeoutMS, error_message) != 0) { /* We don't care which failure it was, it just failed */ return -31; } // windows gives a WSAEFAULT if you try to get more bytes num = recv((int) (long)con->socket, (char*)data, len, 0); if (num < 0) { return -31; } data = (char*)data + num; received += num; } return received; }