📄 connects.cpp
字号:
// ConnectS.cpp : implementation file
//
// Microsoft Technical Support, Developer Support
// Copyright (c) 1998 Microsoft Corporation. All rights reserved.
#include "stdafx.h"
#include "ConnectS.h"
#include "..\mailbox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CConnectSoc
CConnectSoc::CConnectSoc()
: m_pConnector(0)
{
m_nBytesSent = m_nSendDataLen = 0;
m_fConnected = FALSE;
m_bReadDataLength = TRUE;
m_pBox = NULL;
}
CConnectSoc::~CConnectSoc()
{
delete m_pConnector;
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CConnectSoc, CAsyncSocket)
//{{AFX_MSG_MAP(CConnectSoc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CConnectSoc member functions
// Peer has closed the tcp connection.
// send WM_QUIT message to the thread containing the socket
// to shutdown once the connection is closed.
void CConnectSoc::OnClose(int nErrorCode)
{
// passive close
m_fConnected = FALSE;
ShutDown();
Close();
TRACE(_TEXT("CConnectSoc::OnClose: CAsyncSocket::Close() called\n"));
CAsyncSocket::OnClose(nErrorCode);
}
// Receives data with a 4 byte preappended data length header.
// The function is set up to call Receive only once as recommended.
void CConnectSoc::OnReceive(int nErrorCode)
{
try
{
OutputDebugString(_TEXT("CConnectSoc::OnReceive\n"));
BYTE buffer[1024];
int nRead = Receive(buffer, 1024);
m_pConnector->ReadCompleted(buffer, nRead);
}
catch(...)
{
m_fConnected = FALSE;
}
}
void CConnectSoc::OnConnect( int nErrorCode )
{
OutputDebugString(_TEXT("CConnectSoc::OnConnect\n"));
if (nErrorCode == 0)
{
m_fConnected = TRUE;
if (m_pBox)
m_pBox->OnSSLEvent(1);
}
else
{
// Error in doing a Connect to peer, I will just quit this thread.
// Or you might want to notify the parent thread of nErrorCode.
m_fConnected = FALSE;
}
CAsyncSocket::OnConnect(nErrorCode);
}
void CConnectSoc::OnSend(int nErrorCode)
{
OutputDebugString(_TEXT("CConnectSoc::OnSend\n"));
// Make sure we are connected to peer before sending data.
// OnSend will also be called right after connection is established,
// DoAsyncSendBuff() will not send any data because the initial
// state of this CConnectSoc object has 0 bytes to send.
if (m_fConnected)
DoAsyncSendBuff();
CAsyncSocket::OnSend(nErrorCode);
}
void CConnectSoc::AsyncSendBuff(void* lpBuf, int nBufLen)
{
// We don't queue up data here.
// If you are going to queue up data packet, it would be better to limit the size
// of the queue and remember to clear up the queue whenever the current packet has been sent.
if (m_nSendDataLen != 0 || nBufLen > MAX_BUFF)
{
TCHAR szError[256];
wsprintf(szError, _TEXT("CConnectSoc::AsyncSendBuff() can't accept more data\n"));
TRACE(szError);
// AfxMessageBox (szError);
return;
}
else
{
if (nBufLen > MAX_BUFF)
{
TCHAR szError[256];
wsprintf(szError, _TEXT("CConnectSoc::AsyncSendBuff() oversize buffer.\n"));
TRACE(szError);
// AfxMessageBox (szError);
return;
}
memcpy(m_sendBuff, lpBuf, nBufLen);
m_nSendDataLen = nBufLen;
m_nBytesSent = 0;
DoAsyncSendBuff();
}
}
// Send the data left in the buffer.
// If TCP stack can not accept more data and gives error of WSAEWOULDBLOCK,
// we breaks out of the while loop. Whenever TCP stack can accept more data,
// our CConnectSoc::OnSend() will be called.
void CConnectSoc::DoAsyncSendBuff()
{
while (m_nBytesSent < m_nSendDataLen)
{
int nBytes;
if ((nBytes = Send((LPCTSTR)m_sendBuff + m_nBytesSent, m_nSendDataLen - m_nBytesSent))
== SOCKET_ERROR)
{
if (GetLastError() == WSAEWOULDBLOCK)
break;
else
{
TCHAR szError[256];
wsprintf(szError, _TEXT("Server Socket failed to send: %d"), GetLastError());
Close();
TRACE(szError);
//AfxMessageBox (szError);
m_nBytesSent = 0;
m_nSendDataLen = sizeof(int);
throw 1;
return;
}
}
else
{
m_nBytesSent += nBytes;
}
}
if (m_nBytesSent == m_nSendDataLen)
{
m_nBytesSent = m_nSendDataLen = 0;
}
}
int CConnectSoc::Receive(void* lpBuf, int nBufLen, int nFlags)
{
OutputDebugString(_TEXT("CConnectSoc::Receive\n"));
return CAsyncSocket::Receive(lpBuf, nBufLen, nFlags);
}
int CConnectSoc::Send(const void* lpBuf, int nBufLen, int nFlags)
{
OutputDebugString(_TEXT("CConnectSoc::Send\n"));
return CAsyncSocket::Send(lpBuf, nBufLen, nFlags);
}
void CConnectSoc::CreateConnector(SSL_CTX *pContext)
{
if (m_pConnector)
delete m_pConnector;
m_pConnector = new CAsyncConnector(pContext, *this);
m_pConnector->ExpectConnect();
}
void CConnectSoc::OnDataAvailable()
{
int nOld = m_aReadBuf.GetSize();
m_aReadBuf.SetSize(nOld + 1024);
LPBYTE pBuf = m_aReadBuf.GetData()+nOld;
int nRead = m_pConnector->Receive(pBuf, 1023);
//pBuf[nRead] = 0;
m_aReadBuf.SetSize(nOld+nRead);
if (m_pBox)
m_pBox->OnSSLEvent(2);
}
int CConnectSoc::GetData(LPTSTR pBuf, int nSize)
{
int nActSize = m_aReadBuf.GetSize();
if (!pBuf)
return nActSize;
if (nActSize == 0)
return 0;
memcpy(pBuf, m_aReadBuf.GetData(), min(nSize, nActSize));
if (nSize < nActSize) // there are more data
m_aReadBuf.RemoveAt(0, nSize);
else
m_aReadBuf.SetSize(0);
return min(nSize, nActSize);
}
int CConnectSoc::WriteString(LPCTSTR pRequest, int requestLength)
{
try
{
m_pConnector->Write((BYTE*)pRequest, requestLength);
return requestLength; // ??
}
catch(...)
{
m_fConnected = FALSE;
}
return SOCKET_ERROR;
}
void CConnectSoc::WriteData(
BYTE *pData,
int dataLength)
{
AsyncSendBuff(pData, dataLength);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -