www.pudn.com > using IOCP.zip > client_0.cpp


 
// Written by Oz Ben Eliezer 
// o_be@hotmail.com 
// September, 2000 
 
#define _WIN32_WINNT 0x0500 
 
#include  
#include  
 
#include "client_0.h" 
#include "commands.h" 
 
int CClient_0::ProcessPacket(tagPacket *p) 
{ 
	// Print the contents of the buffer 
	printf("\nData: %s", p->buffer); 
 
	// Write buffer back! 
	Write(p); 
 
	if (0 == strcmp(p->buffer, "disconnect")) 
		return CMD_DISCONNECT; 
	else if (0 == strcmp(p->buffer, "quit")) 
		return CMD_SHUTDOWN; 
	else 
		return CMD_DO_NOTHING; 
} 
 
void CClient_0::CreateInvalidPacket(tagPacket *p) 
{ 
	p->nLength = -1; 
	if (p->buffer) 
		delete [] p->buffer; 
	p->buffer = NULL; 
} 
 
void CClient_0::Maintenance() 
{ 
	/* 
		Check if client is an abuser. 
		Abusing clients are: 
		1. Clients that connect without sending data, thus not allowing  
		   AcceptEx to return. 
		2. Clients that connect, send something, and remain connected 
		   for too long. 
 
		(there are other kinds of abusive clients, but only these two kinds 
		need to be looked for at a periodic manner.) 
	*/ 
 
	int nSeconds = 0; 
	int nBytes; 
 
	nBytes = sizeof(nSeconds); 
 
	// See if client has attempted to connect without sending data. 
	if (nState == STATE_ACCEPTING) 
	{ 
		if (0 == getsockopt(s,  
			SOL_SOCKET,  
			SO_CONNECT_TIME,  
			(char *)&nSeconds,  
			&nBytes)) 
		{ 
			// Client has been connected for nSeconds so far 
			if (nSeconds != -1) 
			{ 
				if (nSeconds > MAX_CONNECT_SECONDS) 
				{ 
					printf("\nMaintenance is shutting inactive client #%d down.", nSpecificID); 
					Close(); 
					Init(); 
				} 
			} 
		} 
		else 
		{ 
			printf("\nMaintenance error #1 occured: %d", GetLastError()); 
		} 
	} 
	else if (nState == STATE_CONNECTED) 
	{ 
		if (SecondsElapsed() > MAX_SILENT_SECONDS) 
		{ 
			printf("\nMaintenance is shutting inactive client #%d down.", nSpecificID); 
			Close(); 
			Init(); 
		} 
	} 
}