mobizt / ESP_SSLClient

The upgradable Secure Layer Networking (SSL/TLS) TCP Client for Arduino devices that support external networking interfaces e.g., WiFiClient, EthernetClient, and GSMClient.
MIT License
18 stars 2 forks source link

ESP_SSLClient with Ethernet Client connection delay #2

Closed RazmigAvedis closed 1 year ago

RazmigAvedis commented 1 year ago

Hello! I am trying to integrate the library with ESP32 + ethernet W5100 chip. I noticed that there is a connection delay of ~600ms everytime I connect to my HTTPS server with the function .connect(server,port). I need my firmware to be fast in transmitting data, so this delay is a problem for me. Is there a way to reduce this delay with session caching or connecting with a persistant connection? Thank You.

mobizt commented 1 year ago

That is the time require to wait until the SSL handshake was done.

You should keep the session to open since connection established if you don't want SSL handshake to be done everytime you want to send/receive data.

mobizt commented 1 year ago

You should call connect() only once and don't call stop() when you often send/receive data to/from the same host and uri (in case of http).

The memory usage during session opened is relatively small (512 bytes to 16k) when compared to available memory in ESP32 then I recommend keeping connection open is better.

RazmigAvedis commented 1 year ago

I am using the same host but multiple (4) uris. Is session caching handled in the library under the hood or should I do it using the function setSession?

RazmigAvedis commented 1 year ago

Also, if I want to check if the session did not disconnect and reconnect in the case the connection dropped, do I use an if condition on .connected()?

mobizt commented 1 year ago

The connected() or available() method alone without sending the request will not always return the actual server connection status unless your Client has implemented TCP KeepAlive as in ESP32 WiFiClient (via lwIP setsockopt).

The available() returns the remaining bytes that do not yet read from receive buffer even connection was closed.

I don't see TCP keepalive implemented in the Ethernet library driver which is available in W5500 chip firmware.

You can use ping or opening another connection to server to test that server is alive (respond) periodically in this case.

mobizt commented 1 year ago

I forgot your question about session resumption.

You can save crypto settings in BearSSL_Session and resume it for faster handshake.

By define the BearSSL_Session in global and pass its pointer to setSession like this.

BearSSL_Session session; //Global object

ssl_client.setSession(&session);// Assign its pointer
RazmigAvedis commented 1 year ago

I forgot your question about session resumption.

You can save crypto settings in BearSSL_Session and resume it for faster handshake.

By define the BearSSL_Session in global and pass its pointer to setSession like this.

BearSSL_Session session; //Global object

ssl_client.setSession(&session);// Assign its pointer

Hello, I managed to reduce the connection time with this. but i have a question. I am getting the following error:

ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection. ERROR.available: Cannot operate on a closed SSL connection.

when this happens the firmware is getting stuck. What does this mean and how can I fix it? Thanks alot!

mobizt commented 1 year ago

That is because the basic client is stopped or the BearSSL engine was already closed.

mobizt commented 1 year ago

This is the session resumption example.