diff --git a/giambio/core.py b/giambio/core.py index fdf4f75..092e93d 100644 --- a/giambio/core.py +++ b/giambio/core.py @@ -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, diff --git a/giambio/socket.py b/giambio/socket.py new file mode 100644 index 0000000..511b18e --- /dev/null +++ b/giambio/socket.py @@ -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})" \ No newline at end of file