Passing a function in Ethernet Library .connect

I've noticed that the ethernet library has a while() function in Ethernet.connect that it uses when returning connection status. My project involves some analog to digital conversions that I'd like to run as often as possible while connecting and sending data to clients. I am very aware that there are the setRetransmissionTime and setRetransmissionCount functions in the library itself, but i'm trying to pass my analog to digital function into the while loop of the connect function, so the ethernet chip can still run the 8 retries at 200ms...

What i've come up with is this...

In the EthernetClient.h

  virtual int connect(IPAddress ip, uint16_t port, virtual void(*function)());
  virtual int connect(const char *host, uint16_t port, virtual void(*function)());

In the EthernetClient.cpp

int EthernetClient::connect(const char* host, uint16_t port, virtual void (*function)()) {
	// Look up the host first
	int ret = 0;
	DNSClient dns;
	IPAddress remote_addr;

	dns.begin(Ethernet.dnsServerIP());
	ret = dns.getHostByName(host, remote_addr, function);
	if (ret == 1) {
		return connect(remote_addr, port);
		} else {
		return ret;
	}
}


int EthernetClient::connect(IPAddress ip, uint16_t port, virtual void (*function)() ) {
	if (_sock != MAX_SOCK_NUM)
	return 0;

	for (int i = 0; i < MAX_SOCK_NUM; i++) {
		uint8_t s = w5500.readSnSR(i);
		if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
			_sock = i;
			break;
		}
	}

	if (_sock == MAX_SOCK_NUM)
	return 0;

	_srcport++;
	if (_srcport == 0) _srcport = 1024;
	socket(_sock, SnMR::TCP, _srcport, 0);

	if (!::connect(_sock, rawIPAddress(ip), port)) {
		_sock = MAX_SOCK_NUM;
		return 0;
	}

	while (status() != SnSR::ESTABLISHED) {
		 function();
		delay(1);
		if (status() == SnSR::CLOSED) {
			_sock = MAX_SOCK_NUM;
			return 0;
		}
	}

	return 1;
}

So in theory, I should be able to run something like this from the webclient.ino example...

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80, AnalogToDigital)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

void AnalogToDigital(){
 //analog to digital function I wish to pass goes here 
}

As you can see, I modified my .connect function to show client.connect(server, 80, AnalogToDigital)
which should pass the AnalogToDigital function at the bottom.

When i try to compile, all i get is errors regarding pure virtual functions and what not. Again I don't have enough programming experience to solve this myself... I'm hoping its even possible lol... Any help/comments much appreciated!!

-Matt

Any help/comments much appreciated!!

Why are you using virtual in the function prototype? Why should the function being used be virtual?

PaulS:
Why are you using virtual in the function prototype? Why should the function being used be virtual?

That's what the Ethernet library uses by default.

first of all, it is a bad idea to modify library code.

regarding virtual function, EthernetClient is a subclass of Client, which defines the virtual function connect. If you modified the function in EthernetClient, you must modify it in the base class as well, or a "best practice" way to do this is to just create a new connect function (that is not virtual) with the additional parameter and modify the original connect implementation to call the new function with null for the new parameter (you need to do null check before calling the function).
Having said that, I don't think this will actually solve your problem.

The proper solution is to use a timer interrupt to execute your analogtodigital function, assuming it is ISR safe and completes really quickly.

Yeah I realized I had to go modify the client.h in the hardware/sam/core... It compiles now so I'm testing it. It just sucks that the library has the potential to block so much just running while loops until it times out... we'll see how this turns out.

if you really want to modify a library, the proper "best practice" way to do it (and accepted software engineering practice), is to extend the class instead of modifying it.

in case you run into more problems later, the proper solution still is, to use a timer interrupt.

doughboy:
if you really want to modify a library, the proper "best practice" way to do it (and accepted software engineering practice), is to extend the class instead of modifying it.

in case you run into more problems later, the proper solution still is, to use a timer interrupt.

In this case, I think that MAKING and TESTING the changes the way OP is doing it is good. But, once it is proven that they work, the changes should be submitted to the Arduino team for proper inclusion in the existing class.

This is exactly how bad coding habit is born.
bad code works, now you are convinced this is the right way to do it and nobody can convince you otherwise, then you now always code this way. :frowning:

doughboy:
This is exactly how bad coding habit is born.
bad code works, now you are convinced this is the right way to do it and nobody can convince you otherwise, then you now always code this way. :frowning:

You are entitled to your opinion. No matter how wrong it is. 8)

If the current library claims to provide some capability that doesn't work, subclassing to make it work is not, in my opinion, the way to fix it.

^^^ lol