15 #include "bioStreamBuf.h"
16 #include "config_downloader.h"
17 #include "openSSLWrapper.h"
22 #if defined(WIN32_VC) || defined(WIN64_VC)
28 #ifndef HAVE_STREAMSIZE
30 typedef int streamsize;
44 _buffer = (
char *)PANDA_MALLOC_ARRAY(8192);
45 char *ebuf = _buffer + 8192;
46 char *mbuf = _buffer + 4096;
47 setg(_buffer, mbuf, mbuf);
56 char *m = b + (t - b) / 2;
71 PANDA_FREE_ARRAY(_buffer);
81 open(BioPtr *source) {
108 size_t n = pptr() - pbase();
109 if (downloader_cat.is_spam()) {
110 downloader_cat.spam()
111 <<
"BioStreamBuf::overflow, " << n <<
" bytes\n";
114 size_t num_wrote = write_chars(pbase(), n);
116 if (num_wrote != n) {
143 size_t n = pptr() - pbase();
145 if (downloader_cat.is_spam() && n != 0) {
146 downloader_cat.spam()
147 <<
"BioStreamBuf::sync, " << n <<
" bytes\n";
150 size_t num_wrote = write_chars(pbase(), n);
153 if (num_wrote != n) {
169 if (gptr() >= egptr()) {
170 size_t buffer_size = egptr() - eback();
171 gbump(-(
int)buffer_size);
173 size_t num_bytes = buffer_size;
177 int read_count = BIO_read(*_source, gptr(), buffer_size);
178 thread_consider_yield();
180 if (read_count != (
int)num_bytes) {
182 if (read_count <= 0) {
186 #if defined(WIN32_VC) || defined(WIN64_VC)
187 int os_error = WSAGetLastError();
189 int os_error = errno;
196 _read_open = (BIO_should_retry(*_source) != 0);
201 if (!_read_open && os_error == 35) {
202 downloader_cat.warning() <<
"forcing retry to true again and _read_open to true\n";
203 BIO_set_retry_read(*_source);
208 downloader_cat.info()
209 <<
"Lost connection to "
210 << _source->get_server_name() <<
":"
211 << _source->get_port() <<
" (" << read_count <<
").\n";
212 OpenSSLWrapper::get_global_ptr()->notify_ssl_errors();
215 BIO_get_ssl(*_source, &ssl);
216 if (ssl != (SSL *)NULL) {
217 downloader_cat.warning()
218 <<
"OpenSSL error code: " << SSL_get_error(ssl, read_count)
222 #if defined(WIN32_VC) || defined(WIN64_VC)
223 downloader_cat.warning()
224 <<
"Windows error code: " << os_error <<
"\n";
226 downloader_cat.warning()
227 <<
"Unix error code: " << os_error <<
"\n";
235 nassertr(read_count < (
int)num_bytes, EOF);
236 size_t delta = (int)num_bytes - read_count;
237 memmove(gptr() + delta, gptr(), read_count);
241 if (downloader_cat.is_spam()) {
242 downloader_cat.spam()
243 <<
"read " << read_count <<
" bytes from " << _source <<
"\n";
247 return (
unsigned char)*gptr();
257 size_t BioStreamBuf::
258 write_chars(
const char *start,
size_t length) {
260 size_t wrote_so_far = 0;
262 int write_count = BIO_write(*_source, start, length);
263 thread_consider_yield();
264 while (write_count != (
int)(length - wrote_so_far)) {
265 if (write_count <= 0) {
275 _write_open = (BIO_should_write(*_source) != 0 || BIO_should_retry(*_source) != 0);
283 BIO_get_fd(*_source, &fd);
285 downloader_cat.warning()
286 <<
"socket BIO has no file descriptor.\n";
288 if (downloader_cat.is_spam()) {
289 downloader_cat.spam()
290 <<
"waiting to write to BIO.\n";
292 #if defined(HAVE_THREADS) && defined(SIMPLE_THREADS)
301 select(fd + 1, NULL, &wset, NULL, NULL);
302 #endif // SIMPLE_THREADS
307 wrote_so_far += write_count;
308 if (downloader_cat.is_spam()) {
309 downloader_cat.spam()
310 <<
"wrote " << write_count <<
" bytes to " << _source <<
"\n";
315 write_count = BIO_write(*_source, start + wrote_so_far, length - wrote_so_far);
316 if (downloader_cat.is_spam()) {
317 downloader_cat.spam()
318 <<
"continued, wrote " << write_count <<
" bytes.\n";
320 thread_consider_yield();
322 if (downloader_cat.is_spam()) {
323 downloader_cat.spam()
324 <<
"wrote " << write_count <<
" bytes to " << _source <<
"\n";
331 #endif // HAVE_OPENSSL