Beispiel #1
0
int CHttpServer::runClientHandler(int nClientFD)
{

	int result;
	char pBuf[BUF_SIZE];
	void* pvBuf = pBuf;
	http_parser *parser = (http_parser*)malloc(sizeof(http_parser));
	http_parser_settings *settings = (http_parser_settings*)malloc(sizeof(http_parser_settings)); 
	size_t nparsed;
	struct message *m;

	m = (message *)malloc(sizeof(message));
	memset(m, 0, sizeof(message));
	pthread_setspecific(m_threadDatakey, m);
	
	settings->on_url = onURL;
	settings->on_header_field = onHeaderField;
	settings->on_header_value = onHeaderValue;
	settings->on_body = onBody;
	settings->on_message_begin = onMessageBegin;
	settings->on_headers_complete = onHeadersComplete;
	settings->on_message_complete = onMessageComplete;

	http_parser_init(parser, HTTP_REQUEST);
  
	while (1)
	{
		memset(pBuf, 0, sizeof(pBuf));
		result = socketrecv(nClientFD, &pvBuf);
		if (0 >= result)
		{
			nparsed = http_parser_execute(parser, settings, pBuf, 0);
			_DBG("nparsed: %lu state: %d method: %d", nparsed, parser->state, parser->method);
			socketClose(nClientFD);
			_DBG("socket close client: %d", nClientFD);
			break;
		}

		if (0 < result)
		{
			nparsed = http_parser_execute(parser, settings, pBuf, result);
			_DBG("nparsed: %lu state: %d method: %d", nparsed, parser->state, parser->method);
			if (nparsed != (size_t)result)
			{
				/* Error. Close connecttion */
				socketClose(nClientFD);
				_DBG("http_parse error!");
				break;
			}

			if (m->message_complete_cb_called)
			{
				processHttpMessage(nClientFD, m);
			}
		}
	}

	free(parser);
	free(settings);

	sendMessage(EVENT_FILTER_HTTP_SERVER, EVENT_COMMAND_THREAD_EXIT,
			threadHandler->getThreadID(), 0, NULL);

	threadHandler->threadSleep(1);
	threadHandler->threadExit();

	return 0;
}
Beispiel #2
0
int CSocketServer::runClientHandler(int nClientFD)
{
	int nFD;
	int result;
	char pBuf[BUF_SIZE];
	void* pvBuf = pBuf;
	char szTmp[16];

	/**
	 * clientSockaddr is used for UDP server send packet to client.
	 */
	struct sockaddr_in *clientSockaddr;
	clientSockaddr = new struct sockaddr_in;

	while (1)
	{
		memset(pBuf, 0, sizeof(pBuf));
		result = socketrecv(nClientFD, &pvBuf, clientSockaddr);
		if (0 >= result)
		{
			if (externalEvent.isValid() && -1 != externalEvent.m_nEventDisconnect)
			{
				sendMessage(externalEvent.m_nEventFilter, externalEvent.m_nEventDisconnect, nClientFD, 0, 0);
			}
			socketClose(nClientFD);
			_DBG("[Socket Server] socket close client: %d", nClientFD);
			break;
		}

		if (nClientFD == getSocketfd())
		{
			/**
			 * UDP server receive packet
			 */
			nFD = ntohs(clientSockaddr->sin_port);
			memset(szTmp, 0, sizeof(szTmp));
			sprintf(szTmp, "%d", nFD);
			udpClientData->setData(szTmp, *clientSockaddr);
		}
		else
		{
			nFD = nClientFD;
		}

		if (externalEvent.isValid())
		{
			sendMessage(externalEvent.m_nEventFilter, externalEvent.m_nEventRecvCommand, nFD, result, pBuf);
		}
		else
		{
			sendMessage(m_nInternalFilter, EVENT_COMMAND_SOCKET_SERVER_RECEIVE, nFD, result, pBuf);
		}
	}

	delete clientSockaddr;

	sendMessage(m_nInternalFilter, EVENT_COMMAND_THREAD_EXIT, threadHandler->getThreadID(), 0, NULL);

	threadHandler->threadSleep(1);
	threadHandler->threadExit();

	return 0;
}
void CSocketClient::runSMSSocketReceive(int nSocketFD)
{
	int result = 0;
	char szTmp[16];
	int nTotalLen = 0;
	int nBodyLen = 0;
	int nCommand = generic_nack;
	int nSequence = 0;

	CMP_PACKET cmpPacket;
	void* pHeader = &cmpPacket.cmpHeader;
	void* pBody = &cmpPacket.cmpBody;

	CMP_HEADER cmpHeader;
	void *pHeaderResp = &cmpHeader;
	int nCommandResp;

	struct sockaddr_in *clientSockaddr;
	clientSockaddr = new struct sockaddr_in;

	while ( 1 )
	{
		memset( &cmpPacket, 0, sizeof(CMP_PACKET) );
		result = socketrecv( nSocketFD, sizeof(CMP_HEADER), &pHeader, clientSockaddr );

		if ( sizeof(CMP_HEADER) == result )
		{
			nTotalLen = ntohl( cmpPacket.cmpHeader.command_length );
	/*		nCommand = ntohl( cmpPacket.cmpHeader.command_id );
			nSequence = ntohl( cmpPacket.cmpHeader.sequence_number );
			if ( enquire_link_request == nCommand )
			{
				_DBG( "[Socket Server] Receive Enquir Link Request Sequence:%d Socket FD:%d", nSequence, nSocketFD );
				memset( &cmpHeader, 0, sizeof(CMP_HEADER) );
				nCommandResp = generic_nack | nCommand;
				cmpHeader.command_id = htonl( nCommandResp );
				cmpHeader.command_status = htonl( STATUS_ROK );
				cmpHeader.sequence_number = htonl( nSequence );
				cmpHeader.command_length = htonl( sizeof(CMP_HEADER) );
				socketSend( nSocketFD, &cmpHeader, sizeof(CMP_HEADER) );
				_DBG( "[Socket Server] Send Enquir Link Response Sequence:%d Socket FD:%d", nSequence, nSocketFD );
				continue;
			}
	*/
			nBodyLen = nTotalLen - sizeof(CMP_HEADER);

			if ( 0 < nBodyLen )
			{
				result = socketrecv( nSocketFD, nBodyLen, &pBody, 0 );
				if ( result != nBodyLen )
				{
					if ( externalEvent.isValid() && -1 != externalEvent.m_nEventDisconnect )
					{
						sendMessage( externalEvent.m_nEventFilter, externalEvent.m_nEventDisconnect, nSocketFD, 0, 0 );
					}
					socketClose( nSocketFD );
					_DBG( "[Socket Client] socket client close : %d , packet length error: %d != %d", nSocketFD, nBodyLen, result );
					break;
				}
			}
		}
		else
		{
			if ( externalEvent.isValid() && -1 != externalEvent.m_nEventDisconnect )
			{
				sendMessage( externalEvent.m_nEventFilter, externalEvent.m_nEventDisconnect, nSocketFD, 0, 0 );
			}
			socketClose( nSocketFD );
			_DBG( "[Socket Client] socket client close : %d , packet header length error: %d", nSocketFD, result );
			break;
		}

		if ( 0 >= result )
		{
			if ( externalEvent.isValid() && -1 != externalEvent.m_nEventDisconnect )
			{
				sendMessage( externalEvent.m_nEventFilter, externalEvent.m_nEventDisconnect, nSocketFD, 0, 0 );
			}
			socketClose( nSocketFD );
			_DBG( "[Socket Client] socket client close: %d", nSocketFD );
			break;
		}

		if ( externalEvent.isValid() )
		{
			//	_DBG("[Socket Server] Send Message : FD=%d len=%d", nFD, result);
			ClientReceive( nSocketFD, nTotalLen, &cmpPacket );
			//sendMessage( externalEvent.m_nEventFilter, externalEvent.m_nEventRecvCommand, nSocketFD, nTotalLen, &cmpPacket );
		}
		else
		{
			sendMessage( m_nInternalFilter, EVENT_COMMAND_SOCKET_RECEIVE, nSocketFD, nTotalLen, &cmpPacket );
		}
	} // while

	sendMessage( m_nInternalFilter, EVENT_COMMAND_THREAD_EXIT, threadHandler->getThreadID(), 0, NULL );

	threadHandler->threadSleep( 1 );
	threadHandler->threadExit();
}
int CSocketServer::runCMPHandler(int nClientFD)
{
	int nFD;
	int result = 0;
	char szTmp[16];
	int nTotalLen = 0;
	int nBodyLen = 0;
	int nCommand = generic_nack;
	int nSequence = 0;

	CMP_PACKET cmpPacket;
	void* pHeader = &cmpPacket.cmpHeader;
	void* pBody;

	CMP_HEADER cmpHeader;
	//void *pHeaderResp = &cmpHeader;
	int nCommandResp;

	/**
	 * clientSockaddr is used for UDP server send packet to client.
	 */
	struct sockaddr_in *clientSockaddr;
	clientSockaddr = new struct sockaddr_in;

	if(externalEvent.isValid() && -1 != externalEvent.m_nEventConnect)
	{
		sendMessage(externalEvent.m_nEventFilter, externalEvent.m_nEventConnect, nClientFD, 0, 0);
	}

	mapClientThread[nClientFD] = threadHandler->getThreadID();

	while(1)
	{
		memset(&cmpPacket, 0, sizeof(cmpPacket));
		result = socketrecv(nClientFD, sizeof(CMP_HEADER), &pHeader, clientSockaddr);
		if(sizeof(CMP_HEADER) == result)
		{
			nTotalLen = ntohl(cmpPacket.cmpHeader.command_length);
			nCommand = ntohl(cmpPacket.cmpHeader.command_id);
			nSequence = ntohl(cmpPacket.cmpHeader.sequence_number);

			memset(&cmpHeader, 0, sizeof(CMP_HEADER));
			nCommandResp = generic_nack | nCommand;
			cmpHeader.command_id = htonl(nCommandResp);
			cmpHeader.command_status = htonl( STATUS_ROK);
			cmpHeader.sequence_number = htonl(nSequence);
			cmpHeader.command_length = htonl(sizeof(CMP_HEADER));

			if( enquire_link_request == nCommand)
			{
				socketSend(nClientFD, &cmpHeader, sizeof(CMP_HEADER));
				continue;
			}
			nBodyLen = nTotalLen - sizeof(CMP_HEADER);

			if(static_cast<int>(nTotalLen) > MAX_SIZE - 1)
			{
				cmpPacket.cmpBodyUnlimit.cmpdata = new char[nBodyLen + 1];

				pBody = cmpPacket.cmpBodyUnlimit.cmpdata;
				result = socketrecv(nClientFD, nBodyLen, &pBody, clientSockaddr);

				if(result == nBodyLen)
				{
					ServerReceive(nClientFD, static_cast<int>(nTotalLen), &cmpPacket);
					continue;
				}
				else
				{
					socketSend(nClientFD, "Invalid Packet\r\n", strlen("Invalid Packet\r\n"));
					if(externalEvent.isValid() && -1 != externalEvent.m_nEventDisconnect)
					{
						sendMessage(externalEvent.m_nEventFilter, externalEvent.m_nEventDisconnect, nClientFD, 0, 0);
					}
					socketClose(nClientFD);
					_log("[Socket Server] socket close client: %d , packet length error: %d != %d", nClientFD, nBodyLen,
							result);
					break;
				}

			}
			else
			{
				pBody = &cmpPacket.cmpBody;
			}

			if(0 < nBodyLen)
			{
				result = socketrecv(nClientFD, nBodyLen, &pBody, clientSockaddr);
				if(result != nBodyLen)
				{
					socketSend(nClientFD, "Invalid Packet\r\n", strlen("Invalid Packet\r\n"));
					if(externalEvent.isValid() && -1 != externalEvent.m_nEventDisconnect)
					{
						sendMessage(externalEvent.m_nEventFilter, externalEvent.m_nEventDisconnect, nClientFD, 0, 0);
					}
					socketClose(nClientFD);
					_log("[Socket Server] socket close client: %d , packet length error: %d != %d", nClientFD, nBodyLen,
							result);
					break;
				}
			}

			if(access_log_request == nCommand)
			{
				socketSend(nClientFD, &cmpHeader, sizeof(CMP_HEADER));
			}
		}
		else if(0 >= result)
		{
			if(externalEvent.isValid() && -1 != externalEvent.m_nEventDisconnect)
			{
				sendMessage(externalEvent.m_nEventFilter, externalEvent.m_nEventDisconnect, nClientFD, 0, 0);
			}
			socketClose(nClientFD);
			break;
		}
		else
		{
			socketSend(nClientFD, "Control Center: Please use CMP to communicate\r\n",
					strlen("Control Center: Please use CMP to communicate\r\n"));
			_log("[Socket Server] Send Message: Please use CMP to communicate");

			if(externalEvent.isValid() && -1 != externalEvent.m_nEventDisconnect)
			{
				sendMessage(externalEvent.m_nEventFilter, externalEvent.m_nEventDisconnect, nClientFD, 0, 0);
			}

			socketClose(nClientFD);
			_log("[Socket Server] socket close client: %d , packet header length error: %d", nClientFD, result);
			break;
		}

		if(nClientFD == getSocketfd())
		{
			/**
			 * UDP server receive packet,record client information
			 */
			nFD = ntohs(clientSockaddr->sin_port);
			memset(szTmp, 0, sizeof(szTmp));
			sprintf(szTmp, "%d", nFD);
			udpClientData->setData(szTmp, *clientSockaddr);
		}
		else
		{
			nFD = nClientFD;
		}

		switch(mnPacketHandle)
		{
		case PK_MSQ:
			if(externalEvent.isValid())
			{
				sendMessage(externalEvent.m_nEventFilter, externalEvent.m_nEventRecvCommand, nFD, nTotalLen,
						&cmpPacket);
			}
			else
			{
				sendMessage(m_nInternalFilter, EVENT_COMMAND_SOCKET_SERVER_RECEIVE, nFD, nTotalLen, &cmpPacket);

			}
			break;
		case PK_ASYNC:
			ServerReceive(nFD, nTotalLen, &cmpPacket);
			break;
		}

	} // while

	mapClientThread.erase(nClientFD);

	delete clientSockaddr;

	sendMessage(m_nInternalFilter, EVENT_COMMAND_THREAD_EXIT, threadHandler->getThreadID(), 0, NULL);

	threadHandler->threadSleep(1);
	threadHandler->threadExit();

	return 0;
}