DWORD WINAPI ConnectThrProc(LPVOID lpParam)	{
#else
void* ConnectThrProc(void* lpParam)	{
#endif

	int EndThread = 0;
	CLITCP* pCLITCP = (CLITCP*)lpParam;
	int connected = 0;

	do
	{
		memcpy_ts(&connected, &pCLITCP->connected, sizeof(int), &pCLITCP->CSconnected); // Thread-safe copy
		if (connected != 1)	{ // The client is not connected to the server, trying to connect or reconnect
			StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
			if (
				(InitTCP(&pCLITCP->ConnectSocket, pCLITCP->address, pCLITCP->port, &pCLITCP->addrinf) == EXIT_SUCCESS)&&
//				(SetSockOptTCP(pCLITCP->ConnectSocket, 1, 10000) == EXIT_SUCCESS)&& // Setting timeouts on the client socket
				(ConnectTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf) == EXIT_SUCCESS)//&&
//				(SetSockOptTCP(pCLITCP->ConnectSocket, 1, 10000) == EXIT_SUCCESS) // Setting timeouts on the client socket
				)	{
					fprintf(stdout, "Connection successful\n");
					connected = 1;
					memcpy_ts(&pCLITCP->connected, &connected, sizeof(int), &pCLITCP->CSconnected); // Thread-safe copy
			}
			else fprintf(stdout, "Unable to connect\n");
		}
		mSleep(1000);
		memcpy_ts(&EndThread, &pCLITCP->EndThread, sizeof(int), &pCLITCP->CSEndThread); // Thread-safe copy
	} while(!EndThread);

	return 0; 
} 
Exemple #2
0
void GetHostName()
{
  HOSTENT* phe;

  if (!InitTCP()){

    strcpy(hostname,"YOUR IP: ");
    gethostname(blah,255);
    phe = gethostbyname(blah);
    strcpy(blah, inet_ntoa(*(struct in_addr*)phe->h_addr));
    strcat(hostname,blah);
  }
}
Exemple #3
0
/** Main program entry point. This routine configures the hardware required by the application, then
 *  enters a loop to run the application tasks in sequence.
 */
int main(void)
{
	uint8_t cyc;
	SetupHardware();

	/* Webserver Initialization */
		setup_spi();

	DDRD |= _BV(6);
	PORTD &= ~_BV(6);
	PORTD = 0;

	GlobalInterruptEnable();

	//sendstr( "Boot" );

	et_init( 0 );

	InitTCP();
	InitDumbcraft();


	for (;;)
	{
//	SetManyWS( 0xff, 0x00, 0x00, 20 );

		RNDIS_Task();
		USB_USBTask();
		_delay_us(200);
		UpdateServer();
		cyc++;
		if( (cyc & 0x7f) == 0 )
		{
			struct Player * p = &Players[0];
//			sendhex2( p->npitch );
//			 = 1;
//			if( p->active )

			PORTD |= _BV(6);
			TickServer();
			TickTCP();
			PORTD &= ~_BV(6);
		}
	}
}
/*
Initializes the TCP client.

CLITCP* pCLITCP : (IN,OUT) pointer to the data structure representing the client
char* address : (IN) server address to connect
char* port : (IN) server port to connect

Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error
*/
int InitCLITCP(CLITCP* pCLITCP, char* address, char* port)	{

	int EndThread = 1;

	if((pCLITCP == NULL) || (pCLITCP->initialized == 1))	{
		return EXIT_FAILURE;
	}

	pCLITCP->address = (char*)calloc(strlen(address)+1, sizeof(char));
	sprintf(pCLITCP->address, "%s", address);
	pCLITCP->port = (char*)calloc(strlen(port)+1, sizeof(char));
	sprintf(pCLITCP->port, "%s", port);
	pCLITCP->ConnectSocket = INVALID_SOCKET;
	pCLITCP->addrinf = NULL;
	pCLITCP->EndThread = 0;
	pCLITCP->connected = 0;

	if (
		(InitTCP(&pCLITCP->ConnectSocket, pCLITCP->address, pCLITCP->port, &pCLITCP->addrinf) == EXIT_SUCCESS)&&
//		(SetSockOptTCP(pCLITCP->ConnectSocket, 1, 10000) == EXIT_SUCCESS)&& // Setting timeouts on the client socket
		(ConnectTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf) == EXIT_SUCCESS)//&&
//		(SetSockOptTCP(pCLITCP->ConnectSocket, 1, 10000) == EXIT_SUCCESS) // Setting timeouts on the client socket
		)	{
			fprintf(stdout, "Connection successful\n");
			pCLITCP->connected = 1;
	}
	else fprintf(stdout, "Unable to connect\n");

	if (InitCriticalSection(&pCLITCP->CSEndThread) != EXIT_SUCCESS)	{
		StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}
	if (InitCriticalSection(&pCLITCP->CSconnected) != EXIT_SUCCESS)	{
		DelCriticalSection(&pCLITCP->CSEndThread);
		StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (CreateDefaultThread(ConnectThrProc, pCLITCP, &pCLITCP->ThrId) != EXIT_SUCCESS)	{
		DelCriticalSection(&pCLITCP->CSEndThread);
		DelCriticalSection(&pCLITCP->CSconnected);
		StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}
	if (SetThreadDefaultPriority(pCLITCP->ThrId, THREAD_PRIORITY_NORMAL) != EXIT_SUCCESS)	{
		EndThread = 1;
		memcpy_ts((char*)&pCLITCP->EndThread, (char*)&EndThread, sizeof(int), &pCLITCP->CSEndThread); // Thread-safe copy
		WaitForThread(pCLITCP->ThrId);
		DelCriticalSection(&pCLITCP->CSEndThread);
		DelCriticalSection(&pCLITCP->CSconnected);
		StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}

	pCLITCP->initialized = 1;

	return EXIT_SUCCESS;
}