diff --git a/Utils/socket_stream.h b/Utils/socket_stream.h index e87ddadc..f18d8e65 100644 --- a/Utils/socket_stream.h +++ b/Utils/socket_stream.h @@ -11,6 +11,7 @@ // $Id$ /////////////////////////////////////////////////////////////////////// +#include #ifndef ARABICA_WINDOWS #include #include @@ -71,7 +72,7 @@ class basic_socketbuf : public std::basic_streambuf static const size_t bufferSize_; static const size_t pbSize_; -#ifndef _MSC_VER +#ifndef ARABICA_WINDOWS static const int INVALID_SOCKET; static const int SOCKET_ERROR; #endif @@ -82,7 +83,7 @@ const size_t basic_socketbuf::bufferSize_ = 1024; template const size_t basic_socketbuf::pbSize_ = 4; // why 4? both Josuttis and Langer&Kreft use 4. -#ifndef _MSC_VER +#ifndef ARABICA_WINDOWS template const int basic_socketbuf::INVALID_SOCKET = -1; template @@ -241,51 +242,15 @@ template bool basic_socketbuf::writeSocket() { // write to the socket - size_t length = pptr() - &(outBuffer_[0]); + charT* from_next = &(outBuffer_[0]); + size_t length = pptr() - from_next; if(!length) return true; - bool ok(true); - const std::codecvt& cvt = -#if !(defined _MSC_VER) || !(_MSC_VER < 1300) - std::use_facet >(this->getloc()); -#else - std::use_facet(this->getloc(), (std::codecvt*)0, true); -#endif + bool ok = (send(sock_, from_next, length, 0) != SOCKET_ERROR); - if(cvt.always_noconv()) - ok = (send(sock_, &(outBuffer_[0]), length, 0) != SOCKET_ERROR); - else - { - // we must do code conversion - std::vector to(length); - char* to_begin = &(to[0]); - const charT* from_next = &(outBuffer_[0]); - std::codecvt_base::result r; - - do - { - char* to_next; - r = cvt.out(outState_, from_next, pptr(), from_next, - to_begin, to_begin + length, to_next); - - if(r == std::codecvt_base::noconv) - { - ok = (send(sock_, from_next, length, 0) != SOCKET_ERROR); - break; - } - ok = (send(sock_, to_begin, to_next - to_begin, 0) != SOCKET_ERROR); - } - while((r == std::codecvt_base::partial) && (ok)); - - ok = ok ? (r != std::codecvt_base::error) : false; - } // if(cvt.always_noconv()) - if(ok) - { - charT* from_next = &(outBuffer_[0]); setp(from_next, from_next + outBuffer_.capacity()); - } // if(ok) return ok; } // writeSocket @@ -304,54 +269,17 @@ int basic_socketbuf::readSocket() if(!inBuffer_.capacity()) growInBuffer(); -#if !(defined _MSC_VER) || !(_MSC_VER < 1300) +#ifdef ARABICA_VS6_WORKAROUND size_t pbCount = std::min(gptr() - eback(), pbSize_); #else size_t pbCount = min(gptr() - eback(), pbSize_); #endif - memcpy(&(inBuffer_[0]) + (pbSize_-pbCount)*sizeof(charT), + memcpy(&(inBuffer_[0]) + (pbSize_-pbCount)*sizeof(charT), gptr() - pbCount*sizeof(charT), pbCount*sizeof(charT)); - const std::codecvt& cvt = -#if !(defined _MSC_VER) || !(_MSC_VER < 1300) - std::use_facet >(this->getloc()); -#else - std::use_facet(this->getloc(), (std::codecvt*)0, true); -#endif - std::vector from(inBuffer_.capacity() - pbSize_); - int res = recv(sock_, &(from[0]), from.capacity(), 0); - if(res > 0) - { - std::codecvt_base::result r; - do - { - const char* from_begin = &(from[0]); - const char* from_next; - charT* to_begin = &(inBuffer_[0]) + pbSize_; - charT* to_next; - charT* to_end = &(inBuffer_[0]) + inBuffer_.capacity(); - - r = cvt.in(inState_, from_begin, from_begin + res, from_next, - to_begin, to_end, to_next); - - if(r == std::codecvt_base::noconv) - memcpy(to_begin, from_begin, res); - else - res = to_next - to_begin; - if(r == std::codecvt_base::partial) - growInBuffer(); - } - while(r == std::codecvt_base::partial); - - if(r == std::codecvt_base::error) - { - // couldn't convert - let's bail - close(); - return 0; - } // if(r == std::codecvt_base::error) - } - else if(res == 0) + int res = recv(sock_, &(inBuffer_[0]) + pbSize_, inBuffer_.capacity() - pbSize_, 0); + if(res == 0) { // server closed the socket close(); @@ -359,7 +287,7 @@ int basic_socketbuf::readSocket() } // if(res == 0) else if(res == SOCKET_ERROR) { -#ifdef _MSC_VER +#ifdef ARABICA_WINDOWS if(GetLastError() == WSAEMSGSIZE) { // buffer was too small, so make it bigger @@ -382,7 +310,7 @@ int basic_socketbuf::readSocket() template int basic_socketbuf::closeSocket(int sock) const { -#ifdef _MSC_VER +#ifdef ARABICA_WINDOWS return closesocket(sock); #else return ::close(sock);