134 lines
3.2 KiB
C++
134 lines
3.2 KiB
C++
//////////////////////////////////////////////////////////////////////
|
|
#ifndef SO7_TCPIP_H_INCLUDED_
|
|
#define SO7_TCPIP_H_INCLUDED_
|
|
|
|
|
|
#if _MSC_VER > 1000
|
|
#pragma once
|
|
#endif // _MSC_VER > 1000
|
|
|
|
#include <afxwin.h>
|
|
#include <afxcmn.h>
|
|
#include <Winsock2.h>
|
|
#define WM_SOCKET WM_USER + 0x060
|
|
#define WM_TCPIP_RECV_DATA WM_USER + 0x061
|
|
#define WM_TCPIP_MSG WM_USER + 0x062
|
|
|
|
#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_D_DATA,
|
|
TCPIP_CMD_WRITE_D_DATA,
|
|
|
|
TCPIP_CMD_TATAL
|
|
};
|
|
|
|
typedef enum {
|
|
TCPIP_CONNECT_OK=0,
|
|
|
|
TCPIP_INIT_WINSOCK_ERROR,
|
|
TCPIP_INVAILD_SOCKET,
|
|
TCPIP_INVAILD_IP_ADDRESS,
|
|
TCPIP_INVAILD_PORT_NUMBER,
|
|
TCPIP_CONNECT_SERVER_FAILED,
|
|
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;
|
|
long long* _lData;
|
|
} s_recv_data;
|
|
struct s_status
|
|
{
|
|
TCPIP_RETURN_CODE _SendReturnCode;
|
|
TCPIP_RETURN_CODE _RecvReturnCode;
|
|
bool _HandshakingSucceed;
|
|
bool _WriteDataCompleted;
|
|
bool _ReadDataCompleted;
|
|
} s_status;
|
|
};
|
|
#pragma pack(pop)
|
|
//======================================================================================
|
|
class CSo7_TCPIP
|
|
{
|
|
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 DisConnect();
|
|
TCPIP_RETURN_CODE GetHostIPAddr(in_addr& _IPAddress);
|
|
TCPIP_RETURN_CODE Handshaking();
|
|
TCPIP_RETURN_CODE _Send_Cmd_Write_D_Data(const short& _Addr,short* _Data,int _DataSize);
|
|
TCPIP_RETURN_CODE _Send_Cmd_Read_D_Data(const short& _StartAddr,const short& _DataSize);
|
|
|
|
|
|
LRESULT OnSocket(WPARAM wParam, LPARAM lParam);
|
|
private:
|
|
HWND m_hMsgWnd;
|
|
|
|
double *m_dReceiveDBuf;
|
|
int m_iReceiveDBufSize;
|
|
static int m_iReceiveMaxBufSize;
|
|
static int m_iSendMaxBufSize;
|
|
|
|
static SOCKET m_Socket;
|
|
static in_addr m_SreverIPAddress;
|
|
static in_addr m_ClientIPAddress;
|
|
static u_short m_iServerPortNumber;
|
|
|
|
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);
|
|
};
|
|
|
|
#endif
|