1
0
Fork 0

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).
This commit is contained in:
krcroft 2020-02-19 07:45:38 -08:00 committed by Patryk Obara
parent 6468e3c149
commit 7df5a30f98
2 changed files with 23 additions and 26 deletions

View file

@ -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());

View file

@ -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;