-
Notifications
You must be signed in to change notification settings - Fork 74
Add Server creation and management support #59
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
Changes from 19 commits
5b28118
5e09b7b
faf031a
8c4035d
b8fd02e
5bd6c86
83d1525
f6d9415
d469ab9
94fce44
c429455
92d0c66
6af7675
d1fe777
842ac46
c2b7cb0
4f3d07f
fc97dac
41eab1c
e6bfd36
473f850
a93c099
fd4e191
6951f8c
a5d59f8
8dbeef0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ def set_interface(iface): | |
|
||
SOCK_STREAM = const(1) | ||
AF_INET = const(2) | ||
NO_SOCKET_AVAIL = const(255) | ||
|
||
MAX_PACKET = const(4000) | ||
|
||
|
@@ -59,14 +60,16 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0): | |
class socket: | ||
"""A simplified implementation of the Python 'socket' class, for connecting | ||
through an interface to a remote device""" | ||
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None): | ||
# pylint: disable=too-many-arguments | ||
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None, socknum=None): | ||
if family != AF_INET: | ||
raise RuntimeError("Only AF_INET family supported") | ||
if type != SOCK_STREAM: | ||
raise RuntimeError("Only SOCK_STREAM type supported") | ||
self._buffer = b'' | ||
self._socknum = _the_interface.get_socket() | ||
self._socknum = socknum if socknum else _the_interface.get_socket() | ||
self.settimeout(0) | ||
# pylint: enable=too-many-arguments | ||
|
||
def connect(self, address, conntype=None): | ||
"""Connect the socket to the 'address' (which can be 32bit packed IP or | ||
|
@@ -90,7 +93,7 @@ def readline(self): | |
stamp = time.monotonic() | ||
while b'\r\n' not in self._buffer: | ||
# there's no line already in there, read some more | ||
avail = min(_the_interface.socket_available(self._socknum), MAX_PACKET) | ||
avail = self.available() | ||
if avail: | ||
self._buffer += _the_interface.socket_read(self._socknum, avail) | ||
elif self._timeout > 0 and time.monotonic() - stamp > self._timeout: | ||
|
@@ -106,7 +109,7 @@ def read(self, size=0): | |
#print("Socket read", size) | ||
if size == 0: # read as much as we can at the moment | ||
while True: | ||
avail = min(_the_interface.socket_available(self._socknum), MAX_PACKET) | ||
avail = self.available() | ||
if avail: | ||
self._buffer += _the_interface.socket_read(self._socknum, avail) | ||
else: | ||
|
@@ -122,7 +125,7 @@ def read(self, size=0): | |
received = [] | ||
while to_read > 0: | ||
#print("Bytes to read:", to_read) | ||
avail = min(_the_interface.socket_available(self._socknum), MAX_PACKET) | ||
avail = self.available() | ||
if avail: | ||
stamp = time.monotonic() | ||
recv = _the_interface.socket_read(self._socknum, min(to_read, avail)) | ||
|
@@ -148,6 +151,39 @@ def settimeout(self, value): | |
"""Set the read timeout for sockets, if value is 0 it will block""" | ||
self._timeout = value | ||
|
||
def available(self): | ||
"""Returns how many bytes of data are available to be read (up to the MAX_PACKET length)""" | ||
if self.socknum != NO_SOCKET_AVAIL: | ||
return min(_the_interface.socket_available(self._socknum), MAX_PACKET) | ||
return 0 | ||
|
||
def connected(self): | ||
"""Whether or not we are connected to the socket""" | ||
if self.socknum == NO_SOCKET_AVAIL: | ||
return False | ||
elif self.available(): | ||
return True | ||
else: | ||
status = _the_interface.socket_status(self.socknum) | ||
# TODO: why is esp.<ConstantName> not defined? using magic numbers in mean time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to use the same constants as defined in Any ideas? We obviously don't want the magic numbers here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which line are you referring to here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all of the numbers in All of those are constants corresponding to ones defined in esp32spi.py https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI/blob/master/adafruit_esp32spi/adafruit_esp32spi.py#L101-L111 My initial attempt to use them wasn't working, I'm sure there is just something silly i'm missing with being able to re-use those constants here. |
||
result = status not in (1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for context, stole this logic from the arduino |
||
0, | ||
5, | ||
6, | ||
10, | ||
2, | ||
3, | ||
7) | ||
if not result: | ||
self.close() | ||
self._socknum = NO_SOCKET_AVAIL | ||
return result | ||
|
||
@property | ||
def socknum(self): | ||
"""The socket number""" | ||
return self._socknum | ||
|
||
def close(self): | ||
"""Close the socket, after reading whatever remains""" | ||
_the_interface.socket_close(self._socknum) | ||
|
Uh oh!
There was an error while loading. Please reload this page.