⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp.cpp

📁 站长以前用Brew写的五子棋程序
💻 CPP
字号:
#include "AEEAppGen.h"          // Applet interface definitions
#include "AEEShell.h"           // Shell interface definitions
#include "AEEGraphics.h"
#include "AEENet.h"
#include "aeestdlib.h"

#include "five.h"
#include "event.h"
#include "tcp.h"
#include "error.h"
#include "util.h"

void CBF_DNSQuery(void *pdata);
void CBF_Connect(void *pdata, int nError);
void CBF_Read(void *pdata);
void CBF_Write(void *pdata);

CSocket::CSocket()
{
	m_pMgr =NULL;
	m_pSocket =NULL;
	m_f_connect =0;
	m_pRecvBuf =NULL;
	m_pSendBuf =NULL;
	m_pData =NULL;
}

CSocket::~CSocket()
{
	close();

	if(m_pMgr)
	{
		INETMGR_Release(m_pMgr);
		m_pMgr =NULL;
	}
	if(m_pRecvBuf)
	{
		FREE(m_pRecvBuf);
		m_pRecvBuf =NULL;
	}
	if(m_pSendBuf)
	{
		FREE(m_pSendBuf);
		m_pSendBuf =NULL;
	}
}

int CSocket::Init(void *pdata)
{
	m_pData =pdata;

	if (ISHELL_CreateInstance( ((APP_DATA *)pdata)->a.m_pIShell, AEECLSID_NET, (void**)( &m_pMgr ) ) != SUCCESS)
		return -1;
	if((m_pSocket =INETMGR_OpenSocket(m_pMgr, AEE_SOCK_STREAM)) ==NULL)
	{
		INETMGR_Release(m_pMgr);
		m_pMgr =NULL;
		return -1;
	}

	return 0;
}

int CSocket::connect(char *host, int port)
{
	STRCPY(m_hostname, host);
	m_remote_port =port;

	CALLBACK_Init(&m_cb_dns,CBF_DNSQuery, this);
	INETMGR_GetHostByName(m_pMgr, &m_dns_result, host, &m_cb_dns);

	OnEventNetDNSQuerying(this);

	return 0;
}

int CSocket::recv(int len, int timeout)
{
	if(m_f_connect ==false) return ENET_NOTCONNECT;
	
	m_len_recved =0;
	m_len_to_recv =len;
	m_timeout_recv =timeout;

	if(len <0)
		len =MAX_PACKET_SIZE;
	if(m_pRecvBuf !=NULL)
		FREE(m_pRecvBuf);
	if((m_pRecvBuf =(char *)MALLOC(len)) ==NULL)
		return EMEM_ALLOC;

	m_t1_recv =GETTIMEMS();

	CBF_Read(this);

	return m_len_recved;
}

int CSocket::send(char *buf, int len, int timeout)
{
	if(m_f_connect ==false) return ENET_NOTCONNECT;
	
	m_len_sent =0;
	m_len_to_send =len;
	m_timeout_send =timeout;

	if(len <0)
		len =MAX_PACKET_SIZE;
	if(m_pSendBuf !=NULL)
		FREE(m_pSendBuf);
	if((m_pSendBuf =(char *)MALLOC(len)) ==NULL)
		return EMEM_ALLOC;
	MEMCPY(m_pSendBuf, buf, len);

	m_t1_send =GETTIMEMS();

	CBF_Write(this);

	return m_len_sent;
}

int CSocket::close()
{

	if(m_cb_dns.pfnCancel !=NULL)
		CALLBACK_Cancel(&m_cb_dns);
	if(m_pSocket)
	{
		ISOCKET_Release(m_pSocket);
		m_pSocket =NULL;
	}
	m_f_connect =false;

	return 0;
}

void CBF_DNSQuery(void *pdata)
{
	CSocket *psock =(CSocket *)pdata;
	int nErr =psock->m_dns_result.nResult;
	if(nErr > AEEDNSMAXADDRS)
	{
		OnEventDNSQueryFailed(psock);
		return;
	}
	OnEventDNSQuerySuccess(psock);
}

void CBF_Connect(void *pdata, int nError)
{
	CSocket *psock =(CSocket *)pdata;

	if(nError ==AEE_NET_SUCCESS || nError==AEE_NET_EISCONN)
	{
		psock->m_f_connect =true;
		OnEventNetConnectSuccess(psock);
	}
	else
	{
		psock->m_f_connect =false;
		OnEventNetConnectFailed(psock);
	}
}

void CBF_Read(void *pdata)  
{
	CSocket *psocket = (CSocket *)pdata;

	if(!psocket->m_f_connect) return;

	int  ret;

	if(psocket->m_len_to_recv >0)
		ret = ISOCKET_Read(psocket->m_pSocket, &psocket->m_pRecvBuf[psocket->m_len_recved], psocket->m_len_to_recv-psocket->m_len_recved);
	else
		ret = ISOCKET_Read(psocket->m_pSocket, psocket->m_pRecvBuf, MAX_PACKET_SIZE);

	switch(ret)
	{
	case AEE_NET_WOULDBLOCK:
	FIVE_ShowStat((APP_DATA *)psocket->m_pData, "would block");
		break;
	case AEE_NET_ERROR:
	FIVE_ShowStat((APP_DATA *)psocket->m_pData, "failed");
		psocket->close();
		OnEventNetRecvFailed(psocket, ENET_OTHER);
		return;
	case 0:
	FIVE_ShowStat((APP_DATA *)psocket->m_pData, "closed");
		psocket->close();
		OnEventNetRecvFailed(psocket, ENET_CONNECTRESET);
		return;
	default:
		psocket->m_len_recved +=ret;
		break;
	}
	uint32 t2 =GETTIMEMS();
	//FIVE_ShowStat((APP_DATA *)psocket->m_pData, "0001");
	if(psocket->m_timeout_recv >=0 && (int)(t2-psocket->m_t1_recv) >=psocket->m_timeout_recv*1000)
	{
	FIVE_ShowStat((APP_DATA *)psocket->m_pData, "0002");
		OnEventNetRecvFailed(psocket, ENET_TIMEOUT);
		return;  // timeout
	}
	if(psocket->m_len_to_recv >0)
	{
	FIVE_ShowStat((APP_DATA *)psocket->m_pData, "0003");
		if(psocket->m_len_recved <psocket->m_len_to_recv)
			ISOCKET_Readable(psocket->m_pSocket, CBF_Read, psocket);   // continue to recv
		else if(psocket->m_len_recved ==psocket->m_len_to_recv)
			OnEventNetRecvSuccess(psocket);    // not continue to recv
		return;
	}
	else if(psocket->m_len_recved >0)
	{
		OnEventNetRecvSuccess(psocket);
		FIVE_ShowStat((APP_DATA *)psocket->m_pData, "0004");
	}
	ISOCKET_Readable(psocket->m_pSocket, CBF_Read, psocket);  // m_len_to_recv <0, continue to recv
}

void CBF_Write(void *pdata)  
{
	CSocket *psocket = (CSocket *)pdata;

	if(!psocket->m_f_connect) return;

	int  ret;


	if(psocket->m_len_to_send >0)
		ret = ISOCKET_Write(psocket->m_pSocket, (unsigned char *)&psocket->m_pSendBuf[psocket->m_len_sent], psocket->m_len_to_send-psocket->m_len_sent);
	else
		return;

	switch(ret)
	{
	case AEE_NET_WOULDBLOCK:
		break;
	case AEE_NET_ERROR:
		psocket->close();
		OnEventNetSendFailed(psocket, ENET_OTHER);
		return;
	case 0:
		psocket->close();
		OnEventNetSendFailed(psocket, ENET_CONNECTRESET);
		return;
	default:
		psocket->m_len_sent +=ret;
		break;
	}
	uint32 t2 =GETTIMEMS();
	if(psocket->m_timeout_send >=0 && (int)(t2-psocket->m_t1_send) >=psocket->m_timeout_send)
	{
		OnEventNetSendFailed(psocket, ENET_TIMEOUT);
		return;  // timeout
	}
	if(psocket->m_len_to_send >0)
	{
		if(psocket->m_len_sent <psocket->m_len_to_send)
			ISOCKET_Writeable(psocket->m_pSocket, CBF_Write, psocket);   // continue to send
		else if(psocket->m_len_sent ==psocket->m_len_to_send)
			OnEventNetSendSuccess(psocket);    // not continue to send
		return;
	}
}

int OnEventDNSQueryFailed(CSocket *psock)
{
	OnEventNetConnectFailed(psock);

	return 0;
}

int OnEventDNSQuerySuccess(CSocket *psock)
{
	if(ISOCKET_Connect(psock->m_pSocket, psock->m_dns_result.addrs[0], AEE_htons(psock->m_remote_port), CBF_Connect, psock) ==AEE_NET_ERROR)
	{
		OnEventNetConnectFailed(psock);
		return -1;
	}

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -