www.pudn.com > 221315556.rar > ClientSocket.cpp


#include "clientsocket.h" 
#include  
DWORD	CClientSocketThread::Proc( LPVOID lpParam ) 
{ 
	CClientSocket * pCS = (CClientSocket *)lpParam; 
	pCS->CleanBuffer(); 
	fd_set	fdset; 
	timeval	tv; 
	int ierr = 0; 
	tv.tv_sec = 0; 
	tv.tv_usec = 50; 
	//FD_ZERO( &fdset ); 
	printf( "进入线程!\n" ); 
	while( ThreadActive()) 
	{ 
		Sleep( 10 ); 
		FD_ZERO( &fdset ); 
		FD_SET( pCS->m_sClient, &fdset ); 
		select( 0, &fdset, 0, 0, &tv ); 
		if( FD_ISSET( pCS->m_sClient, &fdset ) ) 
		{ 
			ierr = recv( pCS->m_sClient, m_recvBuffer, 1024, 0 ); 
			if( ierr == SOCKET_ERROR || ierr == 0) 
			{ 
				pCS->Disconnect(); 
				//MessageBox( 0, "退出线程!\n", 0, 0 ); 
				printf( "接收数据出错,错误号是%d\n", WSAGetLastError()); 
				return 1; 
			} 
			pCS->SaveMsg( m_recvBuffer, ierr ); 
		} 
 
	} 
	//MessageBox( 0, "退出线程!\n", 0, 0 ); 
	return 0; 
} 
CClientSocket::CClientSocket(void) 
{ 
	m_Mode = (GAMECLIENTMODE | THREAD_SOCKET); 
	m_pSSMsgQueue = 0; 
	m_pClientThread = new CClientSocketThread; 
	m_pMsgQueue = new CSimpleMsgQueue; 
	m_sClient = 0; 
} 
CClientSocket::CClientSocket(int mode) 
{ 
	m_pMsgQueue = 0; 
	m_pSSMsgQueue = 0; 
	m_pClientThread = 0; 
	m_Mode = mode; 
	if( m_Mode & GAMESERVERMODE) 
	{ 
		m_pSSMsgQueue = new CSSMsgQueue2; 
		printf( "User set server mode!\n"); 
	} 
	else 
	{ 
		m_pMsgQueue = new CSimpleMsgQueue; 
		printf( "User set client mode!\n"); 
	} 
	if( m_Mode & MANUAL_SOCKET) 
	{ 
 
	} 
	else 
	{ 
		m_pClientThread = new CClientSocketThread; 
	} 
	m_sClient = 0; 
 
} 
 
CClientSocket::~CClientSocket(void) 
{ 
	if( m_pClientThread != NULL ) 
	{ 
		m_pClientThread->SafeTerminate( 20 ); 
		delete m_pClientThread; 
	} 
	if( m_pMsgQueue != NULL ) 
		delete m_pMsgQueue; 
	if( m_pSSMsgQueue != NULL ) 
		delete m_pSSMsgQueue; 
	if( m_sClient != 0 ) 
		Disconnect(); 
} 
 
// 连接服务器 
BOOL CClientSocket::Connect(const char * IpAddress, const unsigned int Port) 
{ 
	DWORD	dwID = 0; 
	m_sClient = m_socket.OpenConnection( IpAddress, Port ); 
	if( m_sClient == 0 ) 
		return 0; 
	if( m_socket.SetNoBlock( m_sClient ) == SOCKET_ERROR ) 
	{ 
		m_socket.CloseConnection( m_sClient ); 
		return 0; 
	} 
	if( m_Mode & MANUAL_SOCKET ) 
	{ 
		printf( "User set to manual socket\n"); 
	} 
	else 
	{ 
		printf( "User set to thread socket\n"); 
		if( m_pClientThread->Create( 1024 * 128, (LPVOID) this, &dwID  )==NULL) 
			printf( "thread create null\n"); 
	} 
	return 1; 
} 
 
// 断开连接 
BOOL CClientSocket::Disconnect(void) 
{ 
	if( m_sClient <= 0 ) 
		return FALSE; 
	m_socket.CloseConnection( m_sClient ); 
	m_sClient = 0; 
	Sleep(100); 
	return TRUE; 
} 
 
// 清除消息队列存储区 
BOOL CClientSocket::CleanBuffer(void) 
{ 
	(m_Mode&GAMESERVERMODE?m_pSSMsgQueue->Clean():m_pMsgQueue->Clean()); 
	return 1; 
} 
 
// 取得一个消息 
BOOL CClientSocket::GetMsg(SCMSG* pmsg) 
{ 
	return m_pMsgQueue->GetMsg( pmsg); 
} 
 
// 发送一个消息 
BOOL CClientSocket::SendMsg(SCMSG* pmsg) 
{ 
	return (m_socket.Send( m_sClient, (void*)pmsg, MSGLENGTH )==MSGLENGTH); 
} 
 
BOOL CClientSocket::IsConnected(void) 
{ 
	return ( m_sClient != 0 ); 
} 
BOOL CClientSocket::SaveMsg(CHAR * pBuffer , int nSize) 
{ 
	return (m_Mode&GAMESERVERMODE?m_pSSMsgQueue->SaveMsg(pBuffer,nSize):m_pMsgQueue->SaveMsg(pBuffer, nSize)); 
} 
BOOL CClientSocket::GetMsg(SSMSG* pmsg) 
{ 
	return m_pSSMsgQueue->GetMsg( pmsg); 
} 
 
// 发送一个消息 
BOOL CClientSocket::SendMsg(SSMSG* pmsg) 
{ 
	return (m_socket.Send( m_sClient, (void*)pmsg, SSMSGLENGTH )==SSMSGLENGTH); 
} 
 
BOOL	CClientSocket::Update() 
{ 
	fd_set	fdset; 
	timeval	tv; 
	int ierr = 0; 
	tv.tv_sec = 0; 
	tv.tv_usec = 50; 
	FD_ZERO( &fdset ); 
	FD_SET( m_sClient, &fdset ); 
	select( 0, &fdset, 0, 0, &tv ); 
	if( FD_ISSET( m_sClient, &fdset ) ) 
	{ 
		ierr = recv(m_sClient, m_recvBuffer, 1024, 0 ); 
		if( ierr == SOCKET_ERROR || ierr == 0) 
		{ 
			Disconnect(); 
			printf( "接收数据出错,错误号是%d\n", WSAGetLastError()); 
			return FALSE; 
		} 
		SaveMsg( m_recvBuffer, ierr ); 
	} 
	return TRUE; 
}