Skip to content

Commit 038e0bf

Browse files
bsergeanclaude
andauthored
Replace ssize_t with std::ptrdiff_t to avoid typedef clashes on MSVC (fix #537) (#597)
MSVC has no ssize_t, so IXSocket.h and IXUdpSocket.h injected a global 'typedef SSIZE_T ssize_t' which collides with other libraries defining their own ssize_t shim. Use std::ptrdiff_t instead, which is the same underlying type on all supported 64-bit platforms, and drop the Windows/Apple header shims entirely. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 181e2a8 commit 038e0bf

13 files changed

Lines changed: 42 additions & 58 deletions

ixwebsocket/IXHttpClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ namespace ix
414414
// Parse response:
415415
if (headers.find("Content-Length") != headers.end())
416416
{
417-
ssize_t contentLength = -1;
417+
std::ptrdiff_t contentLength = -1;
418418
ss.str("");
419419
ss << headers["Content-Length"];
420420
ss >> contentLength;

ixwebsocket/IXSelectInterruptPipe.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ namespace ix
121121
int fd = _fildes[kPipeWriteIndex];
122122
if (fd == -1) return false;
123123

124-
ssize_t ret = -1;
124+
std::ptrdiff_t ret = -1;
125125
do
126126
{
127127
ret = ::write(fd, &value, sizeof(value));
@@ -139,7 +139,7 @@ namespace ix
139139

140140
uint64_t value = 0;
141141

142-
ssize_t readret = -1;
142+
std::ptrdiff_t readret = -1;
143143
do
144144
{
145145
readret = ::read(fd, &value, sizeof(value));

ixwebsocket/IXSocket.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ namespace ix
240240
_sockfd = -1;
241241
}
242242

243-
ssize_t Socket::send(char* buffer, size_t length)
243+
std::ptrdiff_t Socket::send(char* buffer, size_t length)
244244
{
245245
int flags = 0;
246246
#ifdef MSG_NOSIGNAL
@@ -254,12 +254,12 @@ namespace ix
254254
#endif
255255
}
256256

257-
ssize_t Socket::send(const std::string& buffer)
257+
std::ptrdiff_t Socket::send(const std::string& buffer)
258258
{
259259
return send((char*) &buffer[0], buffer.size());
260260
}
261261

262-
ssize_t Socket::recv(void* buffer, size_t length)
262+
std::ptrdiff_t Socket::recv(void* buffer, size_t length)
263263
{
264264
int flags = 0;
265265
#ifdef MSG_NOSIGNAL
@@ -333,7 +333,7 @@ namespace ix
333333
{
334334
if (isCancellationRequested && isCancellationRequested()) return false;
335335

336-
ssize_t ret = send((char*) &str[offset], len);
336+
std::ptrdiff_t ret = send((char*) &str[offset], len);
337337

338338
// We wrote some bytes, as needed, all good.
339339
if (ret > 0)
@@ -368,7 +368,7 @@ namespace ix
368368
{
369369
if (isCancellationRequested && isCancellationRequested()) return false;
370370

371-
ssize_t ret;
371+
std::ptrdiff_t ret;
372372
ret = recv(buffer, 1);
373373

374374
// We read one byte, as needed, all good.
@@ -434,7 +434,7 @@ namespace ix
434434
}
435435

436436
size_t size = std::min(readBuffer.size(), length - bytesRead);
437-
ssize_t ret = recv((char*) &readBuffer[0], size);
437+
std::ptrdiff_t ret = recv((char*) &readBuffer[0], size);
438438

439439
if (ret > 0)
440440
{

ixwebsocket/IXSocket.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,13 @@
77
#pragma once
88

99
#include <atomic>
10+
#include <cstddef>
1011
#include <cstdint>
1112
#include <functional>
1213
#include <memory>
1314
#include <mutex>
1415
#include <string>
1516

16-
#ifdef __APPLE__
17-
#include <sys/types.h>
18-
#endif
19-
20-
#ifdef _WIN32
21-
#include <basetsd.h>
22-
#ifdef _MSC_VER
23-
typedef SSIZE_T ssize_t;
24-
#endif
25-
#endif
26-
2717
#include "IXCancellationRequest.h"
2818
#include "IXNetSystem.h"
2919
#include "IXProgressCallback.h"
@@ -65,9 +55,9 @@ namespace ix
6555
const CancellationRequest& isCancellationRequested);
6656
virtual void close();
6757

68-
virtual ssize_t send(char* buffer, size_t length);
69-
ssize_t send(const std::string& buffer);
70-
virtual ssize_t recv(void* buffer, size_t length);
58+
virtual std::ptrdiff_t send(char* buffer, size_t length);
59+
std::ptrdiff_t send(const std::string& buffer);
60+
virtual std::ptrdiff_t recv(void* buffer, size_t length);
7161

7262
// Blocking and cancellable versions, working with socket that can be set
7363
// to non blocking mode. Used during HTTP upgrade.

ixwebsocket/IXSocketAppleSSL.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace ix
7676

7777
size_t requested_sz = *len;
7878

79-
ssize_t status = read(fd, data, requested_sz);
79+
std::ptrdiff_t status = read(fd, data, requested_sz);
8080

8181
if (status > 0)
8282
{
@@ -123,7 +123,7 @@ namespace ix
123123
assert(len != nullptr);
124124

125125
size_t to_write_sz = *len;
126-
ssize_t status = write(fd, data, to_write_sz);
126+
std::ptrdiff_t status = write(fd, data, to_write_sz);
127127

128128
if (status > 0)
129129
{
@@ -251,7 +251,7 @@ namespace ix
251251
Socket::close();
252252
}
253253

254-
ssize_t SocketAppleSSL::send(char* buf, size_t nbyte)
254+
std::ptrdiff_t SocketAppleSSL::send(char* buf, size_t nbyte)
255255
{
256256
OSStatus status = errSSLWouldBlock;
257257
while (status == errSSLWouldBlock)
@@ -260,7 +260,7 @@ namespace ix
260260
std::lock_guard<std::mutex> lock(_mutex);
261261
status = SSLWrite(_sslContext, buf, nbyte, &processed);
262262

263-
if (processed > 0) return (ssize_t) processed;
263+
if (processed > 0) return (std::ptrdiff_t) processed;
264264

265265
// The connection was reset, inform the caller that this
266266
// Socket should close
@@ -281,7 +281,7 @@ namespace ix
281281
}
282282

283283
// No wait support
284-
ssize_t SocketAppleSSL::recv(void* buf, size_t nbyte)
284+
std::ptrdiff_t SocketAppleSSL::recv(void* buf, size_t nbyte)
285285
{
286286
OSStatus status = errSSLWouldBlock;
287287
while (status == errSSLWouldBlock)
@@ -290,7 +290,7 @@ namespace ix
290290
std::lock_guard<std::mutex> lock(_mutex);
291291
status = SSLRead(_sslContext, buf, nbyte, &processed);
292292

293-
if (processed > 0) return (ssize_t) processed;
293+
if (processed > 0) return (std::ptrdiff_t) processed;
294294

295295
// The connection was reset, inform the caller that this
296296
// Socket should close

ixwebsocket/IXSocketAppleSSL.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ namespace ix
3030
const CancellationRequest& isCancellationRequested) final;
3131
virtual void close() final;
3232

33-
virtual ssize_t send(char* buffer, size_t length) final;
34-
virtual ssize_t recv(void* buffer, size_t length) final;
33+
virtual std::ptrdiff_t send(char* buffer, size_t length) final;
34+
virtual std::ptrdiff_t recv(void* buffer, size_t length) final;
3535

3636
private:
3737
static std::string getSSLErrorDescription(OSStatus status);

ixwebsocket/IXSocketMbedTLS.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,11 @@ namespace ix
335335
Socket::close();
336336
}
337337

338-
ssize_t SocketMbedTLS::send(char* buf, size_t nbyte)
338+
std::ptrdiff_t SocketMbedTLS::send(char* buf, size_t nbyte)
339339
{
340340
std::lock_guard<std::mutex> lock(_mutex);
341341

342-
ssize_t res = mbedtls_ssl_write(&_ssl, (unsigned char*) buf, nbyte);
342+
std::ptrdiff_t res = mbedtls_ssl_write(&_ssl, (unsigned char*) buf, nbyte);
343343

344344
if (res > 0)
345345
{
@@ -356,13 +356,13 @@ namespace ix
356356
}
357357
}
358358

359-
ssize_t SocketMbedTLS::recv(void* buf, size_t nbyte)
359+
std::ptrdiff_t SocketMbedTLS::recv(void* buf, size_t nbyte)
360360
{
361361
while (true)
362362
{
363363
std::lock_guard<std::mutex> lock(_mutex);
364364

365-
ssize_t res = mbedtls_ssl_read(&_ssl, (unsigned char*) buf, (int) nbyte);
365+
std::ptrdiff_t res = mbedtls_ssl_read(&_ssl, (unsigned char*) buf, (int) nbyte);
366366

367367
if (res > 0)
368368
{

ixwebsocket/IXSocketMbedTLS.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ namespace ix
3838
const CancellationRequest& isCancellationRequested) final;
3939
virtual void close() final;
4040

41-
virtual ssize_t send(char* buffer, size_t length) final;
42-
virtual ssize_t recv(void* buffer, size_t length) final;
41+
virtual std::ptrdiff_t send(char* buffer, size_t length) final;
42+
virtual std::ptrdiff_t recv(void* buffer, size_t length) final;
4343

4444
private:
4545
mbedtls_ssl_context _ssl;

ixwebsocket/IXSocketOpenSSL.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ namespace ix
818818
Socket::close();
819819
}
820820

821-
ssize_t SocketOpenSSL::send(char* buf, size_t nbyte)
821+
std::ptrdiff_t SocketOpenSSL::send(char* buf, size_t nbyte)
822822
{
823823
std::lock_guard<std::mutex> lock(_mutex);
824824

@@ -828,7 +828,7 @@ namespace ix
828828
}
829829

830830
ERR_clear_error();
831-
ssize_t write_result = SSL_write(_ssl_connection, buf, (int) nbyte);
831+
std::ptrdiff_t write_result = SSL_write(_ssl_connection, buf, (int) nbyte);
832832
int reason = SSL_get_error(_ssl_connection, (int) write_result);
833833

834834
if (reason == SSL_ERROR_NONE)
@@ -846,7 +846,7 @@ namespace ix
846846
}
847847
}
848848

849-
ssize_t SocketOpenSSL::recv(void* buf, size_t nbyte)
849+
std::ptrdiff_t SocketOpenSSL::recv(void* buf, size_t nbyte)
850850
{
851851
while (true)
852852
{
@@ -858,7 +858,7 @@ namespace ix
858858
}
859859

860860
ERR_clear_error();
861-
ssize_t read_result = SSL_read(_ssl_connection, buf, (int) nbyte);
861+
std::ptrdiff_t read_result = SSL_read(_ssl_connection, buf, (int) nbyte);
862862

863863
if (read_result > 0)
864864
{

ixwebsocket/IXSocketOpenSSL.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ namespace ix
3333
const CancellationRequest& isCancellationRequested) final;
3434
virtual void close() final;
3535

36-
virtual ssize_t send(char* buffer, size_t length) final;
37-
virtual ssize_t recv(void* buffer, size_t length) final;
36+
virtual std::ptrdiff_t send(char* buffer, size_t length) final;
37+
virtual std::ptrdiff_t recv(void* buffer, size_t length) final;
3838

3939
private:
4040
void openSSLInitialize();

0 commit comments

Comments
 (0)