1
0
Fork 0

Initialize missed fields in TCPClientSocket

Also, do some code and style cleanup in c-tors.
This commit is contained in:
Patryk Obara 2020-02-18 20:12:45 +01:00 committed by Patryk Obara
parent 534a603cb8
commit 651aeb906c
2 changed files with 47 additions and 49 deletions

View file

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

View file

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