From 7df5a30f98b6af8241813fa997d07756d13890ef Mon Sep 17 00:00:00 2001 From: krcroft Date: Wed, 19 Feb 2020 07:45:38 -0800 Subject: [PATCH] Simplify by using the _TCPsocketX struct as-is Unlike an array of 1-byte chars which only requires 1-byte alignment (because the array's largest element is only one byte), when we fool the compiler into allocating a _TCPsocketX as an arra of chars, we similarly tell the compiler we only need 1-byte alignment. But this requirement changes when we cast the 1-byte array back to the _TCPsocketX and try to operate on members having sizes larger than 1-byte (such as int32's); we now require 4-byte alignment (after allocation). --- src/hardware/serialport/misc_util.cpp | 37 +++++++++------------------ src/hardware/serialport/misc_util.h | 12 ++++++++- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/hardware/serialport/misc_util.cpp b/src/hardware/serialport/misc_util.cpp index c6b14c0e..9e4ef497 100644 --- a/src/hardware/serialport/misc_util.cpp +++ b/src/hardware/serialport/misc_util.cpp @@ -21,17 +21,6 @@ #if C_MODEM #include "misc_util.h" - -struct _TCPsocketX { - int ready; -#ifdef NATIVESOCKETS - SOCKET channel; -#endif - IPaddress remoteAddress; - IPaddress localAddress; - int sflag; -}; - Bit32u Netwrapper_GetCapabilities() { Bit32u retval=0; @@ -42,8 +31,7 @@ Bit32u Netwrapper_GetCapabilities() #ifdef NATIVESOCKETS TCPClientSocket::TCPClientSocket(int platformsocket) { - nativetcpstruct = new Bit8u[sizeof(struct _TCPsocketX)]; - + nativetcpstruct = new _TCPsocketX; mysock = (TCPsocket)nativetcpstruct; if (!SDLNetInited) { if (SDLNet_Init() == -1) { @@ -53,20 +41,18 @@ TCPClientSocket::TCPClientSocket(int platformsocket) SDLNetInited = true; } // fill the SDL socket manually - ((struct _TCPsocketX*)nativetcpstruct)->ready=0; - ((struct _TCPsocketX*)nativetcpstruct)->sflag=0; - ((struct _TCPsocketX*)nativetcpstruct)->channel=(SOCKET) platformsocket; + nativetcpstruct->ready = 0; + nativetcpstruct->sflag = 0; + nativetcpstruct->channel = (SOCKET) platformsocket; sockaddr_in sa; socklen_t sz; sz=sizeof(sa); if(getpeername(platformsocket, (sockaddr *)(&sa), &sz)==0) { - ((struct _TCPsocketX*)nativetcpstruct)-> - remoteAddress.host=/*ntohl(*/sa.sin_addr.s_addr;//); - ((struct _TCPsocketX*)nativetcpstruct)-> - remoteAddress.port=/*ntohs(*/sa.sin_port;//); + nativetcpstruct->remoteAddress.host = /*ntohl(*/sa.sin_addr.s_addr;//); + nativetcpstruct->remoteAddress.port = /*ntohs(*/sa.sin_port;//); } else { - mysock=0; + mysock = nullptr; return; } sz=sizeof(sa); @@ -77,10 +63,10 @@ TCPClientSocket::TCPClientSocket(int platformsocket) localAddress.port=/*ntohs(*/sa.sin_port;//); } else { - mysock=0; + mysock = nullptr; return; } - if(mysock!=0) { + if(mysock) { listensocketset = SDLNet_AllocSocketSet(1); if(!listensocketset) return; SDLNet_TCP_AddSocket(listensocketset, mysock); @@ -127,7 +113,8 @@ TCPClientSocket::TCPClientSocket(const char* destination, Bit16u port) listensocketset = SDLNet_AllocSocketSet(1); if(!listensocketset) return; mysock = SDLNet_TCP_Open(&openip); - if(!mysock) return; + if (!mysock) + return; SDLNet_TCP_AddSocket(listensocketset, mysock); isopen=true; } @@ -247,7 +234,7 @@ void TCPClientSocket::SetSendBufferSize(Bitu bufsize) TCPServerSocket::TCPServerSocket(Bit16u port) { isopen = false; - mysock = 0; + mysock = nullptr; if(!SDLNetInited) { if(SDLNet_Init()==-1) { LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError()); diff --git a/src/hardware/serialport/misc_util.h b/src/hardware/serialport/misc_util.h index ce80db18..5f5a8a51 100644 --- a/src/hardware/serialport/misc_util.h +++ b/src/hardware/serialport/misc_util.h @@ -57,6 +57,16 @@ Bit32u Netwrapper_GetCapabilities(); +struct _TCPsocketX { + int ready; +#ifdef NATIVESOCKETS + SOCKET channel; +#endif + IPaddress remoteAddress; + IPaddress localAddress; + int sflag; +}; + class TCPClientSocket { public: TCPClientSocket(TCPsocket source); @@ -92,7 +102,7 @@ public: private: #ifdef NATIVESOCKETS - Bit8u *nativetcpstruct = nullptr; + _TCPsocketX *nativetcpstruct = nullptr; #endif TCPsocket mysock = nullptr;