Skip to main content
gamedev.net
🔒 Locked

Server using cURL

Started by Linaxys Apr 22, 2009 at 4:52 PM 4 replies 1.3k views
Original Post
Linaxys
Linaxys
Hello, I would like my server to use cURL to send a little GET request, could someone explain me how to use pthread with cURL so it doesn't freeze the whole process while doing the request ? It's to check if the user id is inside a database... Thanks ! EDIT: I found, please delete it. [Edited by - Linaxys on April 23, 2009 3:57:51 AM]
smitty1276
smitty1276
Click edit, and then delete it.
hplus0603
hplus0603
You don't delete posts just because you know what to do. Others may have the same question.

What you should do, given that you asked the community for help (even if it didn't have time to answer this particular question), is to post the answer to your question as a reply, and then let the thread live on, so that others with the same question can find it through the Search function. That's how forums become helpful.
enum Bool { True, False, FileNotFound };
Cornstalks
Cornstalks
Quote:
Original post by hplus0603
You don't delete posts just because you know what to do. Others may have the same question.

What you should do, given that you asked the community for help (even if it didn't have time to answer this particular question), is to post the answer to your question as a reply, and then let the thread live on, so that others with the same question can find it through the Search function. That's how forums become helpful.

To expand on that, it also gives those in the community the option to review, comment on, and critique your solution. You may have found a working solution, but there could possibly be better solutions.
Linaxys
Linaxys
It was pretty stupid to find, but well, sorry for that one, it won't happen again.

I simply had to create a pthread to call my curl function when I want to make a request to my database with a PHP script :

pthread_t nt;char *playerName;pthread_create(&nt, NULL, thread_IsAllowed, (void*)playerName);


The function to process the request with cURL :

static void *thread_IsAllowed(void *arg) {	char *name = (char*)arg;	CURL *curl;	CURLcode res;	struct MemoryStruct chunk;	chunk.memory=NULL;	chunk.size = 0;	curl = curl_easy_init();	if(!curl) return NULL;	char request[128];	sprintf(request, "login=%s", name);	curl_easy_setopt(curl, CURLOPT_URL, "http://www.site.com/login.php");	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);	curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(request));	curl_easy_setopt(curl, CURLOPT_POST, 1L);	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);	res = curl_easy_perform(curl);	if (res) {		return NULL;	}	curl_easy_cleanup(curl);	if (!stricmp(chunk.memory, "allowed")) {		// User is allowed, create it.	} else {		// Alright, reject it.	}	return NULL;}


Little piece of code about my PHP script :

if ($_POST['login'] == "goodName")	echo "allowed";else	echo "denied";


So, why creating a pthread ? Simply to use cURL into another thread so it doesn't freeze the whole server's process just for a little request, the remote site could make take a very long time to answer.
hplus0603
hplus0603
Thanks for sharing, and good luck on your project!
enum Bool { True, False, FileNotFound };

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.