2
3
mirror of https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git synced 2025-07-14 02:27:40 -04:00

CWE Directory Reorganization

Rearrange directory structure of CWE to be loosely equivalent to
the H'uru Plasma repository.

Part 1: Movement of directories and files.
This commit is contained in:
rarified
2021-05-15 12:49:46 -06:00
parent c3f4a640a3
commit 96903e8dca
4002 changed files with 159 additions and 644 deletions

View File

@ -0,0 +1,212 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "plBufferedSocketReader.h"
#include "plTcpSocket.h"
#include <string.h>
plBufferedSocketReader::plBufferedSocketReader(int size)
: plRingBuffer(size)
{
}
int plBufferedSocketReader::ReadBlock(char * buf, int buflen, plTcpSocket & sck)
{
if(GetBlock(buf, buflen))
return kSuccessWithData;
int ans = ReadFrom(sck);
if(ans<=0)
return ans;
if(GetBlock(buf, buflen))
return kSuccessWithData;
return kSuccessNoData;
}
int plBufferedSocketReader::ReadString(char * buf, int buflen, char * termChars, plTcpSocket & sck)
{
if(GetString(buf, buflen, termChars))
return kSuccessWithData;
int ans = kSuccessNoData;
while ( ans>=0 )
{
ans = ReadFrom(sck);
if(ans>0)
{
if ( GetString(buf, buflen, termChars) )
return kSuccessWithData;
}
}
return ans;
}
int plBufferedSocketReader::ReadStringInPlace(char ** buf, char * termChars, plTcpSocket & sck)
{
if(GetStringInPlace(buf, termChars))
return kSuccessWithData;
int ans = kSuccessNoData;
while ( ans>=0 )
{
ans = ReadFrom(sck);
if(ans>0)
{
if ( GetStringInPlace(buf, termChars) )
return kSuccessWithData;
}
}
return ans;
}
void plBufferedSocketReader::Reset()
{
plRingBuffer::Reset();
}
int plBufferedSocketReader::ReadFrom(plTcpSocket & sck) // this is where things get ugly.
{
int ans = kSuccessNoData;
int readSize = BufferAvailable();
if(readSize < 1)
{
Compress();
readSize = BufferAvailable();
}
if(readSize > 0)
{
char * dst = GetBufferOpen();
int nBytesRead = sck.RecvData(dst, readSize);
if(nBytesRead < 0)
{
int err = plNet::GetError();
if(err != kBlockingError)
{
ans = kFailedReadError;
}
}
else if(nBytesRead > 0)
{
fEndPos += nBytesRead;
ans = kSuccessWithData;
}
else
{
ans = kFailedSocketClosed;
}
}
else
{
ans = kFailedNoBufferSpace;
}
return ans;
}
bool plBufferedSocketReader::GetBlock(char * buf, int buflen)
{
int dataAvailable = FastAmountBuffered();
int maxRead = buflen;
if(maxRead > dataAvailable)
maxRead = dataAvailable;
if (maxRead==0)
return false;
char * wrk = FastGetBufferStart();
memcpy(buf,FastGetBufferStart(),maxRead);
return true;
}
bool plBufferedSocketReader::GetString(char * buf, int buflen, char * termChars)
{
bool ans = false;
int dataAvailable = FastAmountBuffered();
int maxRead = buflen;
if(maxRead > dataAvailable)
maxRead = dataAvailable;
char * wrk = FastGetBufferStart();
for(int i=0; i<maxRead; i++)
{
if(strchr(termChars,wrk[i])!=0)
{
memcpy(buf,wrk,i);
buf[i] = '\0';
fStartPos += i+1;
Compress();
ans = true;
break;
}
}
return ans;
}
bool plBufferedSocketReader::GetStringInPlace(char ** buf, char * termChars)
{
bool ans = false;
int dataAvailable = FastAmountBuffered();
*buf = FastGetBufferStart();
for(int i=0; i<dataAvailable; i++)
{
if(strchr(termChars,(*buf)[i])!=0)
{
(*buf)[i] = '\0';
fStartPos += i+1;
Compress();
ans = true;
break;
}
}
return ans;
}

View File

@ -0,0 +1,77 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plBufferedSocketReader_h_inc
#define plBufferedSocketReader_h_inc
#include "plRingBuffer.h"
class plTcpSocket;
class plBufferedSocketReader : protected plRingBuffer
{
public:
enum ReadResult
{
kSuccessNoData = 0,
kSuccessWithData = 1,
kFailedReadError =-1,
kFailedSocketClosed =-2,
kFailedNoBufferSpace =-3,
};
public:
plBufferedSocketReader(int size=8192);
int ReadBlock(char * buf, int buflen, plTcpSocket & sck);
int ReadString(char * buf, int buflen, char * termChars, plTcpSocket & sck);
int ReadStringInPlace(char ** buf, char * termChars, plTcpSocket & sck);
void Reset();
protected:
int ReadFrom(plTcpSocket & sck);
bool GetBlock(char * buf, int buflen);
bool GetString(char * buf, int buflen, char * termChars);
bool GetStringInPlace(char ** buf, char * termChars);
};
#endif // plBufferedSocketReader_h_inc

View File

@ -0,0 +1,125 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "plBufferedSocketWriter.h"
#include "plTcpSocket.h"
plBufferedSocketWriter::plBufferedSocketWriter(int size, int bytesPerFlush, bool blockOnSend, int flushPoint)
: plRingBuffer(size)
, fFlushPoint(flushPoint)
, fBlockOnSend(blockOnSend)
, fBytesPerFlush(bytesPerFlush)
{}
int plBufferedSocketWriter::WriteBlock(const char * data, int len, plTcpSocket & sck)
{
int ans = kSuccessNoDataSent;
if(len > BufferAvailable())
ans = Flush(sck);
if(ans >= 0)
ans = WriteBlock(data,len);
if(ans >= 0 && fFlushPoint >= 0 && fFlushPoint < AmountBuffered())
ans = Flush(sck);
return ans;
}
int plBufferedSocketWriter::WriteBlock(const char * data, int len)
{
int ans = kSuccessNoDataSent;
if(!Put(data,len))
ans = kFailedNoBufferSpace;
return ans;
}
int plBufferedSocketWriter::Flush(plTcpSocket & sck) // this is where things get ugly.
{
int ans = kSuccessNoDataSent;
int writeSize = MIN(FastAmountBuffered(),fBytesPerFlush);
if(writeSize > 0)
{
int nBytesWritten = 0;
// int nBytesWritten = sck.SendData(FastGetBufferStart(), writeSize);
// if (nBytesWritten<0 && fBlockOnSend
// && plNet::GetError()==kBlockingError)
{
bool wasBlockingOrNot = sck.IsBlocking();
sck.SetBlocking(fBlockOnSend);
nBytesWritten = sck.SendData(FastGetBufferStart(), writeSize);
sck.SetBlocking(wasBlockingOrNot);
}
if (nBytesWritten > 0)
{
fStartPos += nBytesWritten;
FullCompress();
ans = kSuccessDataSent;
}
else if (nBytesWritten < 0)
{
if (plNet::GetError()!=kBlockingError)
ans = kFailedWriteError;
}
else
{
ans = kFailedSocketClosed;
}
}
return ans;
}
bool plBufferedSocketWriter::IsEmpty()
{
return !(FastAmountBuffered() > 0);
}
void plBufferedSocketWriter::Reset()
{
plRingBuffer::Reset();
}

View File

@ -0,0 +1,78 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plBufferedSocketWriter_h_inc
#define plBufferedSocketWriter_h_inc
#include "plRingBuffer.h"
class plTcpSocket;
class plBufferedSocketWriter : protected plRingBuffer
{
protected:
bool fBlockOnSend;
int fFlushPoint;
int fBytesPerFlush;
public:
enum WriteResult
{
kSuccessNoDataSent = 0,
kSuccessDataSent = 1,
kFailedWriteError =-1,
kFailedSocketClosed =-2,
kFailedNoBufferSpace =-3,
};
public:
plBufferedSocketWriter(int size=8192, int bytesPerFlush=4096, bool blockOnSend=true, int flushPoint=-1);
int WriteBlock(const char * data, int len, plTcpSocket & sck);
int WriteBlock(const char * data, int len);
int Flush(plTcpSocket & sck);
bool IsEmpty();
void Reset();
};
#endif // plBufferedSocketWriter_h_inc

View File

@ -0,0 +1,173 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "plFdSet.h"
#include "plSocket.h"
#if HS_BUILD_FOR_UNIX
#include <sys/time.h>
#endif
plFdSet::plFdSet(void)
{
ZeroFds();
}
void plFdSet::SetForSocket(plSocket & in)
{
SOCKET sck = in.GetSocket();
hsAssert(sck>=0, "plFdSet::SetForSocket: socket<0");
if ( sck<0 )
return;
FD_SET(sck, &fFds);
FD_SET(sck, &fErrFds);
if(fMaxFd < sck)
fMaxFd = sck;
}
void plFdSet::ClearForSocket(plSocket & in)
{
SOCKET sck = in.GetSocket();
hsAssert(sck>=0, "plFdSet::ClearForSocket: socket<0");
if ( sck<0 )
return;
FD_CLR(sck, &fFds);
FD_CLR(sck, &fErrFds);
}
bool plFdSet::IsSetFor(plSocket & in)
{
SOCKET sck = in.GetSocket();
hsAssert(sck>=0, "plFdSet::IsSetFor: socket<0");
if ( sck<0 )
return false;
return (FD_ISSET(sck, &fFds) != 0);
}
bool plFdSet::IsErrFor(plSocket & in)
{
SOCKET sck = in.GetSocket();
hsAssert(sck>=0, "plFdSet::IsErrFor: socket<0");
if ( sck<0 )
return false;
return (FD_ISSET(sck, &fErrFds) != 0);
}
int plFdSet::WaitForRead(bool shouldZeroFds, unsigned long timeoutMillis)
{
int ret_val = 0;
if(timeoutMillis == kInfinite)
{
ret_val = select(fMaxFd+1,&fFds,NULL,&fErrFds,NULL);
}
else
{
struct timeval tv;
tv.tv_sec = timeoutMillis / 1000;
tv.tv_usec = (timeoutMillis % 1000) * 1000;
ret_val = select(fMaxFd+1,&fFds,NULL,&fErrFds,&tv);
}
if (shouldZeroFds)
ZeroFds();
return ret_val;
}
int plFdSet::WaitForWrite(bool shouldZeroFds, unsigned long timeoutMillis)
{
int ret_val = 0;
if(timeoutMillis == kInfinite)
{
ret_val = select(fMaxFd+1,NULL,&fFds,&fErrFds,NULL);
}
else
{
timeval tv;
tv.tv_sec = timeoutMillis / 1000;
tv.tv_usec = (timeoutMillis % 1000) * 1000;
ret_val = select(fMaxFd+1,NULL,&fFds,&fErrFds,&tv);
}
if (shouldZeroFds)
ZeroFds();
return ret_val;
}
int plFdSet::WaitForError(bool shouldZeroFds, unsigned long timeoutMillis)
{
int ret_val = 0;
if(timeoutMillis == kInfinite)
{
ret_val = select(fMaxFd+1,NULL,NULL,&fErrFds,NULL);
}
else
{
timeval tv;
tv.tv_sec = timeoutMillis / 1000;
tv.tv_usec = (timeoutMillis % 1000) * 1000;
ret_val = select(fMaxFd+1,NULL,NULL,&fErrFds,&tv);
}
if (shouldZeroFds)
ZeroFds();
return ret_val;
}
void plFdSet::ZeroFds(void)
{
fMaxFd = 0;
FD_ZERO(&fFds);
FD_ZERO(&fErrFds);
}

View File

@ -0,0 +1,78 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plFdSet_h_inc
#define plFdSet_h_inc
#include "plNet.h"
////////////////////////////////////////////////////
class plSocket;
////////////////////////////////////////////////////
class plFdSet
{
public:
enum { kInfinite = 0xffffffff };
public:
plFdSet();
void SetForSocket(plSocket & in);
void ClearForSocket(plSocket & in);
bool IsSetFor(plSocket & in);
bool IsErrFor(plSocket & in);
int WaitForRead(bool shouldZeroFds, unsigned long timeoutMillis=kInfinite);
int WaitForWrite(bool shouldZeroFds, unsigned long timeoutMillis=kInfinite);
int WaitForError(bool shouldZeroFds, unsigned long timeoutMillis=0);
void ZeroFds();
private:
unsigned fMaxFd;
fd_set fFds;
fd_set fErrFds;
};
#endif //plFdSet_h_inc

View File

@ -0,0 +1,151 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "hsUtils.h"
#include "plMemBuffer.h"
/////////////////////////////////////////////////
plMemBuffer::plMemBuffer()
{
fBuffer = nil;
fBufferLocal = false;
fBufferLen = 0;
}
plMemBuffer::plMemBuffer(int len)
{
AllocBuffer(len);
}
plMemBuffer::plMemBuffer(char * data, int len)
{
fBufferLocal = false;
fBufferLen = len;
fBuffer = data;
}
plMemBuffer::~plMemBuffer()
{
ClearBuffer();
}
void plMemBuffer::SetBuffer(char * data, int len)
{
ClearBuffer();
fBufferLocal = false;
fBufferLen = len;
fBuffer = data;
}
void plMemBuffer::CopyBuffer(char * data, int len)
{
char * tmp = TRACKED_NEW char[len];
memcpy(tmp,data,len);
ClearBuffer();
AllocBuffer(len);
memcpy(fBuffer,tmp,len);
delete [] tmp;
}
void plMemBuffer::GrowBuffer(int newLen)
{
const int kThrashSize = 25;
if(newLen >= fBufferLen)
{
int len = newLen + kThrashSize;
len = len+len;
char * tmp = TRACKED_NEW char[len];
if(fBuffer != nil)
memcpy(tmp,fBuffer,fBufferLen);
ClearBuffer();
fBuffer = tmp;
fBufferLocal = true;
fBufferLen = len;
}
}
int plMemBuffer::GetBufferSize()
{
return fBufferLen;
};
char * plMemBuffer::GetBuffer()
{
return fBuffer;
};
bool plMemBuffer::InBufferRange(char * inpos)
{
return (inpos >= fBuffer && inpos <= (fBuffer + fBufferLen));
}
void plMemBuffer::ClearBuffer()
{
if(fBufferLocal == true)
{
if(fBuffer != nil)
delete [] fBuffer;
fBuffer = nil;
}
}
void plMemBuffer::AllocBuffer(int len)
{
fBuffer = TRACKED_NEW char[len];
fBufferLocal = true;
fBufferLen = len;
}

View File

@ -0,0 +1,71 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plMemBuffer_h_inc
#define plMemBuffer_h_inc
class plMemBuffer
{
public:
plMemBuffer();
plMemBuffer(int len);
plMemBuffer(char * data, int len);
virtual ~plMemBuffer();
void SetBuffer(char * data, int len);
void CopyBuffer(char * data, int len);
void GrowBuffer(int len);
int GetBufferSize();
char * GetBuffer();
bool InBufferRange(char *);
protected:
bool fBufferLocal;
int fBufferLen;
char * fBuffer;
void ClearBuffer();
void AllocBuffer(int len);
};
#endif // plMemBuffer_h_inc

View File

@ -0,0 +1,294 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "plNet.h"
////////////////////////////////////////////////////
plNet plNet::_;
////////////////////////////////////////////////////
// Windows socket interface
#if HS_BUILD_FOR_WIN32
plNet::plNet()
{
static struct WSAData wsa;
WSAStartup(0x0101, &wsa);
}
plNet::~plNet()
{
WSACleanup();
}
SOCKET plNet::NewUDP()
{
return ::socket(AF_INET, SOCK_DGRAM, 0);
}
SOCKET plNet::NewTCP()
{
SOCKET x = ::socket(AF_INET, SOCK_STREAM, 0);
unsigned int timeoutval;
timeoutval = kDefaultSocketTimeout;
setsockopt(x, SOL_SOCKET, (int)SO_RCVTIMEO,(const char*)&timeoutval,sizeof(timeoutval));
timeoutval = kDefaultSocketTimeout;
setsockopt(x, SOL_SOCKET, (int)SO_SNDTIMEO,(const char*)&timeoutval,sizeof(timeoutval));
return x;
}
int plNet::GetError()
{
return WSAGetLastError();
}
int plNet::Read(const SOCKET sck, char * buf, const int size)
{
return ::recv(sck,buf,size,0);
}
int plNet::Write(const SOCKET sck, const char * buf, const int len)
{
return ::send(sck,buf,len,0);
}
int plNet::ReadFrom(const SOCKET sck, char * buf, int len, sockaddr_in * addr)
{
int addrlen = sizeof(sockaddr);
return ::recvfrom(sck,buf,len,0,reinterpret_cast<sockaddr*>(addr),&addrlen);
}
int plNet::WriteTo(const SOCKET sck, const char * buf, const int len, sockaddr_in * addr)
{
return ::sendto(sck,buf,len,0,reinterpret_cast<const sockaddr*>(addr),sizeof(sockaddr));
}
int plNet::Connect(const SOCKET sck, const sockaddr_in * addr)
{
return ::connect(sck, reinterpret_cast<const sockaddr*>(addr), sizeof(sockaddr));
}
int plNet::Close(const SOCKET sck)
{
return ::closesocket(sck);
}
int plNet::Bind(const SOCKET sck, const sockaddr_in * addr)
{
return ::bind(sck,reinterpret_cast<const sockaddr*>(addr),sizeof(sockaddr));
}
int plNet::Listen(const SOCKET sck, const int qsize)
{
return ::listen(sck,qsize);
}
int plNet::Accept(const SOCKET sck, sockaddr_in * addr)
{
int addrlen = sizeof(sockaddr);
return ::accept(sck,reinterpret_cast<sockaddr*>(addr),&addrlen);
}
int plNet::Ioctl(const SOCKET sck, const long flags, unsigned long * val)
{
return ::ioctlsocket(sck,flags,val);
}
// static
const char * plNet::GetErrorMsg(int error)
{
switch(error)
{
case WSAEINTR: return "Interrupted system call"; break;
case WSAEBADF: return "Bad file number"; break;
case WSAEACCES: return "Permission denied"; break;
case WSAEFAULT: return "Bad address"; break;
case WSAEINVAL: return "Invalid argument"; break;
case WSAEMFILE: return "Too many open sockets"; break;
case WSAEWOULDBLOCK: return "Operation would block"; break;
case WSAEINPROGRESS: return "Operation now in progress"; break;
case WSAEALREADY: return "Operation already in progress"; break;
case WSAENOTSOCK: return "Socket operation on non-socket"; break;
case WSAEDESTADDRREQ: return "Destination address required"; break;
case WSAEMSGSIZE: return "Message too long"; break;
case WSAEPROTOTYPE: return "Protocol wrong type for socket"; break;
case WSAENOPROTOOPT: return "Bad protocol option"; break;
case WSAEPROTONOSUPPORT: return "Protocol not supported"; break;
case WSAESOCKTNOSUPPORT: return "Socket type not supported"; break;
case WSAEOPNOTSUPP: return "Operation not supported on socket"; break;
case WSAEPFNOSUPPORT: return "Protocol family not supported"; break;
case WSAEAFNOSUPPORT: return "Address family not supported"; break;
case WSAEADDRINUSE: return "Address already in use"; break;
case WSAEADDRNOTAVAIL: return "Can't assign requested address"; break;
case WSAENETDOWN: return "Network is down"; break;
case WSAENETUNREACH: return "Network is unreachable"; break;
case WSAENETRESET: return "Net connection reset"; break;
case WSAECONNABORTED: return "Software caused connection abort"; break;
case WSAECONNRESET: return "Connection reset by peer"; break;
case WSAENOBUFS: return "No buffer space available"; break;
case WSAEISCONN: return "Socket is already connected"; break;
case WSAENOTCONN: return "Socket is not connected"; break;
case WSAESHUTDOWN: return "Can't send after socket shutdown"; break;
case WSAETOOMANYREFS: return "Too many references, can't splice"; break;
case WSAETIMEDOUT: return "Connection timed out"; break;
case WSAECONNREFUSED: return "Connection refused"; break;
case WSAELOOP: return "Too many levels of symbolic links"; break;
case WSAENAMETOOLONG: return "File name too long"; break;
case WSAEHOSTDOWN: return "Host is down"; break;
case WSAEHOSTUNREACH: return "No route to host"; break;
case WSAENOTEMPTY: return "Directory not empty"; break;
case WSAEPROCLIM: return "Too many processes"; break;
case WSAEUSERS: return "Too many users"; break;
case WSAEDQUOT: return "Disc quota exceeded"; break;
case WSAESTALE: return "Stale NFS file handle"; break;
case WSAEREMOTE: return "Too many levels of remote in path"; break;
case WSASYSNOTREADY: return "Network subsystem is unavailable"; break;
case WSAVERNOTSUPPORTED: return "Winsock version not supported"; break;
case WSANOTINITIALISED: return "Winsock not yet initialized"; break;
case WSAHOST_NOT_FOUND: return "Host not found"; break;
case WSATRY_AGAIN: return "Non-authoritative host not found"; break;
case WSANO_RECOVERY: return "Non-recoverable errors"; break;
case WSANO_DATA: return "Valid name, no data record of requested type"; break;
case WSAEDISCON: return "Graceful disconnect in progress"; break;
case WSASYSCALLFAILURE: return "System call failure"; break;
case WSA_NOT_ENOUGH_MEMORY: return "Insufficient memory available"; break;
case WSA_OPERATION_ABORTED: return "Overlapped operation aborted"; break;
case WSA_IO_INCOMPLETE: return "Overlapped I/O object not signalled"; break;
case WSA_IO_PENDING: return "Overlapped I/O will complete later"; break;
//case WSAINVALIDPROCTABLE: return "Invalid proc. table from service provider"; break;
//case WSAINVALIDPROVIDER: return "Invalid service provider version number"; break;
//case WSAPROVIDERFAILEDINIT, return "Unable to init service provider"; break;
case WSA_INVALID_PARAMETER: return "One or more parameters are invalid"; break;
case WSA_INVALID_HANDLE: return "Event object handle not valid"; break;
};
return "\0";
}
////////////////////////////////////////////////////
// UNIX socket interface
#elif HS_BUILD_FOR_UNIX
#include <unistd.h>
plNet::plNet()
{ }
plNet::~plNet()
{ }
SOCKET plNet::NewUDP()
{
return ::socket(AF_INET, SOCK_DGRAM, 0);
}
SOCKET plNet::NewTCP()
{
SOCKET x = ::socket(AF_INET, SOCK_STREAM, 0);
unsigned int timeoutval;
timeoutval = kDefaultSocketTimeout;
setsockopt(x, SOL_SOCKET, (int)SO_RCVTIMEO,(const char*)&timeoutval,sizeof(timeoutval));
timeoutval = kDefaultSocketTimeout;
setsockopt(x, SOL_SOCKET, (int)SO_SNDTIMEO,(const char*)&timeoutval,sizeof(timeoutval));
return x;
}
int plNet::GetError()
{
return errno;
}
int plNet::Read(const SOCKET sck, char * buf, const int size)
{
return ::recv(sck,buf,size,0);
}
int plNet::Write(const SOCKET sck, const char * buf, const int len)
{
return ::send(sck,buf,len,0);
}
int plNet::ReadFrom(const SOCKET sck, char * buf, int len, sockaddr_in * addr)
{
unsigned addrlen = sizeof(sockaddr);
return ::recvfrom(sck,buf,len,0,reinterpret_cast<sockaddr*>(addr),&addrlen);
}
int plNet::WriteTo(const SOCKET sck, const char * buf, const int len, sockaddr_in * addr)
{
return ::sendto(sck,buf,len,0,reinterpret_cast<const sockaddr*>(addr),sizeof(sockaddr));
}
int plNet::Connect(const SOCKET sck, const sockaddr_in * addr)
{
return ::connect(sck, reinterpret_cast<const sockaddr*>(addr), sizeof(sockaddr));
}
int plNet::Close(const SOCKET sck)
{
return ::close(sck);
}
int plNet::Bind(const SOCKET sck, const sockaddr_in * addr)
{
return ::bind(sck,reinterpret_cast<const sockaddr*>(addr),sizeof(sockaddr));
}
int plNet::Listen(const SOCKET sck, const int qsize)
{
return ::listen(sck,qsize);
}
int plNet::Accept(const SOCKET sck, sockaddr_in * addr)
{
unsigned addrlen = sizeof(sockaddr);
return ::accept(sck,reinterpret_cast<sockaddr*>(addr),&addrlen);
}
int plNet::Ioctl(const SOCKET sck, const long flags, unsigned long * val)
{
return ::ioctl(sck,flags,val);
}
#else
#error "plNet: Sockets not ported"
#endif

View File

@ -0,0 +1,117 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plNet_h_inc
#define plNet_h_inc
#include "hsTypes.h" // for hsAssert
////////////////////////////////////////////////////
// Windows net types
#if HS_BUILD_FOR_WIN32
#include <winsock2.h>
const int kBlockingError = WSAEWOULDBLOCK;
const int kTimeoutError = WSAETIMEDOUT;
const SOCKET kBadSocket = 0xffffffff;
typedef int socklen_t;
////////////////////////////////////////////////////
// UNIX net types
#else
#ifdef HS_BUILD_FOR_FREEBSD
#include <sys/time.h>
#endif
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
typedef int SOCKET;
const int kBlockingError = EWOULDBLOCK;
const int kTimeoutError = ETIMEDOUT;
const SOCKET kBadSocket = -1;
// must #define BSDBLOCK if compiling on BSD
#endif
const unsigned int kDefaultSocketTimeout = 5*60*1000; // 5 mins in millis
////////////////////////////////////////////////////
// OS socket interface wrapper
struct plNet
{
static SOCKET NewUDP();
static SOCKET NewTCP();
static int GetError();
static int Read(const SOCKET sck, char * buf, const int size);
static int Write(const SOCKET sck, const char * buf, const int len);
static int ReadFrom(const SOCKET sck, char * buf, int len, sockaddr_in * addr);
static int WriteTo(const SOCKET sck, const char * buf, const int len, sockaddr_in * addr);
static int Connect(const SOCKET sck, const sockaddr_in * addr);
static int Close(const SOCKET sck);
static int Bind(const SOCKET sck, const sockaddr_in * addr);
static int Listen(const SOCKET sck, const int qsize);
static int Accept(const SOCKET sck, sockaddr_in * addr);
static int Ioctl(const SOCKET sck, const long flags, unsigned long * val);
static const char * plNet::GetErrorMsg(int error);
// TODO: Add get/setsockopt() here
~plNet();
private:
static plNet _;
plNet();
// not impl
plNet(const plNet &);
plNet & operator=(const plNet &);
};
#endif // plNet_h_inc

View File

@ -0,0 +1,111 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "plOutgoingUdpSocket.h"
#include "plFdSet.h"
#include "../pnNetCommon/plNetAddress.h"
#if HS_BUILD_FOR_UNIX
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/udp.h>
#endif
plOutgoingUdpSocket::plOutgoingUdpSocket()
{
}
plOutgoingUdpSocket::plOutgoingUdpSocket(SOCKET sck)
: plSocket(sck)
{
}
bool plOutgoingUdpSocket::operator==(const plOutgoingUdpSocket & rhs)
{
return fSocket==rhs.fSocket;
}
int plOutgoingUdpSocket::SetSendBufferSize(int insize)
{
if (setsockopt(fSocket, (int) SOL_SOCKET, (int) SO_SNDBUF, (char *) &insize, sizeof(int)))
return -1;
return 0;
}
bool plOutgoingUdpSocket::ActiveOpen(plNetAddress & addr)
{
SetSocket(plNet::NewUDP());
if(fSocket == kBadSocket)
return false;
if(plNet::Connect(fSocket, &addr.GetAddressInfo()) != 0)
return ErrorClose();
return true;
}
bool plOutgoingUdpSocket::ActiveOpenNonBlocking(plNetAddress & addr)
{
SetSocket(plNet::NewUDP());
if(fSocket == kBadSocket)
return false;
SetBlocking(false);
if(plNet::Connect(fSocket, &addr.GetAddressInfo()) != 0)
return ErrorClose();
return true;
}
// Returns:
// - if error
// 0 if socket closed or size=0
// + bytes written
int plOutgoingUdpSocket::SendData(const char * data, int size)
{
return plNet::Write(fSocket,data,size);
}

View File

@ -0,0 +1,76 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plOutgoingUdpSocket_h_inc
#define plOutgoingUdpSocket_h_inc
#include "plSocket.h"
class plNetAddress;
class plOutgoingUdpSocket : public plSocket
{
public:
plOutgoingUdpSocket();
plOutgoingUdpSocket(SOCKET sck);
bool operator==(const plOutgoingUdpSocket & rhs);
int SetSendBufferSize(int size);
bool ActiveOpen(plNetAddress & addr);
bool ActiveOpenNonBlocking(plNetAddress & addr);
int SendData(const char * data, int size);
friend bool operator<(const plOutgoingUdpSocket & lhs, const plOutgoingUdpSocket & rhs);
friend bool operator==(const plOutgoingUdpSocket & lhs, const plOutgoingUdpSocket & rhs);
};
inline bool operator<(const plOutgoingUdpSocket & lhs, const plOutgoingUdpSocket & rhs)
{
return lhs.fSocket<rhs.fSocket;
}
inline bool operator==(const plOutgoingUdpSocket & lhs, const plOutgoingUdpSocket & rhs)
{
return lhs.fSocket==rhs.fSocket;
}
#endif //plOutgoingUdpSocket_h_inc

View File

@ -0,0 +1,152 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "hsUtils.h"
#include "plRingBuffer.h"
plRingBuffer::plRingBuffer(int size)
: plMemBuffer(size)
{
fEndPos = 0;
fStartPos = 0;
}
void plRingBuffer::FullCompress()
{
if (fStartPos == fEndPos)
{
fStartPos = 0;
fEndPos = 0;
}
else
{
ForceWindowSlide();
}
}
void plRingBuffer::Compress()
{
if (fStartPos == fEndPos)
{
fStartPos = 0;
fEndPos = 0;
}
else if(fStartPos >= GetBufferSize() / 2)
{
ForceWindowSlide();
}
}
bool plRingBuffer::Put(const char * data, int len)
{
bool ans = false;
if (len > BufferAvailable())
Compress();
if (len <= BufferAvailable())
{
memcpy(GetBufferOpen(), data, len);
fEndPos += len;
ans = true;
}
return ans;
}
bool plRingBuffer::Get(char * data, int len)
{
bool ans = false;
if (len < AmountBuffered())
{
memcpy(data, GetBufferStart(), len);
fStartPos += len;
Compress();
ans = true;
}
return ans;
}
int plRingBuffer::AmountBuffered()
{
return fEndPos - fStartPos;
}
int plRingBuffer::BufferAvailable()
{
return GetBufferSize() - fEndPos;
}
void plRingBuffer::Reset()
{
fStartPos = 0;
fEndPos = 0;
}
char * plRingBuffer::GetBufferStart()
{
return fBuffer+fStartPos;
}
char * plRingBuffer::GetBufferOpen()
{
return fBuffer+fEndPos;
}
void plRingBuffer::ForceWindowSlide()
{
int len = AmountBuffered();
if(len > 0 && fStartPos != 0)
{
memmove(fBuffer, GetBufferStart(), len);
fStartPos = 0;
fEndPos = len;
}
}
bool plRingBuffer::PutFast(const char * data, int len)
{
// no checking be careful
memcpy(GetBufferOpen(), data, len); // or memmove?
fEndPos += len;
return true;
}

View File

@ -0,0 +1,74 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plRingBuffer_h_inc
#define plRingBuffer_h_inc
#include "plMemBuffer.h"
class plRingBuffer : protected plMemBuffer
{
public:
plRingBuffer(int size=4096);
void FullCompress();
void Compress();
bool Put(const char * data, int len);
bool Get(char * data, int len);
int AmountBuffered();
int BufferAvailable();
void Reset();
protected:
int fStartPos;
int fEndPos;
char * GetBufferStart();
char * GetBufferOpen();
void ForceWindowSlide();
#define FastGetBufferStart() (fBuffer+fStartPos)
#define FastAmountBuffered() (fEndPos-fStartPos)
bool PutFast(const char * data, int len);
};
#endif // plRingBuffer_h_inc

View File

@ -0,0 +1,211 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "plSocket.h"
#if HS_BUILD_FOR_UNIX
#include <sys/ioctl.h>
#endif
/////////////////////////////////////////////////
plSocket::plSocket()
{
fCloseOnDestroy = false;
fCloseBeforeSet = false;
fTimeoutsSet = 0;
fSocket = kBadSocket;
}
plSocket::plSocket(SOCKET sck)
{
fCloseOnDestroy = false;
fCloseBeforeSet = false;
fTimeoutsSet = 0;
SetSocket(sck);
}
plSocket::~plSocket()
{
if (fCloseOnDestroy)
Close();
}
bool plSocket::operator==(const plSocket & rhs)
{
return fSocket == rhs.fSocket;
}
bool plSocket::ErrorClose()
{
if(Active())
plNet::Close(fSocket);
fSocket = kBadSocket;
return false;
}
bool plSocket::Active()
{
return (fSocket != kBadSocket);
}
void plSocket::Close()
{
if(Active())
plNet::Close(fSocket);
fSocket = kBadSocket;
}
int plSocket::GetLastError()
{
return plNet::GetError();
}
void plSocket::SetSocket(SOCKET sck)
{
if (fSocket!=sck)
{
if (fCloseBeforeSet)
Close();
fSocket = sck;
if (fTimeoutsSet & kRecvTimeoutSet)
setsockopt(fSocket, SOL_SOCKET, (int)SO_RCVTIMEO,(const char*)&fRecvTimeOut,sizeof(fRecvTimeOut));
if (fTimeoutsSet & kSendTimeoutSet)
setsockopt(fSocket, SOL_SOCKET, (int)SO_SNDTIMEO,(const char*)&fSendTimeOut,sizeof(fSendTimeOut));
IGetTimeOutsFromSocket();
}
}
int plSocket::SetRecvBufferSize(int insize)
{
if (setsockopt(fSocket, (int)SOL_SOCKET, (int)SO_RCVBUF, (char *)&insize, sizeof(int)))
return 0;
return -1;
}
void plSocket::SetBlocking(bool value)
{
unsigned long val = value?0:1;
#ifdef BSDBLOCK
// NOTE: Bsd does this a little differently, using fcntl()
#error "BSDBLOCK: This code needs to be verified right now. Don't assume it will work at all."
int flags = fcntl(fSocket,F_GETFL,val);
flags = flags |O_NONBLOCK;
fcntl(fSocket,F_SETFL,flags);
#else
plNet::Ioctl(fSocket,FIONBIO,&val);
#endif
}
bool plSocket::IsBlocking()
{
#ifdef BSDBLOCK
#error "BSDBLOCK: TODO: IsBlocking() for bsd."
#else
int ans = plNet::Ioctl(fSocket,FIONBIO,NULL);
return (ans!=0);
#endif
}
void plSocket::SetReuseAddress()
{
int opt = 1;
setsockopt(fSocket, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt));
}
int plSocket::SetSendTimeOut(unsigned int milliSecs/* =0 */)
{
fTimeoutsSet |= kSendTimeoutSet;
fSendTimeOut = milliSecs;
if (fSocket != kBadSocket)
{
if (setsockopt(fSocket, SOL_SOCKET, (int)SO_SNDTIMEO,(const char*)&fSendTimeOut,sizeof(fSendTimeOut)) != 0)
return -1;
}
return 0;
}
int plSocket::SetRecvTimeOut(unsigned int milliSecs/* =0 */)
{
fTimeoutsSet |= kRecvTimeoutSet;
fRecvTimeOut = milliSecs;
if (fSocket != kBadSocket)
{
if (setsockopt(fSocket, SOL_SOCKET, (int)SO_RCVTIMEO,(const char*)&fRecvTimeOut,sizeof(fRecvTimeOut)) != 0)
return -1;
}
return 0;
}
int plSocket::IGetTimeOutsFromSocket()
{
unsigned int rtimeoutval, stimeoutval;
if (fSocket != kBadSocket)
{
socklen_t sizeval;
sizeval = sizeof(rtimeoutval);
if (getsockopt(fSocket, SOL_SOCKET, (int)SO_RCVTIMEO,(char*)&rtimeoutval,&sizeval) != 0)
return -1;
sizeval = sizeof(stimeoutval);
if (getsockopt(fSocket, SOL_SOCKET, (int)SO_SNDTIMEO,(char*)&stimeoutval,&sizeval) != 0)
return -1;
fRecvTimeOut = rtimeoutval;
fSendTimeOut = stimeoutval;
}
return 0;
}

View File

@ -0,0 +1,98 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plSocket_h_inc
#define plSocket_h_inc
#include "plNet.h"
////////////////////////////////////////////////////
// Socket base class
//
// plSocket
// plTcpSocket
// plTcpListenSocket
// plIncomingUdpSocket - TODO (simple to impl, no immediate need)
// plOutgoingUdpSocket - TODO ""
// plConnectedOutgoingUdpSocket - TODO ""
// plFdSet
//
class plSocket
{
public:
plSocket();
plSocket(SOCKET sck);
virtual ~plSocket();
bool operator==(const plSocket & rhs);
static int GetLastError();
void Close();
void SetBlocking(bool value);
bool IsBlocking();
void SetReuseAddress();
bool Active();
int SetRecvBufferSize(int size);
void SetSocket(SOCKET sck);
SOCKET GetSocket() const { return fSocket; }
void CloseOnDestroy(bool value) { fCloseOnDestroy=value; }
void CloseBeforeSet(bool value) { fCloseBeforeSet=value; }
int SetSendTimeOut(unsigned int milliSecs=0);
int SetRecvTimeOut(unsigned int milliSecs=0);
protected:
enum TimeoutsSet
{
kRecvTimeoutSet = 1<<0,
kSendTimeoutSet = 1<<1,
};
bool ErrorClose();
SOCKET fSocket;
bool fCloseOnDestroy;
bool fCloseBeforeSet;
unsigned int fTimeoutsSet;
unsigned int fRecvTimeOut;
unsigned int fSendTimeOut;
int IGetTimeOutsFromSocket();
};
#endif // plSocket_h_inc

View File

@ -0,0 +1,91 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "plTcpListenSocket.h"
#include "../pnNetCommon/plNetAddress.h"
// Initialize a socket for listening
bool plTcpListenSocket::OpenForListen(plNetAddress & inAddess, int backlogSize)
{
ErrorClose();
SetSocket(plNet::NewTCP());
SetReuseAddress();
if(plNet::Bind(fSocket, &inAddess.GetAddressInfo()) != 0)
return ErrorClose();
if(plNet::Listen(fSocket, backlogSize) != 0)
return ErrorClose();
return true;
}
// Initialize a socket for listening. non blocking version
bool plTcpListenSocket::OpenForListenNonBlocking(plNetAddress & inAddess, int backlogSize)
{
ErrorClose();
SetSocket(plNet::NewTCP());
SetReuseAddress();
SetBlocking(false); // so GetIncomingConnection() won't block.
if(plNet::Bind(fSocket, &inAddess.GetAddressInfo()) != 0)
return ErrorClose();
if(plNet::Listen(fSocket, backlogSize) != 0)
return ErrorClose();
return true;
}
// Accept an incoming connection
bool plTcpListenSocket::GetIncomingConnection(SOCKET & outNewSession, plNetAddress & outAddress)
{
outNewSession = plNet::Accept(fSocket, &outAddress.GetAddressInfo());
if(outNewSession == kBadSocket)
return false;
return true;
}

View File

@ -0,0 +1,59 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plTcpListenSocket_h_inc
#define plTcpListenSocket_h_inc
#include "plSocket.h"
class plNetAddress;
class plTcpListenSocket : public plSocket
{
public:
bool OpenForListen(plNetAddress & inAddress, int backlogSize=1024);
bool OpenForListenNonBlocking(plNetAddress & inAddress, int backlogSize=1024);
bool GetIncomingConnection(SOCKET & outNewSession, plNetAddress & outRemoteAddress);
};
#endif // plTcpListenSocket_h_inc

View File

@ -0,0 +1,156 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "plTcpSocket.h"
#include "plFdSet.h"
#include "../pnNetCommon/plNetAddress.h"
#if HS_BUILD_FOR_UNIX
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#endif
plTcpSocket::plTcpSocket()
{
}
plTcpSocket::plTcpSocket(SOCKET sck)
: plSocket(sck)
{
}
bool plTcpSocket::operator==(const plTcpSocket & rhs)
{
return fSocket==rhs.fSocket;
}
// Disable Nagle algorithm.
int plTcpSocket::SetNoDelay(void)
{
int nodel = 1;
int ret1;
ret1 = setsockopt(fSocket, IPPROTO_TCP, TCP_NODELAY,(char *)&nodel, sizeof(nodel));
if(ret1 != 0)
return -1;
return 0;
}
// Control the behavior of SO_LINGER
int plTcpSocket::SetLinger(int intervalSecs)
{
linger ll;
ll.l_linger = intervalSecs;
ll.l_onoff = intervalSecs?1:0;
int ret1 = setsockopt(fSocket, SOL_SOCKET, SO_LINGER, (const char *)&ll,sizeof(linger));
if(ret1 != 0)
return -1;
return 0;
}
int plTcpSocket::SetSendBufferSize(int insize)
{
if (setsockopt(fSocket, (int) SOL_SOCKET, (int) SO_SNDBUF, (char *) &insize, sizeof(int)))
return -1;
return 0;
}
bool plTcpSocket::ActiveOpen(plNetAddress & addr)
{
SetSocket(plNet::NewTCP());
if(fSocket == kBadSocket)
return false;
if(plNet::Connect(fSocket, &addr.GetAddressInfo()) != 0)
return ErrorClose();
return true;
}
bool plTcpSocket::ActiveOpenNonBlocking(plNetAddress & addr)
{
SetSocket(plNet::NewTCP());
if(fSocket == kBadSocket)
return false;
SetBlocking(false);
if(plNet::Connect(fSocket, &addr.GetAddressInfo()) != 0)
return ErrorClose();
return true;
}
// Returns:
// - if error
// 0 if socket closed or size=0
// + bytes written
int plTcpSocket::SendData(const char * data, int size)
{
return plNet::Write(fSocket,data,size);
}
// Returns:
// - if error
// 0 if socket closed or size=0
// + bytes read
int plTcpSocket::RecvData(char * data, int len)
{
plFdSet fdset;
fdset.SetForSocket(*this);
fdset.WaitForRead(false,fRecvTimeOut);
if (fdset.IsErrFor(*this))
return -1;
if (fdset.IsSetFor(*this))
return plNet::Read(fSocket,data,len); // returns -1 on error
else
return -2;
};

View File

@ -0,0 +1,79 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef plTcpSocket_h_inc
#define plTcpSocket_h_inc
#include "plSocket.h"
class plNetAddress;
class plTcpSocket : public plSocket
{
public:
plTcpSocket();
plTcpSocket(SOCKET sck);
bool operator==(const plTcpSocket & rhs);
int SetNoDelay();
int SetLinger(int intervalSecs=0);
int SetSendBufferSize(int size);
bool ActiveOpen(plNetAddress & addr);
bool ActiveOpenNonBlocking(plNetAddress & addr);
int SendData(const char * data, int size);
int RecvData(char * data, int size);
friend bool operator<(const plTcpSocket & lhs, const plTcpSocket & rhs);
friend bool operator==(const plTcpSocket & lhs, const plTcpSocket & rhs);
};
inline bool operator<(const plTcpSocket & lhs, const plTcpSocket & rhs)
{
return lhs.fSocket<rhs.fSocket;
}
inline bool operator==(const plTcpSocket & lhs, const plTcpSocket & rhs)
{
return lhs.fSocket==rhs.fSocket;
}
#endif //plTcpSocket_h_inc