vsocket.cpp
来自「这是一个比较复杂的远程控制工具,分为服务器与客户斋,让你了解socket编程的知」· C++ 代码 · 共 786 行 · 第 1/2 页
CPP
786 行
{
if (pHost->h_addr == NULL)
return 0;
addr = ((struct in_addr *)pHost->h_addr)->s_addr;
}
else
return 0;
}
// Return the resolved IP address as an integer
return addr;
}
////////////////////////////
VBool
VSocket::SetTimeout(VCard32 msecs)
{
if (LOBYTE(winsockVersion) < 2)
return VFalse;
int timeout=msecs;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) == SOCKET_ERROR)
{
return VFalse;
}
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout)) == SOCKET_ERROR)
{
return VFalse;
}
return VTrue;
}
////////////////////////////
VInt
VSocket::Send(const char *buff, const VCard bufflen)
{
int newsize=queuebuffersize+bufflen;
char *buff2;
buff2=(char*)buff;
unsigned int bufflen2=bufflen;
if (newsize >8192)
{
memcpy(queuebuffer+queuebuffersize,buff2,8192-queuebuffersize);
send(sock,queuebuffer,8192,0);
// vnclog.Print(LL_SOCKERR, VNCLOG("SEND %i\n") ,8192);
buff2+=(8192-queuebuffersize);
bufflen2-=(8192-queuebuffersize);
queuebuffersize=0;
while (bufflen2 > 8192)
{
if (!send(sock,buff2,8192,0)) return false;
// vnclog.Print(LL_SOCKERR, VNCLOG("SEND 1 %i\n") ,8192);
buff2+=8192;
bufflen2-=8192;
}
}
memcpy(queuebuffer+queuebuffersize,buff2,bufflen2);
queuebuffersize+=bufflen2;
if (!send(sock,queuebuffer,queuebuffersize,0)) return false;
// vnclog.Print(LL_SOCKERR, VNCLOG("SEND 2 %i\n") ,queuebuffersize);
queuebuffersize=0;
return bufflen;
}
////////////////////////////
VInt
VSocket::SendQueued(const char *buff, const VCard bufflen)
{
int newsize=queuebuffersize+bufflen;
char *buff2;
buff2=(char*)buff;
unsigned int bufflen2=bufflen;
if (newsize >8192)
{
memcpy(queuebuffer+queuebuffersize,buff2,8192-queuebuffersize);
send(sock,queuebuffer,8192,0);
// vnclog.Print(LL_SOCKERR, VNCLOG("SEND Q %i\n") ,8192);
buff2+=(8192-queuebuffersize);
bufflen2-=(8192-queuebuffersize);
queuebuffersize=0;
while (bufflen2 > 8192)
{
if (!send(sock,buff2,8192,0)) return false;
// vnclog.Print(LL_SOCKERR, VNCLOG("SEND Q %i\n") ,8192);
buff2+=8192;
bufflen2-=8192;
}
}
memcpy(queuebuffer+queuebuffersize,buff2,bufflen2);
queuebuffersize+=bufflen2;
return bufflen;
}
/////////////////////////////
// sf@2002 - DSMPlugin
VBool
VSocket::SendExact(const char *buff, const VCard bufflen, unsigned char msgType)
{
//vnclog.Print(LL_SOCKERR, VNCLOG("SendExactMsg %i\n") ,bufflen);
if (m_fUsePlugin && m_pDSMPlugin->IsEnabled())
{
// Send the transformed message type first
char t = (char)msgType;
SendExact(&t, 1);
// Then we send the transformed rfb message content
SendExact(buff, bufflen);
}
else
SendExact(buff, bufflen);
return VTrue;
}
//////////////////////////////////////////
VBool
VSocket::SendExact(const char *buff, const VCard bufflen)
{
// vnclog.Print(LL_SOCKERR, VNCLOG("SendExact %i\n") ,bufflen);
// sf@2002 - DSMPlugin
VCard nBufflen = bufflen;
char* pBuffer = NULL;
if (m_fUsePlugin && m_pDSMPlugin->IsEnabled())
{
// omni_mutex_lock l(m_TransMutex);
// If required to store data into memory
if (m_fWriteToNetRectBuf)
{
memcpy((char*)(m_pNetRectBuf + m_nNetRectBufOffset), buff, bufflen);
m_nNetRectBufOffset += bufflen;
return VTrue;
}
else // Tell the plugin to transform data
{
int nTransDataLen = 0;
pBuffer = (char*)(m_pDSMPlugin->TransformBuffer((BYTE*)buff, bufflen, &nTransDataLen));
if (pBuffer == NULL || (bufflen > 0 && nTransDataLen == 0))
{
// throw WarningException("SendExact: DSMPlugin-TransformBuffer Error.");
}
nBufflen = nTransDataLen;
}
}
else
pBuffer = (char*) buff;
VInt result=Send(pBuffer, nBufflen);
return result == (VInt)nBufflen;
}
///////////////////////////////////////
VBool
VSocket::SendExactQueue(const char *buff, const VCard bufflen)
{
// vnclog.Print(LL_SOCKERR, VNCLOG("SendExactQueue %i %i\n") ,bufflen,queuebuffersize);
// vnclog.Print(LL_SOCKERR, VNCLOG("socket size %i\n") ,bufflen);
// sf@2002 - DSMPlugin
VCard nBufflen = bufflen;
char* pBuffer = NULL;
if (m_fUsePlugin && m_pDSMPlugin->IsEnabled())
{
// omni_mutex_lock l(m_TransMutex);
// If required to store data into memory
if (m_fWriteToNetRectBuf)
{
memcpy((char*)(m_pNetRectBuf + m_nNetRectBufOffset), buff, bufflen);
m_nNetRectBufOffset += bufflen;
return VTrue;
}
else // Tell the plugin to transform data
{
int nTransDataLen = 0;
pBuffer = (char*)(m_pDSMPlugin->TransformBuffer((BYTE*)buff, bufflen, &nTransDataLen));
if (pBuffer == NULL || (bufflen > 0 && nTransDataLen == 0))
{
// throw WarningException("SendExact: DSMPlugin-TransformBuffer Error.");
}
nBufflen = nTransDataLen;
}
}
else
pBuffer = (char*) buff;
VInt result=SendQueued(pBuffer, nBufflen);
return result == (VInt)nBufflen;
}
///////////////////////////////
void
VSocket::ClearQueue()
{
if (queuebuffersize!=0)
{
send(sock,queuebuffer,queuebuffersize,0);
queuebuffersize=0;
}
}
////////////////////////////
VInt
VSocket::Read(char *buff, const VCard bufflen)
{
return recv(sock, buff, bufflen, 0);
}
////////////////////////////
VBool
VSocket::ReadExact(char *buff, const VCard bufflen)
{
int n;
VCard currlen = bufflen;
// sf@2002 - DSM Plugin
if (m_fUsePlugin && m_pDSMPlugin->IsEnabled())
{
omni_mutex_lock l(m_pDSMPlugin->m_RestMutex);
// Get the DSMPlugin destination buffer where to put transformed incoming data
// The number of bytes to read calculated from bufflen is given back in nTransDataLen
int nTransDataLen = 0;
BYTE* pTransBuffer = m_pDSMPlugin->RestoreBufferStep1(NULL, bufflen, &nTransDataLen);
if (pTransBuffer == NULL)
{
// m_pDSMPlugin->RestoreBufferUnlock();
// throw WarningException("WriteExact: DSMPlugin-RestoreBuffer Alloc Error.");
vnclog.Print(LL_SOCKERR, VNCLOG("WriteExact: DSMPlugin-RestoreBuffer Alloc Error\n"));
return VFalse;
}
// Read bytes directly into Plugin Dest Rest. buffer
int nTransDataLenSave = nTransDataLen;
while (nTransDataLen > 0)
{
// Try to read some data in
n = Read((char*)pTransBuffer, nTransDataLen);
if (n > 0)
{
// Adjust the buffer position and size
pTransBuffer += n;
nTransDataLen -= n;
} else if (n == 0) {
//m_pDSMPlugin->RestoreBufferUnlock();
vnclog.Print(LL_SOCKERR, VNCLOG("zero bytes read1\n"));
return VFalse;
} else {
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
//m_pDSMPlugin->RestoreBufferUnlock();
vnclog.Print(LL_SOCKERR, VNCLOG("socket error 1: %d\n"), WSAGetLastError());
return VFalse;
}
}
}
// Ask plugin to restore data from rest. buffer into inbuf
int nRestDataLen = 0;
nTransDataLen = nTransDataLenSave;
BYTE* pPipo = m_pDSMPlugin->RestoreBufferStep2((BYTE*)buff, nTransDataLen, &nRestDataLen);
// Check if we actually get the real original data length
if ((VCard)nRestDataLen != bufflen)
{
// throw WarningException("WriteExact: DSMPlugin-RestoreBuffer Error.");
}
}
else // Non-Transformed
{
while (currlen > 0)
{
// Try to read some data in
n = Read(buff, currlen);
if (n > 0)
{
// Adjust the buffer position and size
buff += n;
currlen -= n;
} else if (n == 0) {
vnclog.Print(LL_SOCKERR, VNCLOG("zero bytes read2\n"));
return VFalse;
} else {
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
//int aa=WSAGetLastError();
//vnclog.Print(LL_SOCKERR, VNCLOG("socket error 2: %d\n"), aa);
return VFalse;
}
}
}
}
return VTrue;
}
//
// sf@2002 - DSMPlugin
//
//
// Ensures that the temporary "alignement" buffer in large enough
//
void VSocket::CheckNetRectBufferSize(int nBufSize)
{
omni_mutex_lock l(m_CheckMutex);
if (m_nNetRectBufSize > nBufSize) return;
BYTE *newbuf = new BYTE[nBufSize + 256];
if (newbuf == NULL)
{
// throw ErrorException("Insufficient memory to allocate network crypt buffer.");
}
if (m_pNetRectBuf != NULL)
{
// memcpy(newbuf, m_pNetRectBuf, m_nNetRectBufSize);
delete [] m_pNetRectBuf;
}
m_pNetRectBuf = newbuf;
m_nNetRectBufSize = nBufSize + 256;
// vnclog.Print(4, _T("crypt bufsize expanded to %d\n"), m_netbufsize);
}
// sf@2002 - DSMPlugin
// Necessary for HTTP server (we'll see later if we can do something more intelligent...
VBool
VSocket::SendExactHTTP(const char *buff, const VCard bufflen)
{
return Send(buff, bufflen) == (VInt)bufflen;
}
////////////////////////////
VBool
VSocket::ReadExactHTTP(char *buff, const VCard bufflen)
{
int n;
VCard currlen = bufflen;
while (currlen > 0)
{
// Try to read some data in
n = Read(buff, currlen);
if (n > 0)
{
// Adjust the buffer position and size
buff += n;
currlen -= n;
} else if (n == 0) {
vnclog.Print(LL_SOCKERR, VNCLOG("zero bytes read3\n"));
return VFalse;
} else {
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
vnclog.Print(LL_SOCKERR, VNCLOG("HTTP socket error: %d\n"), WSAGetLastError());
return VFalse;
}
}
}
return VTrue;
}
#ifdef HTTP_SAMEPORT
VBool
VSocket::ReadSelect(VCard to)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET((unsigned long)sock,&fds);
struct timeval tv;
tv.tv_sec = to/1000;
tv.tv_usec = (to % 1000)*1000;
int rc = select(sock+1,&fds,0,0,&tv);
if (rc>0) return true;
return false;
}
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?