Skip to content

Standardise sockets #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jan 25, 2023
Merged

Standardise sockets #87

merged 27 commits into from
Jan 25, 2023

Conversation

BiffoBear
Copy link
Contributor

@BiffoBear BiffoBear commented Dec 25, 2022

Updated adafruit_wiznet5k_socket.py to match as closely as possible CPython socket. Functions that exist in Cpython socket have matching parameters and return types. Functions that exist in wiznet5k socket but not in Cpython socket were renamed with a leading _ except socket.setinterface.

Main differences:

  • flags still not implemented, not planned to be.
  • socket.bind can only bind to the IP address that the Wiznet chip was initialized with. All the hardware sockets share this single address. Changing the IP address after initialization leaves previously connected sockets in an unknown state. Raises ValueError if the IP address does not match.
  • socket.bind should raise an exception if the socket is already bound. Making this check currently causes socket.accept to fail because it calls 'bind' on a bound socket (now solved).

Not attempted, TODO: at a later date:

  • Raise appropriate exceptions from adafruit_wiznet5k.py
  • Refactor modules to stop calling socket nonstandard functions then remove nonstandard functions.

This has been tested by running simpletest.py and simpleserver.py.

More testing would be appreciated.

BiffoBear added 18 commits December 22, 2022 20:54
… package to call socket.recv with a bufsize.
…accept IPv4 address as a 4-tuple to match the parameters used in CPython.
…Use __setattr__ to work around accessing a protected item.
…n. Removed timeout from socket.__init__. Remove socket._socknum property.
@anecdata anecdata requested a review from a team January 3, 2023 00:53
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few questions about possible tweaks.

I will also test this branch out on a device this week and report back how it goes.

Thank you again for all of the hard work you've put into the improvements for this library @BiffoBear!

@BiffoBear
Copy link
Contributor Author

@FoamyGuy Please have a look at this too, the function can take either 2 or 3 args and CP does not support overloading, so I don't know how to type this function.

def sendto(self, data: bytearray, *flags_and_or_address: any) -> int:
        """
        Send data to the socket. The socket should not be connected to a remote socket, since the
        destination socket is specified by address. Return the number of bytes sent..

        Either:
        :param bytearray data: Data to send to the socket.
        :param [Tuple[str, int]] address: Remote socket as a (host, port) tuple.

        Or:
        :param bytearray data: Data to send to the socket.
        :param int flags: Not implemented, kept for compatibility.
        :param Tuple[int, Tuple(str, int)] address: Remote socket as a (host, port) tuple
        """

@BiffoBear
Copy link
Contributor Author

Refactored bind() into bind() and _bind() to allow accept() to call bind on an already bound socket.

@FoamyGuy
Copy link
Contributor

@FoamyGuy Please have a look at this too, the function can take either 2 or 3 args and CP does not support overloading, so I don't know how to type this function.

I saw that one. I Do think think way you've done it is good. I don't know of any better way that we could type / document it.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good to me.

I have tested this version successfully with a Feather ESP32-S2 TFT and Ethernet Featherwing with:

  • wiznet5k_simpletest.py
  • wiznet5k_simpleserver.py
  • wiznet5k_wsgiserver.py
  • wiznet5k_aio_post.py

and all are working as expected.

I also checked htonl() against the currently released version to ensure they output the same thing from same input. Lastly I compared inet_aton and inet_ntoa outputs to output from the same function from cpython built-in module socket. All are matching as expected.

Thank you again @BiffoBear for working on this and all of the other improvements!

@FoamyGuy FoamyGuy merged commit c8fabd0 into adafruit:main Jan 25, 2023
@BiffoBear BiffoBear deleted the Standardise_Sockets branch January 25, 2023 03:01
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants