From 651aeb906c647c4075b8a74c237fbfea4f0f6fe2 Mon Sep 17 00:00:00 2001 From: Patryk Obara Date: Tue, 18 Feb 2020 20:12:45 +0100 Subject: [PATCH] Initialize missed fields in TCPClientSocket Also, do some code and style cleanup in c-tors. --- src/hardware/serialport/misc_util.cpp | 62 +++++++++++++-------------- src/hardware/serialport/misc_util.h | 34 +++++++-------- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/hardware/serialport/misc_util.cpp b/src/hardware/serialport/misc_util.cpp index 8fcb70c4..ce1caecd 100644 --- a/src/hardware/serialport/misc_util.cpp +++ b/src/hardware/serialport/misc_util.cpp @@ -40,14 +40,13 @@ Bit32u Netwrapper_GetCapabilities() } #ifdef NATIVESOCKETS -TCPClientSocket::TCPClientSocket(int platformsocket) { - sendbuffer=0; +TCPClientSocket::TCPClientSocket(int platformsocket) +{ nativetcpstruct = new Bit8u[sizeof(struct _TCPsocketX)]; mysock = (TCPsocket)nativetcpstruct; - isopen = false; - if(!SDLNetInited) { - if(SDLNet_Init()==-1) { + if (!SDLNetInited) { + if (SDLNet_Init() == -1) { LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError()); return; } @@ -88,27 +87,23 @@ TCPClientSocket::TCPClientSocket(int platformsocket) { isopen=true; return; } - mysock=0; return; } #endif // NATIVESOCKETS -TCPClientSocket::TCPClientSocket(TCPsocket source) { +TCPClientSocket::TCPClientSocket(TCPsocket source) +{ #ifdef NATIVESOCKETS nativetcpstruct=0; #endif - sendbuffer=0; - isopen = false; - if(!SDLNetInited) { - if(SDLNet_Init()==-1) { + if (!SDLNetInited) { + if (SDLNet_Init() == -1) { LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError()); return; } SDLNetInited = true; - } + } - mysock=0; - listensocketset=0; if(source!=0) { mysock = source; listensocketset = SDLNet_AllocSocketSet(1); @@ -118,21 +113,19 @@ TCPClientSocket::TCPClientSocket(TCPsocket source) { isopen=true; } } -TCPClientSocket::TCPClientSocket(const char* destination, Bit16u port) { + +TCPClientSocket::TCPClientSocket(const char* destination, Bit16u port) +{ #ifdef NATIVESOCKETS nativetcpstruct=0; #endif - sendbuffer=0; - isopen = false; - if(!SDLNetInited) { - if(SDLNet_Init()==-1) { + if (!SDLNetInited) { + if (SDLNet_Init() == -1) { LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError()); return; } SDLNetInited = true; - } - mysock=0; - listensocketset=0; + } IPaddress openip; //Ancient versions of SDL_net had this as char*. People still appear to be using this one. @@ -146,8 +139,8 @@ TCPClientSocket::TCPClientSocket(const char* destination, Bit16u port) { } } -TCPClientSocket::~TCPClientSocket() { - +TCPClientSocket::~TCPClientSocket() +{ if(sendbuffer) delete [] sendbuffer; #ifdef NATIVESOCKETS if(nativetcpstruct) delete [] nativetcpstruct; @@ -160,6 +153,7 @@ TCPClientSocket::~TCPClientSocket() { if(listensocketset) SDLNet_FreeSocketSet(listensocketset); } + bool TCPClientSocket::GetRemoteAddressString(Bit8u* buffer) { IPaddress* remote_ip; Bit8u b1, b2, b3, b4; @@ -224,6 +218,9 @@ bool TCPClientSocket::SendArray(Bit8u* data, Bitu bufsize) bool TCPClientSocket::SendByteBuffered(Bit8u data) { + if (sendbuffersize == 0) + return false; + if (sendbufferindex < (sendbuffersize - 1)) { sendbuffer[sendbufferindex] = data; sendbufferindex++; @@ -244,14 +241,15 @@ void TCPClientSocket::FlushBuffer() } } -void TCPClientSocket::SetSendBufferSize(Bitu bufsize) { - if(sendbuffer) delete [] sendbuffer; +void TCPClientSocket::SetSendBufferSize(Bitu bufsize) +{ + if (sendbuffer) + delete [] sendbuffer; sendbuffer = new Bit8u[bufsize]; - sendbuffersize=bufsize; - sendbufferindex=0; + sendbuffersize = bufsize; + sendbufferindex = 0; } - TCPServerSocket::TCPServerSocket(Bit16u port) { isopen = false; @@ -273,8 +271,10 @@ TCPServerSocket::TCPServerSocket(Bit16u port) isopen = true; } -TCPServerSocket::~TCPServerSocket() { - if(mysock) SDLNet_TCP_Close(mysock); +TCPServerSocket::~TCPServerSocket() +{ + if (mysock) + SDLNet_TCP_Close(mysock); } TCPClientSocket* TCPServerSocket::Accept() { diff --git a/src/hardware/serialport/misc_util.h b/src/hardware/serialport/misc_util.h index f8113db6..6a433065 100644 --- a/src/hardware/serialport/misc_util.h +++ b/src/hardware/serialport/misc_util.h @@ -62,7 +62,7 @@ Bit32u Netwrapper_GetCapabilities(); class TCPClientSocket { - public: +public: TCPClientSocket(TCPsocket source); TCPClientSocket(const char* destination, Bit16u port); #ifdef NATIVESOCKETS @@ -70,48 +70,46 @@ class TCPClientSocket { TCPClientSocket(int platformsocket); #endif ~TCPClientSocket(); - + // return: // -1: no data // -2: socket closed // >0: data char Bits GetcharNonBlock(); - - + bool Putchar(Bit8u data); bool SendArray(Bit8u* data, Bitu bufsize); bool ReceiveArray(Bit8u* data, Bitu* size); - bool isopen; + + bool isopen = false; bool GetRemoteAddressString(Bit8u* buffer); void FlushBuffer(); void SetSendBufferSize(Bitu bufsize); - + // buffered send functions bool SendByteBuffered(Bit8u data); - private: - TCPsocket mysock; - SDLNet_SocketSet listensocketset; +private: + TCPsocket mysock = 0; + SDLNet_SocketSet listensocketset = nullptr; // Items for send buffering - Bitu sendbuffersize; - Bitu sendbufferindex; - - Bit8u* sendbuffer; + Bitu sendbuffersize = 0; + Bitu sendbufferindex = 0; + Bit8u *sendbuffer = nullptr; }; -class TCPServerSocket { - public: - bool isopen; - TCPsocket mysock; +struct TCPServerSocket { + bool isopen = false; + TCPsocket mysock = 0; + TCPServerSocket(Bit16u port); ~TCPServerSocket(); TCPClientSocket* Accept(); }; - #endif //C_MODEM #endif //# SDLNETWRAPPER_H