Add thread.
This commit is contained in:
@@ -10,6 +10,11 @@ const int WSA_MINOR_VERSION = 1;
|
||||
const char BufferHeader[4]={0x46,0x49,0x4E,0x53};
|
||||
const char HandShaking[]={0x46,0x49,0x4E,0x53,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
|
||||
|
||||
|
||||
HANDLE CSo7_TCPIP::m_Thread_Id=NULL;
|
||||
HANDLE CSo7_TCPIP::m_Thread_Mutex=NULL;
|
||||
int CSo7_TCPIP::m_Thread_State=TCPIP_THREAD_PAUSED;
|
||||
|
||||
int CSo7_TCPIP::m_iReceiveMaxBufSize=1024;
|
||||
int CSo7_TCPIP::m_iSendMaxBufSize=1024;
|
||||
|
||||
@@ -17,20 +22,30 @@ SOCKET CSo7_TCPIP::m_Socket=INVALID_SOCKET;
|
||||
in_addr CSo7_TCPIP::m_SreverIPAddress;
|
||||
in_addr CSo7_TCPIP::m_ClientIPAddress;
|
||||
u_short CSo7_TCPIP::m_iServerPortNumber=static_cast<u_short>(0);
|
||||
struct_so7_tcpip_buff CSo7_TCPIP::m_TCPIPBuf[lChannelSize];
|
||||
struct_so7_tcpip_data CSo7_TCPIP::m_TCPIPData;
|
||||
|
||||
|
||||
//================================================================
|
||||
CSo7_TCPIP::CSo7_TCPIP()
|
||||
{
|
||||
m_hMsgWnd=NULL;
|
||||
m_ReceiveBuf=NULL;
|
||||
m_SendBuf=NULL;
|
||||
|
||||
m_iBytesToReceive = 0;
|
||||
m_iBytesReceived = 0;
|
||||
m_iBytesToSend = 0;
|
||||
m_iBytesSent = 0;
|
||||
m_ReceiveBuf = new char[m_iReceiveMaxBufSize];
|
||||
m_SendBuf = new char[m_iSendMaxBufSize];
|
||||
for (int i=0;i<lChannelSize;i++)
|
||||
{
|
||||
m_TCPIPBuf[i]._size = 0;
|
||||
m_TCPIPBuf[i]._CompletedSize = 0;
|
||||
m_TCPIPBuf[i]._save_send_cmd = 99;
|
||||
m_TCPIPBuf[i]._buffer = (char *)malloc(TCPIP_MAX_BUFF_SIZE);
|
||||
m_TCPIPBuf[i]._hProtoPending = false;
|
||||
m_TCPIPBuf[i]._event = NULL;
|
||||
};
|
||||
m_TCPIPData.s_status._handshaking=false;
|
||||
m_TCPIPData.s_status._SendReturnCode=TCPIP_CONNECT_OK;
|
||||
m_TCPIPData.s_status._RecvReturnCode=TCPIP_CONNECT_OK;
|
||||
m_TCPIPData.s_recv_data._type=0;
|
||||
m_TCPIPData.s_recv_data._DataSize=0;
|
||||
m_TCPIPData.s_recv_data._dbBuff=(double *)malloc(TCPIP_MAX_DAT_SIZE);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,12 +53,101 @@ CSo7_TCPIP::CSo7_TCPIP()
|
||||
CSo7_TCPIP::~CSo7_TCPIP()
|
||||
{
|
||||
DisConnect();
|
||||
if(m_SendBuf)
|
||||
delete [] m_SendBuf;
|
||||
m_SendBuf=NULL;
|
||||
if(m_ReceiveBuf)
|
||||
delete [] m_ReceiveBuf;
|
||||
m_ReceiveBuf=NULL;
|
||||
for (int i=0;i<lChannelSize;i++)
|
||||
{
|
||||
free(m_TCPIPBuf[i]._buffer);
|
||||
m_TCPIPBuf[i]._buffer=NULL;
|
||||
}
|
||||
free(m_TCPIPData.s_recv_data._dbBuff);
|
||||
m_TCPIPData.s_recv_data._dbBuff=NULL;
|
||||
}
|
||||
//================================================================
|
||||
void CSo7_TCPIP::Create_Thread()
|
||||
{
|
||||
if (!m_Thread_Id)
|
||||
{
|
||||
m_Thread_State = TCPIP_THREAD_RUNNING;
|
||||
m_TCPIPBuf[CH_SEND]._event = CreateEvent(NULL,FALSE,NULL,NULL);
|
||||
m_Thread_Id = CreateThread((LPSECURITY_ATTRIBUTES)NULL,0,(LPTHREAD_START_ROUTINE)m_Thread,(LPVOID)this,0,NULL);
|
||||
m_Thread_Mutex = CreateMutex(NULL,FALSE,NULL);
|
||||
}
|
||||
}
|
||||
//================================================================
|
||||
void CSo7_TCPIP::Exit_Thread()
|
||||
{
|
||||
m_Thread_State = TCPIP_THREAD_EXIT;
|
||||
SetEvent(m_TCPIPBuf[CH_SEND]._event );
|
||||
if(m_Thread_Id)
|
||||
{
|
||||
DWORD dwCode = STILL_ACTIVE;
|
||||
while(dwCode == STILL_ACTIVE)
|
||||
{
|
||||
GetExitCodeThread(m_Thread_Id,&dwCode);
|
||||
Sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
SetEvent(m_TCPIPBuf[CH_SEND]._event );
|
||||
CloseHandle(m_TCPIPBuf[CH_SEND]._event );
|
||||
m_Thread_State = TCPIP_THREAD_EXIT;
|
||||
ReleaseMutex(m_Thread_Mutex);
|
||||
CloseHandle(m_Thread_Mutex);
|
||||
m_Thread_Id=NULL;
|
||||
}
|
||||
//================================================================
|
||||
void CSo7_TCPIP::_do_single_threaded_tcpip_comm(bool _bWaitForRsponse)
|
||||
{
|
||||
while ((m_TCPIPBuf[CH_SEND]._hProtoPending == TRUE) || (m_TCPIPBuf[CH_RECV]._hProtoPending == TRUE))
|
||||
{
|
||||
ASSERT(0);
|
||||
Sleep(3);
|
||||
}
|
||||
m_TCPIPBuf[CH_SEND]._hProtoPending = TRUE;
|
||||
if (_bWaitForRsponse)
|
||||
{
|
||||
m_TCPIPBuf[CH_RECV]._hProtoPending = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_TCPIPBuf[CH_RECV]._hProtoPending = FALSE;
|
||||
}
|
||||
SetEvent(m_TCPIPBuf[CH_SEND]._event);
|
||||
while ((m_TCPIPBuf[CH_SEND]._hProtoPending == TRUE) || (m_TCPIPBuf[CH_RECV]._hProtoPending == TRUE))
|
||||
{
|
||||
Sleep(3);
|
||||
}
|
||||
}
|
||||
|
||||
//================================================================
|
||||
unsigned __stdcall CSo7_TCPIP::m_Thread(LPVOID pThis)
|
||||
{
|
||||
CSo7_TCPIP* _This = (CSo7_TCPIP*)pThis;
|
||||
for(;;)
|
||||
{
|
||||
if(m_Thread_State == TCPIP_THREAD_EXIT)
|
||||
ExitThread(0);
|
||||
WaitForSingleObject(m_TCPIPBuf[CH_SEND]._event ,INFINITE);
|
||||
|
||||
switch(m_Thread_State)
|
||||
{
|
||||
case TCPIP_THREAD_RUNNING:
|
||||
{
|
||||
_This->SendBuffer();
|
||||
break;
|
||||
}
|
||||
case TCPIP_THREAD_PAUSED:
|
||||
{
|
||||
break;
|
||||
}
|
||||
case TCPIP_THREAD_EXIT:
|
||||
{
|
||||
ExitThread(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
m_Thread_State=TCPIP_THREAD_EXIT;
|
||||
ExitThread(0);
|
||||
}
|
||||
//================================================================
|
||||
int CSo7_TCPIP::Init_Winsock()
|
||||
@@ -83,6 +187,7 @@ TCPIP_RETURN_CODE CSo7_TCPIP::Connect(const HWND& _hWnd,const in_addr& _IPAddres
|
||||
}
|
||||
else
|
||||
{
|
||||
Create_Thread();
|
||||
// 设置socket为窗口通知消息类型
|
||||
::WSAAsyncSelect(m_Socket, m_hMsgWnd,WM_SOCKET, FD_CONNECT | FD_CLOSE | FD_WRITE | FD_READ);
|
||||
|
||||
@@ -110,8 +215,10 @@ TCPIP_RETURN_CODE CSo7_TCPIP::DisConnect()
|
||||
{
|
||||
return TCPIP_INVAILD_SOCKET;
|
||||
}
|
||||
::closesocket(m_Socket);
|
||||
closesocket(m_Socket);
|
||||
m_Socket = INVALID_SOCKET;
|
||||
m_ClearSendBuf();
|
||||
Exit_Thread();
|
||||
return TCPIP_CONNECT_OK;
|
||||
}
|
||||
//================================================================
|
||||
@@ -138,41 +245,16 @@ TCPIP_RETURN_CODE CSo7_TCPIP::GetHostIPAddr(in_addr& _IPAddress)
|
||||
//================================================================
|
||||
TCPIP_RETURN_CODE CSo7_TCPIP::Handshaking()
|
||||
{
|
||||
WaitForSingleObject(m_Thread_Mutex, INFINITE);
|
||||
m_ClearSendBuf();
|
||||
m_iBytesToSend=sizeof(HandShaking);
|
||||
memcpy(m_SendBuf,HandShaking,m_iBytesToSend);
|
||||
m_SendBuf[m_iBytesToSend-1]=m_ClientIPAddress.S_un.S_un_b.s_b4;
|
||||
return SendBuffer();
|
||||
}
|
||||
//================================================================
|
||||
TCPIP_RETURN_CODE CSo7_TCPIP::SendBuffer()
|
||||
{
|
||||
int errorCode, numBytesSent;
|
||||
bool bInfinite=true;
|
||||
while(bInfinite)
|
||||
{
|
||||
numBytesSent = send(m_Socket, &(m_SendBuf[m_iBytesSent]), m_iBytesToSend-m_iBytesSent, 0);
|
||||
if(numBytesSent == SOCKET_ERROR)
|
||||
{
|
||||
errorCode = WSAGetLastError();
|
||||
if(errorCode == WSAEWOULDBLOCK)
|
||||
{
|
||||
return (TCPIP_RETURN_CODE)errorCode; //Should get a FD_WRITE event if this happens. Send the rest from there.
|
||||
}
|
||||
return (TCPIP_RETURN_CODE)errorCode; //should check for other errors here, and set an error code or something.
|
||||
}
|
||||
else
|
||||
{
|
||||
m_iBytesSent += numBytesSent;
|
||||
}
|
||||
if(m_iBytesSent>=m_iBytesToSend)
|
||||
{
|
||||
m_iBytesToSend = 0;
|
||||
m_iBytesSent = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return TCPIP_CONNECT_OK;
|
||||
m_TCPIPBuf[CH_SEND]._size=sizeof(HandShaking);
|
||||
memcpy(m_TCPIPBuf[CH_SEND]._buffer,HandShaking,m_TCPIPBuf[CH_SEND]._size);
|
||||
m_TCPIPBuf[CH_SEND]._buffer[m_TCPIPBuf[CH_SEND]._size-1]=m_ClientIPAddress.S_un.S_un_b.s_b4;
|
||||
m_TCPIPBuf[CH_SEND]._save_send_cmd=TCPIP_CMD_HANDSHAKING;
|
||||
m_Thread_State=TCPIP_THREAD_RUNNING;
|
||||
_do_single_threaded_tcpip_comm();
|
||||
ReleaseMutex(m_Thread_Mutex);
|
||||
return m_TCPIPData.s_status._SendReturnCode;
|
||||
}
|
||||
|
||||
//================================================================
|
||||
@@ -238,10 +320,46 @@ void CSo7_TCPIP::m_ClearSendBuf(void)
|
||||
int i;
|
||||
for(i=0;i<m_iSendMaxBufSize;i++)
|
||||
{
|
||||
m_SendBuf[i] = 0x0;
|
||||
m_TCPIPBuf[CH_SEND]._buffer[i] = 0x0;
|
||||
}
|
||||
m_TCPIPBuf[CH_SEND]._CompletedSize=0;
|
||||
m_TCPIPBuf[CH_SEND]._size=0;
|
||||
m_TCPIPBuf[CH_RECV]._CompletedSize=0;
|
||||
m_TCPIPBuf[CH_RECV]._size=0;
|
||||
}
|
||||
//================================================================
|
||||
void CSo7_TCPIP::SendBuffer()
|
||||
{
|
||||
int errorCode, numBytesSent;
|
||||
bool bInfinite=true;
|
||||
while(bInfinite)
|
||||
{
|
||||
numBytesSent = send(m_Socket, &(m_TCPIPBuf[CH_SEND]._buffer[m_TCPIPBuf[CH_SEND]._CompletedSize]), m_TCPIPBuf[CH_SEND]._size-m_TCPIPBuf[CH_SEND]._CompletedSize, 0);
|
||||
if(numBytesSent == SOCKET_ERROR)
|
||||
{
|
||||
errorCode = WSAGetLastError();
|
||||
if(errorCode == WSAEWOULDBLOCK)
|
||||
{
|
||||
m_TCPIPData.s_status._SendReturnCode=(TCPIP_RETURN_CODE)errorCode;
|
||||
m_TCPIPBuf[CH_SEND]._hProtoPending=FALSE;
|
||||
return; //Should get a FD_WRITE event if this happens. Send the rest from there.
|
||||
}
|
||||
m_TCPIPData.s_status._SendReturnCode=(TCPIP_RETURN_CODE)errorCode;
|
||||
m_TCPIPBuf[CH_SEND]._hProtoPending=FALSE;
|
||||
return; //should check for other errors here, and set an error code or something.
|
||||
}
|
||||
else
|
||||
{
|
||||
m_TCPIPBuf[CH_SEND]._CompletedSize += numBytesSent;
|
||||
}
|
||||
if(m_TCPIPBuf[CH_SEND]._CompletedSize>=m_TCPIPBuf[CH_SEND]._size)
|
||||
{
|
||||
m_TCPIPBuf[CH_SEND]._size = 0;
|
||||
m_TCPIPBuf[CH_SEND]._CompletedSize = 0;
|
||||
m_TCPIPBuf[CH_SEND]._hProtoPending=FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_iBytesSent=0;
|
||||
m_iBytesToSend=0;
|
||||
}
|
||||
|
||||
//================================================================
|
||||
@@ -249,16 +367,17 @@ void CSo7_TCPIP::m_ProcessSocketWriteEvent(void)
|
||||
{
|
||||
int errorCode, numBytesSent;
|
||||
SOCKET s = m_Socket;
|
||||
if(m_iBytesToSend <= 0)
|
||||
if(m_TCPIPBuf[CH_SEND]._size <= 0)
|
||||
return;
|
||||
char* sendBuf = m_SendBuf;
|
||||
char* sendBuf = m_TCPIPBuf[CH_SEND]._buffer;
|
||||
|
||||
while(1)
|
||||
{
|
||||
numBytesSent = send(s, &(sendBuf[m_iBytesSent]), m_iBytesToSend-m_iBytesSent, 0);
|
||||
numBytesSent = send(s, &(sendBuf[m_TCPIPBuf[CH_SEND]._CompletedSize]), m_TCPIPBuf[CH_SEND]._size-m_TCPIPBuf[CH_SEND]._CompletedSize, 0);
|
||||
if(numBytesSent == SOCKET_ERROR)
|
||||
{
|
||||
errorCode = WSAGetLastError();
|
||||
m_TCPIPData.s_status._SendReturnCode=(TCPIP_RETURN_CODE)errorCode;
|
||||
if(errorCode == WSAEWOULDBLOCK)
|
||||
{
|
||||
return; //Should get a FD_WRITE event if this happens. Send the rest from there.
|
||||
@@ -271,11 +390,11 @@ void CSo7_TCPIP::m_ProcessSocketWriteEvent(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
m_iBytesSent += numBytesSent;
|
||||
m_TCPIPBuf[CH_SEND]._CompletedSize += numBytesSent;
|
||||
}
|
||||
if(m_iBytesSent>=m_iBytesToSend)
|
||||
if(m_TCPIPBuf[CH_SEND]._CompletedSize>=m_TCPIPBuf[CH_SEND]._size)
|
||||
{
|
||||
m_iBytesSent = 0;
|
||||
m_TCPIPBuf[CH_SEND]._CompletedSize = 0;
|
||||
m_ClearSendBuf();
|
||||
break;
|
||||
}
|
||||
@@ -286,10 +405,11 @@ void CSo7_TCPIP::m_ProcessSocketReadEvent(SOCKET s)
|
||||
{
|
||||
int bytesReceived=0;
|
||||
int errorCode;
|
||||
bytesReceived = recv(s, &(m_ReceiveBuf[0]), m_iReceiveMaxBufSize, 0);
|
||||
bytesReceived = recv(s, &(m_TCPIPBuf[CH_RECV]._buffer[0]), m_iReceiveMaxBufSize, 0);
|
||||
if(bytesReceived == SOCKET_ERROR)
|
||||
{
|
||||
errorCode = WSAGetLastError();
|
||||
m_TCPIPData.s_status._RecvReturnCode=(TCPIP_RETURN_CODE)errorCode;
|
||||
if (errorCode == WSAEWOULDBLOCK)
|
||||
{
|
||||
//have to wait for the next receive event
|
||||
@@ -301,11 +421,23 @@ void CSo7_TCPIP::m_ProcessSocketReadEvent(SOCKET s)
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_iBytesReceived += bytesReceived;
|
||||
if(m_iBytesReceived < sizeof(unsigned long) + sizeof(char))
|
||||
{//like this will ever happen... Have to wait for the next receive event
|
||||
return;
|
||||
m_TCPIPBuf[CH_RECV]._CompletedSize += bytesReceived;
|
||||
m_TCPIPBuf[CH_RECV]._size = m_TCPIPBuf[CH_RECV]._CompletedSize;
|
||||
switch (m_TCPIPBuf[CH_SEND]._save_send_cmd)
|
||||
{
|
||||
case TCPIP_CMD_HANDSHAKING:
|
||||
if (m_TCPIPBuf[CH_RECV]._CompletedSize==24)
|
||||
{
|
||||
m_TCPIPData.s_status._handshaking=true;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_TCPIPData.s_status._handshaking=false;
|
||||
}
|
||||
break;
|
||||
default:break;
|
||||
}
|
||||
m_iBytesToReceive = m_iBytesReceived;
|
||||
m_iBytesReceived = 0;
|
||||
m_TCPIPBuf[CH_RECV]._CompletedSize = 0;
|
||||
m_TCPIPBuf[CH_RECV]._hProtoPending=FALSE;
|
||||
::PostMessage(m_hMsgWnd,WM_TCPIP_RECV_DATA,m_TCPIPBuf[CH_SEND]._save_send_cmd,m_TCPIPData.s_status._RecvReturnCode);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user