diff --git a/include/mitsuba/core/mmap.h b/include/mitsuba/core/mmap.h index 5b6ef947..ed4a1bd1 100644 --- a/include/mitsuba/core/mmap.h +++ b/include/mitsuba/core/mmap.h @@ -31,12 +31,12 @@ MTS_NAMESPACE_BEGIN */ class MTS_EXPORT_CORE MemoryMappedFile : public Object { public: - /// Map the specified file into memory - MemoryMappedFile(const fs::path &filename); - /// Create a new memory-mapped file of the specified size MemoryMappedFile(const fs::path &filename, size_t size); + /// Map the specified file into memory + MemoryMappedFile(const fs::path &filename, bool readOnly = true); + /// Return a pointer to the file contents in memory void *getData(); @@ -46,6 +46,17 @@ public: /// Return the size of the mapped region size_t getSize() const; + /** + * \brief Resize the memory-mapped file + * + * This involves remapping the file, which will + * generally change the pointer obtained via getData() + */ + void resize(size_t size); + + /// Return whether the mapped memory region is read-only + bool isReadOnly() const; + /// Return a string representation std::string toString() const; diff --git a/src/libcore/appender.cpp b/src/libcore/appender.cpp index f96f79da..a6f73d25 100644 --- a/src/libcore/appender.cpp +++ b/src/libcore/appender.cpp @@ -18,8 +18,9 @@ #include #include -#ifdef WIN32 -#include + +#if defined(__WINDOWS__) +# include #endif MTS_NAMESPACE_BEGIN @@ -110,7 +111,7 @@ UnbufferedAppender::UnbufferedAppender(int fd) void UnbufferedAppender::append(ELogLevel level, const std::string &text) { std::string value = text + std::string("\n"); -#if defined(WIN32) +#if defined(__WINDOWS__) write(m_fd, value.c_str(), (unsigned int) value.length()); #else if (write(m_fd, value.c_str(), value.length()) != (ssize_t) value.length()) diff --git a/src/libcore/fstream.cpp b/src/libcore/fstream.cpp index f31c91d4..fad3f3d5 100644 --- a/src/libcore/fstream.cpp +++ b/src/libcore/fstream.cpp @@ -19,7 +19,7 @@ #include #include -#if !defined(WIN32) +#if !defined(__WINDOWS__) # include #else # include @@ -30,7 +30,7 @@ MTS_NAMESPACE_BEGIN struct FileStream::FileStreamPrivate { -#if defined(WIN32) +#if defined(__WINDOWS__) HANDLE file; #else FILE* file; @@ -80,7 +80,7 @@ void FileStream::open(const fs::path &path, EFileMode mode) { d->write = true; d->read = true; -#ifdef WIN32 +#if defined(__WINDOWS__) DWORD dwDesiredAccess = GENERIC_READ; DWORD dwCreationDisposition = OPEN_EXISTING; @@ -165,7 +165,7 @@ void FileStream::close() { AssertEx(d->file != 0, "No file is currently open"); Log(ETrace, "Closing \"%s\"", d->path.string().c_str()); -#ifdef WIN32 +#if defined(__WINDOWS__) if (!CloseHandle(d->file)) { Log(EError, "Error while trying to close file \"%s\": %s", d->path.string().c_str(), lastErrorText().c_str()); @@ -190,7 +190,7 @@ void FileStream::remove() { void FileStream::seek(size_t pos) { AssertEx(d->file != 0, "No file is currently open"); -#ifdef WIN32 +#if defined(__WINDOWS__) LARGE_INTEGER fpos; fpos.QuadPart = pos; if (SetFilePointerEx(d->file, fpos, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { @@ -207,7 +207,7 @@ void FileStream::seek(size_t pos) { size_t FileStream::getPos() const { AssertEx(d->file != 0, "No file is currently open"); -#ifdef WIN32 +#if defined(__WINDOWS__) DWORD pos = SetFilePointer(d->file, 0, 0, FILE_CURRENT); if (pos == INVALID_SET_FILE_POINTER) { Log(EError, "Error while looking up the position in file \"%s\": %s", @@ -228,7 +228,7 @@ size_t FileStream::getPos() const { size_t FileStream::getSize() const { AssertEx(d->file != 0, "No file is currently open"); -#ifdef WIN32 +#if defined(__WINDOWS__) LARGE_INTEGER result; if (GetFileSizeEx(d->file, &result) == 0) { Log(EError, "Error while getting the file size of \"%s\": %s", @@ -260,7 +260,7 @@ void FileStream::truncate(size_t size) { if (pos > size) pos = size; -#ifdef WIN32 +#if defined(__WINDOWS__) seek(size); if (!SetEndOfFile(d->file)) { Log(EError, "Error while truncating file \"%s\": %s", @@ -281,7 +281,7 @@ void FileStream::truncate(size_t size) { void FileStream::flush() { AssertEx(d->file != 0, "No file is currently open"); AssertEx(d->write, "File is not open with write access"); -#ifdef WIN32 +#if defined(__WINDOWS__) if (!FlushFileBuffers(d->file)) { Log(EError, "Error while flusing the buffers of \"%s\": %s", d->path.string().c_str(), lastErrorText().c_str()); @@ -300,7 +300,7 @@ void FileStream::read(void *pPtr, size_t size) { if (size == 0) return; -#ifdef WIN32 +#if defined(__WINDOWS__) DWORD lpNumberOfBytesRead; if (!ReadFile(d->file, pPtr, (DWORD) size, &lpNumberOfBytesRead, 0)) { Log(EError, "Error while reading from file \"%s\": %s", @@ -329,7 +329,7 @@ void FileStream::write(const void *pPtr, size_t size) { if (size == 0) return; -#ifdef WIN32 +#if defined(__WINDOWS__) DWORD lpNumberOfBytesWritten; if (!WriteFile(d->file, pPtr, (DWORD) size, &lpNumberOfBytesWritten, 0)) { Log(EError, "Error while writing to file \"%s\": %s", diff --git a/src/libcore/logger.cpp b/src/libcore/logger.cpp index 179288bb..abdc2885 100644 --- a/src/libcore/logger.cpp +++ b/src/libcore/logger.cpp @@ -23,7 +23,7 @@ #if defined(__OSX__) # include -#elif defined(WIN32) +#elif defined(__WINDOWS__) # include #endif @@ -62,7 +62,7 @@ void Logger::log(ELogLevel level, const Class *theClass, char tmp[512], *msg = tmp; va_list iterator; -#if defined(WIN32) +#if defined(__WINDOWS__) va_start(iterator, fmt); size_t size = _vscprintf(fmt, iterator) + 1; @@ -133,7 +133,7 @@ void Logger::log(ELogLevel level, const Class *theClass, if (runningInDebugger) __asm__ ("int $3"); -#elif defined(WIN32) +#elif defined(__WINDOWS__) if (IsDebuggerPresent()) __debugbreak(); #endif diff --git a/src/libcore/mmap.cpp b/src/libcore/mmap.cpp index 98df6237..c00586c4 100644 --- a/src/libcore/mmap.cpp +++ b/src/libcore/mmap.cpp @@ -3,130 +3,153 @@ #if defined(__LINUX__) || defined(__OSX__) # include # include -#elif defined(WIN32) +#elif defined(__WINDOWS__) # include #endif MTS_NAMESPACE_BEGIN -struct MemoryMappedFile::MemoryMappedFilePrivate -{ +struct MemoryMappedFile::MemoryMappedFilePrivate { fs::path filename; -#if defined(WIN32) +#if defined(__WINDOWS__) HANDLE file; HANDLE fileMapping; #endif size_t size; void *data; + bool readOnly; - MemoryMappedFilePrivate(const fs::path & f, size_t s = 0) : - filename(f), size(s), data(NULL) {} + MemoryMappedFilePrivate(const fs::path & f, size_t s = 0) + : filename(f), size(s), data(NULL), readOnly(false) {} + + void create() { + #if defined(__LINUX__) || defined(__OSX__) + int fd = open(filename.string().c_str(), O_RDWR | O_CREAT | O_TRUNC, 0664); + if (fd == -1) + Log(EError, "Could not open \"%s\"!", filename.string().c_str()); + int result = lseek(fd, size-1, SEEK_SET); + if (result == -1) + Log(EError, "Could not set file size of \"%s\"!", filename.string().c_str()); + result = write(fd, "", 1); + if (result != 1) + Log(EError, "Could not write to \"%s\"!", filename.string().c_str()); + data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (data == NULL) + Log(EError, "Could not map \"%s\" to memory!", filename.string().c_str()); + if (close(fd) != 0) + Log(EError, "close(): unable to close file!"); + #elif defined(___WINDOWS__) + file = CreateFile(filename.string().c_str(), GENERIC_WRITE | GENERIC_READ, + FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (file == INVALID_HANDLE_VALUE) + Log(EError, "Could not open \"%s\": %s", filename.string().c_str(), + lastErrorText().c_str()); + fileMapping = CreateFileMapping(file, NULL, PAGE_READWRITE, 0, + static_cast(size), NULL); + if (fileMapping == NULL) + Log(EError, "CreateFileMapping: Could not map \"%s\" to memory: %s", + filename.string().c_str(), lastErrorText().c_str()); + data = (void *) MapViewOfFile(fileMapping, FILE_MAP_WRITE, 0, 0, 0); + if (data == NULL) + Log(EError, "MapViewOfFile: Could not map \"%s\" to memory: %s", + filename.string().c_str(), lastErrorText().c_str()); + #endif + readOnly = false; + } + + void map() { + if (!fs::exists(filename)) + Log(EError, "The file \"%s\" does not exist!", filename.string().c_str()); + size = (size_t) fs::file_size(filename); + #if defined(__LINUX__) || defined(__OSX__) + int fd = open(filename.string().c_str(), readOnly ? O_RDONLY : O_RDWR); + if (fd == -1) + Log(EError, "Could not open \"%s\"!", filename.string().c_str()); + data = mmap(NULL, size, PROT_READ | (readOnly ? 0 : PROT_WRITE), MAP_SHARED, fd, 0); + if (data == NULL) + Log(EError, "Could not map \"%s\" to memory!", filename.string().c_str()); + if (close(fd) != 0) + Log(EError, "close(): unable to close file!"); + #elif defined(__WINDOWS__) + file = CreateFile(filename.string().c_str(), GENERIC_READ | (readOnly ? 0 : GENERIC_WRITE), + FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); + if (file == INVALID_HANDLE_VALUE) + Log(EError, "Could not open \"%s\": %s", filename.string().c_str(), + lastErrorText().c_str()); + fileMapping = CreateFileMapping(file, NULL, readOnly ? PAGE_READONLY : PAGE_READWRITE, 0, 0, NULL); + if (fileMapping == NULL) + Log(EError, "CreateFileMapping: Could not map \"%s\" to memory: %s", + filename.string().c_str(), lastErrorText().c_str()); + data = (void *) MapViewOfFile(fileMapping, readOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0); + if (data == NULL) + Log(EError, "MapViewOfFile: Could not map \"%s\" to memory: %s", + filename.string().c_str(), lastErrorText().c_str()); + #endif + } + + void unmap() { + SLog(ETrace, "Unmapping \"%s\" from memory", + filename.string().c_str()); + #if defined(__LINUX__) || defined(__OSX__) + int retval = munmap(data, size); + if (retval != 0) + Log(EError, "munmap(): unable to unmap memory!"); + #elif defined(__WINDOWS__) + if (!UnmapViewOfFile(data)) + Log(EError, "UnmapViewOfFile(): unable to unmap memory: %s", lastErrorText().c_str()); + if (!CloseHandle(fileMapping)) + Log(EError, "CloseHandle(): unable to close file mapping: %s", lastErrorText().c_str()); + if (!CloseHandle(file)) + Log(EError, "CloseHandle(): unable to close file: %s", lastErrorText().c_str()); + #endif + data = NULL; + size = 0; + } }; MemoryMappedFile::MemoryMappedFile(const fs::path &filename, size_t size) : d(new MemoryMappedFilePrivate(filename, size)) { - Log(ETrace, "Creating memory-mapped file \"%s\" (%s)..", - filename.filename().string().c_str(), memString(d->size).c_str()); -#if defined(__LINUX__) || defined(__OSX__) - int fd = open(filename.string().c_str(), O_RDWR | O_CREAT | O_TRUNC, 0664); - if (fd == -1) - Log(EError, "Could not open \"%s\"!", d->filename.string().c_str()); - int result = lseek(fd, size-1, SEEK_SET); - if (result == -1) - Log(EError, "Could not set file size of \"%s\"!", d->filename.string().c_str()); - result = write(fd, "", 1); - if (result != 1) - Log(EError, "Could not write to \"%s\"!", d->filename.string().c_str()); - d->data = mmap(NULL, d->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (d->data == NULL) - Log(EError, "Could not map \"%s\" to memory!", d->filename.string().c_str()); - if (close(fd) != 0) - Log(EError, "close(): unable to close file!"); -#elif defined(_WIN32) - d->file = CreateFile(filename.string().c_str(), GENERIC_WRITE | GENERIC_READ, - FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (d->file == INVALID_HANDLE_VALUE) - Log(EError, "Could not open \"%s\": %s", d->filename.string().c_str(), - lastErrorText().c_str()); - d->fileMapping = CreateFileMapping(d->file, NULL, PAGE_READWRITE, 0, - static_cast(size), NULL); - if (d->fileMapping == NULL) - Log(EError, "CreateFileMapping: Could not map \"%s\" to memory: %s", - d->filename.string().c_str(), lastErrorText().c_str()); - d->data = (void *) MapViewOfFile(d->fileMapping, FILE_MAP_WRITE, 0, 0, 0); - if (d->data == NULL) - Log(EError, "MapViewOfFile: Could not map \"%s\" to memory: %s", - d->filename.string().c_str(), lastErrorText().c_str()); -#endif + SLog(ETrace, "Creating memory-mapped file \"%s\" (%s)..", + filename.filename().string().c_str(), memString(d->size).c_str()); + d->create(); } -MemoryMappedFile::MemoryMappedFile(const fs::path &filename) +MemoryMappedFile::MemoryMappedFile(const fs::path &filename, bool readOnly) : d(new MemoryMappedFilePrivate(filename)) { - if (!fs::exists(filename)) - Log(EError, "The file \"%s\" does not exist!", filename.string().c_str()); - d->size = (size_t) fs::file_size(filename); - Log(ETrace, "Mapping \"%s\" into memory (%s)..", - filename.filename().string().c_str(), memString(d->size).c_str()); -#if defined(__LINUX__) || defined(__OSX__) - int fd = open(filename.string().c_str(), O_RDONLY); - if (fd == -1) - Log(EError, "Could not open \"%s\"!", d->filename.string().c_str()); - d->data = mmap(NULL, d->size, PROT_READ, MAP_SHARED, fd, 0); - if (d->data == NULL) - Log(EError, "Could not map \"%s\" to memory!", d->filename.string().c_str()); - if (close(fd) != 0) - Log(EError, "close(): unable to close file!"); -#elif defined(WIN32) - d->file = CreateFile(filename.string().c_str(), GENERIC_READ, - FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, NULL); - if (d->file == INVALID_HANDLE_VALUE) - Log(EError, "Could not open \"%s\": %s", d->filename.string().c_str(), - lastErrorText().c_str()); - d->fileMapping = CreateFileMapping(d->file, NULL, PAGE_READONLY, 0, 0, NULL); - if (d->fileMapping == NULL) - Log(EError, "CreateFileMapping: Could not map \"%s\" to memory: %s", - d->filename.string().c_str(), lastErrorText().c_str()); - d->data = (void *) MapViewOfFile(d->fileMapping, FILE_MAP_READ, 0, 0, 0); - if (d->data == NULL) - Log(EError, "MapViewOfFile: Could not map \"%s\" to memory: %s", - d->filename.string().c_str(), lastErrorText().c_str()); -#endif + d->readOnly = readOnly; + d->map(); + Log(ETrace, "Mapped \"%s\" into memory (%s)..", + filename.filename().string().c_str(), memString(d->size).c_str()); } MemoryMappedFile::~MemoryMappedFile() { - if (d->data != NULL) { - Log(ETrace, "Unmapping \"%s\" from memory", - d->filename.string().c_str()); - - #if defined(__LINUX__) || defined(__OSX__) - int retval = munmap(d->data, d->size); - if (retval != 0) - Log(EWarn, "munmap(): unable to unmap memory!"); - #elif defined(WIN32) - if (!UnmapViewOfFile(d->data)) { - Log(EWarn, "UnmapViewOfFile(): unable to unmap memory: %s", lastErrorText().c_str()); - return; - } - if (!CloseHandle(d->fileMapping)) { - Log(EWarn, "CloseHandle(): unable to close file mapping: %s", lastErrorText().c_str()); - return; - } - if (!CloseHandle(d->file)) { - Log(EWarn, "CloseHandle(): unable to close file: %s", lastErrorText().c_str()); - return; - } - #endif + if (d->data) { + try { + d->unmap(); + } catch (std::exception &e) { + /* Don't throw exceptions from a constructor */ + Log(EWarn, "%s", e.what()); + } } } -void * MemoryMappedFile::getData() { +void MemoryMappedFile::resize(size_t size) { + if (!d->data) + Log(EError, "Internal error in MemoryMappedFile::resize()!"); + d->unmap(); + fs::resize_file(d->filename, size); + d->size = size; + d->map(); +} + +void *MemoryMappedFile::getData() { return d->data; } /// Return a pointer to the file contents in memory (const version) -const void * MemoryMappedFile::getData() const { +const void *MemoryMappedFile::getData() const { return d->data; } @@ -134,10 +157,14 @@ size_t MemoryMappedFile::getSize() const { return d->size; } +bool MemoryMappedFile::isReadOnly() const { + return d->readOnly; +} + std::string MemoryMappedFile::toString() const { std::ostringstream oss; oss << "MemoryMappedFile[filename=\"" - << d->filename.string() << "\", size=" + << d->filename.string() << "\", size=" << memString(d->size) << "]"; return oss.str(); } diff --git a/src/libcore/plugin.cpp b/src/libcore/plugin.cpp index 070179c3..25116f25 100644 --- a/src/libcore/plugin.cpp +++ b/src/libcore/plugin.cpp @@ -24,7 +24,7 @@ #include #include -#if !defined(WIN32) +#if !defined(__WINDOWS__) # include #else # include @@ -43,7 +43,7 @@ namespace { } struct Plugin::PluginPrivate { -#if defined(WIN32) +#if defined(__WINDOWS__) HMODULE handle; #else void *handle; @@ -61,7 +61,7 @@ struct Plugin::PluginPrivate { Plugin::Plugin(const std::string &shortName, const fs::path &path) : d(new PluginPrivate(shortName, path)) { -#if defined(_WIN32) +#if defined(___WINDOWS__) d->handle = LoadLibraryW(path.c_str()); if (!d->handle) { SLog(EError, "Error while loading plugin \"%s\": %s", @@ -77,7 +77,7 @@ Plugin::Plugin(const std::string &shortName, const fs::path &path) try { d->getDescription = (GetDescriptionFunc) getSymbol("GetDescription"); } catch (...) { -#if defined(_WIN32) +#if defined(___WINDOWS__) FreeLibrary(d->handle); #else dlclose(d->handle); @@ -102,7 +102,7 @@ Plugin::Plugin(const std::string &shortName, const fs::path &path) } bool Plugin::hasSymbol(const std::string &sym) const { -#if defined(_WIN32) +#if defined(___WINDOWS__) void *ptr = GetProcAddress(d->handle, sym.c_str()); #else void *ptr = dlsym(d->handle, sym.c_str()); @@ -111,7 +111,7 @@ bool Plugin::hasSymbol(const std::string &sym) const { } void *Plugin::getSymbol(const std::string &sym) { -#if defined(_WIN32) +#if defined(___WINDOWS__) void *data = GetProcAddress(d->handle, sym.c_str()); if (!data) { SLog(EError, "Could not resolve symbol \"%s\" in \"%s\": %s", @@ -152,7 +152,7 @@ const std::string& Plugin::getShortName() const { } Plugin::~Plugin() { -#if defined(_WIN32) +#if defined(___WINDOWS__) FreeLibrary(d->handle); #else dlclose(d->handle); @@ -226,7 +226,7 @@ void PluginManager::ensurePluginLoaded(const std::string &name) { /* Build the full plugin file name */ fs::path shortName = fs::path("plugins") / name; -#if defined(WIN32) +#if defined(__WINDOWS__) shortName.replace_extension(".dll"); #elif defined(__OSX__) shortName.replace_extension(".dylib"); diff --git a/src/libcore/random.cpp b/src/libcore/random.cpp index f85625cb..9f1d8a0f 100644 --- a/src/libcore/random.cpp +++ b/src/libcore/random.cpp @@ -473,7 +473,7 @@ void Random::State::init_by_array(const uint32_t *init_key, int key_length) { Random::Random() : mt(NULL) { mt = (State *) allocAligned(sizeof(State)); Assert(mt != NULL); -#if defined(_WIN32) +#if defined(___WINDOWS__) seed(); #else #if 0 diff --git a/src/libcore/sshstream.cpp b/src/libcore/sshstream.cpp index d775be16..610a6f4b 100644 --- a/src/libcore/sshstream.cpp +++ b/src/libcore/sshstream.cpp @@ -19,7 +19,7 @@ #include #include -#if !defined(WIN32) +#if !defined(__WINDOWS__) # include #else # include @@ -33,7 +33,7 @@ struct SSHStream::SSHStreamPrivate const std::string userName, hostName; const int port, timeout; size_t received, sent; -#if defined(WIN32) +#if defined(__WINDOWS__) HANDLE childInRd, childInWr; HANDLE childOutRd, childOutWr; #else @@ -44,7 +44,7 @@ struct SSHStream::SSHStreamPrivate SSHStreamPrivate(const std::string& uname, const std::string& hname, int p, int tm) : userName(uname), hostName(hname), port(p), timeout(tm), received(0), sent(0), -#if defined(WIN32) +#if defined(__WINDOWS__) childInRd(0), childInWr(0), childOutRd(0), childOutWr(0) #else infd(-1), outfd(-1), input(0), output(0) @@ -60,7 +60,7 @@ SSHStream::SSHStream(const std::string &userName, Log(EInfo, "Establishing a SSH connection to \"%s@%s\"", userName.c_str(), hostName.c_str()); -#if defined(WIN32) +#if defined(__WINDOWS__) /* Inherit pipe handles */ SECURITY_ATTRIBUTES sAttr; sAttr.nLength = sizeof(SECURITY_ATTRIBUTES); @@ -173,7 +173,7 @@ SSHStream::SSHStream(const std::string &userName, SSHStream::~SSHStream() { Log(EDebug, "Closing SSH connection"); -#if defined(WIN32) +#if defined(__WINDOWS__) CloseHandle(d->childInWr); CloseHandle(d->childOutRd); #else @@ -225,7 +225,7 @@ void SSHStream::truncate(size_t size) { } void SSHStream::flush() { -#if defined(WIN32) +#if defined(__WINDOWS__) // No-op #else if (fflush(d->output) == EOF) @@ -235,7 +235,7 @@ void SSHStream::flush() { void SSHStream::read(void *ptr, size_t size) { static StatsCounter bytesRcvd("Network", "Bytes received (SSH)"); -#if defined(WIN32) +#if defined(__WINDOWS__) size_t left = size; char *data = (char *) ptr; while (left > 0) { @@ -260,7 +260,7 @@ void SSHStream::read(void *ptr, size_t size) { void SSHStream::write(const void *ptr, size_t size) { static StatsCounter bytesSent("Network", "Bytes sent (SSH)"); -#if defined(WIN32) +#if defined(__WINDOWS__) size_t left = size; char *data = (char *) ptr; while (left > 0) { diff --git a/src/libcore/sstream.cpp b/src/libcore/sstream.cpp index 57c7da09..9200ab3a 100644 --- a/src/libcore/sstream.cpp +++ b/src/libcore/sstream.cpp @@ -19,7 +19,7 @@ #include #include -#if !defined(WIN32) +#if !defined(__WINDOWS__) # include # include # include @@ -44,7 +44,7 @@ static StatsCounter bytesSent("Network", "Bytes sent"); namespace { -#if defined(WIN32) +#if defined(__WINDOWS__) // This function is natively avaiable since Windows Vista const char *inet_ntop(int af, const void *src, char *dst, socklen_t len) { if (af == AF_INET) { @@ -148,7 +148,7 @@ SocketStream::SocketStream(const std::string &host, int port) } SocketStream::~SocketStream() { -#ifdef WIN32 +#ifdef __WINDOWS__ if (closesocket(m_socket) == SOCKET_ERROR) handleError("closesocket"); #else @@ -161,7 +161,7 @@ void SocketStream::read(void *ptr, size_t size) { const size_t total = size; char *data = (char *) ptr; while (size > 0) { -#if defined(WIN32) +#if defined(__WINDOWS__) int n = recv(m_socket, data, (int) size, 0); #else int n = recv(m_socket, data, size, 0); @@ -190,7 +190,7 @@ void SocketStream::write(const void *ptr, size_t size) { #if defined(__LINUX__) /* Linux: Don't send the EPIPE signal when the connection breaks */ int n = send(m_socket, data, size, MSG_NOSIGNAL); -#elif defined(WIN32) +#elif defined(__WINDOWS__) int n = send(m_socket, data, (int) size, 0); #else int n = send(m_socket, data, size, 0);