Moved AsyncSocket class into a dedicated file

This commit is contained in:
nocturn9x 2020-03-20 17:57:07 +01:00
parent 496431a4d2
commit bf3c477b02
2 changed files with 55 additions and 46 deletions

View File

@ -1,5 +1,4 @@
import types
import datetime
from collections import deque, defaultdict
from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE
from heapq import heappush, heappop
@ -8,6 +7,7 @@ from .exceptions import AlreadyJoinedError, CancelledError
import traceback
from timeit import default_timer
from time import sleep as wait
from .socket import AsyncSocket
class Task:
@ -149,51 +149,6 @@ class EventLoop:
return sock.connect(addr)
class AsyncSocket(object):
"""Abstraction layer for asynchronous sockets"""
def __init__(self, sock: socket.socket, loop: EventLoop):
self.sock = sock
self.sock.setblocking(False)
self.loop = loop
async def receive(self, max_size: int):
"""Receives up to max_size from a socket asynchronously"""
return await self.loop.read_sock(self.sock, max_size)
async def accept(self):
"""Accepts the socket, completing the 3-step TCP handshake asynchronously"""
to_wrap = await self.loop.accept_sock(self.sock)
return self.loop.wrap_socket(to_wrap[0]), to_wrap[1]
async def send_all(self, data: bytes):
"""Sends all data inside the buffer asynchronously until it is empty"""
return await self.loop.sock_sendall(self.sock, data)
async def close(self):
"""Closes the socket asynchronously"""
await self.loop.close_sock(self.sock)
async def connect(self, addr: tuple):
"""Connects the socket to an endpoint"""
await self.loop.connect_sock(self.sock, addr)
def __enter__(self):
return self.sock.__enter__()
def __exit__(self, *args):
return self.sock.__exit__(*args)
def __repr__(self):
return f"AsyncSocket({self.sock}, {self.loop})"
@types.coroutine
def sleep(seconds: int):
"""Pause the execution of a coroutine for the passed amount of seconds,

54
giambio/socket.py Normal file
View File

@ -0,0 +1,54 @@
"""
Basic abstraction layer for giambio asynchronous sockets
"""
from .core import EventLoop
import socket
class AsyncSocket(object):
"""Abstraction layer for asynchronous sockets"""
def __init__(self, sock: socket.socket, loop: EventLoop):
self.sock = sock
self.sock.setblocking(False)
self.loop = loop
async def receive(self, max_size: int):
"""Receives up to max_size from a socket asynchronously"""
return await self.loop.read_sock(self.sock, max_size)
async def accept(self):
"""Accepts the socket, completing the 3-step TCP handshake asynchronously"""
to_wrap = await self.loop.accept_sock(self.sock)
return self.loop.wrap_socket(to_wrap[0]), to_wrap[1]
async def send_all(self, data: bytes):
"""Sends all data inside the buffer asynchronously until it is empty"""
return await self.loop.sock_sendall(self.sock, data)
async def close(self):
"""Closes the socket asynchronously"""
await self.loop.close_sock(self.sock)
async def connect(self, addr: tuple):
"""Connects the socket to an endpoint"""
await self.loop.connect_sock(self.sock, addr)
def __enter__(self):
return self.sock.__enter__()
def __exit__(self, *args):
return self.sock.__exit__(*args)
def __repr__(self):
return f"giambio.socket.AsyncSocket({self.sock}, {self.loop})"