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);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,32 @@
|
||||
#include <afxwin.h>
|
||||
#include <afxcmn.h>
|
||||
#include <Winsock2.h>
|
||||
#define WM_SOCKET WM_USER + 0x1
|
||||
#define WM_SOCKET WM_USER + 0x060
|
||||
#define WM_TCPIP_RECV_DATA WM_USER + 0x061
|
||||
|
||||
#define lChannelSize 2
|
||||
#define CH_SEND 0
|
||||
#define CH_RECV 1
|
||||
|
||||
#define TCPIP_MAX_BUFF_SIZE 1024
|
||||
#define TCPIP_MAX_DAT_SIZE 1024
|
||||
|
||||
#define TCPIP_THREAD_RUNNING 0
|
||||
#define TCPIP_THREAD_PAUSED 1
|
||||
#define TCPIP_THREAD_EXIT -1
|
||||
|
||||
#pragma pack(push)
|
||||
#pragma pack(1)
|
||||
|
||||
enum TCPIP_CMD
|
||||
{
|
||||
TCPIP_CMD_NONE=0,
|
||||
TCPIP_CMD_HANDSHAKING,
|
||||
TCPIP_CMD_READ_DATA,
|
||||
TCPIP_CMD_WRITE_DATA,
|
||||
|
||||
TCPIP_CMD_TATAL
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
TCPIP_CONNECT_OK=0,
|
||||
@@ -20,8 +45,38 @@ typedef enum {
|
||||
TCPIP_INVAILD_IP_ADDRESS,
|
||||
TCPIP_INVAILD_PORT_NUMBER,
|
||||
TCPIP_CONNECT_SERVER_FAILED,
|
||||
} TCPIP_RETURN_CODE;
|
||||
TCPIP_CONNECT_STATUS_UNKNOWN
|
||||
|
||||
} TCPIP_RETURN_CODE;
|
||||
//======================
|
||||
struct struct_so7_tcpip_buff
|
||||
{
|
||||
BYTE _save_send_cmd;
|
||||
BYTE _save_send_cmd0;
|
||||
BYTE _save_send_cmd1;
|
||||
BYTE _save_send_cmd2;
|
||||
char *_buffer; // MAX_BUFF_SIZE
|
||||
int _size;
|
||||
int _CompletedSize;
|
||||
BOOL _hProtoPending;
|
||||
HANDLE _event;
|
||||
};
|
||||
struct struct_so7_tcpip_data
|
||||
{ // g_machine structure
|
||||
struct s_recv_data
|
||||
{
|
||||
int _type;
|
||||
int _DataSize;
|
||||
double *_dbBuff;
|
||||
} s_recv_data;
|
||||
struct s_status
|
||||
{
|
||||
TCPIP_RETURN_CODE _SendReturnCode;
|
||||
TCPIP_RETURN_CODE _RecvReturnCode;
|
||||
bool _handshaking;
|
||||
} s_status;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
//======================================================================================
|
||||
class CSo7_TCPIP
|
||||
{
|
||||
@@ -29,24 +84,27 @@ public:
|
||||
CSo7_TCPIP();
|
||||
virtual ~CSo7_TCPIP();
|
||||
|
||||
static struct_so7_tcpip_buff m_TCPIPBuf[lChannelSize];
|
||||
static struct_so7_tcpip_data m_TCPIPData;
|
||||
|
||||
static unsigned __stdcall m_Thread(LPVOID pThis) ;
|
||||
static HANDLE m_Thread_Id;
|
||||
static HANDLE m_Thread_Mutex;
|
||||
static int m_Thread_State;
|
||||
|
||||
TCPIP_RETURN_CODE Connect(const HWND& _hWnd,const in_addr& _IPAddress,const u_short& _nPortNumber);
|
||||
TCPIP_RETURN_CODE Send(int _Addr,int _Data);
|
||||
TCPIP_RETURN_CODE Recv(int _Addr,int& _Data,bool _bWait);
|
||||
TCPIP_RETURN_CODE DisConnect();
|
||||
TCPIP_RETURN_CODE GetHostIPAddr(in_addr& _IPAddress);
|
||||
TCPIP_RETURN_CODE Handshaking();
|
||||
|
||||
TCPIP_RETURN_CODE Handshaking();
|
||||
|
||||
LRESULT OnSocket(WPARAM wParam, LPARAM lParam);
|
||||
private:
|
||||
private:
|
||||
HWND m_hMsgWnd;
|
||||
|
||||
char *m_ReceiveBuf;
|
||||
char *m_SendBuf;
|
||||
double *m_dReceiveDBuf;
|
||||
int m_iReceiveDBufSize;
|
||||
int m_iBytesToReceive,m_iBytesReceived;
|
||||
int m_iBytesToSend,m_iBytesSent;
|
||||
static int m_iReceiveMaxBufSize;
|
||||
static int m_iSendMaxBufSize;
|
||||
|
||||
@@ -55,9 +113,14 @@ private:
|
||||
static in_addr m_ClientIPAddress;
|
||||
static u_short m_iServerPortNumber;
|
||||
|
||||
TCPIP_RETURN_CODE SendBuffer();
|
||||
void SendBuffer();
|
||||
void m_ClearSendBuf();
|
||||
|
||||
void Create_Thread();
|
||||
void Exit_Thread();
|
||||
void _do_single_threaded_tcpip_comm(bool _bWaitForRsponse=false);
|
||||
|
||||
|
||||
int Init_Winsock();
|
||||
void m_ProcessSocketWriteEvent();
|
||||
void m_ProcessSocketReadEvent(SOCKET s);
|
||||
|
||||
@@ -13,8 +13,8 @@ extern CSo7_TCPIP* g_pSo7_TCPIP;
|
||||
|
||||
IMPLEMENT_DYNAMIC(CSo7_Util_PLC_TCPIP, CDialog)
|
||||
|
||||
CSo7_Util_PLC_TCPIP::CSo7_Util_PLC_TCPIP(CWnd* pParent /*=NULL*/)
|
||||
: CDialog(CSo7_Util_PLC_TCPIP::IDD, pParent)
|
||||
CSo7_Util_PLC_TCPIP::CSo7_Util_PLC_TCPIP(CWnd* pParent /*=NULL*/)
|
||||
: CDialog(CSo7_Util_PLC_TCPIP::IDD, pParent)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -25,9 +25,9 @@ CSo7_Util_PLC_TCPIP::~CSo7_Util_PLC_TCPIP()
|
||||
|
||||
void CSo7_Util_PLC_TCPIP::DoDataExchange(CDataExchange* pDX)
|
||||
{
|
||||
CDialog::DoDataExchange(pDX);
|
||||
CDialog::DoDataExchange(pDX);
|
||||
DDX_Control(pDX, IDC_EDIT_MSG, m_edMSG);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ BEGIN_MESSAGE_MAP(CSo7_Util_PLC_TCPIP, CDialog)
|
||||
ON_BN_CLICKED(IDC_BUTTON_PLC_TCPIP_CLEAR_MSG, &CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipClearMsg)
|
||||
ON_BN_CLICKED(IDCANCEL, &CSo7_Util_PLC_TCPIP::OnBnClickedCancel)
|
||||
ON_MESSAGE(WM_SOCKET,&CSo7_Util_PLC_TCPIP::OnSocket)
|
||||
ON_MESSAGE(WM_TCPIP_RECV_DATA,&CSo7_Util_PLC_TCPIP::OnTCPIPRecv)
|
||||
ON_BN_CLICKED(IDCANCEL, &CSo7_Util_PLC_TCPIP::OnBnClickedCancel)
|
||||
ON_BN_CLICKED(IDC_BUTTON_PLC_TCPIP_HANDSHAKING, &CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipHandshaking)
|
||||
END_MESSAGE_MAP()
|
||||
@@ -52,10 +53,10 @@ void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipConnect()
|
||||
in_addr IPAddress;
|
||||
IPAddress.S_un.S_addr=inet_addr("192.168.0.5");
|
||||
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->Connect(m_hWnd,IPAddress,9600);
|
||||
m_csMSG.Format(_T("Server:192.168.0.5[9600] Connect return code:%d."),rCode);
|
||||
m_csMSG.Format(_T("Server:192.168.0.5[9600] Connect ReturnCode:%d."),rCode);
|
||||
OutputWithScroll(m_csMSG,m_edMSG);
|
||||
rCode=g_pSo7_TCPIP->GetHostIPAddr(IPAddress);
|
||||
m_csMSG.Format(_T("Client:%d.%d.%d.%d return code:%d."),
|
||||
m_csMSG.Format(_T("Client:%d.%d.%d.%d ReturnCode:%d."),
|
||||
IPAddress.S_un.S_un_b.s_b1,IPAddress.S_un.S_un_b.s_b2,
|
||||
IPAddress.S_un.S_un_b.s_b3,IPAddress.S_un.S_un_b.s_b4,rCode);
|
||||
OutputWithScroll(m_csMSG,m_edMSG);
|
||||
@@ -65,7 +66,7 @@ void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipConnect()
|
||||
void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipDisconnect()
|
||||
{
|
||||
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->DisConnect();
|
||||
m_csMSG.Format(_T("DisConnect return code:%d."),rCode);
|
||||
m_csMSG.Format(_T("[DisConnect] ReturnCode:%d."),rCode);
|
||||
OutputWithScroll(m_csMSG,m_edMSG);
|
||||
}
|
||||
|
||||
@@ -78,14 +79,14 @@ void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipRead()
|
||||
void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipHandshaking()
|
||||
{
|
||||
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->Handshaking();
|
||||
m_csMSG.Format(_T("Handshaking return code:%d."),rCode);
|
||||
m_csMSG.Format(_T("[Handshaking] Send ReturnCode:%d."),rCode);
|
||||
OutputWithScroll(m_csMSG,m_edMSG);
|
||||
}
|
||||
|
||||
void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipWrite()
|
||||
{
|
||||
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->Send(1001,0);
|
||||
m_csMSG.Format(_T("Write return code:%d."),rCode);
|
||||
m_csMSG.Format(_T("Write ReturnCode:%d."),rCode);
|
||||
OutputWithScroll(m_csMSG,m_edMSG);
|
||||
}
|
||||
|
||||
@@ -131,3 +132,42 @@ LRESULT CSo7_Util_PLC_TCPIP::OnSocket(WPARAM w, LPARAM p)
|
||||
|
||||
return( lResult );
|
||||
}
|
||||
LRESULT CSo7_Util_PLC_TCPIP::OnTCPIPRecv(WPARAM w, LPARAM p)
|
||||
{
|
||||
LRESULT lResult=0;
|
||||
switch(w)
|
||||
{
|
||||
case TCPIP_CMD_HANDSHAKING:
|
||||
{
|
||||
if (g_pSo7_TCPIP)
|
||||
{
|
||||
m_csMSG.Format(_T(">> Handshaking status:%d."),g_pSo7_TCPIP->m_TCPIPData.s_status._handshaking);
|
||||
OutputWithScroll(m_csMSG,m_edMSG);
|
||||
}
|
||||
m_csMSG=_T("[Handshaking]");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
m_csMSG=_T("[Unknown]");
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (g_pSo7_TCPIP)
|
||||
{
|
||||
CString csTmp;
|
||||
csTmp.Format(_T("ReturnCode:%d;"),p);
|
||||
m_csMSG+=csTmp;
|
||||
csTmp.Format(_T("RecvSize:%d;"),g_pSo7_TCPIP->m_TCPIPBuf[CH_RECV]._size);
|
||||
m_csMSG+=csTmp;
|
||||
m_csMSG+=_T("RecvData:");
|
||||
for (int i=0;i<g_pSo7_TCPIP->m_TCPIPBuf[CH_RECV]._size;i++)
|
||||
{
|
||||
csTmp.Format(_T("0x%X "),g_pSo7_TCPIP->m_TCPIPBuf[CH_RECV]._buffer[i]);
|
||||
m_csMSG+=csTmp;
|
||||
}
|
||||
}
|
||||
m_csMSG+=_T(".");
|
||||
OutputWithScroll(m_csMSG,m_edMSG);
|
||||
return( lResult );
|
||||
}
|
||||
|
||||
@@ -28,5 +28,6 @@ public:
|
||||
afx_msg void OnBnClickedButtonPlcTcpipClearMsg();
|
||||
afx_msg void OnBnClickedCancel();
|
||||
afx_msg LRESULT OnSocket(WPARAM w, LPARAM p);
|
||||
afx_msg LRESULT OnTCPIPRecv(WPARAM w, LPARAM p);
|
||||
afx_msg void OnBnClickedButtonPlcTcpipHandshaking();
|
||||
};
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user