Skip to content

Accept method #30

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 6 commits into from
Jan 21, 2021
Merged

Accept method #30

merged 6 commits into from
Jan 21, 2021

Conversation

AdamCummick
Copy link
Contributor

Adds socket accept API
Adds context manager methods for socket
Adds setter for socket socknum
Adds status property for socket
Corrects bug in remote_port method

Tested with a SAMD51 running CircuitPython 6.0.0

CircuitPython test code:

import time
import board
import busio
import digitalio
import adafruit_requests as requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket

cs = digitalio.DigitalInOut(board.D5)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
eth = WIZNET5K(spi_bus, cs, is_dhcp=False, debug=False)

socket.set_interface(eth)
s = socket.socket()
HOST = '192.168.3.56'  # Standard loopback interface address (localhost)
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)

s.bind((HOST, PORT)) # Bind to IP and Port
s.listen() # begin listening

while True:
    listen_status = eth.socket_status(s.socknum)
    conn, addr = s.accept()
    with conn:
        data = conn.recv()
        print(f"data is {data}")
        conn.send(data)

Python client test code:

import time
import socket

HOST = '192.168.3.56'  # The server's hostname or IP address
PORT = 65432        # The port used by the server
while True:

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')
        data = s.recv(1024)
        print('Received', repr(data))
        time.sleep(1)

Adds socket accept API
Adds context manager methods for socket
Adds setter for socket socknum
Adds status property for socket
Corrects bug in remote_port method
@brentru brentru self-requested a review January 20, 2021 18:38
@brentru
Copy link
Member

brentru commented Jan 20, 2021

@AdamCummick Could you move the test code for this to the examples/ folder as adafruit_wiznet5k_simpleserver.py or similar? I think it'll be useful for others to build off of.

@AdamCummick
Copy link
Contributor Author

@brentru Didn't see your message until after that last commit. I'll get a more standard looking example off of the test code shortly.

Copy link
Member

@brentru brentru left a comment

Choose a reason for hiding this comment

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

@AdamCummick Thanks for adding the accept method - I have some suggestions and comments.

Adds an __exit__ timeout for disconnect
removes socknum setter in favor of direct reference
Corrects documentation errors
@AdamCummick
Copy link
Contributor Author

@brentru I added the requested changes as well as the example.

Copy link
Member

@brentru brentru left a comment

Choose a reason for hiding this comment

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

one little q

Adds additional timeout wait on socket close in __exit__

Corrects for occasional duplicate ACK packet that caused a 0 len message to be read.
@AdamCummick
Copy link
Contributor Author

Noticed an occasional bug where a zero length packet would be read. Wireshark was flagging a duplicate ACK. This was corrected by adding a wait state for the socket to actually close in the exit method.

@brentru brentru merged commit f71ca90 into adafruit:master Jan 21, 2021
@AdamCummick AdamCummick deleted the accept-method branch January 21, 2021 16:00
@AdamCummick AdamCummick restored the accept-method branch January 21, 2021 16:44
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 22, 2021
Updating https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_SSD1306 to 1.2.3 from 1.2.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_DisplayIO_SSD1306#17 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_DRV2605 to 1.1.10 from 1.1.9:
  > Merge pull request adafruit/Adafruit_CircuitPython_DRV2605#26 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_DS1307 to 2.1.4 from 2.1.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_DS1307#20 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_FXOS8700 to 2.1.4 from 2.1.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_FXOS8700#21 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_HCSR04 to 0.4.4 from 0.4.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_HCSR04#23 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_MAX31855 to 3.2.5 from 3.2.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_MAX31855#22 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_MAX9744 to 1.2.4 from 1.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_MAX9744#14 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx to 2.4.3 from 2.4.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP230xx#37 from adafruit/reset-keyword

Updating https://github.com/adafruit/Adafruit_CircuitPython_MCP4725 to 1.4.1 from 1.4.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP4725#20 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_MMA8451 to 1.3.4 from 1.3.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_MMA8451#15 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2 to 1.2.4 from 1.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_MPL3115A2#16 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_MPR121 to 2.1.4 from 2.1.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_MPR121#28 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_PCF8523 to 1.5.2 from 1.5.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_PCF8523#21 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_PCT2075 to 1.1.6 from 1.1.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_PCT2075#15 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_PN532 to 2.3.1 from 2.3.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_PN532#45 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 5.1.1 from 5.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#103 from makermelissa/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_RFM69 to 2.1.1 from 2.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_RFM69#34 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_SI4713 to 1.2.4 from 1.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_SI4713#17 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_SI5351 to 1.2.4 from 1.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_SI5351#17 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_SSD1305 to 1.3.4 from 1.3.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1305#12 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_SSD1306 to 2.10.1 from 2.10.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1306#54 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_TC74 to 1.0.1 from 1.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_TC74#3 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_TMP006 to 2.1.4 from 2.1.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_TMP006#13 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_Trellis to 1.3.5 from 1.3.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_Trellis#17 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_TSL2561 to 3.3.4 from 3.3.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_TSL2561#34 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_TSL2591 to 1.2.5 from 1.2.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_TSL2591#19 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_VCNL4010 to 0.10.5 from 0.10.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_VCNL4010#15 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_VEML6070 to 3.1.4 from 3.1.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_VEML6070#18 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_VEML6075 to 1.1.4 from 1.1.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_VEML6075#14 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_VL6180X to 1.2.4 from 1.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_VL6180X#16 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_VS1053 to 1.2.4 from 1.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_VS1053#14 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 1.8.0 from 1.7.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#30 from AdamCummick/accept-method

Updating https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO to 5.0.3 from 5.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#57 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_BitmapSaver to 1.1.3 from 1.1.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_BitmapSaver#13 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE_Adafruit to 1.2.2 from 1.2.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE_Adafruit#11 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_Debouncer to 1.3.8 from 1.3.7:
  > Merge pull request adafruit/Adafruit_CircuitPython_Debouncer#26 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_Display_Button to 1.4.1 from 1.4.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Display_Button#24 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_framebuf to 1.4.4 from 1.4.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_framebuf#40 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_hashlib to 1.3.2 from 1.3.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_hashlib#12 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright
  > Added --force-reinstall to pylint, black, and sphinx install directive

Updating https://github.com/adafruit/Adafruit_CircuitPython_IterTools to 1.1.3 from 1.1.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_IterTools#11 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_OAuth2 to 1.0.2 from 1.0.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#3 from brentru/fix-dependency-requests

Updating https://github.com/adafruit/Adafruit_CircuitPython_TinyLoRa to 2.1.3 from 2.1.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_TinyLoRa#34 from adafruit/REUSE
  > Added pre-commit-config file
  > Added pre-commit and SPDX copyright

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_PIOASM
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.

2 participants