Skip to content

Commit

Permalink
[DOCS] Added How-to guide for UDP clients
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-clairicia committed Oct 29, 2023
1 parent 96af3c9 commit fc7fea1
Show file tree
Hide file tree
Showing 13 changed files with 531 additions and 0 deletions.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

import asyncio

from easynetwork.api_async.client import AsyncUDPNetworkClient
from easynetwork.protocol import DatagramProtocol
from easynetwork.serializers import JSONSerializer


async def main() -> None:
protocol = DatagramProtocol(JSONSerializer())
address = ("localhost", 9000)

async with AsyncUDPNetworkClient(address, protocol) as client:
print(f"Remote address: {client.get_remote_address()}")

...


if __name__ == "__main__":
asyncio.run(main())
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import annotations

import asyncio
import socket

from easynetwork.api_async.client import AsyncUDPNetworkClient
from easynetwork.protocol import DatagramProtocol
from easynetwork.serializers import JSONSerializer


async def obtain_a_connected_socket() -> socket.socket:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

...

return sock


async def main() -> None:
protocol = DatagramProtocol(JSONSerializer())
sock = await obtain_a_connected_socket()

async with AsyncUDPNetworkClient(sock, protocol) as client:
print(f"Remote address: {client.get_remote_address()}")

...


if __name__ == "__main__":
asyncio.run(main())
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations

from easynetwork.api_sync.client import UDPNetworkClient
from easynetwork.protocol import DatagramProtocol
from easynetwork.serializers import JSONSerializer


def main() -> None:
protocol = DatagramProtocol(JSONSerializer())
address = ("127.0.0.1", 9000)

with UDPNetworkClient(address, protocol) as client:
print(f"Remote address: {client.get_remote_address()}")

...


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

import socket

from easynetwork.api_sync.client import UDPNetworkClient
from easynetwork.protocol import DatagramProtocol
from easynetwork.serializers import JSONSerializer


def obtain_a_connected_socket() -> socket.socket:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

...

return sock


def main() -> None:
protocol = DatagramProtocol(JSONSerializer())
sock = obtain_a_connected_socket()

with UDPNetworkClient(sock, protocol) as client:
print(f"Remote address: {client.get_remote_address()}")

...


if __name__ == "__main__":
main()
Empty file.
67 changes: 67 additions & 0 deletions docs/source/_include/examples/howto/udp_clients/usage/api_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from __future__ import annotations

import asyncio
import socket
from typing import Any

from easynetwork.api_async.client import AsyncUDPNetworkClient
from easynetwork.exceptions import DatagramProtocolParseError

###############
# Basic usage #
###############


async def send_packet_example1(client: AsyncUDPNetworkClient[Any, Any]) -> None:
# [start]
await client.send_packet({"data": 42})


async def recv_packet_example1(client: AsyncUDPNetworkClient[Any, Any]) -> None:
# [start]
packet = await client.recv_packet()
print(f"Received packet: {packet!r}")


async def recv_packet_example2(client: AsyncUDPNetworkClient[Any, Any]) -> None:
# [start]
try:
async with asyncio.timeout(30):
packet = await client.recv_packet()
except TimeoutError:
print("Timed out")
else:
print(f"Received packet: {packet!r}")


async def recv_packet_example3(client: AsyncUDPNetworkClient[Any, Any]) -> None:
# [start]
try:
async with asyncio.timeout(30):
packet = await client.recv_packet()
except DatagramProtocolParseError:
print("Received something, but was not valid")
except TimeoutError:
print("Timed out")
else:
print(f"Received packet: {packet!r}")


async def recv_packet_example4(client: AsyncUDPNetworkClient[Any, Any]) -> None:
# [start]
all_packets = [p async for p in client.iter_received_packets()]


async def recv_packet_example5(client: AsyncUDPNetworkClient[Any, Any]) -> None:
# [start]
all_packets = [p async for p in client.iter_received_packets(timeout=1)]


##################
# Advanced usage #
##################


async def socket_proxy_example(client: AsyncUDPNetworkClient[Any, Any]) -> None:
# [start]
client.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
64 changes: 64 additions & 0 deletions docs/source/_include/examples/howto/udp_clients/usage/api_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import annotations

import socket
from typing import Any

from easynetwork.api_sync.client import UDPNetworkClient
from easynetwork.exceptions import DatagramProtocolParseError

###############
# Basic usage #
###############


def send_packet_example1(client: UDPNetworkClient[Any, Any]) -> None:
# [start]
client.send_packet({"data": 42})


def recv_packet_example1(client: UDPNetworkClient[Any, Any]) -> None:
# [start]
packet = client.recv_packet()
print(f"Received packet: {packet!r}")


def recv_packet_example2(client: UDPNetworkClient[Any, Any]) -> None:
# [start]
try:
packet = client.recv_packet(timeout=30)
except TimeoutError:
print("Timed out")
else:
print(f"Received packet: {packet!r}")


def recv_packet_example3(client: UDPNetworkClient[Any, Any]) -> None:
# [start]
try:
packet = client.recv_packet(timeout=30)
except DatagramProtocolParseError:
print("Received something, but was not valid")
except TimeoutError:
print("Timed out")
else:
print(f"Received packet: {packet!r}")


def recv_packet_example4(client: UDPNetworkClient[Any, Any]) -> None:
# [start]
all_packets = [p for p in client.iter_received_packets()]


def recv_packet_example5(client: UDPNetworkClient[Any, Any]) -> None:
# [start]
all_packets = [p for p in client.iter_received_packets(timeout=1)]


##################
# Advanced usage #
##################


def socket_proxy_example(client: UDPNetworkClient[Any, Any]) -> None:
# [start]
client.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
1 change: 1 addition & 0 deletions docs/source/howto/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ How-to Guide
serializers
tcp_clients
tcp_servers
udp_clients
Loading

0 comments on commit fc7fea1

Please sign in to comment.