Esempio n. 1
0
/**
 * Handle client communications
 */
void client_task_impl() {

	uip_tcp_appstate_t *app = &(uip_conn->appstate);
	GETrequest *req = (GETrequest*)app->request;

	if (uip_connected()) {

		if (verbose) {
			Serial.print("Connected to ");
			Serial.println(req->hostName);
		}
		app->ackedCount = 0;
		sendRequest();
	}

	// Did we get an ack for the last packet?
	if (uip_acked()) {
		// Record the bytes that were successfully sent
		app->ackedCount += app->sentCount;
		app->sentCount = 0;

		// Check if we're done or need to send more content for this
		// request
		if (app->ackedCount != (int)app->cursor) {
			// Generate the post again to send the next packet of data
			sendRequest();
		}
	}

	if (uip_rexmit()) {
		sendRequest();
	}

 	if (uip_newdata())  {
 		setRXPin(HIGH);

		if (verbose) {
			Serial.print("RX ");
			Serial.print(uip_datalen());
			Serial.print(" bytes from ");
			Serial.println(req->hostName);
		}

		// Check if the sketch cares about the returned data
	 	if ((req->returnFunc) && (uip_datalen() > 0)){
			// Call the sketch's callback function
	 		req->returnFunc((char*)uip_appdata, uip_datalen());
	 	}
 	}

	if (uip_aborted() || uip_timedout() || uip_closed()) {
		if (req != NULL) {
			if (verbose) {
				Serial.print("Ended connection with ");
				Serial.println(req->hostName);
			}

			if (req->returnFunc) {
				// Call the sketch's callback function with 0 bytes to indicate End Of Data
				req->returnFunc((char*)uip_appdata, 0);
			}
			// Remove the request from the connection
			app->request = NULL;
			// Request is no longer active
			req->active = false;
		}
	}
}
/**
 * Handle client communications
 */
void client_task_impl() {

	uip_tcp_appstate_t *app = &(uip_conn->appstate);
	GETrequest *req = (GETrequest*)app->request;

	if (uip_connected()) {

		TCP_client_connected = 1;		// JM - indicate that an end point client connection is in progress
		TCP_new_connection = 1;		// JM - indicate a new connection was made - cleared by application
		#ifdef DEBUG
			DebugPrintFO(f_ct); 	// Serial.print("-->Connected to ");
			DebugPrint((char *)(req->hostName));
        #endif
		app->ackedCount = 0;
		sendRequest();
	}

	// Did we get an ack for the last packet?
	if (uip_acked()) {
		// Record the bytes that were successfully sent
		app->ackedCount += app->sentCount;
		app->sentCount = 0;

		// Check if we're done or need to send more content for this
		// request
		if (app->ackedCount != (int)app->cursor) {
			// Generate the post again to send the next packet of data
			sendRequest();
		}
	}

	if (uip_rexmit()) {
		sendRequest();
	}

 	if (uip_newdata())  {
 		setRXPin(HIGH);

		#ifdef DEBUG
			DebugPrintFO(f_rx);			// Serial.print("\n<--RX ");
			Serial.print(uip_datalen());
			DebugPrintFO(f_bf);			// Serial.print(" bytes from ");
			DebugPrint((char *)req->hostName);
        #endif
		// Check if the sketch cares about the returned data
	 	if ((req->returnFunc) && (uip_datalen() > 0)){
			// Call the sketch's callback function
	 		req->returnFunc((char*)uip_appdata, uip_datalen());
	 	}
 	}

	if (uip_aborted() || uip_timedout() || uip_closed()) {
		if (req != NULL) {
			TCP_client_connected = 0;			// JM end point connection no longer in progress
			TCP_connection_terminated = 1;		// JM indicate a connection attempt or connection terminated
			#ifdef DEBUG
				DebugPrintFO(f_ec);
				DebugPrint((char *)req->hostName);
            #endif

			if (req->returnFunc) {
				// Call the sketch's callback function with 0 bytes to indicate End Of Data
				req->returnFunc((char*)uip_appdata, 0);
			}
			// Remove the request from the connection
			app->request = NULL;
			// Request is no longer active
			req->active = false;
		}
	}
}