-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit 'e22ec9a271b5b263c7f58bdee7c9691ec8e07571' as 'vendor/as…
…yncudp'
- Loading branch information
Showing
17 changed files
with
1,333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
github: eerimoq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
|
||
linux: | ||
|
||
runs-on: ubuntu-20.04 | ||
strategy: | ||
max-parallel: 4 | ||
matrix: | ||
python-version: [3.7, 3.8, 3.9] | ||
|
||
steps: | ||
- uses: actions/checkout@v1 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Test | ||
run: | | ||
python -m unittest | ||
release: | ||
needs: [linux] | ||
runs-on: ubuntu-20.04 | ||
if: startsWith(github.ref, 'refs/tags') | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v1 | ||
- name: Set up Python 3.9 | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: 3.9 | ||
- name: Install pypa/build | ||
run: | | ||
python -m pip install build --user | ||
- name: Build a source tarball | ||
run: | | ||
git clean -dfx | ||
python -m build --sdist --outdir dist/ . | ||
- name: Publish distribution 📦 to PyPI | ||
uses: pypa/gh-action-pypi-publish@master | ||
with: | ||
skip_existing: true | ||
password: ${{secrets.pypi_password}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
||
# C extensions | ||
*.so | ||
*.o | ||
a.out | ||
|
||
# Distribution / packaging | ||
.Python | ||
env/ | ||
venv/ | ||
.venv/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*,cover | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Vim IDE | ||
*~ | ||
*.swp | ||
*.swo | ||
|
||
# IntelliJ IDEA | ||
.idea/ | ||
|
||
# pyenv | ||
.python-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021 Erik Moqvist | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include LICENSE | ||
recursive-include tests *.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
Asyncio high level UDP sockets | ||
============================== | ||
|
||
Asyncio high level UDP sockets. | ||
|
||
Project homepage: https://github.com/eerimoq/asyncudp | ||
|
||
Documentation: https://asyncudp.readthedocs.org/en/latest | ||
|
||
Installation | ||
============ | ||
|
||
.. code-block:: python | ||
$ pip install asyncudp | ||
Example client | ||
============== | ||
|
||
.. code-block:: python | ||
import asyncio | ||
import asyncudp | ||
async def main(): | ||
sock = await asyncudp.create_socket(remote_addr=('127.0.0.1', 9999)) | ||
sock.sendto(b'Hello!') | ||
print(await sock.recvfrom()) | ||
sock.close() | ||
asyncio.run(main()) | ||
Example server | ||
============== | ||
|
||
.. code-block:: python | ||
import asyncio | ||
import asyncudp | ||
async def main(): | ||
sock = await asyncudp.create_socket(local_addr=('127.0.0.1', 9999)) | ||
while True: | ||
data, addr = await sock.recvfrom() | ||
print(data, addr) | ||
sock.sendto(data, addr) | ||
asyncio.run(main()) | ||
Test | ||
==== | ||
|
||
.. code-block:: | ||
$ python3 -m unittest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import asyncio | ||
|
||
from .version import __version__ | ||
|
||
|
||
class ClosedError(Exception): | ||
pass | ||
|
||
|
||
class _SocketProtocol: | ||
|
||
def __init__(self, packets_queue_max_size): | ||
self._error = None | ||
self._packets = asyncio.Queue(packets_queue_max_size) | ||
|
||
def connection_made(self, transport): | ||
pass | ||
|
||
def connection_lost(self, transport): | ||
self._packets.put_nowait(None) | ||
|
||
def datagram_received(self, data, addr): | ||
self._packets.put_nowait((data, addr)) | ||
|
||
def error_received(self, exc): | ||
self._error = exc | ||
self._packets.put_nowait(None) | ||
|
||
async def recvfrom(self): | ||
return await self._packets.get() | ||
|
||
def raise_if_error(self): | ||
if self._error is None: | ||
return | ||
|
||
error = self._error | ||
self._error = None | ||
|
||
raise error | ||
|
||
|
||
class Socket: | ||
"""A UDP socket. Use :func:`~asyncudp.create_socket()` to create an | ||
instance of this class. | ||
""" | ||
|
||
def __init__(self, transport, protocol): | ||
self._transport = transport | ||
self._protocol = protocol | ||
|
||
def close(self): | ||
"""Close the socket. | ||
""" | ||
|
||
self._transport.close() | ||
|
||
def sendto(self, data, addr=None): | ||
"""Send given packet to given address ``addr``. Sends to | ||
``remote_addr`` given to the constructor if ``addr`` is | ||
``None``. | ||
Raises an error if a connection error has occurred. | ||
>>> sock.sendto(b'Hi!') | ||
""" | ||
|
||
self._transport.sendto(data, addr) | ||
self._protocol.raise_if_error() | ||
|
||
async def recvfrom(self): | ||
"""Receive a UDP packet. | ||
Raises ClosedError on connection error, often by calling the | ||
close() method from another task. May raise other errors as | ||
well. | ||
>>> data, addr = sock.recvfrom() | ||
""" | ||
|
||
packet = await self._protocol.recvfrom() | ||
self._protocol.raise_if_error() | ||
|
||
if packet is None: | ||
raise ClosedError() | ||
|
||
return packet | ||
|
||
def getsockname(self): | ||
"""Get bound infomation. | ||
>>> local_address, local_port = sock.getsockname() | ||
""" | ||
|
||
return self._transport.get_extra_info('sockname') | ||
|
||
async def __aenter__(self): | ||
return self | ||
|
||
async def __aexit__(self, *exc_info): | ||
self.close() | ||
|
||
|
||
async def create_socket(local_addr=None, | ||
remote_addr=None, | ||
packets_queue_max_size=0, | ||
reuse_port=None): | ||
"""Create a UDP socket with given local and remote addresses. | ||
>>> sock = await asyncudp.create_socket(local_addr=('127.0.0.1', 9999)) | ||
""" | ||
|
||
loop = asyncio.get_running_loop() | ||
transport, protocol = await loop.create_datagram_endpoint( | ||
lambda: _SocketProtocol(packets_queue_max_size), | ||
local_addr=local_addr, | ||
remote_addr=remote_addr, | ||
reuse_port=reuse_port) | ||
|
||
return Socket(transport, protocol) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = '0.10.0' |
Oops, something went wrong.