Skip to content

Commit

Permalink
change: BytesIO to socket for asyncio
Browse files Browse the repository at this point in the history
select allow only socket on Windows
  • Loading branch information
namuyan committed Aug 12, 2020
1 parent d07d4d9 commit 7efc6f3
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 202 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Features
* UDP hole punching
* high performance (4Mbps/s Up&Down when 10mb)
* ipv4/ipv6
* optional asyncio

Requirement
----
Expand Down Expand Up @@ -59,6 +60,40 @@ print("closed", sock)
```
Another side, receive the message and show immediately.

asyncio usage
----
```python
from srudp import SecureReliableSocket
import asyncio

# Get a reference to the current event loop
loop = asyncio.get_event_loop()

# create a socket
sock = SecureReliableSocket()

# connect() on another thread because block event loop
address = ("example.com", 3000)
await loop.run_in_executor(None, sock.connect, (address,))

# Register the open socket to wait for data
reader, writer = await asyncio.open_connection(sock=sock)

# read
data = await reader.read(1024)

# write
writer.write(b"hello")
writer.write(b"world")
await writer.drain()

# close
writer.close()
```
You can do just like a normal TCP socket.
But if you don't intend, like HTTP protocol which requires a lot of connections,
you don't have to use async method.

to avoid troubles
----
* Do not think **always success connection establish**.
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
classifiers=[
'Typing :: Typed',
'Topic :: Internet',
'Framework :: AsyncIO',
'Topic :: System :: Networking',
'Development Status :: 3 - Alpha',
'Programming Language :: Python :: 3',
Expand Down
Loading

0 comments on commit 7efc6f3

Please sign in to comment.