Exemple #1
0
int client_method(int id, void *in, void **out)
{
	int lenreq, lenres, err, skip;
	lenreq  = write_request_header(bufreq, BUF_REQ_SIZE, id);
	lenreq += (Operations[id].constructinfunc)(bufreq+lenreq, BUF_REQ_SIZE-lenreq, in);
	lenreq += write_request_footer(bufreq+lenreq, BUF_REQ_SIZE-lenreq);
	err = request_response(id, bufreq, lenreq, bufres, &lenres);
	*out = NULL;
	if (!err) {
		skip = skip_response_header(bufres, lenres);
		if (skip >= 0) {
			*out = (Operations[id].parseoutfunc)(bufres+skip, lenres-skip);
		} else {
			err = skip;
		}
	}
	return err;
}
Exemple #2
0
bool http_connection_t::open(const char* const host, const uint16_t port, const char* const path) {
	
	socket = cc3000_socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if( socket < SOCKET_OK ) {
		return false;
	}
	
	in_addr_t host_address;
	const auto result_gethostbyname = cc3000_socket.gethostbyname(host, host_address);
	if( result_gethostbyname < SOCKET_OK ) {
		close();
		return false;
	}

	const auto result_connect = cc3000_socket.connect(socket, host_address, port);
	if( result_connect < SOCKET_OK ) {
		close();
		return false;
	}
	
	write_request_header(host, path);
	
	state = STATE_NONE;
	
	http_parser_init(&parser, HTTP_RESPONSE);
	parser.data = (void*)this;

	bool result = false;
	while(true) {
		std::array<uint8_t, 256> recv_data;
		const auto bytes_read_from_cc3000 = cc3000_socket.recv(socket, recv_data.data(), recv_data.size());
		if( bytes_read_from_cc3000 <= 0 ) {
			break;
		}
		http_parser_execute(&parser, &parser_settings, (char*)recv_data.data(), bytes_read_from_cc3000);
		if( state == STATE_MESSAGE_COMPLETE ) {
			result = true;
			break;
		}
	}

	close();
	return result;
}