mirror of https://github.com/nocturn9x/giambio.git
Moved AsyncSocket class into a dedicated file
This commit is contained in:
parent
496431a4d2
commit
bf3c477b02
|
@ -1,5 +1,4 @@
|
||||||
import types
|
import types
|
||||||
import datetime
|
|
||||||
from collections import deque, defaultdict
|
from collections import deque, defaultdict
|
||||||
from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE
|
from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE
|
||||||
from heapq import heappush, heappop
|
from heapq import heappush, heappop
|
||||||
|
@ -8,6 +7,7 @@ from .exceptions import AlreadyJoinedError, CancelledError
|
||||||
import traceback
|
import traceback
|
||||||
from timeit import default_timer
|
from timeit import default_timer
|
||||||
from time import sleep as wait
|
from time import sleep as wait
|
||||||
|
from .socket import AsyncSocket
|
||||||
|
|
||||||
|
|
||||||
class Task:
|
class Task:
|
||||||
|
@ -149,51 +149,6 @@ class EventLoop:
|
||||||
return sock.connect(addr)
|
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
|
@types.coroutine
|
||||||
def sleep(seconds: int):
|
def sleep(seconds: int):
|
||||||
"""Pause the execution of a coroutine for the passed amount of seconds,
|
"""Pause the execution of a coroutine for the passed amount of seconds,
|
||||||
|
|
|
@ -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})"
|
Loading…
Reference in New Issue