Error StreamPeerWinsock::write(const uint8_t* p_data,int p_bytes, int &r_sent, bool p_block) { if (status == STATUS_NONE || status == STATUS_ERROR) { return FAILED; }; if (status != STATUS_CONNECTED) { if (_poll_connection(p_block) != OK) { return FAILED; }; if (status != STATUS_CONNECTED) { r_sent = 0; return OK; }; }; int data_to_send = p_bytes; const uint8_t *offset = p_data; if (sockfd == -1) return FAILED; errno = 0; int total_sent = 0; while (data_to_send) { int sent_amount = send(sockfd, (const char*)offset, data_to_send, 0); //printf("Sent TCP data of %d bytes, errno %d\n", sent_amount, errno); if (sent_amount == -1) { if (WSAGetLastError() != WSAEWOULDBLOCK) { perror("shit?"); disconnect(); ERR_PRINT("Server disconnected!\n"); return FAILED; }; if (!p_block) { r_sent = total_sent; return OK; }; _block(sockfd, false, true); } else { data_to_send -= sent_amount; offset += sent_amount; total_sent += sent_amount; }; } r_sent = total_sent; return OK; };
StreamPeerTCP::Status StreamPeerWinsock::get_status() const { if (status == STATUS_CONNECTING) { _poll_connection(); }; return status; };
Error StreamPeerWinsock::read(uint8_t* p_buffer, int p_bytes,int &r_received, bool p_block) { if (!is_connected()) { return FAILED; }; if (status != STATUS_CONNECTED) { if (_poll_connection(p_block) != OK) { return FAILED; }; if (status != STATUS_CONNECTED) { r_received = 0; return OK; }; }; int to_read = p_bytes; int total_read = 0; errno = 0; while (to_read) { int read = recv(sockfd, (char*)p_buffer + total_read, to_read, 0); if (read == -1) { if (WSAGetLastError() != WSAEWOULDBLOCK) { perror("shit?"); disconnect(); ERR_PRINT("Server disconnected!\n"); return FAILED; }; if (!p_block) { r_received = total_read; return OK; }; _block(sockfd, true, false); } else if (read == 0) { disconnect(); return ERR_FILE_EOF; } else { to_read -= read; total_read += read; }; }; r_received = total_read; return OK; };