[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Re: Crash in evhttp_send_reply_chunk()



Hi,

I have attached code snippet. Please let me know if it is better now.

Regards,
Alap


From: Mark Ellzey <mthomas@xxxxxxxxxx>
To: libevent-users@xxxxxxxxxxxxx
Sent: Thursday, May 31, 2012 9:39 AM
Subject: Re: [Libevent-users] Re: Crash in evhttp_send_reply_chunk()

On Thu, May 31, 2012 at 06:32:38AM -0700, Alap Kumar Sinha wrote:
> Hi,
>
> Any help on this please?
>
> Regards,
> Alap
>

Can you either put this up on pastebin/gist, or make the code an
attachment? You are using a non-plaintext encoding so it's kind of hard
to read.

***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.


#define KEY_STRING "CHECK"

struct evhttp_request *req;  //Structure holds the req, on which chunk reply is sent
 
pthread_t SenderThread;
bool startSend = false;
 
void connectionClosed(struct evhttp_connection* evcon, void* key)
{
     printf("Connection closed by client");
}

//****** Function to send some data every 5 secs************/
void sendChunk(struct evhttp_connection* evcon, void* key)
{
	int counter = 0;
	printf("Start sending data everyy 5 secs");
	while(1)
	{
		if(startSend) //startSend is set to true when client does a GET & SSEEvents_cb() is invoked
		{
			evbuffer *databuf = evbuffer_new();
			if (databuf != NULL)
			{
				evbuffer_add(databuf,"event:remoteEvent\n",strlen("event:remoteEvent\n"));
				evbuffer_add_printf(databuf,"data:TestDATA%d\n",counter);

				evhttp_send_reply_chunk(req, databuf);
				evbuffer_free(databuf);
				counter++;
				if(10000 == counter)
					counter = 0;
			}
			sleep(5);
		}
		else
		{
			sleep(2); //Sleep for 2 secs & then check if the flag is set or not
		}

	}
}
 ///******Callback when Client does a GET on URL "/SSEEvents"********

void SSEEvents_cb(struct evhttp_request *aReq, void *key)
{

	if (EVHTTP_REQ_GET == aReq->type)
	{
		const char * getUri = evhttp_request_get_uri(aReq);
		if( NULL == getUri )
		{
			evhttp_send_reply(aReq, 400, "Bad request", NULL);   //Response back to client
		}

		req = (evhttp_request*) aReq;

		evhttp_request_own(aReq);

		evhttp_connection_set_closecb(evhttp_request_get_connection(aReq),&connectionClosed, KEY_STRING);

		evhttp_add_header(aReq->output_headers,"Content-Type","text/event-stream"); //Mandatory
		evhttp_send_reply_start(aReq, 200, "OK"); ///Start the reply & then set the flag so that chunk reply can be sent every 5 secs

		// Set this flag to true so that the sender thread can start sending chunk data
		startSend = true;

	}
	else
	{
		evhttp_send_reply(aReq, 501, " SET Not Implemented", NULL);
	}
}
int StartServer(char *aIPAddress, char *aPort)
{
	base = event_base_new();
	if (!base)
	{
		printf("Couldn't create an event_base: So Exiting");
		return 1;
	}

	/* Create a new evhttp object to handle requests. */
	http = evhttp_new(base);
	if (!http)
	{
		printf("Couldn't create evhttp: So Exiting....");
		return 1;
	}
	//Register for callback for any HTTP request for url "SSEEvents"
	evhttp_set_cb(http, "/SSEEvents", SSEEvents_cb, this);

	/* Now we tell the evhttp what port to listen on */
	uint16_t port = atoi(aPort);   
	handle = evhttp_bind_socket_with_handle(http, aIPAddress, port);
	if (!handle)
	{
		printf("couldn't bind to IP & port %d. Exiting.",(uint16_t)port);
		return -1;
	}
	///** Launch Thread to send chunk reply after http client makes request   
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setstacksize(&attr, (128 * 1024));     //128kbs
	int threadCreate = -1;
	threadCreate = pthread_create(&SenderThread, NULL, &sendChunk, NULL);

	event_base_dispatch(base);
	return 0;
}   
int main(int argc , char *argv[])
{
     StartServer(argv[1], argv[2]);
}