Ejemplo n.º 1
0
DWORD WINAPI mailThread(LPVOID arg) {

	char buffer[1024];
	planet_type *planet = (planet_type*)malloc(sizeof(planet_type));
	DWORD bytesRead;
	static int posY = 0;
	char mailSlotString[18] = "\\\\.\\mailslot\\", inputString[1024];
	HANDLE serverMailslot;

							/* create a mailslot that clients can use to pass requests through   */
							/* (the clients use the name below to get contact with the mailslot) */
							/* NOTE: The name of a mailslot must start with "\\\\.\\mailslot\\"  */
	

	serverMailslot = mailslotCreate("\\\\.\\mailslot\\serverMailslot");
	if (serverMailslot == INVALID_HANDLE_VALUE)
	{
		MessageBox(0, "Could not create mailslot, exiting.", "Error", 1);
		Sleep(3000);
		exit(1);
	}

							/* (ordinary file manipulating functions are used to read from mailslots) */
							/* in this example the server receives strings from the client side and   */
							/* displays them in the presentation window                               */
							/* NOTE: binary data can also be sent and received, e.g. planet structures*/
 
	while (1)
	{
		bytesRead = mailslotRead(serverMailslot, (void *)planet, sizeof(planet_type));

		if (bytesRead != 0) {
			// If planet name doesnt exist, add it to linked list
			if (!planetExists(planet)) {
				EnterCriticalSection(&dbAccess);
				InsertAtHead(*planet);
				LeaveCriticalSection(&dbAccess);
				threadCreate(calculatePosition, &(head->data));
			}
			else
			{
				sendErrorToCreator(planet, 2);
			}
		}
		else {
			/* failed reading from mailslot                              */
			/* (in this example we ignore this, and happily continue...) */
		}
	}
  
	free(planet);
	return 0;
}
Ejemplo n.º 2
0
DWORD WINAPI mailThread(LPVOID arg) {

	char buffer[1024];
	DWORD bytesRead;
	static int posY = 0;
	int flag = 0;
	HANDLE mailbox;
							/* create a mailslot that clients can use to pass requests through   */
							/* (the clients use the name below to get contact with the mailslot) */
							/* NOTE: The name of a mailslot must start with "\\\\.\\mailslot\\"  */

	
	mailbox = mailslotCreate ("\\\\.\\mailslot\\mailbox");

	InitializeCriticalSection(&criticalSection);
	for(;;) {				
							/* (ordinary file manipulating functions are used to read from mailslots) */
							/* in this example the server receives strings from the client side and   */
							/* displays them in the presentation window                               */
							/* NOTE: binary data can also be sent and received, e.g. planet structures*/
		
		bytesRead = mailslotRead (mailbox, &buffer, strlen(buffer)); 
		//TESTING PLANETS
		//if(flag==0)
		//{
		//	planet_type p1 = { "p1", 300, 300, 0, 0, 1000000000, NULL, 1000, NULL };
		//	planet_type p2 = { "p2", 200, 300, 0, 0.008, 1000, NULL, 1000, NULL};
		//	planet_type p3 = { "p3", 210, 300, 0.008, 0.008, 5000000, NULL, 1000, NULL };
		//	planet_type *p11 = malloc(sizeof(planet_type));
		//	memcpy(p11, &p1, sizeof(planet_type));
		//	p11->next = NULL;
		//	planet_type *p22 = malloc(sizeof(planet_type));
		//	memcpy(p22, &p2, sizeof(planet_type));
		//	p22->next = NULL;
		//	planet_type *p33 = malloc(sizeof(planet_type));
		//	memcpy(p33, &p3, sizeof(planet_type));
		//	p33->next = NULL;
		//	addPlanet(p11);
		//	addPlanet(p22);
		//	addPlanet(p33);
		//	threadCreate((LPTHREAD_START_ROUTINE)planetThread, p11);
		//	threadCreate((LPTHREAD_START_ROUTINE)planetThread, p22);
		//	threadCreate((LPTHREAD_START_ROUTINE)planetThread, p33);
		//	flag = 1;
		//}
		//Create planet
		if (bytesRead != 0) {
			planet_type *p = malloc(sizeof(planet_type));
			memcpy(p, buffer, sizeof(planet_type));
			p->next = NULL;
			addPlanet(p);
			threadCreate((LPTHREAD_START_ROUTINE)planetThread, p);
			
		

							/* NOTE: It is appropriate to replace this code with something */
							/*       that match your needs here.                           */
		posY++;  
							/* (hDC is used reference the previously created window) */	
		TextOut(hDC, 10, 50+posY%200, p->name, sizeof(strlen(p->name)));

		
		}
		else {
								/* failed reading from mailslot                              */
								/* (in this example we ignore this, and happily continue...) */
		}
	}
	DeleteCriticalSection(&criticalSection);
  return 0;
}